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/Amd/Ghcb.h>
19 #include <Register/Intel/Msr.h>
20 #include <Register/Intel/LocalApic.h>
21 #include <Register/Intel/Microcode.h>
23 #include <Library/MpInitLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/LocalApicLib.h>
29 #include <Library/CpuLib.h>
30 #include <Library/UefiCpuLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/SynchronizationLib.h>
33 #include <Library/MtrrLib.h>
34 #include <Library/HobLib.h>
35 #include <Library/PcdLib.h>
36 #include <Library/MicrocodeLib.h>
37 #include <ConfidentialComputingGuestAttr.h>
39 #include <Register/Amd/Fam17Msr.h>
40 #include <Register/Amd/Ghcb.h>
42 #include <Guid/MicrocodePatchHob.h>
44 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
46 #define CPU_INIT_MP_LIB_HOB_GUID \
48 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
52 // The MP data for switch BSP
54 #define CPU_SWITCH_STATE_IDLE 0
55 #define CPU_SWITCH_STATE_STORED 1
56 #define CPU_SWITCH_STATE_LOADED 2
59 // Default maximum number of entries to store the microcode patches information
61 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
64 // Data structure for microcode patch information
69 } MICROCODE_PATCH_INFO
;
72 // CPU exchange information for switch BSP
75 UINT8 State
; // offset 0
76 UINTN StackPointer
; // offset 4 / 8
77 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
78 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
79 } CPU_EXCHANGE_ROLE_INFO
;
82 // AP loop state when APs are in idle state
83 // It's value is the same with PcdCpuApLoopMode
92 // AP initialization state during APs wakeup
103 // The state transitions for an AP when it process a procedure are:
104 // Idle ----> Ready ----> Busy ----> Idle
116 // CPU volatile registers around INIT-SIPI-SIPI
128 IA32_DESCRIPTOR Gdtr
;
129 IA32_DESCRIPTOR Idtr
;
131 } CPU_VOLATILE_REGISTERS
;
138 volatile UINT32
*StartupApSignal
;
139 volatile UINTN ApFunction
;
140 volatile UINTN ApFunctionArgument
;
142 volatile CPU_STATE State
;
143 CPU_VOLATILE_REGISTERS VolatileRegisters
;
150 UINT32 ProcessorSignature
;
152 UINT64 MicrocodeEntryAddr
;
153 UINT32 MicrocodeRevision
;
154 SEV_ES_SAVE_AREA
*SevEsSaveArea
;
158 // Basic CPU information saved in Guided HOB.
159 // Because the contents will be shard between PEI and DXE,
160 // we need to make sure the each fields offset same in different
165 UINT32 InitialApicId
;
173 // AP reset code information including code address and size,
174 // this structure will be shared be C code and assembly code.
175 // It is natural aligned by design.
178 UINT8
*RendezvousFunnelAddress
;
179 UINTN ModeEntryOffset
;
180 UINTN RendezvousFunnelSize
;
181 UINT8
*RelocateApLoopFuncAddress
;
182 UINTN RelocateApLoopFuncSize
;
183 UINTN ModeTransitionOffset
;
184 UINTN SwitchToRealSize
;
185 UINTN SwitchToRealOffset
;
186 UINTN SwitchToRealNoNxOffset
;
187 UINTN SwitchToRealPM16ModeOffset
;
188 UINTN SwitchToRealPM16ModeSize
;
189 } MP_ASSEMBLY_ADDRESS_MAP
;
191 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
196 // MP CPU exchange information for AP reset code
197 // This structure is required to be packed because fixed field offsets
198 // into this structure are used in assembly code in this module
204 IA32_DESCRIPTOR GdtrProfile
;
205 IA32_DESCRIPTOR IdtrProfile
;
211 UINTN EnableExecuteDisable
;
214 CPU_INFO_IN_HOB
*CpuInfo
;
215 UINTN NumApsExecuting
;
216 CPU_MP_DATA
*CpuMpData
;
217 UINTN InitializeFloatingPointUnitsAddress
;
218 UINT32 ModeTransitionMemory
;
219 UINT16 ModeTransitionSegment
;
220 UINT32 ModeHighMemory
;
221 UINT16 ModeHighSegment
;
223 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
225 BOOLEAN Enable5LevelPaging
;
226 BOOLEAN SevEsIsEnabled
;
227 BOOLEAN SevSnpIsEnabled
;
229 BOOLEAN ExtTopoAvail
;
230 } MP_CPU_EXCHANGE_INFO
;
235 // CPU MP Data save in memory
237 struct _CPU_MP_DATA
{
242 // The above fields data will be passed from PEI to DXE
243 // Please make sure the fields offset same in the different
248 UINTN CpuApStackSize
;
249 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
251 UINTN WakeupBufferHigh
;
253 UINTN BackupBufferSize
;
255 volatile UINT32 FinishedCount
;
257 BOOLEAN SingleThread
;
258 EFI_AP_PROCEDURE Procedure
;
265 UINTN
**FailedCpuList
;
267 AP_INIT_STATE InitFlag
;
268 BOOLEAN SwitchBspFlag
;
270 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
271 CPU_EXCHANGE_ROLE_INFO APInfo
;
272 MTRR_SETTINGS MtrrTable
;
274 UINT8 ApTargetCState
;
275 UINT16 PmCodeSegment
;
276 UINT16 Pm16CodeSegment
;
277 CPU_AP_DATA
*CpuData
;
278 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
280 UINT32 CurrentTimerCount
;
283 BOOLEAN PeriodicMode
;
284 BOOLEAN TimerInterruptState
;
285 UINT64 MicrocodePatchAddress
;
286 UINT64 MicrocodePatchRegionSize
;
289 // Whether need to use Init-Sipi-Sipi to wake up the APs.
290 // Two cases need to set this value to TRUE. One is in HLT
291 // loop mode, the other is resume from S3 which loop mode
292 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
295 BOOLEAN WakeUpByInitSipiSipi
;
297 BOOLEAN SevEsIsEnabled
;
298 BOOLEAN SevSnpIsEnabled
;
299 BOOLEAN UseSevEsAPMethod
;
301 UINTN SevEsAPResetStackStart
;
302 CPU_MP_DATA
*NewCpuMpData
;
307 #define AP_SAFE_STACK_SIZE 128
308 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
321 Assembly code to move an AP from long mode to real mode.
323 Move an AP from long mode to real mode in preparation to invoking
324 the reset vector. This is used for SEV-ES guests where a hypervisor
325 is not allowed to set the CS and RIP to point to the reset vector.
327 @param[in] BufferStart The reset vector target.
328 @param[in] Code16 16-bit protected mode code segment value.
329 @param[in] Code32 32-bit protected mode code segment value.
330 @param[in] StackStart The start of a stack to be used for transitioning
331 from long mode to real mode.
336 IN UINTN BufferStart
,
342 extern EFI_GUID mCpuInitMpLibHobGuid
;
345 Assembly code to place AP into safe loop mode.
347 Place AP into targeted C-State if MONITOR is supported, otherwise
348 place AP into hlt state.
349 Place AP in protected mode if the current is long mode. Due to AP maybe
350 wakeup by some hardware event. It could avoid accessing page table that
351 may not available during booting to OS.
353 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
354 FALSE indicates MONITOR is not supported.
355 @param[in] ApTargetCState Target C-State value.
356 @param[in] PmCodeSegment Protected mode code segment value.
360 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
361 IN BOOLEAN MwaitSupport
,
362 IN UINTN ApTargetCState
,
363 IN UINTN PmCodeSegment
,
364 IN UINTN TopOfApStack
,
365 IN UINTN NumberToFinish
,
366 IN UINTN Pm16CodeSegment
,
367 IN UINTN SevEsAPJumpTable
,
368 IN UINTN WakeupBuffer
372 Assembly code to get starting address and size of the rendezvous entry for APs.
373 Information for fixing a jump instruction in the code is also returned.
375 @param[out] AddressMap Output buffer for address map information.
380 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
384 This function is called by both the BSP and the AP which is to become the BSP to
385 Exchange execution context including stack between them. After return from this
386 function, the BSP becomes AP and the AP becomes the BSP.
388 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
389 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
395 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
396 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
400 Get the pointer to CPU MP Data structure.
402 @return The pointer to CPU MP Data structure.
410 Save the pointer to CPU MP Data structure.
412 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
416 IN CPU_MP_DATA
*CpuMpData
420 Get available system memory below 1MB by specified size.
422 @param[in] WakeupBufferSize Wakeup buffer size required
424 @retval other Return wakeup buffer address below 1MB.
425 @retval -1 Cannot find free memory below 1MB.
429 IN UINTN WakeupBufferSize
433 Get available EfiBootServicesCode memory below 4GB by specified size.
435 This buffer is required to safely transfer AP from real address mode to
436 protected mode or long mode, due to the fact that the buffer returned by
437 GetWakeupBuffer() may be marked as non-executable.
439 @param[in] BufferSize Wakeup transition buffer size.
441 @retval other Return wakeup transition buffer address below 4GB.
442 @retval 0 Cannot find free memory below 4GB.
450 Return the address of the SEV-ES AP jump table.
452 This buffer is required in order for an SEV-ES guest to transition from
455 @return Return SEV-ES AP jump table buffer
463 This function will be called by BSP to wakeup AP.
465 @param[in] CpuMpData Pointer to CPU MP Data
466 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
467 FALSE: Send IPI to AP by ApicId
468 @param[in] ProcessorNumber The handle number of specified processor
469 @param[in] Procedure The function to be invoked by AP
470 @param[in] ProcedureArgument The argument to be passed into AP function
471 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
475 IN CPU_MP_DATA
*CpuMpData
,
476 IN BOOLEAN Broadcast
,
477 IN UINTN ProcessorNumber
,
478 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
479 IN VOID
*ProcedureArgument OPTIONAL
,
480 IN BOOLEAN WakeUpDisabledAps
484 Initialize global data for MP support.
486 @param[in] CpuMpData The pointer to CPU MP Data structure.
490 IN CPU_MP_DATA
*CpuMpData
494 Worker function to execute a caller provided function on all enabled APs.
496 @param[in] Procedure A pointer to the function to be run on
497 enabled APs of the system.
498 @param[in] SingleThread If TRUE, then all the enabled APs execute
499 the function specified by Procedure one by
500 one, in ascending order of processor handle
501 number. If FALSE, then all the enabled APs
502 execute the function specified by Procedure
504 @param[in] ExcludeBsp Whether let BSP also trig this task.
505 @param[in] WaitEvent The event created by the caller with CreateEvent()
507 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
508 APs to return from Procedure, either for
509 blocking or non-blocking mode.
510 @param[in] ProcedureArgument The parameter passed into Procedure for
512 @param[out] FailedCpuList If all APs finish successfully, then its
513 content is set to NULL. If not all APs
514 finish before timeout expires, then its
515 content is set to address of the buffer
516 holding handle numbers of the failed APs.
518 @retval EFI_SUCCESS In blocking mode, all APs have finished before
520 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
522 @retval others Failed to Startup all APs.
526 StartupAllCPUsWorker (
527 IN EFI_AP_PROCEDURE Procedure
,
528 IN BOOLEAN SingleThread
,
529 IN BOOLEAN ExcludeBsp
,
530 IN EFI_EVENT WaitEvent OPTIONAL
,
531 IN UINTN TimeoutInMicroseconds
,
532 IN VOID
*ProcedureArgument OPTIONAL
,
533 OUT UINTN
**FailedCpuList OPTIONAL
537 Worker function to let the caller get one enabled AP to execute a caller-provided
540 @param[in] Procedure A pointer to the function to be run on
541 enabled APs of the system.
542 @param[in] ProcessorNumber The handle number of the AP.
543 @param[in] WaitEvent The event created by the caller with CreateEvent()
545 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
546 APs to return from Procedure, either for
547 blocking or non-blocking mode.
548 @param[in] ProcedureArgument The parameter passed into Procedure for
550 @param[out] Finished If AP returns from Procedure before the
551 timeout expires, its content is set to TRUE.
552 Otherwise, the value is set to FALSE.
554 @retval EFI_SUCCESS In blocking mode, specified AP finished before
556 @retval others Failed to Startup AP.
560 StartupThisAPWorker (
561 IN EFI_AP_PROCEDURE Procedure
,
562 IN UINTN ProcessorNumber
,
563 IN EFI_EVENT WaitEvent OPTIONAL
,
564 IN UINTN TimeoutInMicroseconds
,
565 IN VOID
*ProcedureArgument OPTIONAL
,
566 OUT BOOLEAN
*Finished OPTIONAL
570 Worker function to switch the requested AP to be the BSP from that point onward.
572 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
573 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
574 enabled AP. Otherwise, it will be disabled.
576 @retval EFI_SUCCESS BSP successfully switched.
577 @retval others Failed to switch BSP.
582 IN UINTN ProcessorNumber
,
583 IN BOOLEAN EnableOldBSP
587 Worker function to let the caller enable or disable an AP from this point onward.
588 This service may only be called from the BSP.
590 @param[in] ProcessorNumber The handle number of AP.
591 @param[in] EnableAP Specifies the new state for the processor for
592 enabled, FALSE for disabled.
593 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
594 the new health status of the AP.
596 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
597 @retval others Failed to Enable/Disable AP.
601 EnableDisableApWorker (
602 IN UINTN ProcessorNumber
,
604 IN UINT32
*HealthFlag OPTIONAL
608 Get pointer to CPU MP Data structure from GUIDed HOB.
610 @return The pointer to CPU MP Data structure.
613 GetCpuMpDataFromGuidedHob (
617 /** Checks status of specified AP.
619 This function checks whether the specified AP has finished the task assigned
620 by StartupThisAP(), and whether timeout expires.
622 @param[in] ProcessorNumber The handle number of processor.
624 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
625 @retval EFI_TIMEOUT The timeout expires.
626 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
630 IN UINTN ProcessorNumber
634 Checks status of all APs.
636 This function checks whether all APs have finished task assigned by StartupAllAPs(),
637 and whether timeout expires.
639 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
640 @retval EFI_TIMEOUT The timeout expires.
641 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
649 Checks APs status and updates APs status if needed.
653 CheckAndUpdateApsStatus (
658 Detect whether specified processor can find matching microcode patch and load it.
660 @param[in] CpuMpData The pointer to CPU MP Data structure.
661 @param[in] ProcessorNumber The handle number of the processor. The range is
662 from 0 to the total number of logical processors
667 IN CPU_MP_DATA
*CpuMpData
,
668 IN UINTN ProcessorNumber
672 Shadow the required microcode patches data into memory.
674 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
677 ShadowMicrocodeUpdatePatch (
678 IN OUT CPU_MP_DATA
*CpuMpData
682 Get the cached microcode patch base address and size from the microcode patch
683 information cache HOB.
685 @param[out] Address Base address of the microcode patches data.
686 It will be updated if the microcode patch
687 information cache HOB is found.
688 @param[out] RegionSize Size of the microcode patches data.
689 It will be updated if the microcode patch
690 information cache HOB is found.
692 @retval TRUE The microcode patch information cache HOB is found.
693 @retval FALSE The microcode patch information cache HOB is not found.
697 GetMicrocodePatchInfoFromHob (
703 Detect whether Mwait-monitor feature is supported.
705 @retval TRUE Mwait-monitor feature is supported.
706 @retval FALSE Mwait-monitor feature is not supported.
714 Enable Debug Agent to support source debugging on AP function.
723 Find the current Processor number by APIC ID.
725 @param[in] CpuMpData Pointer to PEI CPU MP Data
726 @param[out] ProcessorNumber Return the pocessor number found
728 @retval EFI_SUCCESS ProcessorNumber is found and returned.
729 @retval EFI_NOT_FOUND ProcessorNumber is not found.
733 IN CPU_MP_DATA
*CpuMpData
,
734 OUT UINTN
*ProcessorNumber
738 This funtion will try to invoke platform specific microcode shadow logic to
739 relocate microcode update patches into memory.
741 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
743 @retval EFI_SUCCESS Shadow microcode success.
744 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
745 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
749 PlatformShadowMicrocode (
750 IN OUT CPU_MP_DATA
*CpuMpData
754 Allocate the SEV-ES AP jump table buffer.
756 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
759 AllocateSevEsAPMemory (
760 IN OUT CPU_MP_DATA
*CpuMpData
764 Program the SEV-ES AP jump table buffer.
766 @param[in] SipiVector The SIPI vector used for the AP Reset
774 The function puts the AP in halt loop.
776 @param[in] CpuMpData The pointer to CPU MP Data structure.
780 CPU_MP_DATA
*CpuMpData
784 Check if the specified confidential computing attribute is active.
786 @retval TRUE The specified Attr is active.
787 @retval FALSE The specified Attr is not active.
791 ConfidentialComputingGuestHas (
792 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
796 The function fills the exchange data for the AP.
798 @param[in] ExchangeInfo The pointer to CPU Exchange Data structure
801 FillExchangeInfoDataSevEs (
802 IN
volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
806 Issue RMPADJUST to adjust the VMSA attribute of an SEV-SNP page.
808 @param[in] PageAddress
811 @return RMPADJUST return value
815 IN EFI_PHYSICAL_ADDRESS PageAddress
,
820 Create an SEV-SNP AP save area (VMSA) for use in running the vCPU.
822 @param[in] CpuMpData Pointer to CPU MP Data
823 @param[in] CpuData Pointer to CPU AP Data
824 @param[in] ApicId APIC ID of the vCPU
827 SevSnpCreateSaveArea (
828 IN CPU_MP_DATA
*CpuMpData
,
829 IN CPU_AP_DATA
*CpuData
,
836 @param[in] CpuMpData Pointer to CPU MP Data
837 @param[in] ProcessorNumber The handle number of specified processor
842 IN CPU_MP_DATA
*CpuMpData
,
843 IN INTN ProcessorNumber