2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Register/Cpuid.h>
15 #include <Register/Msr.h>
16 #include <Register/LocalApic.h>
17 #include <Register/Microcode.h>
19 #include <Library/MpInitLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/LocalApicLib.h>
25 #include <Library/CpuLib.h>
26 #include <Library/UefiCpuLib.h>
27 #include <Library/TimerLib.h>
28 #include <Library/SynchronizationLib.h>
29 #include <Library/MtrrLib.h>
30 #include <Library/HobLib.h>
32 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
34 #define CPU_INIT_MP_LIB_HOB_GUID \
36 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
40 // The MP data for switch BSP
42 #define CPU_SWITCH_STATE_IDLE 0
43 #define CPU_SWITCH_STATE_STORED 1
44 #define CPU_SWITCH_STATE_LOADED 2
47 // CPU exchange information for switch BSP
50 UINT8 State
; // offset 0
51 UINTN StackPointer
; // offset 4 / 8
52 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
53 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
54 } CPU_EXCHANGE_ROLE_INFO
;
57 // AP loop state when APs are in idle state
58 // It's value is the same with PcdCpuApLoopMode
67 // AP initialization state during APs wakeup
78 // The state transitions for an AP when it process a procedure are:
79 // Idle ----> Ready ----> Busy ----> Idle
91 // CPU volatile registers around INIT-SIPI-SIPI
103 IA32_DESCRIPTOR Gdtr
;
104 IA32_DESCRIPTOR Idtr
;
106 } CPU_VOLATILE_REGISTERS
;
113 volatile UINT32
*StartupApSignal
;
114 volatile UINTN ApFunction
;
115 volatile UINTN ApFunctionArgument
;
117 volatile CPU_STATE State
;
118 CPU_VOLATILE_REGISTERS VolatileRegisters
;
128 // Basic CPU information saved in Guided HOB.
129 // Because the contents will be shard between PEI and DXE,
130 // we need to make sure the each fields offset same in different
135 UINT32 InitialApicId
;
143 // AP reset code information including code address and size,
144 // this structure will be shared be C code and assembly code.
145 // It is natural aligned by design.
148 UINT8
*RendezvousFunnelAddress
;
149 UINTN ModeEntryOffset
;
150 UINTN RendezvousFunnelSize
;
151 UINT8
*RelocateApLoopFuncAddress
;
152 UINTN RelocateApLoopFuncSize
;
153 UINTN ModeTransitionOffset
;
154 } MP_ASSEMBLY_ADDRESS_MAP
;
156 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
161 // MP CPU exchange information for AP reset code
162 // This structure is required to be packed because fixed field offsets
163 // into this structure are used in assembly code in this module
170 IA32_DESCRIPTOR GdtrProfile
;
171 IA32_DESCRIPTOR IdtrProfile
;
177 UINTN EnableExecuteDisable
;
180 CPU_INFO_IN_HOB
*CpuInfo
;
181 UINTN NumApsExecuting
;
182 CPU_MP_DATA
*CpuMpData
;
183 UINTN InitializeFloatingPointUnitsAddress
;
184 UINT32 ModeTransitionMemory
;
185 UINT16 ModeTransitionSegment
;
186 UINT32 ModeHighMemory
;
187 UINT16 ModeHighSegment
;
188 } MP_CPU_EXCHANGE_INFO
;
193 // CPU MP Data save in memory
195 struct _CPU_MP_DATA
{
200 // The above fields data will be passed from PEI to DXE
201 // Please make sure the fields offset same in the different
206 UINTN CpuApStackSize
;
207 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
209 UINTN WakeupBufferHigh
;
211 UINTN BackupBufferSize
;
213 volatile UINT32 FinishedCount
;
215 BOOLEAN SingleThread
;
216 EFI_AP_PROCEDURE Procedure
;
223 UINTN
**FailedCpuList
;
225 AP_INIT_STATE InitFlag
;
226 BOOLEAN X2ApicEnable
;
227 BOOLEAN SwitchBspFlag
;
229 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
230 CPU_EXCHANGE_ROLE_INFO APInfo
;
231 MTRR_SETTINGS MtrrTable
;
233 UINT8 ApTargetCState
;
234 UINT16 PmCodeSegment
;
235 CPU_AP_DATA
*CpuData
;
236 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
238 UINT32 CurrentTimerCount
;
241 BOOLEAN PeriodicMode
;
242 BOOLEAN TimerInterruptState
;
243 UINT64 MicrocodePatchAddress
;
244 UINT64 MicrocodePatchRegionSize
;
246 UINT32 ProcessorSignature
;
247 UINT32 ProcessorFlags
;
248 UINT64 MicrocodeDataAddress
;
249 UINT32 MicrocodeRevision
;
252 // Whether need to use Init-Sipi-Sipi to wake up the APs.
253 // Two cases need to set this value to TRUE. One is in HLT
254 // loop mode, the other is resume from S3 which loop mode
255 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
258 BOOLEAN WakeUpByInitSipiSipi
;
261 extern EFI_GUID mCpuInitMpLibHobGuid
;
264 Assembly code to place AP into safe loop mode.
266 Place AP into targeted C-State if MONITOR is supported, otherwise
267 place AP into hlt state.
268 Place AP in protected mode if the current is long mode. Due to AP maybe
269 wakeup by some hardware event. It could avoid accessing page table that
270 may not available during booting to OS.
272 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
273 FALSE indicates MONITOR is not supported.
274 @param[in] ApTargetCState Target C-State value.
275 @param[in] PmCodeSegment Protected mode code segment value.
279 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
280 IN BOOLEAN MwaitSupport
,
281 IN UINTN ApTargetCState
,
282 IN UINTN PmCodeSegment
,
283 IN UINTN TopOfApStack
,
284 IN UINTN NumberToFinish
288 Assembly code to get starting address and size of the rendezvous entry for APs.
289 Information for fixing a jump instruction in the code is also returned.
291 @param[out] AddressMap Output buffer for address map information.
296 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
300 This function is called by both the BSP and the AP which is to become the BSP to
301 Exchange execution context including stack between them. After return from this
302 function, the BSP becomes AP and the AP becomes the BSP.
304 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
305 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
311 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
312 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
316 Get the pointer to CPU MP Data structure.
318 @return The pointer to CPU MP Data structure.
326 Save the pointer to CPU MP Data structure.
328 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
332 IN CPU_MP_DATA
*CpuMpData
337 Get available system memory below 1MB by specified size.
339 @param[in] WakeupBufferSize Wakeup buffer size required
341 @retval other Return wakeup buffer address below 1MB.
342 @retval -1 Cannot find free memory below 1MB.
346 IN UINTN WakeupBufferSize
350 Get available EfiBootServicesCode memory below 4GB by specified size.
352 This buffer is required to safely transfer AP from real address mode to
353 protected mode or long mode, due to the fact that the buffer returned by
354 GetWakeupBuffer() may be marked as non-executable.
356 @param[in] BufferSize Wakeup transition buffer size.
358 @retval other Return wakeup transition buffer address below 4GB.
359 @retval 0 Cannot find free memory below 4GB.
362 GetModeTransitionBuffer (
367 This function will be called by BSP to wakeup AP.
369 @param[in] CpuMpData Pointer to CPU MP Data
370 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
371 FALSE: Send IPI to AP by ApicId
372 @param[in] ProcessorNumber The handle number of specified processor
373 @param[in] Procedure The function to be invoked by AP
374 @param[in] ProcedureArgument The argument to be passed into AP function
375 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
379 IN CPU_MP_DATA
*CpuMpData
,
380 IN BOOLEAN Broadcast
,
381 IN UINTN ProcessorNumber
,
382 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
383 IN VOID
*ProcedureArgument
, OPTIONAL
384 IN BOOLEAN WakeUpDisabledAps OPTIONAL
388 Initialize global data for MP support.
390 @param[in] CpuMpData The pointer to CPU MP Data structure.
394 IN CPU_MP_DATA
*CpuMpData
398 Worker function to execute a caller provided function on all enabled APs.
400 @param[in] Procedure A pointer to the function to be run on
401 enabled APs of the system.
402 @param[in] SingleThread If TRUE, then all the enabled APs execute
403 the function specified by Procedure one by
404 one, in ascending order of processor handle
405 number. If FALSE, then all the enabled APs
406 execute the function specified by Procedure
408 @param[in] WaitEvent The event created by the caller with CreateEvent()
410 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
411 APs to return from Procedure, either for
412 blocking or non-blocking mode.
413 @param[in] ProcedureArgument The parameter passed into Procedure for
415 @param[out] FailedCpuList If all APs finish successfully, then its
416 content is set to NULL. If not all APs
417 finish before timeout expires, then its
418 content is set to address of the buffer
419 holding handle numbers of the failed APs.
421 @retval EFI_SUCCESS In blocking mode, all APs have finished before
423 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
425 @retval others Failed to Startup all APs.
429 StartupAllAPsWorker (
430 IN EFI_AP_PROCEDURE Procedure
,
431 IN BOOLEAN SingleThread
,
432 IN EFI_EVENT WaitEvent OPTIONAL
,
433 IN UINTN TimeoutInMicroseconds
,
434 IN VOID
*ProcedureArgument OPTIONAL
,
435 OUT UINTN
**FailedCpuList OPTIONAL
439 Worker function to let the caller get one enabled AP to execute a caller-provided
442 @param[in] Procedure A pointer to the function to be run on
443 enabled APs of the system.
444 @param[in] ProcessorNumber The handle number of the AP.
445 @param[in] WaitEvent The event created by the caller with CreateEvent()
447 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
448 APs to return from Procedure, either for
449 blocking or non-blocking mode.
450 @param[in] ProcedureArgument The parameter passed into Procedure for
452 @param[out] Finished If AP returns from Procedure before the
453 timeout expires, its content is set to TRUE.
454 Otherwise, the value is set to FALSE.
456 @retval EFI_SUCCESS In blocking mode, specified AP finished before
458 @retval others Failed to Startup AP.
462 StartupThisAPWorker (
463 IN EFI_AP_PROCEDURE Procedure
,
464 IN UINTN ProcessorNumber
,
465 IN EFI_EVENT WaitEvent OPTIONAL
,
466 IN UINTN TimeoutInMicroseconds
,
467 IN VOID
*ProcedureArgument OPTIONAL
,
468 OUT BOOLEAN
*Finished OPTIONAL
472 Worker function to switch the requested AP to be the BSP from that point onward.
474 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
475 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
476 enabled AP. Otherwise, it will be disabled.
478 @retval EFI_SUCCESS BSP successfully switched.
479 @retval others Failed to switch BSP.
484 IN UINTN ProcessorNumber
,
485 IN BOOLEAN EnableOldBSP
489 Worker function to let the caller enable or disable an AP from this point onward.
490 This service may only be called from the BSP.
492 @param[in] ProcessorNumber The handle number of AP.
493 @param[in] EnableAP Specifies the new state for the processor for
494 enabled, FALSE for disabled.
495 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
496 the new health status of the AP.
498 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
499 @retval others Failed to Enable/Disable AP.
503 EnableDisableApWorker (
504 IN UINTN ProcessorNumber
,
506 IN UINT32
*HealthFlag OPTIONAL
510 Get pointer to CPU MP Data structure from GUIDed HOB.
512 @return The pointer to CPU MP Data structure.
515 GetCpuMpDataFromGuidedHob (
519 /** Checks status of specified AP.
521 This function checks whether the specified AP has finished the task assigned
522 by StartupThisAP(), and whether timeout expires.
524 @param[in] ProcessorNumber The handle number of processor.
526 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
527 @retval EFI_TIMEOUT The timeout expires.
528 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
532 IN UINTN ProcessorNumber
536 Checks status of all APs.
538 This function checks whether all APs have finished task assigned by StartupAllAPs(),
539 and whether timeout expires.
541 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
542 @retval EFI_TIMEOUT The timeout expires.
543 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
551 Checks APs status and updates APs status if needed.
555 CheckAndUpdateApsStatus (
560 Detect whether specified processor can find matching microcode patch and load it.
562 @param[in] CpuMpData The pointer to CPU MP Data structure.
563 @param[in] IsBspCallIn Indicate whether the caller is BSP or not.
567 IN CPU_MP_DATA
*CpuMpData
,
568 IN BOOLEAN IsBspCallIn
572 Detect whether Mwait-monitor feature is supported.
574 @retval TRUE Mwait-monitor feature is supported.
575 @retval FALSE Mwait-monitor feature is not supported.
583 Enable Debug Agent to support source debugging on AP function.