2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
20 The function will check if BSP Execute Disable is enabled.
21 DxeIpl may have enabled Execute Disable for BSP,
22 APs need to get the status and sync up the settings.
24 @retval TRUE BSP Execute Disable is enabled.
25 @retval FALSE BSP Execute Disable is not enabled.
28 IsBspExecuteDisableEnabled (
33 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
34 MSR_IA32_EFER_REGISTER EferMsr
;
38 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
39 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
40 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
43 // Bit 20: Execute Disable Bit available.
45 if (Edx
.Bits
.NX
!= 0) {
46 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
49 // Bit 11: Execute Disable Bit enable.
51 if (EferMsr
.Bits
.NXE
!= 0) {
61 Worker function for SwitchBSP().
63 Worker function for SwitchBSP(), assigned to the AP which is intended
66 @param[in] Buffer Pointer to CPU MP Data
74 CPU_MP_DATA
*DataInHob
;
76 DataInHob
= (CPU_MP_DATA
*) Buffer
;
77 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
81 Get the Application Processors state.
83 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
89 IN CPU_AP_DATA
*CpuData
92 return CpuData
->State
;
96 Set the Application Processors state.
98 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
99 @param[in] State The AP status
103 IN CPU_AP_DATA
*CpuData
,
107 AcquireSpinLock (&CpuData
->ApLock
);
108 CpuData
->State
= State
;
109 ReleaseSpinLock (&CpuData
->ApLock
);
113 Save the volatile registers required to be restored following INIT IPI.
115 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
118 SaveVolatileRegisters (
119 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
122 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
124 VolatileRegisters
->Cr0
= AsmReadCr0 ();
125 VolatileRegisters
->Cr3
= AsmReadCr3 ();
126 VolatileRegisters
->Cr4
= AsmReadCr4 ();
128 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
129 if (VersionInfoEdx
.Bits
.DE
!= 0) {
131 // If processor supports Debugging Extensions feature
132 // by CPUID.[EAX=01H]:EDX.BIT2
134 VolatileRegisters
->Dr0
= AsmReadDr0 ();
135 VolatileRegisters
->Dr1
= AsmReadDr1 ();
136 VolatileRegisters
->Dr2
= AsmReadDr2 ();
137 VolatileRegisters
->Dr3
= AsmReadDr3 ();
138 VolatileRegisters
->Dr6
= AsmReadDr6 ();
139 VolatileRegisters
->Dr7
= AsmReadDr7 ();
144 Restore the volatile registers following INIT IPI.
146 @param[in] VolatileRegisters Pointer to volatile resisters
147 @param[in] IsRestoreDr TRUE: Restore DRx if supported
148 FALSE: Do not restore DRx
151 RestoreVolatileRegisters (
152 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
153 IN BOOLEAN IsRestoreDr
156 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
158 AsmWriteCr0 (VolatileRegisters
->Cr0
);
159 AsmWriteCr3 (VolatileRegisters
->Cr3
);
160 AsmWriteCr4 (VolatileRegisters
->Cr4
);
163 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
164 if (VersionInfoEdx
.Bits
.DE
!= 0) {
166 // If processor supports Debugging Extensions feature
167 // by CPUID.[EAX=01H]:EDX.BIT2
169 AsmWriteDr0 (VolatileRegisters
->Dr0
);
170 AsmWriteDr1 (VolatileRegisters
->Dr1
);
171 AsmWriteDr2 (VolatileRegisters
->Dr2
);
172 AsmWriteDr3 (VolatileRegisters
->Dr3
);
173 AsmWriteDr6 (VolatileRegisters
->Dr6
);
174 AsmWriteDr7 (VolatileRegisters
->Dr7
);
180 Detect whether Mwait-monitor feature is supported.
182 @retval TRUE Mwait-monitor feature is supported.
183 @retval FALSE Mwait-monitor feature is not supported.
190 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
192 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
193 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
199 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
201 @return The AP loop mode.
205 OUT UINT32
*MonitorFilterSize
209 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
211 ASSERT (MonitorFilterSize
!= NULL
);
213 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
214 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
215 if (ApLoopMode
== ApInMwaitLoop
) {
216 if (!IsMwaitSupport ()) {
218 // If processor does not support MONITOR/MWAIT feature,
219 // force AP in Hlt-loop mode
221 ApLoopMode
= ApInHltLoop
;
225 if (ApLoopMode
!= ApInMwaitLoop
) {
226 *MonitorFilterSize
= sizeof (UINT32
);
229 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
230 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
232 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
233 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
240 Sort the APIC ID of all processors.
242 This function sorts the APIC ID of all processors so that processor number is
243 assigned in the ascending order of APIC ID which eases MP debugging.
245 @param[in] CpuMpData Pointer to PEI CPU MP Data
249 IN CPU_MP_DATA
*CpuMpData
256 CPU_INFO_IN_HOB CpuInfo
;
258 CPU_INFO_IN_HOB
*CpuInfoInHob
;
260 ApCount
= CpuMpData
->CpuCount
- 1;
261 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
263 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
266 // Sort key is the hardware default APIC ID
268 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
269 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
270 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
272 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
275 if (Index3
!= Index1
) {
276 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
278 &CpuInfoInHob
[Index3
],
279 &CpuInfoInHob
[Index1
],
280 sizeof (CPU_INFO_IN_HOB
)
282 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
287 // Get the processor number for the BSP
289 ApicId
= GetInitialApicId ();
290 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
291 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
292 CpuMpData
->BspNumber
= (UINT32
) Index1
;
300 Enable x2APIC mode on APs.
302 @param[in, out] Buffer Pointer to private data buffer.
310 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
316 @param[in, out] Buffer Pointer to private data buffer.
324 CPU_MP_DATA
*CpuMpData
;
326 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
328 // Sync BSP's MTRR table to AP
330 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
332 // Load microcode on AP
334 MicrocodeDetect (CpuMpData
);
338 Find the current Processor number by APIC ID.
340 @param[in] CpuMpData Pointer to PEI CPU MP Data
341 @param[in] ProcessorNumber Return the pocessor number found
343 @retval EFI_SUCCESS ProcessorNumber is found and returned.
344 @retval EFI_NOT_FOUND ProcessorNumber is not found.
348 IN CPU_MP_DATA
*CpuMpData
,
349 OUT UINTN
*ProcessorNumber
352 UINTN TotalProcessorNumber
;
354 CPU_INFO_IN_HOB
*CpuInfoInHob
;
356 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
358 TotalProcessorNumber
= CpuMpData
->CpuCount
;
359 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
360 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
361 *ProcessorNumber
= Index
;
365 return EFI_NOT_FOUND
;
369 This function will get CPU count in the system.
371 @param[in] CpuMpData Pointer to PEI CPU MP Data
373 @return CPU count detected
376 CollectProcessorCount (
377 IN CPU_MP_DATA
*CpuMpData
381 // Send 1st broadcast IPI to APs to wakeup APs
383 CpuMpData
->InitFlag
= ApInitConfig
;
384 CpuMpData
->X2ApicEnable
= FALSE
;
385 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
386 CpuMpData
->InitFlag
= ApInitDone
;
387 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
389 // Wait for all APs finished the initialization
391 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
395 if (CpuMpData
->X2ApicEnable
) {
396 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
398 // Wakeup all APs to enable x2APIC mode
400 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
402 // Wait for all known APs finished
404 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
408 // Enable x2APIC on BSP
410 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
412 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
414 // Sort BSP/Aps by CPU APIC ID in ascending order
416 SortApicId (CpuMpData
);
418 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
420 return CpuMpData
->CpuCount
;
424 Initialize CPU AP Data when AP is wakeup at the first time.
426 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
427 @param[in] ProcessorNumber The handle number of processor
428 @param[in] BistData Processor BIST data
433 IN OUT CPU_MP_DATA
*CpuMpData
,
434 IN UINTN ProcessorNumber
,
436 IN UINTN ApTopOfStack
439 CPU_INFO_IN_HOB
*CpuInfoInHob
;
441 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
442 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
443 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
444 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
445 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= (UINT32
) ApTopOfStack
;
447 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
448 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
449 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
451 // Set x2APIC mode if there are any logical processor reporting
452 // an Initial APIC ID of 255 or greater.
454 AcquireSpinLock(&CpuMpData
->MpLock
);
455 CpuMpData
->X2ApicEnable
= TRUE
;
456 ReleaseSpinLock(&CpuMpData
->MpLock
);
459 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
460 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
464 This function will be called from AP reset code if BSP uses WakeUpAP.
466 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
467 @param[in] NumApsExecuting Number of current executing AP
472 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
473 IN UINTN NumApsExecuting
476 CPU_MP_DATA
*CpuMpData
;
477 UINTN ProcessorNumber
;
478 EFI_AP_PROCEDURE Procedure
;
481 volatile UINT32
*ApStartupSignalBuffer
;
482 CPU_INFO_IN_HOB
*CpuInfoInHob
;
486 // AP finished assembly code and begin to execute C code
488 CpuMpData
= ExchangeInfo
->CpuMpData
;
490 ProgramVirtualWireMode ();
493 if (CpuMpData
->InitFlag
== ApInitConfig
) {
497 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
498 ProcessorNumber
= NumApsExecuting
;
500 // This is first time AP wakeup, get BIST information from AP stack
502 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
503 BistData
= *(UINT32
*) (ApTopOfStack
- sizeof (UINTN
));
505 // Do some AP initialize sync
507 ApInitializeSync (CpuMpData
);
509 // Sync BSP's Control registers to APs
511 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
512 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
513 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
516 // Execute AP function if AP is ready
518 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
520 // Clear AP start-up signal when AP waken up
522 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
523 InterlockedCompareExchange32 (
524 (UINT32
*) ApStartupSignalBuffer
,
528 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
530 // Restore AP's volatile registers saved
532 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
535 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
536 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
537 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
538 if (Procedure
!= NULL
) {
539 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
541 // Invoke AP function here
543 Procedure (Parameter
);
544 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
545 if (CpuMpData
->SwitchBspFlag
) {
547 // Re-get the processor number due to BSP/AP maybe exchange in AP function
549 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
550 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
551 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
552 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
553 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
556 // Re-get the CPU APICID and Initial APICID
558 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
559 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
562 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
567 // AP finished executing C code
569 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
572 // Place AP is specified loop mode
574 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
576 // Save AP volatile registers
578 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
580 // Place AP in HLT-loop
583 DisableInterrupts ();
589 DisableInterrupts ();
590 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
592 // Place AP in MWAIT-loop
594 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
595 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
597 // Check AP start-up signal again.
598 // If AP start-up signal is not set, place AP into
599 // the specified C-state
601 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
603 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
605 // Place AP in Run-loop
613 // If AP start-up signal is written, AP is waken up
614 // otherwise place AP in loop again
616 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
624 Wait for AP wakeup and write AP start-up signal till AP is waken up.
626 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
630 IN
volatile UINT32
*ApStartupSignalBuffer
634 // If AP is waken up, StartupApSignal should be cleared.
635 // Otherwise, write StartupApSignal again till AP waken up.
637 while (InterlockedCompareExchange32 (
638 (UINT32
*) ApStartupSignalBuffer
,
647 This function will fill the exchange info structure.
649 @param[in] CpuMpData Pointer to CPU MP Data
653 FillExchangeInfoData (
654 IN CPU_MP_DATA
*CpuMpData
657 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
659 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
660 ExchangeInfo
->Lock
= 0;
661 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
662 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
663 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
664 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
666 ExchangeInfo
->CodeSegment
= AsmReadCs ();
667 ExchangeInfo
->DataSegment
= AsmReadDs ();
669 ExchangeInfo
->Cr3
= AsmReadCr3 ();
671 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
672 ExchangeInfo
->NumApsExecuting
= 0;
673 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
674 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
675 ExchangeInfo
->CpuMpData
= CpuMpData
;
677 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
680 // Get the BSP's data of GDT and IDT
682 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
683 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
687 This function will be called by BSP to wakeup AP.
689 @param[in] CpuMpData Pointer to CPU MP Data
690 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
691 FALSE: Send IPI to AP by ApicId
692 @param[in] ProcessorNumber The handle number of specified processor
693 @param[in] Procedure The function to be invoked by AP
694 @param[in] ProcedureArgument The argument to be passed into AP function
698 IN CPU_MP_DATA
*CpuMpData
,
699 IN BOOLEAN Broadcast
,
700 IN UINTN ProcessorNumber
,
701 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
702 IN VOID
*ProcedureArgument OPTIONAL
705 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
707 CPU_AP_DATA
*CpuData
;
708 BOOLEAN ResetVectorRequired
;
709 CPU_INFO_IN_HOB
*CpuInfoInHob
;
711 CpuMpData
->FinishedCount
= 0;
712 ResetVectorRequired
= FALSE
;
714 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
715 CpuMpData
->InitFlag
!= ApInitDone
) {
716 ResetVectorRequired
= TRUE
;
717 AllocateResetVector (CpuMpData
);
718 FillExchangeInfoData (CpuMpData
);
719 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
721 // Get AP target C-state each time when waking up AP,
722 // for it maybe updated by platform again
724 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
727 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
730 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
731 if (Index
!= CpuMpData
->BspNumber
) {
732 CpuData
= &CpuMpData
->CpuData
[Index
];
733 CpuData
->ApFunction
= (UINTN
) Procedure
;
734 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
735 SetApState (CpuData
, CpuStateReady
);
736 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
737 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
741 if (ResetVectorRequired
) {
745 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
747 if (CpuMpData
->InitFlag
== ApInitConfig
) {
749 // Wait for all potential APs waken up in one specified period
751 MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds
));
754 // Wait all APs waken up if this is not the 1st broadcast of SIPI
756 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
757 CpuData
= &CpuMpData
->CpuData
[Index
];
758 if (Index
!= CpuMpData
->BspNumber
) {
759 WaitApWakeup (CpuData
->StartupApSignal
);
764 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
765 CpuData
->ApFunction
= (UINTN
) Procedure
;
766 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
767 SetApState (CpuData
, CpuStateReady
);
769 // Wakeup specified AP
771 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
772 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
773 if (ResetVectorRequired
) {
774 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
776 CpuInfoInHob
[ProcessorNumber
].ApicId
,
777 (UINT32
) ExchangeInfo
->BufferStart
781 // Wait specified AP waken up
783 WaitApWakeup (CpuData
->StartupApSignal
);
786 if (ResetVectorRequired
) {
787 FreeResetVector (CpuMpData
);
792 Calculate timeout value and return the current performance counter value.
794 Calculate the number of performance counter ticks required for a timeout.
795 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
798 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
799 @param[out] CurrentTime Returns the current value of the performance counter.
801 @return Expected time stamp counter for timeout.
802 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
808 IN UINTN TimeoutInMicroseconds
,
809 OUT UINT64
*CurrentTime
813 // Read the current value of the performance counter
815 *CurrentTime
= GetPerformanceCounter ();
818 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
821 if (TimeoutInMicroseconds
== 0) {
826 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
827 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
828 // it by 1,000,000, to get the number of ticks for the timeout value.
832 GetPerformanceCounterProperties (NULL
, NULL
),
833 TimeoutInMicroseconds
840 Checks whether timeout expires.
842 Check whether the number of elapsed performance counter ticks required for
843 a timeout condition has been reached.
844 If Timeout is zero, which means infinity, return value is always FALSE.
846 @param[in, out] PreviousTime On input, the value of the performance counter
847 when it was last read.
848 On output, the current value of the performance
850 @param[in] TotalTime The total amount of elapsed time in performance
852 @param[in] Timeout The number of performance counter ticks required
853 to reach a timeout condition.
855 @retval TRUE A timeout condition has been reached.
856 @retval FALSE A timeout condition has not been reached.
861 IN OUT UINT64
*PreviousTime
,
862 IN UINT64
*TotalTime
,
875 GetPerformanceCounterProperties (&Start
, &End
);
881 CurrentTime
= GetPerformanceCounter();
882 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
890 *PreviousTime
= CurrentTime
;
891 if (*TotalTime
> Timeout
) {
898 Reset an AP to Idle state.
900 Any task being executed by the AP will be aborted and the AP
901 will be waiting for a new task in Wait-For-SIPI state.
903 @param[in] ProcessorNumber The handle number of processor.
906 ResetProcessorToIdleState (
907 IN UINTN ProcessorNumber
910 CPU_MP_DATA
*CpuMpData
;
912 CpuMpData
= GetCpuMpData ();
914 CpuMpData
->InitFlag
= ApInitReconfig
;
915 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
916 while (CpuMpData
->FinishedCount
< 1) {
919 CpuMpData
->InitFlag
= ApInitDone
;
921 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
925 Searches for the next waiting AP.
927 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
929 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
931 @retval EFI_SUCCESS The next waiting AP has been found.
932 @retval EFI_NOT_FOUND No waiting AP exists.
936 GetNextWaitingProcessorNumber (
937 OUT UINTN
*NextProcessorNumber
940 UINTN ProcessorNumber
;
941 CPU_MP_DATA
*CpuMpData
;
943 CpuMpData
= GetCpuMpData ();
945 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
946 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
947 *NextProcessorNumber
= ProcessorNumber
;
952 return EFI_NOT_FOUND
;
955 /** Checks status of specified AP.
957 This function checks whether the specified AP has finished the task assigned
958 by StartupThisAP(), and whether timeout expires.
960 @param[in] ProcessorNumber The handle number of processor.
962 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
963 @retval EFI_TIMEOUT The timeout expires.
964 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
968 IN UINTN ProcessorNumber
971 CPU_MP_DATA
*CpuMpData
;
972 CPU_AP_DATA
*CpuData
;
974 CpuMpData
= GetCpuMpData ();
975 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
978 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
979 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
980 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
983 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
985 if (GetApState(CpuData
) == CpuStateFinished
) {
986 if (CpuData
->Finished
!= NULL
) {
987 *(CpuData
->Finished
) = TRUE
;
989 SetApState (CpuData
, CpuStateIdle
);
993 // If timeout expires for StartupThisAP(), report timeout.
995 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
996 if (CpuData
->Finished
!= NULL
) {
997 *(CpuData
->Finished
) = FALSE
;
1000 // Reset failed AP to idle state
1002 ResetProcessorToIdleState (ProcessorNumber
);
1007 return EFI_NOT_READY
;
1011 Checks status of all APs.
1013 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1014 and whether timeout expires.
1016 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1017 @retval EFI_TIMEOUT The timeout expires.
1018 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1025 UINTN ProcessorNumber
;
1026 UINTN NextProcessorNumber
;
1029 CPU_MP_DATA
*CpuMpData
;
1030 CPU_AP_DATA
*CpuData
;
1032 CpuMpData
= GetCpuMpData ();
1034 NextProcessorNumber
= 0;
1037 // Go through all APs that are responsible for the StartupAllAPs().
1039 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1040 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1044 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1046 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1047 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1048 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1050 if (GetApState(CpuData
) == CpuStateFinished
) {
1051 CpuMpData
->RunningCount
++;
1052 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1053 SetApState(CpuData
, CpuStateIdle
);
1056 // If in Single Thread mode, then search for the next waiting AP for execution.
1058 if (CpuMpData
->SingleThread
) {
1059 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1061 if (!EFI_ERROR (Status
)) {
1065 (UINT32
) NextProcessorNumber
,
1066 CpuMpData
->Procedure
,
1067 CpuMpData
->ProcArguments
1075 // If all APs finish, return EFI_SUCCESS.
1077 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1082 // If timeout expires, report timeout.
1085 &CpuMpData
->CurrentTime
,
1086 &CpuMpData
->TotalTime
,
1087 CpuMpData
->ExpectedTime
)
1090 // If FailedCpuList is not NULL, record all failed APs in it.
1092 if (CpuMpData
->FailedCpuList
!= NULL
) {
1093 *CpuMpData
->FailedCpuList
=
1094 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1095 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1099 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1101 // Check whether this processor is responsible for StartupAllAPs().
1103 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1105 // Reset failed APs to idle state
1107 ResetProcessorToIdleState (ProcessorNumber
);
1108 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1109 if (CpuMpData
->FailedCpuList
!= NULL
) {
1110 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1114 if (CpuMpData
->FailedCpuList
!= NULL
) {
1115 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1119 return EFI_NOT_READY
;
1123 MP Initialize Library initialization.
1125 This service will allocate AP reset vector and wakeup all APs to do APs
1128 This service must be invoked before all other MP Initialize Library
1129 service are invoked.
1131 @retval EFI_SUCCESS MP initialization succeeds.
1132 @retval Others MP initialization fails.
1137 MpInitLibInitialize (
1141 CPU_MP_DATA
*OldCpuMpData
;
1142 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1143 UINT32 MaxLogicalProcessorNumber
;
1145 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1147 UINT32 MonitorFilterSize
;
1150 CPU_MP_DATA
*CpuMpData
;
1152 UINT8
*MonitorBuffer
;
1154 UINTN ApResetVectorSize
;
1155 UINTN BackupBufferAddr
;
1157 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1158 if (OldCpuMpData
== NULL
) {
1159 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1161 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1163 ASSERT (MaxLogicalProcessorNumber
!= 0);
1165 AsmGetAddressMap (&AddressMap
);
1166 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1167 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1168 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1170 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1171 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1172 BufferSize
+= sizeof (CPU_MP_DATA
);
1173 BufferSize
+= ApResetVectorSize
;
1174 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1175 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1176 ASSERT (MpBuffer
!= NULL
);
1177 ZeroMem (MpBuffer
, BufferSize
);
1178 Buffer
= (UINTN
) MpBuffer
;
1180 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1181 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1182 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1183 CpuMpData
->Buffer
= Buffer
;
1184 CpuMpData
->CpuApStackSize
= ApStackSize
;
1185 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1186 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1187 CpuMpData
->SaveRestoreFlag
= FALSE
;
1188 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1189 CpuMpData
->CpuCount
= 1;
1190 CpuMpData
->BspNumber
= 0;
1191 CpuMpData
->WaitEvent
= NULL
;
1192 CpuMpData
->SwitchBspFlag
= FALSE
;
1193 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1194 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1195 InitializeSpinLock(&CpuMpData
->MpLock
);
1197 // Save BSP's Control registers to APs
1199 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1201 // Set BSP basic information
1203 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1205 // Save assembly code information
1207 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1209 // Finally set AP loop mode
1211 CpuMpData
->ApLoopMode
= ApLoopMode
;
1212 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1214 // Set up APs wakeup signal buffer
1216 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1217 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1218 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1221 // Load Microcode on BSP
1223 MicrocodeDetect (CpuMpData
);
1225 // Store BSP's MTRR setting
1227 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1229 if (OldCpuMpData
== NULL
) {
1230 if (MaxLogicalProcessorNumber
> 1) {
1232 // Wakeup all APs and calculate the processor count in system
1234 CollectProcessorCount (CpuMpData
);
1238 // APs have been wakeup before, just get the CPU Information
1241 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1242 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1243 CpuMpData
->InitFlag
= ApInitReconfig
;
1244 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1245 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1246 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1247 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1248 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1249 CpuMpData
->X2ApicEnable
= TRUE
;
1251 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1252 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1254 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1255 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1256 sizeof (CPU_VOLATILE_REGISTERS
)
1259 if (MaxLogicalProcessorNumber
> 1) {
1261 // Wakeup APs to do some AP initialize sync
1263 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1265 // Wait for all APs finished initialization
1267 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1270 CpuMpData
->InitFlag
= ApInitDone
;
1271 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1272 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1278 // Initialize global data for MP support
1280 InitMpGlobalData (CpuMpData
);
1286 Gets detailed MP-related information on the requested processor at the
1287 instant this call is made. This service may only be called from the BSP.
1289 @param[in] ProcessorNumber The handle number of processor.
1290 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1291 the requested processor is deposited.
1292 @param[out] HealthData Return processor health data.
1294 @retval EFI_SUCCESS Processor information was returned.
1295 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1296 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1297 @retval EFI_NOT_FOUND The processor with the handle specified by
1298 ProcessorNumber does not exist in the platform.
1299 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1304 MpInitLibGetProcessorInfo (
1305 IN UINTN ProcessorNumber
,
1306 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1307 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1310 CPU_MP_DATA
*CpuMpData
;
1312 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1314 CpuMpData
= GetCpuMpData ();
1315 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1318 // Check whether caller processor is BSP
1320 MpInitLibWhoAmI (&CallerNumber
);
1321 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1322 return EFI_DEVICE_ERROR
;
1325 if (ProcessorInfoBuffer
== NULL
) {
1326 return EFI_INVALID_PARAMETER
;
1329 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1330 return EFI_NOT_FOUND
;
1333 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1334 ProcessorInfoBuffer
->StatusFlag
= 0;
1335 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1336 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1338 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1339 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1341 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1342 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1344 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1348 // Get processor location information
1350 GetProcessorLocationByApicId (
1351 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1352 &ProcessorInfoBuffer
->Location
.Package
,
1353 &ProcessorInfoBuffer
->Location
.Core
,
1354 &ProcessorInfoBuffer
->Location
.Thread
1357 if (HealthData
!= NULL
) {
1358 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1365 Worker function to switch the requested AP to be the BSP from that point onward.
1367 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1368 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1369 enabled AP. Otherwise, it will be disabled.
1371 @retval EFI_SUCCESS BSP successfully switched.
1372 @retval others Failed to switch BSP.
1377 IN UINTN ProcessorNumber
,
1378 IN BOOLEAN EnableOldBSP
1381 CPU_MP_DATA
*CpuMpData
;
1384 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1386 CpuMpData
= GetCpuMpData ();
1389 // Check whether caller processor is BSP
1391 MpInitLibWhoAmI (&CallerNumber
);
1392 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1396 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1397 return EFI_NOT_FOUND
;
1401 // Check whether specified AP is disabled
1403 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1404 if (State
== CpuStateDisabled
) {
1405 return EFI_INVALID_PARAMETER
;
1409 // Check whether ProcessorNumber specifies the current BSP
1411 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1412 return EFI_INVALID_PARAMETER
;
1416 // Check whether specified AP is busy
1418 if (State
== CpuStateBusy
) {
1419 return EFI_NOT_READY
;
1422 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1423 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1424 CpuMpData
->SwitchBspFlag
= TRUE
;
1425 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1428 // Clear the BSP bit of MSR_IA32_APIC_BASE
1430 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1431 ApicBaseMsr
.Bits
.BSP
= 0;
1432 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1435 // Need to wakeUp AP (future BSP).
1437 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1439 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1442 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1444 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1445 ApicBaseMsr
.Bits
.BSP
= 1;
1446 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1449 // Wait for old BSP finished AP task
1451 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1455 CpuMpData
->SwitchBspFlag
= FALSE
;
1457 // Set old BSP enable state
1459 if (!EnableOldBSP
) {
1460 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1463 // Save new BSP number
1465 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1471 Worker function to let the caller enable or disable an AP from this point onward.
1472 This service may only be called from the BSP.
1474 @param[in] ProcessorNumber The handle number of AP.
1475 @param[in] EnableAP Specifies the new state for the processor for
1476 enabled, FALSE for disabled.
1477 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1478 the new health status of the AP.
1480 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1481 @retval others Failed to Enable/Disable AP.
1485 EnableDisableApWorker (
1486 IN UINTN ProcessorNumber
,
1487 IN BOOLEAN EnableAP
,
1488 IN UINT32
*HealthFlag OPTIONAL
1491 CPU_MP_DATA
*CpuMpData
;
1494 CpuMpData
= GetCpuMpData ();
1497 // Check whether caller processor is BSP
1499 MpInitLibWhoAmI (&CallerNumber
);
1500 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1501 return EFI_DEVICE_ERROR
;
1504 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1505 return EFI_INVALID_PARAMETER
;
1508 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1509 return EFI_NOT_FOUND
;
1513 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1515 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1518 if (HealthFlag
!= NULL
) {
1519 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1520 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1527 This return the handle number for the calling processor. This service may be
1528 called from the BSP and APs.
1530 @param[out] ProcessorNumber Pointer to the handle number of AP.
1531 The range is from 0 to the total number of
1532 logical processors minus 1. The total number of
1533 logical processors can be retrieved by
1534 MpInitLibGetNumberOfProcessors().
1536 @retval EFI_SUCCESS The current processor handle number was returned
1538 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1539 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1545 OUT UINTN
*ProcessorNumber
1548 CPU_MP_DATA
*CpuMpData
;
1550 if (ProcessorNumber
== NULL
) {
1551 return EFI_INVALID_PARAMETER
;
1554 CpuMpData
= GetCpuMpData ();
1556 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1560 Retrieves the number of logical processor in the platform and the number of
1561 those logical processors that are enabled on this boot. This service may only
1562 be called from the BSP.
1564 @param[out] NumberOfProcessors Pointer to the total number of logical
1565 processors in the system, including the BSP
1567 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1568 processors that exist in system, including
1571 @retval EFI_SUCCESS The number of logical processors and enabled
1572 logical processors was retrieved.
1573 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1574 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1576 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1581 MpInitLibGetNumberOfProcessors (
1582 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1583 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1586 CPU_MP_DATA
*CpuMpData
;
1588 UINTN ProcessorNumber
;
1589 UINTN EnabledProcessorNumber
;
1592 CpuMpData
= GetCpuMpData ();
1594 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1595 return EFI_INVALID_PARAMETER
;
1599 // Check whether caller processor is BSP
1601 MpInitLibWhoAmI (&CallerNumber
);
1602 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1603 return EFI_DEVICE_ERROR
;
1606 ProcessorNumber
= CpuMpData
->CpuCount
;
1607 EnabledProcessorNumber
= 0;
1608 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1609 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1610 EnabledProcessorNumber
++;
1614 if (NumberOfProcessors
!= NULL
) {
1615 *NumberOfProcessors
= ProcessorNumber
;
1617 if (NumberOfEnabledProcessors
!= NULL
) {
1618 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1626 Worker function to execute a caller provided function on all enabled APs.
1628 @param[in] Procedure A pointer to the function to be run on
1629 enabled APs of the system.
1630 @param[in] SingleThread If TRUE, then all the enabled APs execute
1631 the function specified by Procedure one by
1632 one, in ascending order of processor handle
1633 number. If FALSE, then all the enabled APs
1634 execute the function specified by Procedure
1636 @param[in] WaitEvent The event created by the caller with CreateEvent()
1638 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1639 APs to return from Procedure, either for
1640 blocking or non-blocking mode.
1641 @param[in] ProcedureArgument The parameter passed into Procedure for
1643 @param[out] FailedCpuList If all APs finish successfully, then its
1644 content is set to NULL. If not all APs
1645 finish before timeout expires, then its
1646 content is set to address of the buffer
1647 holding handle numbers of the failed APs.
1649 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1650 the timeout expired.
1651 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1653 @retval others Failed to Startup all APs.
1657 StartupAllAPsWorker (
1658 IN EFI_AP_PROCEDURE Procedure
,
1659 IN BOOLEAN SingleThread
,
1660 IN EFI_EVENT WaitEvent OPTIONAL
,
1661 IN UINTN TimeoutInMicroseconds
,
1662 IN VOID
*ProcedureArgument OPTIONAL
,
1663 OUT UINTN
**FailedCpuList OPTIONAL
1667 CPU_MP_DATA
*CpuMpData
;
1668 UINTN ProcessorCount
;
1669 UINTN ProcessorNumber
;
1671 CPU_AP_DATA
*CpuData
;
1672 BOOLEAN HasEnabledAp
;
1675 CpuMpData
= GetCpuMpData ();
1677 if (FailedCpuList
!= NULL
) {
1678 *FailedCpuList
= NULL
;
1681 if (CpuMpData
->CpuCount
== 1) {
1682 return EFI_NOT_STARTED
;
1685 if (Procedure
== NULL
) {
1686 return EFI_INVALID_PARAMETER
;
1690 // Check whether caller processor is BSP
1692 MpInitLibWhoAmI (&CallerNumber
);
1693 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1694 return EFI_DEVICE_ERROR
;
1700 CheckAndUpdateApsStatus ();
1702 ProcessorCount
= CpuMpData
->CpuCount
;
1703 HasEnabledAp
= FALSE
;
1705 // Check whether all enabled APs are idle.
1706 // If any enabled AP is not idle, return EFI_NOT_READY.
1708 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1709 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1710 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1711 ApState
= GetApState (CpuData
);
1712 if (ApState
!= CpuStateDisabled
) {
1713 HasEnabledAp
= TRUE
;
1714 if (ApState
!= CpuStateIdle
) {
1716 // If any enabled APs are busy, return EFI_NOT_READY.
1718 return EFI_NOT_READY
;
1724 if (!HasEnabledAp
) {
1726 // If no enabled AP exists, return EFI_NOT_STARTED.
1728 return EFI_NOT_STARTED
;
1731 CpuMpData
->StartCount
= 0;
1732 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1733 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1734 CpuData
->Waiting
= FALSE
;
1735 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1736 if (CpuData
->State
== CpuStateIdle
) {
1738 // Mark this processor as responsible for current calling.
1740 CpuData
->Waiting
= TRUE
;
1741 CpuMpData
->StartCount
++;
1746 CpuMpData
->Procedure
= Procedure
;
1747 CpuMpData
->ProcArguments
= ProcedureArgument
;
1748 CpuMpData
->SingleThread
= SingleThread
;
1749 CpuMpData
->FinishedCount
= 0;
1750 CpuMpData
->RunningCount
= 0;
1751 CpuMpData
->FailedCpuList
= FailedCpuList
;
1752 CpuMpData
->ExpectedTime
= CalculateTimeout (
1753 TimeoutInMicroseconds
,
1754 &CpuMpData
->CurrentTime
1756 CpuMpData
->TotalTime
= 0;
1757 CpuMpData
->WaitEvent
= WaitEvent
;
1759 if (!SingleThread
) {
1760 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1762 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1763 if (ProcessorNumber
== CallerNumber
) {
1766 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1767 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1773 Status
= EFI_SUCCESS
;
1774 if (WaitEvent
== NULL
) {
1776 Status
= CheckAllAPs ();
1777 } while (Status
== EFI_NOT_READY
);
1784 Worker function to let the caller get one enabled AP to execute a caller-provided
1787 @param[in] Procedure A pointer to the function to be run on
1788 enabled APs of the system.
1789 @param[in] ProcessorNumber The handle number of the AP.
1790 @param[in] WaitEvent The event created by the caller with CreateEvent()
1792 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1793 APs to return from Procedure, either for
1794 blocking or non-blocking mode.
1795 @param[in] ProcedureArgument The parameter passed into Procedure for
1797 @param[out] Finished If AP returns from Procedure before the
1798 timeout expires, its content is set to TRUE.
1799 Otherwise, the value is set to FALSE.
1801 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1802 the timeout expires.
1803 @retval others Failed to Startup AP.
1807 StartupThisAPWorker (
1808 IN EFI_AP_PROCEDURE Procedure
,
1809 IN UINTN ProcessorNumber
,
1810 IN EFI_EVENT WaitEvent OPTIONAL
,
1811 IN UINTN TimeoutInMicroseconds
,
1812 IN VOID
*ProcedureArgument OPTIONAL
,
1813 OUT BOOLEAN
*Finished OPTIONAL
1817 CPU_MP_DATA
*CpuMpData
;
1818 CPU_AP_DATA
*CpuData
;
1821 CpuMpData
= GetCpuMpData ();
1823 if (Finished
!= NULL
) {
1828 // Check whether caller processor is BSP
1830 MpInitLibWhoAmI (&CallerNumber
);
1831 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1832 return EFI_DEVICE_ERROR
;
1836 // Check whether processor with the handle specified by ProcessorNumber exists
1838 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1839 return EFI_NOT_FOUND
;
1843 // Check whether specified processor is BSP
1845 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1846 return EFI_INVALID_PARAMETER
;
1850 // Check parameter Procedure
1852 if (Procedure
== NULL
) {
1853 return EFI_INVALID_PARAMETER
;
1859 CheckAndUpdateApsStatus ();
1862 // Check whether specified AP is disabled
1864 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1865 return EFI_INVALID_PARAMETER
;
1869 // If WaitEvent is not NULL, execute in non-blocking mode.
1870 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1871 // CheckAPsStatus() will check completion and timeout periodically.
1873 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1874 CpuData
->WaitEvent
= WaitEvent
;
1875 CpuData
->Finished
= Finished
;
1876 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
1877 CpuData
->TotalTime
= 0;
1879 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1882 // If WaitEvent is NULL, execute in blocking mode.
1883 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1885 Status
= EFI_SUCCESS
;
1886 if (WaitEvent
== NULL
) {
1888 Status
= CheckThisAP (ProcessorNumber
);
1889 } while (Status
== EFI_NOT_READY
);
1896 Get pointer to CPU MP Data structure from GUIDed HOB.
1898 @return The pointer to CPU MP Data structure.
1901 GetCpuMpDataFromGuidedHob (
1905 EFI_HOB_GUID_TYPE
*GuidHob
;
1907 CPU_MP_DATA
*CpuMpData
;
1910 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
1911 if (GuidHob
!= NULL
) {
1912 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
1913 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
1919 Get available system memory below 1MB by specified size.
1921 @param[in] CpuMpData The pointer to CPU MP Data structure.
1924 BackupAndPrepareWakeupBuffer(
1925 IN CPU_MP_DATA
*CpuMpData
1929 (VOID
*) CpuMpData
->BackupBuffer
,
1930 (VOID
*) CpuMpData
->WakeupBuffer
,
1931 CpuMpData
->BackupBufferSize
1934 (VOID
*) CpuMpData
->WakeupBuffer
,
1935 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
1936 CpuMpData
->AddressMap
.RendezvousFunnelSize
1941 Restore wakeup buffer data.
1943 @param[in] CpuMpData The pointer to CPU MP Data structure.
1946 RestoreWakeupBuffer(
1947 IN CPU_MP_DATA
*CpuMpData
1951 (VOID
*) CpuMpData
->WakeupBuffer
,
1952 (VOID
*) CpuMpData
->BackupBuffer
,
1953 CpuMpData
->BackupBufferSize