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 Get the Application Processors state.
22 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
28 IN CPU_AP_DATA
*CpuData
31 return CpuData
->State
;
35 Set the Application Processors state.
37 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
38 @param[in] State The AP status
42 IN CPU_AP_DATA
*CpuData
,
46 AcquireSpinLock (&CpuData
->ApLock
);
47 CpuData
->State
= State
;
48 ReleaseSpinLock (&CpuData
->ApLock
);
52 Save the volatile registers required to be restored following INIT IPI.
54 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
57 SaveVolatileRegisters (
58 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
61 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
63 VolatileRegisters
->Cr0
= AsmReadCr0 ();
64 VolatileRegisters
->Cr3
= AsmReadCr3 ();
65 VolatileRegisters
->Cr4
= AsmReadCr4 ();
67 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
68 if (VersionInfoEdx
.Bits
.DE
!= 0) {
70 // If processor supports Debugging Extensions feature
71 // by CPUID.[EAX=01H]:EDX.BIT2
73 VolatileRegisters
->Dr0
= AsmReadDr0 ();
74 VolatileRegisters
->Dr1
= AsmReadDr1 ();
75 VolatileRegisters
->Dr2
= AsmReadDr2 ();
76 VolatileRegisters
->Dr3
= AsmReadDr3 ();
77 VolatileRegisters
->Dr6
= AsmReadDr6 ();
78 VolatileRegisters
->Dr7
= AsmReadDr7 ();
83 Restore the volatile registers following INIT IPI.
85 @param[in] VolatileRegisters Pointer to volatile resisters
86 @param[in] IsRestoreDr TRUE: Restore DRx if supported
87 FALSE: Do not restore DRx
90 RestoreVolatileRegisters (
91 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
92 IN BOOLEAN IsRestoreDr
95 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
97 AsmWriteCr0 (VolatileRegisters
->Cr0
);
98 AsmWriteCr3 (VolatileRegisters
->Cr3
);
99 AsmWriteCr4 (VolatileRegisters
->Cr4
);
102 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
103 if (VersionInfoEdx
.Bits
.DE
!= 0) {
105 // If processor supports Debugging Extensions feature
106 // by CPUID.[EAX=01H]:EDX.BIT2
108 AsmWriteDr0 (VolatileRegisters
->Dr0
);
109 AsmWriteDr1 (VolatileRegisters
->Dr1
);
110 AsmWriteDr2 (VolatileRegisters
->Dr2
);
111 AsmWriteDr3 (VolatileRegisters
->Dr3
);
112 AsmWriteDr6 (VolatileRegisters
->Dr6
);
113 AsmWriteDr7 (VolatileRegisters
->Dr7
);
119 Detect whether Mwait-monitor feature is supported.
121 @retval TRUE Mwait-monitor feature is supported.
122 @retval FALSE Mwait-monitor feature is not supported.
129 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
131 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
132 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
138 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
140 @return The AP loop mode.
144 OUT UINT32
*MonitorFilterSize
148 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
150 ASSERT (MonitorFilterSize
!= NULL
);
152 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
153 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
154 if (ApLoopMode
== ApInMwaitLoop
) {
155 if (!IsMwaitSupport ()) {
157 // If processor does not support MONITOR/MWAIT feature,
158 // force AP in Hlt-loop mode
160 ApLoopMode
= ApInHltLoop
;
164 if (ApLoopMode
!= ApInMwaitLoop
) {
165 *MonitorFilterSize
= sizeof (UINT32
);
168 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
169 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
171 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
172 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
178 Initialize CPU AP Data when AP is wakeup at the first time.
180 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
181 @param[in] ProcessorNumber The handle number of processor
182 @param[in] BistData Processor BIST data
187 IN OUT CPU_MP_DATA
*CpuMpData
,
188 IN UINTN ProcessorNumber
,
192 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
193 CpuMpData
->CpuData
[ProcessorNumber
].Health
= BistData
;
194 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
195 CpuMpData
->CpuData
[ProcessorNumber
].ApicId
= GetApicId ();
196 CpuMpData
->CpuData
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
197 if (CpuMpData
->CpuData
[ProcessorNumber
].InitialApicId
>= 0xFF) {
199 // Set x2APIC mode if there are any logical processor reporting
200 // an Initial APIC ID of 255 or greater.
202 AcquireSpinLock(&CpuMpData
->MpLock
);
203 CpuMpData
->X2ApicEnable
= TRUE
;
204 ReleaseSpinLock(&CpuMpData
->MpLock
);
207 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
208 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
212 MP Initialize Library initialization.
214 This service will allocate AP reset vector and wakeup all APs to do APs
217 This service must be invoked before all other MP Initialize Library
220 @retval EFI_SUCCESS MP initialization succeeds.
221 @retval Others MP initialization fails.
226 MpInitLibInitialize (
230 UINT32 MaxLogicalProcessorNumber
;
232 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
234 UINT32 MonitorFilterSize
;
237 CPU_MP_DATA
*CpuMpData
;
239 UINT8
*MonitorBuffer
;
241 UINTN ApResetVectorSize
;
242 UINTN BackupBufferAddr
;
243 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
245 AsmGetAddressMap (&AddressMap
);
246 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
247 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
248 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
250 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
251 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
252 BufferSize
+= sizeof (CPU_MP_DATA
);
253 BufferSize
+= ApResetVectorSize
;
254 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
255 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
256 ASSERT (MpBuffer
!= NULL
);
257 ZeroMem (MpBuffer
, BufferSize
);
258 Buffer
= (UINTN
) MpBuffer
;
260 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
261 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
262 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
263 CpuMpData
->Buffer
= Buffer
;
264 CpuMpData
->CpuApStackSize
= ApStackSize
;
265 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
266 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
267 CpuMpData
->EndOfPeiFlag
= FALSE
;
268 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
269 CpuMpData
->CpuCount
= 1;
270 CpuMpData
->BspNumber
= 0;
271 CpuMpData
->WaitEvent
= NULL
;
272 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
273 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
274 InitializeSpinLock(&CpuMpData
->MpLock
);
276 // Save BSP's Control registers to APs
278 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
280 // Set BSP basic information
282 InitializeApData (CpuMpData
, 0, 0);
284 // Save assembly code information
286 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
288 // Finally set AP loop mode
290 CpuMpData
->ApLoopMode
= ApLoopMode
;
291 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
293 // Set up APs wakeup signal buffer
295 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
296 CpuMpData
->CpuData
[Index
].StartupApSignal
=
297 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
300 // Load Microcode on BSP
302 MicrocodeDetect (CpuMpData
);
304 // Store BSP's MTRR setting
306 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
310 // Initialize global data for MP support
312 InitMpGlobalData (CpuMpData
);
318 Gets detailed MP-related information on the requested processor at the
319 instant this call is made. This service may only be called from the BSP.
321 @param[in] ProcessorNumber The handle number of processor.
322 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
323 the requested processor is deposited.
324 @param[out] HealthData Return processor health data.
326 @retval EFI_SUCCESS Processor information was returned.
327 @retval EFI_DEVICE_ERROR The calling processor is an AP.
328 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
329 @retval EFI_NOT_FOUND The processor with the handle specified by
330 ProcessorNumber does not exist in the platform.
331 @retval EFI_NOT_READY MP Initialize Library is not initialized.
336 MpInitLibGetProcessorInfo (
337 IN UINTN ProcessorNumber
,
338 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
339 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
342 return EFI_UNSUPPORTED
;
345 This return the handle number for the calling processor. This service may be
346 called from the BSP and APs.
348 @param[out] ProcessorNumber Pointer to the handle number of AP.
349 The range is from 0 to the total number of
350 logical processors minus 1. The total number of
351 logical processors can be retrieved by
352 MpInitLibGetNumberOfProcessors().
354 @retval EFI_SUCCESS The current processor handle number was returned
356 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
357 @retval EFI_NOT_READY MP Initialize Library is not initialized.
363 OUT UINTN
*ProcessorNumber
366 return EFI_UNSUPPORTED
;
369 Retrieves the number of logical processor in the platform and the number of
370 those logical processors that are enabled on this boot. This service may only
371 be called from the BSP.
373 @param[out] NumberOfProcessors Pointer to the total number of logical
374 processors in the system, including the BSP
376 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
377 processors that exist in system, including
380 @retval EFI_SUCCESS The number of logical processors and enabled
381 logical processors was retrieved.
382 @retval EFI_DEVICE_ERROR The calling processor is an AP.
383 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
385 @retval EFI_NOT_READY MP Initialize Library is not initialized.
390 MpInitLibGetNumberOfProcessors (
391 OUT UINTN
*NumberOfProcessors
, OPTIONAL
392 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
395 return EFI_UNSUPPORTED
;
398 Get pointer to CPU MP Data structure from GUIDed HOB.
400 @return The pointer to CPU MP Data structure.
403 GetCpuMpDataFromGuidedHob (
407 EFI_HOB_GUID_TYPE
*GuidHob
;
409 CPU_MP_DATA
*CpuMpData
;
412 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
413 if (GuidHob
!= NULL
) {
414 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
415 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);