2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2017, 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.
20 #include <Register/Cpuid.h>
21 #include <Register/Msr.h>
22 #include <Register/LocalApic.h>
23 #include <Register/Microcode.h>
25 #include <Library/MpInitLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/LocalApicLib.h>
31 #include <Library/CpuLib.h>
32 #include <Library/UefiCpuLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/MtrrLib.h>
36 #include <Library/HobLib.h>
38 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
40 #define CPU_INIT_MP_LIB_HOB_GUID \
42 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
46 // The MP data for switch BSP
48 #define CPU_SWITCH_STATE_IDLE 0
49 #define CPU_SWITCH_STATE_STORED 1
50 #define CPU_SWITCH_STATE_LOADED 2
53 // CPU exchange information for switch BSP
56 UINT8 State
; // offset 0
57 UINTN StackPointer
; // offset 4 / 8
58 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
59 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
60 } CPU_EXCHANGE_ROLE_INFO
;
63 // AP loop state when APs are in idle state
64 // It's value is the same with PcdCpuApLoopMode
73 // AP initialization state during APs wakeup
93 // CPU volatile registers around INIT-SIPI-SIPI
105 } CPU_VOLATILE_REGISTERS
;
112 volatile UINT32
*StartupApSignal
;
113 volatile UINTN ApFunction
;
114 volatile UINTN ApFunctionArgument
;
116 volatile CPU_STATE State
;
117 CPU_VOLATILE_REGISTERS VolatileRegisters
;
127 // Basic CPU information saved in Guided HOB.
128 // Because the contents will be shard between PEI and DXE,
129 // we need to make sure the each fields offset same in different
134 UINT32 InitialApicId
;
142 // AP reset code information including code address and size,
143 // this structure will be shared be C code and assembly code.
144 // It is natural aligned by design.
147 UINT8
*RendezvousFunnelAddress
;
148 UINTN ModeEntryOffset
;
149 UINTN RendezvousFunnelSize
;
150 UINT8
*RelocateApLoopFuncAddress
;
151 UINTN RelocateApLoopFuncSize
;
152 } MP_ASSEMBLY_ADDRESS_MAP
;
154 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
159 // MP CPU exchange information for AP reset code
160 // This structure is required to be packed because fixed field offsets
161 // into this structure are used in assembly code in this module
168 IA32_DESCRIPTOR GdtrProfile
;
169 IA32_DESCRIPTOR IdtrProfile
;
175 UINTN EnableExecuteDisable
;
178 CPU_INFO_IN_HOB
*CpuInfo
;
179 UINTN NumApsExecuting
;
180 CPU_MP_DATA
*CpuMpData
;
181 UINTN InitializeFloatingPointUnitsAddress
;
182 } MP_CPU_EXCHANGE_INFO
;
187 // CPU MP Data save in memory
189 struct _CPU_MP_DATA
{
194 // The above fields data will be passed from PEI to DXE
195 // Please make sure the fields offset same in the different
200 UINTN CpuApStackSize
;
201 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
204 UINTN BackupBufferSize
;
206 volatile UINT32 StartCount
;
207 volatile UINT32 FinishedCount
;
208 volatile UINT32 RunningCount
;
209 BOOLEAN SingleThread
;
210 EFI_AP_PROCEDURE Procedure
;
217 UINTN
**FailedCpuList
;
219 AP_INIT_STATE InitFlag
;
220 BOOLEAN X2ApicEnable
;
221 BOOLEAN SwitchBspFlag
;
223 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
224 CPU_EXCHANGE_ROLE_INFO APInfo
;
225 MTRR_SETTINGS MtrrTable
;
227 UINT8 ApTargetCState
;
228 UINT16 PmCodeSegment
;
229 CPU_AP_DATA
*CpuData
;
230 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
232 UINT32 CurrentTimerCount
;
235 BOOLEAN PeriodicMode
;
236 BOOLEAN TimerInterruptState
;
237 UINT64 MicrocodePatchAddress
;
238 UINT64 MicrocodePatchRegionSize
;
241 extern EFI_GUID mCpuInitMpLibHobGuid
;
244 Assembly code to place AP into safe loop mode.
246 Place AP into targeted C-State if MONITOR is supported, otherwise
247 place AP into hlt state.
248 Place AP in protected mode if the current is long mode. Due to AP maybe
249 wakeup by some hardware event. It could avoid accessing page table that
250 may not available during booting to OS.
252 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
253 FALSE indicates MONITOR is not supported.
254 @param[in] ApTargetCState Target C-State value.
255 @param[in] PmCodeSegment Protected mode code segment value.
259 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
260 IN BOOLEAN MwaitSupport
,
261 IN UINTN ApTargetCState
,
262 IN UINTN PmCodeSegment
,
263 IN UINTN TopOfApStack
,
264 IN UINTN NumberToFinish
268 Assembly code to get starting address and size of the rendezvous entry for APs.
269 Information for fixing a jump instruction in the code is also returned.
271 @param[out] AddressMap Output buffer for address map information.
276 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
280 This function is called by both the BSP and the AP which is to become the BSP to
281 Exchange execution context including stack between them. After return from this
282 function, the BSP becomes AP and the AP becomes the BSP.
284 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
285 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
291 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
292 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
296 Get the pointer to CPU MP Data structure.
298 @return The pointer to CPU MP Data structure.
306 Save the pointer to CPU MP Data structure.
308 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
312 IN CPU_MP_DATA
*CpuMpData
317 Get available system memory below 1MB by specified size.
319 @param[in] WakeupBufferSize Wakeup buffer size required
321 @retval other Return wakeup buffer address below 1MB.
322 @retval -1 Cannot find free memory below 1MB.
326 IN UINTN WakeupBufferSize
330 This function will be called by BSP to wakeup AP.
332 @param[in] CpuMpData Pointer to CPU MP Data
333 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
334 FALSE: Send IPI to AP by ApicId
335 @param[in] ProcessorNumber The handle number of specified processor
336 @param[in] Procedure The function to be invoked by AP
337 @param[in] ProcedureArgument The argument to be passed into AP function
341 IN CPU_MP_DATA
*CpuMpData
,
342 IN BOOLEAN Broadcast
,
343 IN UINTN ProcessorNumber
,
344 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
345 IN VOID
*ProcedureArgument OPTIONAL
349 Initialize global data for MP support.
351 @param[in] CpuMpData The pointer to CPU MP Data structure.
355 IN CPU_MP_DATA
*CpuMpData
359 Worker function to execute a caller provided function on all enabled APs.
361 @param[in] Procedure A pointer to the function to be run on
362 enabled APs of the system.
363 @param[in] SingleThread If TRUE, then all the enabled APs execute
364 the function specified by Procedure one by
365 one, in ascending order of processor handle
366 number. If FALSE, then all the enabled APs
367 execute the function specified by Procedure
369 @param[in] WaitEvent The event created by the caller with CreateEvent()
371 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
372 APs to return from Procedure, either for
373 blocking or non-blocking mode.
374 @param[in] ProcedureArgument The parameter passed into Procedure for
376 @param[out] FailedCpuList If all APs finish successfully, then its
377 content is set to NULL. If not all APs
378 finish before timeout expires, then its
379 content is set to address of the buffer
380 holding handle numbers of the failed APs.
382 @retval EFI_SUCCESS In blocking mode, all APs have finished before
384 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
386 @retval others Failed to Startup all APs.
390 StartupAllAPsWorker (
391 IN EFI_AP_PROCEDURE Procedure
,
392 IN BOOLEAN SingleThread
,
393 IN EFI_EVENT WaitEvent OPTIONAL
,
394 IN UINTN TimeoutInMicroseconds
,
395 IN VOID
*ProcedureArgument OPTIONAL
,
396 OUT UINTN
**FailedCpuList OPTIONAL
400 Worker function to let the caller get one enabled AP to execute a caller-provided
403 @param[in] Procedure A pointer to the function to be run on
404 enabled APs of the system.
405 @param[in] ProcessorNumber The handle number of the AP.
406 @param[in] WaitEvent The event created by the caller with CreateEvent()
408 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
409 APs to return from Procedure, either for
410 blocking or non-blocking mode.
411 @param[in] ProcedureArgument The parameter passed into Procedure for
413 @param[out] Finished If AP returns from Procedure before the
414 timeout expires, its content is set to TRUE.
415 Otherwise, the value is set to FALSE.
417 @retval EFI_SUCCESS In blocking mode, specified AP finished before
419 @retval others Failed to Startup AP.
423 StartupThisAPWorker (
424 IN EFI_AP_PROCEDURE Procedure
,
425 IN UINTN ProcessorNumber
,
426 IN EFI_EVENT WaitEvent OPTIONAL
,
427 IN UINTN TimeoutInMicroseconds
,
428 IN VOID
*ProcedureArgument OPTIONAL
,
429 OUT BOOLEAN
*Finished OPTIONAL
433 Worker function to switch the requested AP to be the BSP from that point onward.
435 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
436 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
437 enabled AP. Otherwise, it will be disabled.
439 @retval EFI_SUCCESS BSP successfully switched.
440 @retval others Failed to switch BSP.
445 IN UINTN ProcessorNumber
,
446 IN BOOLEAN EnableOldBSP
450 Worker function to let the caller enable or disable an AP from this point onward.
451 This service may only be called from the BSP.
453 @param[in] ProcessorNumber The handle number of AP.
454 @param[in] EnableAP Specifies the new state for the processor for
455 enabled, FALSE for disabled.
456 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
457 the new health status of the AP.
459 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
460 @retval others Failed to Enable/Disable AP.
464 EnableDisableApWorker (
465 IN UINTN ProcessorNumber
,
467 IN UINT32
*HealthFlag OPTIONAL
471 Get pointer to CPU MP Data structure from GUIDed HOB.
473 @return The pointer to CPU MP Data structure.
476 GetCpuMpDataFromGuidedHob (
480 /** Checks status of specified AP.
482 This function checks whether the specified AP has finished the task assigned
483 by StartupThisAP(), and whether timeout expires.
485 @param[in] ProcessorNumber The handle number of processor.
487 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
488 @retval EFI_TIMEOUT The timeout expires.
489 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
493 IN UINTN ProcessorNumber
497 Checks status of all APs.
499 This function checks whether all APs have finished task assigned by StartupAllAPs(),
500 and whether timeout expires.
502 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
503 @retval EFI_TIMEOUT The timeout expires.
504 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
512 Checks APs status and updates APs status if needed.
516 CheckAndUpdateApsStatus (
521 Detect whether specified processor can find matching microcode patch and load it.
523 @param[in] CpuMpData The pointer to CPU MP Data structure.
527 IN CPU_MP_DATA
*CpuMpData
531 Detect whether Mwait-monitor feature is supported.
533 @retval TRUE Mwait-monitor feature is supported.
534 @retval FALSE Mwait-monitor feature is not supported.
542 Enable Debug Agent to support source debugging on AP function.