2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Register/Intel/Cpuid.h>
17 #include <Register/Amd/Cpuid.h>
18 #include <Register/Intel/Msr.h>
19 #include <Register/Intel/LocalApic.h>
20 #include <Register/Intel/Microcode.h>
22 #include <Library/MpInitLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/LocalApicLib.h>
28 #include <Library/CpuLib.h>
29 #include <Library/UefiCpuLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/SynchronizationLib.h>
32 #include <Library/MtrrLib.h>
33 #include <Library/HobLib.h>
34 #include <Library/PcdLib.h>
36 #include <Guid/MicrocodePatchHob.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 // Default maximum number of entries to store the microcode patches information
55 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
58 // Data structure for microcode patch information
63 } MICROCODE_PATCH_INFO
;
66 // CPU exchange information for switch BSP
69 UINT8 State
; // offset 0
70 UINTN StackPointer
; // offset 4 / 8
71 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
72 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
73 } CPU_EXCHANGE_ROLE_INFO
;
76 // AP loop state when APs are in idle state
77 // It's value is the same with PcdCpuApLoopMode
86 // AP initialization state during APs wakeup
97 // The state transitions for an AP when it process a procedure are:
98 // Idle ----> Ready ----> Busy ----> Idle
110 // CPU volatile registers around INIT-SIPI-SIPI
122 IA32_DESCRIPTOR Gdtr
;
123 IA32_DESCRIPTOR Idtr
;
125 } CPU_VOLATILE_REGISTERS
;
132 volatile UINT32
*StartupApSignal
;
133 volatile UINTN ApFunction
;
134 volatile UINTN ApFunctionArgument
;
136 volatile CPU_STATE State
;
137 CPU_VOLATILE_REGISTERS VolatileRegisters
;
144 UINT32 ProcessorSignature
;
146 UINT64 MicrocodeEntryAddr
;
150 // Basic CPU information saved in Guided HOB.
151 // Because the contents will be shard between PEI and DXE,
152 // we need to make sure the each fields offset same in different
157 UINT32 InitialApicId
;
165 // AP reset code information including code address and size,
166 // this structure will be shared be C code and assembly code.
167 // It is natural aligned by design.
170 UINT8
*RendezvousFunnelAddress
;
171 UINTN ModeEntryOffset
;
172 UINTN RendezvousFunnelSize
;
173 UINT8
*RelocateApLoopFuncAddress
;
174 UINTN RelocateApLoopFuncSize
;
175 UINTN ModeTransitionOffset
;
176 UINTN SwitchToRealSize
;
177 UINTN SwitchToRealOffset
;
178 UINTN SwitchToRealNoNxOffset
;
179 UINTN SwitchToRealPM16ModeOffset
;
180 UINTN SwitchToRealPM16ModeSize
;
181 } MP_ASSEMBLY_ADDRESS_MAP
;
183 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
188 // MP CPU exchange information for AP reset code
189 // This structure is required to be packed because fixed field offsets
190 // into this structure are used in assembly code in this module
196 IA32_DESCRIPTOR GdtrProfile
;
197 IA32_DESCRIPTOR IdtrProfile
;
203 UINTN EnableExecuteDisable
;
206 CPU_INFO_IN_HOB
*CpuInfo
;
207 UINTN NumApsExecuting
;
208 CPU_MP_DATA
*CpuMpData
;
209 UINTN InitializeFloatingPointUnitsAddress
;
210 UINT32 ModeTransitionMemory
;
211 UINT16 ModeTransitionSegment
;
212 UINT32 ModeHighMemory
;
213 UINT16 ModeHighSegment
;
215 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
217 BOOLEAN Enable5LevelPaging
;
218 BOOLEAN SevEsIsEnabled
;
220 } MP_CPU_EXCHANGE_INFO
;
225 // CPU MP Data save in memory
227 struct _CPU_MP_DATA
{
232 // The above fields data will be passed from PEI to DXE
233 // Please make sure the fields offset same in the different
238 UINTN CpuApStackSize
;
239 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
241 UINTN WakeupBufferHigh
;
243 UINTN BackupBufferSize
;
245 volatile UINT32 FinishedCount
;
247 BOOLEAN SingleThread
;
248 EFI_AP_PROCEDURE Procedure
;
255 UINTN
**FailedCpuList
;
257 AP_INIT_STATE InitFlag
;
258 BOOLEAN SwitchBspFlag
;
260 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
261 CPU_EXCHANGE_ROLE_INFO APInfo
;
262 MTRR_SETTINGS MtrrTable
;
264 UINT8 ApTargetCState
;
265 UINT16 PmCodeSegment
;
266 UINT16 Pm16CodeSegment
;
267 CPU_AP_DATA
*CpuData
;
268 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
270 UINT32 CurrentTimerCount
;
273 BOOLEAN PeriodicMode
;
274 BOOLEAN TimerInterruptState
;
275 UINT64 MicrocodePatchAddress
;
276 UINT64 MicrocodePatchRegionSize
;
279 // Whether need to use Init-Sipi-Sipi to wake up the APs.
280 // Two cases need to set this value to TRUE. One is in HLT
281 // loop mode, the other is resume from S3 which loop mode
282 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
285 BOOLEAN WakeUpByInitSipiSipi
;
287 BOOLEAN SevEsIsEnabled
;
289 UINTN SevEsAPResetStackStart
;
290 CPU_MP_DATA
*NewCpuMpData
;
295 #define AP_SAFE_STACK_SIZE 128
296 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
309 Assembly code to move an AP from long mode to real mode.
311 Move an AP from long mode to real mode in preparation to invoking
312 the reset vector. This is used for SEV-ES guests where a hypervisor
313 is not allowed to set the CS and RIP to point to the reset vector.
315 @param[in] BufferStart The reset vector target.
316 @param[in] Code16 16-bit protected mode code segment value.
317 @param[in] Code32 32-bit protected mode code segment value.
318 @param[in] StackStart The start of a stack to be used for transitioning
319 from long mode to real mode.
324 IN UINTN BufferStart
,
330 extern EFI_GUID mCpuInitMpLibHobGuid
;
333 Assembly code to place AP into safe loop mode.
335 Place AP into targeted C-State if MONITOR is supported, otherwise
336 place AP into hlt state.
337 Place AP in protected mode if the current is long mode. Due to AP maybe
338 wakeup by some hardware event. It could avoid accessing page table that
339 may not available during booting to OS.
341 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
342 FALSE indicates MONITOR is not supported.
343 @param[in] ApTargetCState Target C-State value.
344 @param[in] PmCodeSegment Protected mode code segment value.
348 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
349 IN BOOLEAN MwaitSupport
,
350 IN UINTN ApTargetCState
,
351 IN UINTN PmCodeSegment
,
352 IN UINTN TopOfApStack
,
353 IN UINTN NumberToFinish
,
354 IN UINTN Pm16CodeSegment
,
355 IN UINTN SevEsAPJumpTable
,
356 IN UINTN WakeupBuffer
360 Assembly code to get starting address and size of the rendezvous entry for APs.
361 Information for fixing a jump instruction in the code is also returned.
363 @param[out] AddressMap Output buffer for address map information.
368 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
372 This function is called by both the BSP and the AP which is to become the BSP to
373 Exchange execution context including stack between them. After return from this
374 function, the BSP becomes AP and the AP becomes the BSP.
376 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
377 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
383 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
384 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
388 Get the pointer to CPU MP Data structure.
390 @return The pointer to CPU MP Data structure.
398 Save the pointer to CPU MP Data structure.
400 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
404 IN CPU_MP_DATA
*CpuMpData
409 Get available system memory below 1MB by specified size.
411 @param[in] WakeupBufferSize Wakeup buffer size required
413 @retval other Return wakeup buffer address below 1MB.
414 @retval -1 Cannot find free memory below 1MB.
418 IN UINTN WakeupBufferSize
422 Get available EfiBootServicesCode memory below 4GB by specified size.
424 This buffer is required to safely transfer AP from real address mode to
425 protected mode or long mode, due to the fact that the buffer returned by
426 GetWakeupBuffer() may be marked as non-executable.
428 @param[in] BufferSize Wakeup transition buffer size.
430 @retval other Return wakeup transition buffer address below 4GB.
431 @retval 0 Cannot find free memory below 4GB.
434 GetModeTransitionBuffer (
439 Return the address of the SEV-ES AP jump table.
441 This buffer is required in order for an SEV-ES guest to transition from
444 @return Return SEV-ES AP jump table buffer
452 This function will be called by BSP to wakeup AP.
454 @param[in] CpuMpData Pointer to CPU MP Data
455 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
456 FALSE: Send IPI to AP by ApicId
457 @param[in] ProcessorNumber The handle number of specified processor
458 @param[in] Procedure The function to be invoked by AP
459 @param[in] ProcedureArgument The argument to be passed into AP function
460 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
464 IN CPU_MP_DATA
*CpuMpData
,
465 IN BOOLEAN Broadcast
,
466 IN UINTN ProcessorNumber
,
467 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
468 IN VOID
*ProcedureArgument
, OPTIONAL
469 IN BOOLEAN WakeUpDisabledAps OPTIONAL
473 Initialize global data for MP support.
475 @param[in] CpuMpData The pointer to CPU MP Data structure.
479 IN CPU_MP_DATA
*CpuMpData
483 Worker function to execute a caller provided function on all enabled APs.
485 @param[in] Procedure A pointer to the function to be run on
486 enabled APs of the system.
487 @param[in] SingleThread If TRUE, then all the enabled APs execute
488 the function specified by Procedure one by
489 one, in ascending order of processor handle
490 number. If FALSE, then all the enabled APs
491 execute the function specified by Procedure
493 @param[in] ExcludeBsp Whether let BSP also trig this task.
494 @param[in] WaitEvent The event created by the caller with CreateEvent()
496 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
497 APs to return from Procedure, either for
498 blocking or non-blocking mode.
499 @param[in] ProcedureArgument The parameter passed into Procedure for
501 @param[out] FailedCpuList If all APs finish successfully, then its
502 content is set to NULL. If not all APs
503 finish before timeout expires, then its
504 content is set to address of the buffer
505 holding handle numbers of the failed APs.
507 @retval EFI_SUCCESS In blocking mode, all APs have finished before
509 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
511 @retval others Failed to Startup all APs.
515 StartupAllCPUsWorker (
516 IN EFI_AP_PROCEDURE Procedure
,
517 IN BOOLEAN SingleThread
,
518 IN BOOLEAN ExcludeBsp
,
519 IN EFI_EVENT WaitEvent OPTIONAL
,
520 IN UINTN TimeoutInMicroseconds
,
521 IN VOID
*ProcedureArgument OPTIONAL
,
522 OUT UINTN
**FailedCpuList OPTIONAL
526 Worker function to let the caller get one enabled AP to execute a caller-provided
529 @param[in] Procedure A pointer to the function to be run on
530 enabled APs of the system.
531 @param[in] ProcessorNumber The handle number of the AP.
532 @param[in] WaitEvent The event created by the caller with CreateEvent()
534 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
535 APs to return from Procedure, either for
536 blocking or non-blocking mode.
537 @param[in] ProcedureArgument The parameter passed into Procedure for
539 @param[out] Finished If AP returns from Procedure before the
540 timeout expires, its content is set to TRUE.
541 Otherwise, the value is set to FALSE.
543 @retval EFI_SUCCESS In blocking mode, specified AP finished before
545 @retval others Failed to Startup AP.
549 StartupThisAPWorker (
550 IN EFI_AP_PROCEDURE Procedure
,
551 IN UINTN ProcessorNumber
,
552 IN EFI_EVENT WaitEvent OPTIONAL
,
553 IN UINTN TimeoutInMicroseconds
,
554 IN VOID
*ProcedureArgument OPTIONAL
,
555 OUT BOOLEAN
*Finished OPTIONAL
559 Worker function to switch the requested AP to be the BSP from that point onward.
561 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
562 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
563 enabled AP. Otherwise, it will be disabled.
565 @retval EFI_SUCCESS BSP successfully switched.
566 @retval others Failed to switch BSP.
571 IN UINTN ProcessorNumber
,
572 IN BOOLEAN EnableOldBSP
576 Worker function to let the caller enable or disable an AP from this point onward.
577 This service may only be called from the BSP.
579 @param[in] ProcessorNumber The handle number of AP.
580 @param[in] EnableAP Specifies the new state for the processor for
581 enabled, FALSE for disabled.
582 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
583 the new health status of the AP.
585 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
586 @retval others Failed to Enable/Disable AP.
590 EnableDisableApWorker (
591 IN UINTN ProcessorNumber
,
593 IN UINT32
*HealthFlag OPTIONAL
597 Get pointer to CPU MP Data structure from GUIDed HOB.
599 @return The pointer to CPU MP Data structure.
602 GetCpuMpDataFromGuidedHob (
606 /** Checks status of specified AP.
608 This function checks whether the specified AP has finished the task assigned
609 by StartupThisAP(), and whether timeout expires.
611 @param[in] ProcessorNumber The handle number of processor.
613 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
614 @retval EFI_TIMEOUT The timeout expires.
615 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
619 IN UINTN ProcessorNumber
623 Checks status of all APs.
625 This function checks whether all APs have finished task assigned by StartupAllAPs(),
626 and whether timeout expires.
628 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
629 @retval EFI_TIMEOUT The timeout expires.
630 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
638 Checks APs status and updates APs status if needed.
642 CheckAndUpdateApsStatus (
647 Detect whether specified processor can find matching microcode patch and load it.
649 @param[in] CpuMpData The pointer to CPU MP Data structure.
650 @param[in] ProcessorNumber The handle number of the processor. The range is
651 from 0 to the total number of logical processors
656 IN CPU_MP_DATA
*CpuMpData
,
657 IN UINTN ProcessorNumber
661 Shadow the required microcode patches data into memory.
663 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
666 ShadowMicrocodeUpdatePatch (
667 IN OUT CPU_MP_DATA
*CpuMpData
671 Get the cached microcode patch base address and size from the microcode patch
672 information cache HOB.
674 @param[out] Address Base address of the microcode patches data.
675 It will be updated if the microcode patch
676 information cache HOB is found.
677 @param[out] RegionSize Size of the microcode patches data.
678 It will be updated if the microcode patch
679 information cache HOB is found.
681 @retval TRUE The microcode patch information cache HOB is found.
682 @retval FALSE The microcode patch information cache HOB is not found.
686 GetMicrocodePatchInfoFromHob (
692 Detect whether Mwait-monitor feature is supported.
694 @retval TRUE Mwait-monitor feature is supported.
695 @retval FALSE Mwait-monitor feature is not supported.
703 Enable Debug Agent to support source debugging on AP function.
712 Find the current Processor number by APIC ID.
714 @param[in] CpuMpData Pointer to PEI CPU MP Data
715 @param[out] ProcessorNumber Return the pocessor number found
717 @retval EFI_SUCCESS ProcessorNumber is found and returned.
718 @retval EFI_NOT_FOUND ProcessorNumber is not found.
722 IN CPU_MP_DATA
*CpuMpData
,
723 OUT UINTN
*ProcessorNumber
727 This funtion will try to invoke platform specific microcode shadow logic to
728 relocate microcode update patches into memory.
730 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
732 @retval EFI_SUCCESS Shadow microcode success.
733 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
734 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
738 PlatformShadowMicrocode (
739 IN OUT CPU_MP_DATA
*CpuMpData