2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Register/Intel/Cpuid.h>
15 #include <Register/Intel/Msr.h>
16 #include <Register/Intel/LocalApic.h>
17 #include <Register/Intel/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 // Default maximum number of entries to store the microcode patches information
49 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
52 // Data structure for microcode patch information
58 } MICROCODE_PATCH_INFO
;
61 // CPU exchange information for switch BSP
64 UINT8 State
; // offset 0
65 UINTN StackPointer
; // offset 4 / 8
66 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
67 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
68 } CPU_EXCHANGE_ROLE_INFO
;
71 // AP loop state when APs are in idle state
72 // It's value is the same with PcdCpuApLoopMode
81 // AP initialization state during APs wakeup
92 // The state transitions for an AP when it process a procedure are:
93 // Idle ----> Ready ----> Busy ----> Idle
105 // CPU volatile registers around INIT-SIPI-SIPI
117 IA32_DESCRIPTOR Gdtr
;
118 IA32_DESCRIPTOR Idtr
;
120 } CPU_VOLATILE_REGISTERS
;
127 volatile UINT32
*StartupApSignal
;
128 volatile UINTN ApFunction
;
129 volatile UINTN ApFunctionArgument
;
131 volatile CPU_STATE State
;
132 CPU_VOLATILE_REGISTERS VolatileRegisters
;
139 UINT32 ProcessorSignature
;
141 UINT64 MicrocodeEntryAddr
;
145 // Basic CPU information saved in Guided HOB.
146 // Because the contents will be shard between PEI and DXE,
147 // we need to make sure the each fields offset same in different
152 UINT32 InitialApicId
;
160 // AP reset code information including code address and size,
161 // this structure will be shared be C code and assembly code.
162 // It is natural aligned by design.
165 UINT8
*RendezvousFunnelAddress
;
166 UINTN ModeEntryOffset
;
167 UINTN RendezvousFunnelSize
;
168 UINT8
*RelocateApLoopFuncAddress
;
169 UINTN RelocateApLoopFuncSize
;
170 UINTN ModeTransitionOffset
;
171 } MP_ASSEMBLY_ADDRESS_MAP
;
173 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
178 // MP CPU exchange information for AP reset code
179 // This structure is required to be packed because fixed field offsets
180 // into this structure are used in assembly code in this module
187 IA32_DESCRIPTOR GdtrProfile
;
188 IA32_DESCRIPTOR IdtrProfile
;
194 UINTN EnableExecuteDisable
;
197 CPU_INFO_IN_HOB
*CpuInfo
;
198 UINTN NumApsExecuting
;
199 CPU_MP_DATA
*CpuMpData
;
200 UINTN InitializeFloatingPointUnitsAddress
;
201 UINT32 ModeTransitionMemory
;
202 UINT16 ModeTransitionSegment
;
203 UINT32 ModeHighMemory
;
204 UINT16 ModeHighSegment
;
206 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
208 BOOLEAN Enable5LevelPaging
;
209 } MP_CPU_EXCHANGE_INFO
;
214 // CPU MP Data save in memory
216 struct _CPU_MP_DATA
{
220 UINT64 MicrocodePatchAddress
;
221 UINT64 MicrocodePatchRegionSize
;
223 // The above fields data will be passed from PEI to DXE
224 // Please make sure the fields offset same in the different
229 UINTN CpuApStackSize
;
230 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
232 UINTN WakeupBufferHigh
;
234 UINTN BackupBufferSize
;
236 volatile UINT32 FinishedCount
;
238 BOOLEAN SingleThread
;
239 EFI_AP_PROCEDURE Procedure
;
246 UINTN
**FailedCpuList
;
248 AP_INIT_STATE InitFlag
;
249 BOOLEAN SwitchBspFlag
;
251 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
252 CPU_EXCHANGE_ROLE_INFO APInfo
;
253 MTRR_SETTINGS MtrrTable
;
255 UINT8 ApTargetCState
;
256 UINT16 PmCodeSegment
;
257 CPU_AP_DATA
*CpuData
;
258 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
260 UINT32 CurrentTimerCount
;
263 BOOLEAN PeriodicMode
;
264 BOOLEAN TimerInterruptState
;
266 UINT32 ProcessorSignature
;
267 UINT32 ProcessorFlags
;
268 UINT64 MicrocodeDataAddress
;
269 UINT32 MicrocodeRevision
;
272 // Whether need to use Init-Sipi-Sipi to wake up the APs.
273 // Two cases need to set this value to TRUE. One is in HLT
274 // loop mode, the other is resume from S3 which loop mode
275 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
278 BOOLEAN WakeUpByInitSipiSipi
;
281 extern EFI_GUID mCpuInitMpLibHobGuid
;
284 Assembly code to place AP into safe loop mode.
286 Place AP into targeted C-State if MONITOR is supported, otherwise
287 place AP into hlt state.
288 Place AP in protected mode if the current is long mode. Due to AP maybe
289 wakeup by some hardware event. It could avoid accessing page table that
290 may not available during booting to OS.
292 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
293 FALSE indicates MONITOR is not supported.
294 @param[in] ApTargetCState Target C-State value.
295 @param[in] PmCodeSegment Protected mode code segment value.
299 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
300 IN BOOLEAN MwaitSupport
,
301 IN UINTN ApTargetCState
,
302 IN UINTN PmCodeSegment
,
303 IN UINTN TopOfApStack
,
304 IN UINTN NumberToFinish
308 Assembly code to get starting address and size of the rendezvous entry for APs.
309 Information for fixing a jump instruction in the code is also returned.
311 @param[out] AddressMap Output buffer for address map information.
316 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
320 This function is called by both the BSP and the AP which is to become the BSP to
321 Exchange execution context including stack between them. After return from this
322 function, the BSP becomes AP and the AP becomes the BSP.
324 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
325 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
331 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
332 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
336 Get the pointer to CPU MP Data structure.
338 @return The pointer to CPU MP Data structure.
346 Save the pointer to CPU MP Data structure.
348 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
352 IN CPU_MP_DATA
*CpuMpData
357 Get available system memory below 1MB by specified size.
359 @param[in] WakeupBufferSize Wakeup buffer size required
361 @retval other Return wakeup buffer address below 1MB.
362 @retval -1 Cannot find free memory below 1MB.
366 IN UINTN WakeupBufferSize
370 Get available EfiBootServicesCode memory below 4GB by specified size.
372 This buffer is required to safely transfer AP from real address mode to
373 protected mode or long mode, due to the fact that the buffer returned by
374 GetWakeupBuffer() may be marked as non-executable.
376 @param[in] BufferSize Wakeup transition buffer size.
378 @retval other Return wakeup transition buffer address below 4GB.
379 @retval 0 Cannot find free memory below 4GB.
382 GetModeTransitionBuffer (
387 This function will be called by BSP to wakeup AP.
389 @param[in] CpuMpData Pointer to CPU MP Data
390 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
391 FALSE: Send IPI to AP by ApicId
392 @param[in] ProcessorNumber The handle number of specified processor
393 @param[in] Procedure The function to be invoked by AP
394 @param[in] ProcedureArgument The argument to be passed into AP function
395 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
399 IN CPU_MP_DATA
*CpuMpData
,
400 IN BOOLEAN Broadcast
,
401 IN UINTN ProcessorNumber
,
402 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
403 IN VOID
*ProcedureArgument
, OPTIONAL
404 IN BOOLEAN WakeUpDisabledAps OPTIONAL
408 Initialize global data for MP support.
410 @param[in] CpuMpData The pointer to CPU MP Data structure.
414 IN CPU_MP_DATA
*CpuMpData
418 Worker function to execute a caller provided function on all enabled APs.
420 @param[in] Procedure A pointer to the function to be run on
421 enabled APs of the system.
422 @param[in] SingleThread If TRUE, then all the enabled APs execute
423 the function specified by Procedure one by
424 one, in ascending order of processor handle
425 number. If FALSE, then all the enabled APs
426 execute the function specified by Procedure
428 @param[in] ExcludeBsp Whether let BSP also trig this task.
429 @param[in] WaitEvent The event created by the caller with CreateEvent()
431 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
432 APs to return from Procedure, either for
433 blocking or non-blocking mode.
434 @param[in] ProcedureArgument The parameter passed into Procedure for
436 @param[out] FailedCpuList If all APs finish successfully, then its
437 content is set to NULL. If not all APs
438 finish before timeout expires, then its
439 content is set to address of the buffer
440 holding handle numbers of the failed APs.
442 @retval EFI_SUCCESS In blocking mode, all APs have finished before
444 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
446 @retval others Failed to Startup all APs.
450 StartupAllCPUsWorker (
451 IN EFI_AP_PROCEDURE Procedure
,
452 IN BOOLEAN SingleThread
,
453 IN BOOLEAN ExcludeBsp
,
454 IN EFI_EVENT WaitEvent OPTIONAL
,
455 IN UINTN TimeoutInMicroseconds
,
456 IN VOID
*ProcedureArgument OPTIONAL
,
457 OUT UINTN
**FailedCpuList OPTIONAL
461 Worker function to let the caller get one enabled AP to execute a caller-provided
464 @param[in] Procedure A pointer to the function to be run on
465 enabled APs of the system.
466 @param[in] ProcessorNumber The handle number of the AP.
467 @param[in] WaitEvent The event created by the caller with CreateEvent()
469 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
470 APs to return from Procedure, either for
471 blocking or non-blocking mode.
472 @param[in] ProcedureArgument The parameter passed into Procedure for
474 @param[out] Finished If AP returns from Procedure before the
475 timeout expires, its content is set to TRUE.
476 Otherwise, the value is set to FALSE.
478 @retval EFI_SUCCESS In blocking mode, specified AP finished before
480 @retval others Failed to Startup AP.
484 StartupThisAPWorker (
485 IN EFI_AP_PROCEDURE Procedure
,
486 IN UINTN ProcessorNumber
,
487 IN EFI_EVENT WaitEvent OPTIONAL
,
488 IN UINTN TimeoutInMicroseconds
,
489 IN VOID
*ProcedureArgument OPTIONAL
,
490 OUT BOOLEAN
*Finished OPTIONAL
494 Worker function to switch the requested AP to be the BSP from that point onward.
496 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
497 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
498 enabled AP. Otherwise, it will be disabled.
500 @retval EFI_SUCCESS BSP successfully switched.
501 @retval others Failed to switch BSP.
506 IN UINTN ProcessorNumber
,
507 IN BOOLEAN EnableOldBSP
511 Worker function to let the caller enable or disable an AP from this point onward.
512 This service may only be called from the BSP.
514 @param[in] ProcessorNumber The handle number of AP.
515 @param[in] EnableAP Specifies the new state for the processor for
516 enabled, FALSE for disabled.
517 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
518 the new health status of the AP.
520 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
521 @retval others Failed to Enable/Disable AP.
525 EnableDisableApWorker (
526 IN UINTN ProcessorNumber
,
528 IN UINT32
*HealthFlag OPTIONAL
532 Get pointer to CPU MP Data structure from GUIDed HOB.
534 @return The pointer to CPU MP Data structure.
537 GetCpuMpDataFromGuidedHob (
541 /** Checks status of specified AP.
543 This function checks whether the specified AP has finished the task assigned
544 by StartupThisAP(), and whether timeout expires.
546 @param[in] ProcessorNumber The handle number of processor.
548 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
549 @retval EFI_TIMEOUT The timeout expires.
550 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
554 IN UINTN ProcessorNumber
558 Checks status of all APs.
560 This function checks whether all APs have finished task assigned by StartupAllAPs(),
561 and whether timeout expires.
563 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
564 @retval EFI_TIMEOUT The timeout expires.
565 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
573 Checks APs status and updates APs status if needed.
577 CheckAndUpdateApsStatus (
582 Detect whether specified processor can find matching microcode patch and load it.
584 @param[in] CpuMpData The pointer to CPU MP Data structure.
585 @param[in] ProcessorNumber The handle number of the processor. The range is
586 from 0 to the total number of logical processors
591 IN CPU_MP_DATA
*CpuMpData
,
592 IN UINTN ProcessorNumber
596 Load the required microcode patches data into memory.
598 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
602 IN OUT CPU_MP_DATA
*CpuMpData
606 Detect whether Mwait-monitor feature is supported.
608 @retval TRUE Mwait-monitor feature is supported.
609 @retval FALSE Mwait-monitor feature is not supported.
617 Enable Debug Agent to support source debugging on AP function.
626 Find the current Processor number by APIC ID.
628 @param[in] CpuMpData Pointer to PEI CPU MP Data
629 @param[out] ProcessorNumber Return the pocessor number found
631 @retval EFI_SUCCESS ProcessorNumber is found and returned.
632 @retval EFI_NOT_FOUND ProcessorNumber is not found.
636 IN CPU_MP_DATA
*CpuMpData
,
637 OUT UINTN
*ProcessorNumber