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 // Enable source debugging on AP function
546 // Invoke AP function here
548 Procedure (Parameter
);
549 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
550 if (CpuMpData
->SwitchBspFlag
) {
552 // Re-get the processor number due to BSP/AP maybe exchange in AP function
554 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
555 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
556 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
557 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
558 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
561 // Re-get the CPU APICID and Initial APICID
563 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
564 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
567 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
572 // AP finished executing C code
574 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
577 // Place AP is specified loop mode
579 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
581 // Save AP volatile registers
583 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
585 // Place AP in HLT-loop
588 DisableInterrupts ();
594 DisableInterrupts ();
595 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
597 // Place AP in MWAIT-loop
599 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
600 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
602 // Check AP start-up signal again.
603 // If AP start-up signal is not set, place AP into
604 // the specified C-state
606 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
608 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
610 // Place AP in Run-loop
618 // If AP start-up signal is written, AP is waken up
619 // otherwise place AP in loop again
621 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
629 Wait for AP wakeup and write AP start-up signal till AP is waken up.
631 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
635 IN
volatile UINT32
*ApStartupSignalBuffer
639 // If AP is waken up, StartupApSignal should be cleared.
640 // Otherwise, write StartupApSignal again till AP waken up.
642 while (InterlockedCompareExchange32 (
643 (UINT32
*) ApStartupSignalBuffer
,
652 This function will fill the exchange info structure.
654 @param[in] CpuMpData Pointer to CPU MP Data
658 FillExchangeInfoData (
659 IN CPU_MP_DATA
*CpuMpData
662 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
664 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
665 ExchangeInfo
->Lock
= 0;
666 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
667 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
668 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
669 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
671 ExchangeInfo
->CodeSegment
= AsmReadCs ();
672 ExchangeInfo
->DataSegment
= AsmReadDs ();
674 ExchangeInfo
->Cr3
= AsmReadCr3 ();
676 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
677 ExchangeInfo
->NumApsExecuting
= 0;
678 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
679 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
680 ExchangeInfo
->CpuMpData
= CpuMpData
;
682 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
685 // Get the BSP's data of GDT and IDT
687 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
688 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
692 Helper function that waits until the finished AP count reaches the specified
693 limit, or the specified timeout elapses (whichever comes first).
695 @param[in] CpuMpData Pointer to CPU MP Data.
696 @param[in] FinishedApLimit The number of finished APs to wait for.
697 @param[in] TimeLimit The number of microseconds to wait for.
700 TimedWaitForApFinish (
701 IN CPU_MP_DATA
*CpuMpData
,
702 IN UINT32 FinishedApLimit
,
707 This function will be called by BSP to wakeup AP.
709 @param[in] CpuMpData Pointer to CPU MP Data
710 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
711 FALSE: Send IPI to AP by ApicId
712 @param[in] ProcessorNumber The handle number of specified processor
713 @param[in] Procedure The function to be invoked by AP
714 @param[in] ProcedureArgument The argument to be passed into AP function
718 IN CPU_MP_DATA
*CpuMpData
,
719 IN BOOLEAN Broadcast
,
720 IN UINTN ProcessorNumber
,
721 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
722 IN VOID
*ProcedureArgument OPTIONAL
725 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
727 CPU_AP_DATA
*CpuData
;
728 BOOLEAN ResetVectorRequired
;
729 CPU_INFO_IN_HOB
*CpuInfoInHob
;
731 CpuMpData
->FinishedCount
= 0;
732 ResetVectorRequired
= FALSE
;
734 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
735 CpuMpData
->InitFlag
!= ApInitDone
) {
736 ResetVectorRequired
= TRUE
;
737 AllocateResetVector (CpuMpData
);
738 FillExchangeInfoData (CpuMpData
);
739 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
741 // Get AP target C-state each time when waking up AP,
742 // for it maybe updated by platform again
744 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
747 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
750 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
751 if (Index
!= CpuMpData
->BspNumber
) {
752 CpuData
= &CpuMpData
->CpuData
[Index
];
753 CpuData
->ApFunction
= (UINTN
) Procedure
;
754 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
755 SetApState (CpuData
, CpuStateReady
);
756 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
757 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
761 if (ResetVectorRequired
) {
765 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
767 if (CpuMpData
->InitFlag
== ApInitConfig
) {
769 // Wait for all potential APs waken up in one specified period
771 TimedWaitForApFinish (
773 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
774 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
778 // Wait all APs waken up if this is not the 1st broadcast of SIPI
780 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
781 CpuData
= &CpuMpData
->CpuData
[Index
];
782 if (Index
!= CpuMpData
->BspNumber
) {
783 WaitApWakeup (CpuData
->StartupApSignal
);
788 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
789 CpuData
->ApFunction
= (UINTN
) Procedure
;
790 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
791 SetApState (CpuData
, CpuStateReady
);
793 // Wakeup specified AP
795 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
796 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
797 if (ResetVectorRequired
) {
798 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
800 CpuInfoInHob
[ProcessorNumber
].ApicId
,
801 (UINT32
) ExchangeInfo
->BufferStart
805 // Wait specified AP waken up
807 WaitApWakeup (CpuData
->StartupApSignal
);
810 if (ResetVectorRequired
) {
811 FreeResetVector (CpuMpData
);
816 Calculate timeout value and return the current performance counter value.
818 Calculate the number of performance counter ticks required for a timeout.
819 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
822 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
823 @param[out] CurrentTime Returns the current value of the performance counter.
825 @return Expected time stamp counter for timeout.
826 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
832 IN UINTN TimeoutInMicroseconds
,
833 OUT UINT64
*CurrentTime
837 // Read the current value of the performance counter
839 *CurrentTime
= GetPerformanceCounter ();
842 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
845 if (TimeoutInMicroseconds
== 0) {
850 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
851 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
852 // it by 1,000,000, to get the number of ticks for the timeout value.
856 GetPerformanceCounterProperties (NULL
, NULL
),
857 TimeoutInMicroseconds
864 Checks whether timeout expires.
866 Check whether the number of elapsed performance counter ticks required for
867 a timeout condition has been reached.
868 If Timeout is zero, which means infinity, return value is always FALSE.
870 @param[in, out] PreviousTime On input, the value of the performance counter
871 when it was last read.
872 On output, the current value of the performance
874 @param[in] TotalTime The total amount of elapsed time in performance
876 @param[in] Timeout The number of performance counter ticks required
877 to reach a timeout condition.
879 @retval TRUE A timeout condition has been reached.
880 @retval FALSE A timeout condition has not been reached.
885 IN OUT UINT64
*PreviousTime
,
886 IN UINT64
*TotalTime
,
899 GetPerformanceCounterProperties (&Start
, &End
);
905 CurrentTime
= GetPerformanceCounter();
906 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
914 *PreviousTime
= CurrentTime
;
915 if (*TotalTime
> Timeout
) {
922 Helper function that waits until the finished AP count reaches the specified
923 limit, or the specified timeout elapses (whichever comes first).
925 @param[in] CpuMpData Pointer to CPU MP Data.
926 @param[in] FinishedApLimit The number of finished APs to wait for.
927 @param[in] TimeLimit The number of microseconds to wait for.
930 TimedWaitForApFinish (
931 IN CPU_MP_DATA
*CpuMpData
,
932 IN UINT32 FinishedApLimit
,
937 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
938 // "infinity", so check for (TimeLimit == 0) explicitly.
940 if (TimeLimit
== 0) {
944 CpuMpData
->TotalTime
= 0;
945 CpuMpData
->ExpectedTime
= CalculateTimeout (
947 &CpuMpData
->CurrentTime
949 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
951 &CpuMpData
->CurrentTime
,
952 &CpuMpData
->TotalTime
,
953 CpuMpData
->ExpectedTime
958 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
961 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
965 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
966 GetPerformanceCounterProperties (NULL
, NULL
),
974 Reset an AP to Idle state.
976 Any task being executed by the AP will be aborted and the AP
977 will be waiting for a new task in Wait-For-SIPI state.
979 @param[in] ProcessorNumber The handle number of processor.
982 ResetProcessorToIdleState (
983 IN UINTN ProcessorNumber
986 CPU_MP_DATA
*CpuMpData
;
988 CpuMpData
= GetCpuMpData ();
990 CpuMpData
->InitFlag
= ApInitReconfig
;
991 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
992 while (CpuMpData
->FinishedCount
< 1) {
995 CpuMpData
->InitFlag
= ApInitDone
;
997 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1001 Searches for the next waiting AP.
1003 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1005 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1007 @retval EFI_SUCCESS The next waiting AP has been found.
1008 @retval EFI_NOT_FOUND No waiting AP exists.
1012 GetNextWaitingProcessorNumber (
1013 OUT UINTN
*NextProcessorNumber
1016 UINTN ProcessorNumber
;
1017 CPU_MP_DATA
*CpuMpData
;
1019 CpuMpData
= GetCpuMpData ();
1021 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1022 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1023 *NextProcessorNumber
= ProcessorNumber
;
1028 return EFI_NOT_FOUND
;
1031 /** Checks status of specified AP.
1033 This function checks whether the specified AP has finished the task assigned
1034 by StartupThisAP(), and whether timeout expires.
1036 @param[in] ProcessorNumber The handle number of processor.
1038 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1039 @retval EFI_TIMEOUT The timeout expires.
1040 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1044 IN UINTN ProcessorNumber
1047 CPU_MP_DATA
*CpuMpData
;
1048 CPU_AP_DATA
*CpuData
;
1050 CpuMpData
= GetCpuMpData ();
1051 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1054 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1055 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1056 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1059 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1061 if (GetApState(CpuData
) == CpuStateFinished
) {
1062 if (CpuData
->Finished
!= NULL
) {
1063 *(CpuData
->Finished
) = TRUE
;
1065 SetApState (CpuData
, CpuStateIdle
);
1069 // If timeout expires for StartupThisAP(), report timeout.
1071 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1072 if (CpuData
->Finished
!= NULL
) {
1073 *(CpuData
->Finished
) = FALSE
;
1076 // Reset failed AP to idle state
1078 ResetProcessorToIdleState (ProcessorNumber
);
1083 return EFI_NOT_READY
;
1087 Checks status of all APs.
1089 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1090 and whether timeout expires.
1092 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1093 @retval EFI_TIMEOUT The timeout expires.
1094 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1101 UINTN ProcessorNumber
;
1102 UINTN NextProcessorNumber
;
1105 CPU_MP_DATA
*CpuMpData
;
1106 CPU_AP_DATA
*CpuData
;
1108 CpuMpData
= GetCpuMpData ();
1110 NextProcessorNumber
= 0;
1113 // Go through all APs that are responsible for the StartupAllAPs().
1115 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1116 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1120 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1122 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1123 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1124 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1126 if (GetApState(CpuData
) == CpuStateFinished
) {
1127 CpuMpData
->RunningCount
++;
1128 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1129 SetApState(CpuData
, CpuStateIdle
);
1132 // If in Single Thread mode, then search for the next waiting AP for execution.
1134 if (CpuMpData
->SingleThread
) {
1135 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1137 if (!EFI_ERROR (Status
)) {
1141 (UINT32
) NextProcessorNumber
,
1142 CpuMpData
->Procedure
,
1143 CpuMpData
->ProcArguments
1151 // If all APs finish, return EFI_SUCCESS.
1153 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1158 // If timeout expires, report timeout.
1161 &CpuMpData
->CurrentTime
,
1162 &CpuMpData
->TotalTime
,
1163 CpuMpData
->ExpectedTime
)
1166 // If FailedCpuList is not NULL, record all failed APs in it.
1168 if (CpuMpData
->FailedCpuList
!= NULL
) {
1169 *CpuMpData
->FailedCpuList
=
1170 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1171 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1175 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1177 // Check whether this processor is responsible for StartupAllAPs().
1179 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1181 // Reset failed APs to idle state
1183 ResetProcessorToIdleState (ProcessorNumber
);
1184 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1185 if (CpuMpData
->FailedCpuList
!= NULL
) {
1186 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1190 if (CpuMpData
->FailedCpuList
!= NULL
) {
1191 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1195 return EFI_NOT_READY
;
1199 MP Initialize Library initialization.
1201 This service will allocate AP reset vector and wakeup all APs to do APs
1204 This service must be invoked before all other MP Initialize Library
1205 service are invoked.
1207 @retval EFI_SUCCESS MP initialization succeeds.
1208 @retval Others MP initialization fails.
1213 MpInitLibInitialize (
1217 CPU_MP_DATA
*OldCpuMpData
;
1218 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1219 UINT32 MaxLogicalProcessorNumber
;
1221 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1223 UINT32 MonitorFilterSize
;
1226 CPU_MP_DATA
*CpuMpData
;
1228 UINT8
*MonitorBuffer
;
1230 UINTN ApResetVectorSize
;
1231 UINTN BackupBufferAddr
;
1233 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1234 if (OldCpuMpData
== NULL
) {
1235 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1237 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1239 ASSERT (MaxLogicalProcessorNumber
!= 0);
1241 AsmGetAddressMap (&AddressMap
);
1242 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1243 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1244 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1246 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1247 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1248 BufferSize
+= sizeof (CPU_MP_DATA
);
1249 BufferSize
+= ApResetVectorSize
;
1250 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1251 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1252 ASSERT (MpBuffer
!= NULL
);
1253 ZeroMem (MpBuffer
, BufferSize
);
1254 Buffer
= (UINTN
) MpBuffer
;
1256 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1257 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1258 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1259 CpuMpData
->Buffer
= Buffer
;
1260 CpuMpData
->CpuApStackSize
= ApStackSize
;
1261 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1262 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1263 CpuMpData
->SaveRestoreFlag
= FALSE
;
1264 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1265 CpuMpData
->CpuCount
= 1;
1266 CpuMpData
->BspNumber
= 0;
1267 CpuMpData
->WaitEvent
= NULL
;
1268 CpuMpData
->SwitchBspFlag
= FALSE
;
1269 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1270 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1271 InitializeSpinLock(&CpuMpData
->MpLock
);
1273 // Save BSP's Control registers to APs
1275 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1277 // Set BSP basic information
1279 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1281 // Save assembly code information
1283 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1285 // Finally set AP loop mode
1287 CpuMpData
->ApLoopMode
= ApLoopMode
;
1288 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1290 // Set up APs wakeup signal buffer
1292 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1293 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1294 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1297 // Load Microcode on BSP
1299 MicrocodeDetect (CpuMpData
);
1301 // Store BSP's MTRR setting
1303 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1305 if (OldCpuMpData
== NULL
) {
1306 if (MaxLogicalProcessorNumber
> 1) {
1308 // Wakeup all APs and calculate the processor count in system
1310 CollectProcessorCount (CpuMpData
);
1314 // APs have been wakeup before, just get the CPU Information
1317 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1318 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1319 CpuMpData
->InitFlag
= ApInitReconfig
;
1320 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1321 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1322 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1323 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1324 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1325 CpuMpData
->X2ApicEnable
= TRUE
;
1327 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1328 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1330 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1331 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1332 sizeof (CPU_VOLATILE_REGISTERS
)
1335 if (MaxLogicalProcessorNumber
> 1) {
1337 // Wakeup APs to do some AP initialize sync
1339 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1341 // Wait for all APs finished initialization
1343 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1346 CpuMpData
->InitFlag
= ApInitDone
;
1347 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1348 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1354 // Initialize global data for MP support
1356 InitMpGlobalData (CpuMpData
);
1362 Gets detailed MP-related information on the requested processor at the
1363 instant this call is made. This service may only be called from the BSP.
1365 @param[in] ProcessorNumber The handle number of processor.
1366 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1367 the requested processor is deposited.
1368 @param[out] HealthData Return processor health data.
1370 @retval EFI_SUCCESS Processor information was returned.
1371 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1372 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1373 @retval EFI_NOT_FOUND The processor with the handle specified by
1374 ProcessorNumber does not exist in the platform.
1375 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1380 MpInitLibGetProcessorInfo (
1381 IN UINTN ProcessorNumber
,
1382 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1383 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1386 CPU_MP_DATA
*CpuMpData
;
1388 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1390 CpuMpData
= GetCpuMpData ();
1391 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1394 // Check whether caller processor is BSP
1396 MpInitLibWhoAmI (&CallerNumber
);
1397 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1398 return EFI_DEVICE_ERROR
;
1401 if (ProcessorInfoBuffer
== NULL
) {
1402 return EFI_INVALID_PARAMETER
;
1405 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1406 return EFI_NOT_FOUND
;
1409 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1410 ProcessorInfoBuffer
->StatusFlag
= 0;
1411 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1412 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1414 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1415 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1417 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1418 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1420 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1424 // Get processor location information
1426 GetProcessorLocationByApicId (
1427 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1428 &ProcessorInfoBuffer
->Location
.Package
,
1429 &ProcessorInfoBuffer
->Location
.Core
,
1430 &ProcessorInfoBuffer
->Location
.Thread
1433 if (HealthData
!= NULL
) {
1434 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1441 Worker function to switch the requested AP to be the BSP from that point onward.
1443 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1444 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1445 enabled AP. Otherwise, it will be disabled.
1447 @retval EFI_SUCCESS BSP successfully switched.
1448 @retval others Failed to switch BSP.
1453 IN UINTN ProcessorNumber
,
1454 IN BOOLEAN EnableOldBSP
1457 CPU_MP_DATA
*CpuMpData
;
1460 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1462 CpuMpData
= GetCpuMpData ();
1465 // Check whether caller processor is BSP
1467 MpInitLibWhoAmI (&CallerNumber
);
1468 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1472 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1473 return EFI_NOT_FOUND
;
1477 // Check whether specified AP is disabled
1479 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1480 if (State
== CpuStateDisabled
) {
1481 return EFI_INVALID_PARAMETER
;
1485 // Check whether ProcessorNumber specifies the current BSP
1487 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1488 return EFI_INVALID_PARAMETER
;
1492 // Check whether specified AP is busy
1494 if (State
== CpuStateBusy
) {
1495 return EFI_NOT_READY
;
1498 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1499 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1500 CpuMpData
->SwitchBspFlag
= TRUE
;
1501 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1504 // Clear the BSP bit of MSR_IA32_APIC_BASE
1506 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1507 ApicBaseMsr
.Bits
.BSP
= 0;
1508 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1511 // Need to wakeUp AP (future BSP).
1513 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1515 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1518 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1520 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1521 ApicBaseMsr
.Bits
.BSP
= 1;
1522 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1525 // Wait for old BSP finished AP task
1527 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1531 CpuMpData
->SwitchBspFlag
= FALSE
;
1533 // Set old BSP enable state
1535 if (!EnableOldBSP
) {
1536 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1539 // Save new BSP number
1541 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1547 Worker function to let the caller enable or disable an AP from this point onward.
1548 This service may only be called from the BSP.
1550 @param[in] ProcessorNumber The handle number of AP.
1551 @param[in] EnableAP Specifies the new state for the processor for
1552 enabled, FALSE for disabled.
1553 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1554 the new health status of the AP.
1556 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1557 @retval others Failed to Enable/Disable AP.
1561 EnableDisableApWorker (
1562 IN UINTN ProcessorNumber
,
1563 IN BOOLEAN EnableAP
,
1564 IN UINT32
*HealthFlag OPTIONAL
1567 CPU_MP_DATA
*CpuMpData
;
1570 CpuMpData
= GetCpuMpData ();
1573 // Check whether caller processor is BSP
1575 MpInitLibWhoAmI (&CallerNumber
);
1576 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1577 return EFI_DEVICE_ERROR
;
1580 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1581 return EFI_INVALID_PARAMETER
;
1584 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1585 return EFI_NOT_FOUND
;
1589 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1591 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1594 if (HealthFlag
!= NULL
) {
1595 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1596 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1603 This return the handle number for the calling processor. This service may be
1604 called from the BSP and APs.
1606 @param[out] ProcessorNumber Pointer to the handle number of AP.
1607 The range is from 0 to the total number of
1608 logical processors minus 1. The total number of
1609 logical processors can be retrieved by
1610 MpInitLibGetNumberOfProcessors().
1612 @retval EFI_SUCCESS The current processor handle number was returned
1614 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1615 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1621 OUT UINTN
*ProcessorNumber
1624 CPU_MP_DATA
*CpuMpData
;
1626 if (ProcessorNumber
== NULL
) {
1627 return EFI_INVALID_PARAMETER
;
1630 CpuMpData
= GetCpuMpData ();
1632 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1636 Retrieves the number of logical processor in the platform and the number of
1637 those logical processors that are enabled on this boot. This service may only
1638 be called from the BSP.
1640 @param[out] NumberOfProcessors Pointer to the total number of logical
1641 processors in the system, including the BSP
1643 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1644 processors that exist in system, including
1647 @retval EFI_SUCCESS The number of logical processors and enabled
1648 logical processors was retrieved.
1649 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1650 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1652 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1657 MpInitLibGetNumberOfProcessors (
1658 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1659 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1662 CPU_MP_DATA
*CpuMpData
;
1664 UINTN ProcessorNumber
;
1665 UINTN EnabledProcessorNumber
;
1668 CpuMpData
= GetCpuMpData ();
1670 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1671 return EFI_INVALID_PARAMETER
;
1675 // Check whether caller processor is BSP
1677 MpInitLibWhoAmI (&CallerNumber
);
1678 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1679 return EFI_DEVICE_ERROR
;
1682 ProcessorNumber
= CpuMpData
->CpuCount
;
1683 EnabledProcessorNumber
= 0;
1684 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1685 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1686 EnabledProcessorNumber
++;
1690 if (NumberOfProcessors
!= NULL
) {
1691 *NumberOfProcessors
= ProcessorNumber
;
1693 if (NumberOfEnabledProcessors
!= NULL
) {
1694 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1702 Worker function to execute a caller provided function on all enabled APs.
1704 @param[in] Procedure A pointer to the function to be run on
1705 enabled APs of the system.
1706 @param[in] SingleThread If TRUE, then all the enabled APs execute
1707 the function specified by Procedure one by
1708 one, in ascending order of processor handle
1709 number. If FALSE, then all the enabled APs
1710 execute the function specified by Procedure
1712 @param[in] WaitEvent The event created by the caller with CreateEvent()
1714 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1715 APs to return from Procedure, either for
1716 blocking or non-blocking mode.
1717 @param[in] ProcedureArgument The parameter passed into Procedure for
1719 @param[out] FailedCpuList If all APs finish successfully, then its
1720 content is set to NULL. If not all APs
1721 finish before timeout expires, then its
1722 content is set to address of the buffer
1723 holding handle numbers of the failed APs.
1725 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1726 the timeout expired.
1727 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1729 @retval others Failed to Startup all APs.
1733 StartupAllAPsWorker (
1734 IN EFI_AP_PROCEDURE Procedure
,
1735 IN BOOLEAN SingleThread
,
1736 IN EFI_EVENT WaitEvent OPTIONAL
,
1737 IN UINTN TimeoutInMicroseconds
,
1738 IN VOID
*ProcedureArgument OPTIONAL
,
1739 OUT UINTN
**FailedCpuList OPTIONAL
1743 CPU_MP_DATA
*CpuMpData
;
1744 UINTN ProcessorCount
;
1745 UINTN ProcessorNumber
;
1747 CPU_AP_DATA
*CpuData
;
1748 BOOLEAN HasEnabledAp
;
1751 CpuMpData
= GetCpuMpData ();
1753 if (FailedCpuList
!= NULL
) {
1754 *FailedCpuList
= NULL
;
1757 if (CpuMpData
->CpuCount
== 1) {
1758 return EFI_NOT_STARTED
;
1761 if (Procedure
== NULL
) {
1762 return EFI_INVALID_PARAMETER
;
1766 // Check whether caller processor is BSP
1768 MpInitLibWhoAmI (&CallerNumber
);
1769 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1770 return EFI_DEVICE_ERROR
;
1776 CheckAndUpdateApsStatus ();
1778 ProcessorCount
= CpuMpData
->CpuCount
;
1779 HasEnabledAp
= FALSE
;
1781 // Check whether all enabled APs are idle.
1782 // If any enabled AP is not idle, return EFI_NOT_READY.
1784 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1785 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1786 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1787 ApState
= GetApState (CpuData
);
1788 if (ApState
!= CpuStateDisabled
) {
1789 HasEnabledAp
= TRUE
;
1790 if (ApState
!= CpuStateIdle
) {
1792 // If any enabled APs are busy, return EFI_NOT_READY.
1794 return EFI_NOT_READY
;
1800 if (!HasEnabledAp
) {
1802 // If no enabled AP exists, return EFI_NOT_STARTED.
1804 return EFI_NOT_STARTED
;
1807 CpuMpData
->StartCount
= 0;
1808 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1809 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1810 CpuData
->Waiting
= FALSE
;
1811 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1812 if (CpuData
->State
== CpuStateIdle
) {
1814 // Mark this processor as responsible for current calling.
1816 CpuData
->Waiting
= TRUE
;
1817 CpuMpData
->StartCount
++;
1822 CpuMpData
->Procedure
= Procedure
;
1823 CpuMpData
->ProcArguments
= ProcedureArgument
;
1824 CpuMpData
->SingleThread
= SingleThread
;
1825 CpuMpData
->FinishedCount
= 0;
1826 CpuMpData
->RunningCount
= 0;
1827 CpuMpData
->FailedCpuList
= FailedCpuList
;
1828 CpuMpData
->ExpectedTime
= CalculateTimeout (
1829 TimeoutInMicroseconds
,
1830 &CpuMpData
->CurrentTime
1832 CpuMpData
->TotalTime
= 0;
1833 CpuMpData
->WaitEvent
= WaitEvent
;
1835 if (!SingleThread
) {
1836 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1838 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1839 if (ProcessorNumber
== CallerNumber
) {
1842 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1843 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1849 Status
= EFI_SUCCESS
;
1850 if (WaitEvent
== NULL
) {
1852 Status
= CheckAllAPs ();
1853 } while (Status
== EFI_NOT_READY
);
1860 Worker function to let the caller get one enabled AP to execute a caller-provided
1863 @param[in] Procedure A pointer to the function to be run on
1864 enabled APs of the system.
1865 @param[in] ProcessorNumber The handle number of the AP.
1866 @param[in] WaitEvent The event created by the caller with CreateEvent()
1868 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1869 APs to return from Procedure, either for
1870 blocking or non-blocking mode.
1871 @param[in] ProcedureArgument The parameter passed into Procedure for
1873 @param[out] Finished If AP returns from Procedure before the
1874 timeout expires, its content is set to TRUE.
1875 Otherwise, the value is set to FALSE.
1877 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1878 the timeout expires.
1879 @retval others Failed to Startup AP.
1883 StartupThisAPWorker (
1884 IN EFI_AP_PROCEDURE Procedure
,
1885 IN UINTN ProcessorNumber
,
1886 IN EFI_EVENT WaitEvent OPTIONAL
,
1887 IN UINTN TimeoutInMicroseconds
,
1888 IN VOID
*ProcedureArgument OPTIONAL
,
1889 OUT BOOLEAN
*Finished OPTIONAL
1893 CPU_MP_DATA
*CpuMpData
;
1894 CPU_AP_DATA
*CpuData
;
1897 CpuMpData
= GetCpuMpData ();
1899 if (Finished
!= NULL
) {
1904 // Check whether caller processor is BSP
1906 MpInitLibWhoAmI (&CallerNumber
);
1907 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1908 return EFI_DEVICE_ERROR
;
1912 // Check whether processor with the handle specified by ProcessorNumber exists
1914 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1915 return EFI_NOT_FOUND
;
1919 // Check whether specified processor is BSP
1921 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1922 return EFI_INVALID_PARAMETER
;
1926 // Check parameter Procedure
1928 if (Procedure
== NULL
) {
1929 return EFI_INVALID_PARAMETER
;
1935 CheckAndUpdateApsStatus ();
1938 // Check whether specified AP is disabled
1940 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1941 return EFI_INVALID_PARAMETER
;
1945 // If WaitEvent is not NULL, execute in non-blocking mode.
1946 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1947 // CheckAPsStatus() will check completion and timeout periodically.
1949 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1950 CpuData
->WaitEvent
= WaitEvent
;
1951 CpuData
->Finished
= Finished
;
1952 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
1953 CpuData
->TotalTime
= 0;
1955 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1958 // If WaitEvent is NULL, execute in blocking mode.
1959 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1961 Status
= EFI_SUCCESS
;
1962 if (WaitEvent
== NULL
) {
1964 Status
= CheckThisAP (ProcessorNumber
);
1965 } while (Status
== EFI_NOT_READY
);
1972 Get pointer to CPU MP Data structure from GUIDed HOB.
1974 @return The pointer to CPU MP Data structure.
1977 GetCpuMpDataFromGuidedHob (
1981 EFI_HOB_GUID_TYPE
*GuidHob
;
1983 CPU_MP_DATA
*CpuMpData
;
1986 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
1987 if (GuidHob
!= NULL
) {
1988 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
1989 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
1995 Get available system memory below 1MB by specified size.
1997 @param[in] CpuMpData The pointer to CPU MP Data structure.
2000 BackupAndPrepareWakeupBuffer(
2001 IN CPU_MP_DATA
*CpuMpData
2005 (VOID
*) CpuMpData
->BackupBuffer
,
2006 (VOID
*) CpuMpData
->WakeupBuffer
,
2007 CpuMpData
->BackupBufferSize
2010 (VOID
*) CpuMpData
->WakeupBuffer
,
2011 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2012 CpuMpData
->AddressMap
.RendezvousFunnelSize
2017 Restore wakeup buffer data.
2019 @param[in] CpuMpData The pointer to CPU MP Data structure.
2022 RestoreWakeupBuffer(
2023 IN CPU_MP_DATA
*CpuMpData
2027 (VOID
*) CpuMpData
->WakeupBuffer
,
2028 (VOID
*) CpuMpData
->BackupBuffer
,
2029 CpuMpData
->BackupBufferSize