2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2022, 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 volatile registers around INIT-SIPI-SIPI
87 } CPU_VOLATILE_REGISTERS
;
90 // CPU exchange information for switch BSP
93 UINT8 State
; // offset 0
94 UINTN StackPointer
; // offset 4 / 8
95 CPU_VOLATILE_REGISTERS VolatileRegisters
; // offset 8 / 16
96 } CPU_EXCHANGE_ROLE_INFO
;
99 // AP loop state when APs are in idle state
100 // It's value is the same with PcdCpuApLoopMode
109 // AP initialization state during APs wakeup
120 // The state transitions for an AP when it process a procedure are:
121 // Idle ----> Ready ----> Busy ----> Idle
137 volatile UINT32
*StartupApSignal
;
138 volatile UINTN ApFunction
;
139 volatile UINTN ApFunctionArgument
;
141 volatile CPU_STATE State
;
142 CPU_VOLATILE_REGISTERS VolatileRegisters
;
149 UINT32 ProcessorSignature
;
151 UINT64 MicrocodeEntryAddr
;
152 UINT32 MicrocodeRevision
;
153 SEV_ES_SAVE_AREA
*SevEsSaveArea
;
157 // Basic CPU information saved in Guided HOB.
158 // Because the contents will be shard between PEI and DXE,
159 // we need to make sure the each fields offset same in different
164 UINT32 InitialApicId
;
172 // AP reset code information including code address and size,
173 // this structure will be shared be C code and assembly code.
174 // It is natural aligned by design.
177 UINT8
*RendezvousFunnelAddress
;
178 UINTN ModeEntryOffset
;
179 UINTN RendezvousFunnelSize
;
180 UINT8
*RelocateApLoopFuncAddress
;
181 UINTN RelocateApLoopFuncSize
;
182 UINTN ModeTransitionOffset
;
183 UINTN SwitchToRealNoNxOffset
;
184 UINTN SwitchToRealPM16ModeOffset
;
185 UINTN SwitchToRealPM16ModeSize
;
186 } MP_ASSEMBLY_ADDRESS_MAP
;
188 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
193 // MP CPU exchange information for AP reset code
194 // This structure is required to be packed because fixed field offsets
195 // into this structure are used in assembly code in this module
201 IA32_DESCRIPTOR GdtrProfile
;
202 IA32_DESCRIPTOR IdtrProfile
;
208 UINTN EnableExecuteDisable
;
211 CPU_INFO_IN_HOB
*CpuInfo
;
212 UINTN NumApsExecuting
;
213 CPU_MP_DATA
*CpuMpData
;
214 UINTN InitializeFloatingPointUnitsAddress
;
215 UINT32 ModeTransitionMemory
;
216 UINT16 ModeTransitionSegment
;
217 UINT32 ModeHighMemory
;
218 UINT16 ModeHighSegment
;
220 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
222 BOOLEAN Enable5LevelPaging
;
223 BOOLEAN SevEsIsEnabled
;
224 BOOLEAN SevSnpIsEnabled
;
226 BOOLEAN ExtTopoAvail
;
227 } MP_CPU_EXCHANGE_INFO
;
232 // CPU MP Data save in memory
234 struct _CPU_MP_DATA
{
239 // The above fields data will be passed from PEI to DXE
240 // Please make sure the fields offset same in the different
245 UINTN CpuApStackSize
;
246 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
248 UINTN WakeupBufferHigh
;
250 UINTN BackupBufferSize
;
252 volatile UINT32 FinishedCount
;
254 BOOLEAN SingleThread
;
255 EFI_AP_PROCEDURE Procedure
;
262 UINTN
**FailedCpuList
;
264 AP_INIT_STATE InitFlag
;
265 BOOLEAN SwitchBspFlag
;
267 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
268 CPU_EXCHANGE_ROLE_INFO APInfo
;
269 MTRR_SETTINGS MtrrTable
;
271 UINT8 ApTargetCState
;
272 UINT16 PmCodeSegment
;
273 UINT16 Pm16CodeSegment
;
274 CPU_AP_DATA
*CpuData
;
275 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
277 UINT32 CurrentTimerCount
;
280 BOOLEAN PeriodicMode
;
281 BOOLEAN TimerInterruptState
;
282 UINT64 MicrocodePatchAddress
;
283 UINT64 MicrocodePatchRegionSize
;
286 // Whether need to use Init-Sipi-Sipi to wake up the APs.
287 // Two cases need to set this value to TRUE. One is in HLT
288 // loop mode, the other is resume from S3 which loop mode
289 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
292 BOOLEAN WakeUpByInitSipiSipi
;
294 BOOLEAN SevEsIsEnabled
;
295 BOOLEAN SevSnpIsEnabled
;
296 BOOLEAN UseSevEsAPMethod
;
298 UINTN SevEsAPResetStackStart
;
299 CPU_MP_DATA
*NewCpuMpData
;
305 // AP_STACK_DATA is stored at the top of each AP stack.
312 #define AP_SAFE_STACK_SIZE 128
313 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
326 Assembly code to move an AP from long mode to real mode.
328 Move an AP from long mode to real mode in preparation to invoking
329 the reset vector. This is used for SEV-ES guests where a hypervisor
330 is not allowed to set the CS and RIP to point to the reset vector.
332 @param[in] BufferStart The reset vector target.
333 @param[in] Code16 16-bit protected mode code segment value.
334 @param[in] Code32 32-bit protected mode code segment value.
335 @param[in] StackStart The start of a stack to be used for transitioning
336 from long mode to real mode.
341 IN UINTN BufferStart
,
347 extern EFI_GUID mCpuInitMpLibHobGuid
;
350 Assembly code to place AP into safe loop mode.
352 Place AP into targeted C-State if MONITOR is supported, otherwise
353 place AP into hlt state.
354 Place AP in protected mode if the current is long mode. Due to AP maybe
355 wakeup by some hardware event. It could avoid accessing page table that
356 may not available during booting to OS.
358 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
359 FALSE indicates MONITOR is not supported.
360 @param[in] ApTargetCState Target C-State value.
361 @param[in] PmCodeSegment Protected mode code segment value.
365 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
366 IN BOOLEAN MwaitSupport
,
367 IN UINTN ApTargetCState
,
368 IN UINTN PmCodeSegment
,
369 IN UINTN TopOfApStack
,
370 IN UINTN NumberToFinish
,
371 IN UINTN Pm16CodeSegment
,
372 IN UINTN SevEsAPJumpTable
,
373 IN UINTN WakeupBuffer
377 Assembly code to get starting address and size of the rendezvous entry for APs.
378 Information for fixing a jump instruction in the code is also returned.
380 @param[out] AddressMap Output buffer for address map information.
385 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
389 This function is called by both the BSP and the AP which is to become the BSP to
390 Exchange execution context including stack between them. After return from this
391 function, the BSP becomes AP and the AP becomes the BSP.
393 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
394 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
400 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
401 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
405 Get the pointer to CPU MP Data structure.
407 @return The pointer to CPU MP Data structure.
415 Save the pointer to CPU MP Data structure.
417 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
421 IN CPU_MP_DATA
*CpuMpData
425 Get available system memory below 1MB by specified size.
427 @param[in] WakeupBufferSize Wakeup buffer size required
429 @retval other Return wakeup buffer address below 1MB.
430 @retval -1 Cannot find free memory below 1MB.
434 IN UINTN WakeupBufferSize
438 Get available EfiBootServicesCode memory below 4GB by specified size.
440 This buffer is required to safely transfer AP from real address mode to
441 protected mode or long mode, due to the fact that the buffer returned by
442 GetWakeupBuffer() may be marked as non-executable.
444 @param[in] BufferSize Wakeup transition buffer size.
446 @retval other Return wakeup transition buffer address below 4GB.
447 @retval 0 Cannot find free memory below 4GB.
455 Return the address of the SEV-ES AP jump table.
457 This buffer is required in order for an SEV-ES guest to transition from
460 @return Return SEV-ES AP jump table buffer
468 This function will be called by BSP to wakeup AP.
470 @param[in] CpuMpData Pointer to CPU MP Data
471 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
472 FALSE: Send IPI to AP by ApicId
473 @param[in] ProcessorNumber The handle number of specified processor
474 @param[in] Procedure The function to be invoked by AP
475 @param[in] ProcedureArgument The argument to be passed into AP function
476 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
480 IN CPU_MP_DATA
*CpuMpData
,
481 IN BOOLEAN Broadcast
,
482 IN UINTN ProcessorNumber
,
483 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
484 IN VOID
*ProcedureArgument OPTIONAL
,
485 IN BOOLEAN WakeUpDisabledAps
489 Initialize global data for MP support.
491 @param[in] CpuMpData The pointer to CPU MP Data structure.
495 IN CPU_MP_DATA
*CpuMpData
499 Worker function to execute a caller provided function on all enabled APs.
501 @param[in] Procedure A pointer to the function to be run on
502 enabled APs of the system.
503 @param[in] SingleThread If TRUE, then all the enabled APs execute
504 the function specified by Procedure one by
505 one, in ascending order of processor handle
506 number. If FALSE, then all the enabled APs
507 execute the function specified by Procedure
509 @param[in] ExcludeBsp Whether let BSP also trig this task.
510 @param[in] WaitEvent The event created by the caller with CreateEvent()
512 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
513 APs to return from Procedure, either for
514 blocking or non-blocking mode.
515 @param[in] ProcedureArgument The parameter passed into Procedure for
517 @param[out] FailedCpuList If all APs finish successfully, then its
518 content is set to NULL. If not all APs
519 finish before timeout expires, then its
520 content is set to address of the buffer
521 holding handle numbers of the failed APs.
523 @retval EFI_SUCCESS In blocking mode, all APs have finished before
525 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
527 @retval others Failed to Startup all APs.
531 StartupAllCPUsWorker (
532 IN EFI_AP_PROCEDURE Procedure
,
533 IN BOOLEAN SingleThread
,
534 IN BOOLEAN ExcludeBsp
,
535 IN EFI_EVENT WaitEvent OPTIONAL
,
536 IN UINTN TimeoutInMicroseconds
,
537 IN VOID
*ProcedureArgument OPTIONAL
,
538 OUT UINTN
**FailedCpuList OPTIONAL
542 Worker function to let the caller get one enabled AP to execute a caller-provided
545 @param[in] Procedure A pointer to the function to be run on
546 enabled APs of the system.
547 @param[in] ProcessorNumber The handle number of the AP.
548 @param[in] WaitEvent The event created by the caller with CreateEvent()
550 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
551 APs to return from Procedure, either for
552 blocking or non-blocking mode.
553 @param[in] ProcedureArgument The parameter passed into Procedure for
555 @param[out] Finished If AP returns from Procedure before the
556 timeout expires, its content is set to TRUE.
557 Otherwise, the value is set to FALSE.
559 @retval EFI_SUCCESS In blocking mode, specified AP finished before
561 @retval others Failed to Startup AP.
565 StartupThisAPWorker (
566 IN EFI_AP_PROCEDURE Procedure
,
567 IN UINTN ProcessorNumber
,
568 IN EFI_EVENT WaitEvent OPTIONAL
,
569 IN UINTN TimeoutInMicroseconds
,
570 IN VOID
*ProcedureArgument OPTIONAL
,
571 OUT BOOLEAN
*Finished OPTIONAL
575 Worker function to switch the requested AP to be the BSP from that point onward.
577 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
578 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
579 enabled AP. Otherwise, it will be disabled.
581 @retval EFI_SUCCESS BSP successfully switched.
582 @retval others Failed to switch BSP.
587 IN UINTN ProcessorNumber
,
588 IN BOOLEAN EnableOldBSP
592 Worker function to let the caller enable or disable an AP from this point onward.
593 This service may only be called from the BSP.
595 @param[in] ProcessorNumber The handle number of AP.
596 @param[in] EnableAP Specifies the new state for the processor for
597 enabled, FALSE for disabled.
598 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
599 the new health status of the AP.
601 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
602 @retval others Failed to Enable/Disable AP.
606 EnableDisableApWorker (
607 IN UINTN ProcessorNumber
,
609 IN UINT32
*HealthFlag OPTIONAL
613 Get pointer to CPU MP Data structure from GUIDed HOB.
615 @return The pointer to CPU MP Data structure.
618 GetCpuMpDataFromGuidedHob (
622 /** Checks status of specified AP.
624 This function checks whether the specified AP has finished the task assigned
625 by StartupThisAP(), and whether timeout expires.
627 @param[in] ProcessorNumber The handle number of processor.
629 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
630 @retval EFI_TIMEOUT The timeout expires.
631 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
635 IN UINTN ProcessorNumber
639 Checks status of all APs.
641 This function checks whether all APs have finished task assigned by StartupAllAPs(),
642 and whether timeout expires.
644 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
645 @retval EFI_TIMEOUT The timeout expires.
646 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
654 Checks APs status and updates APs status if needed.
658 CheckAndUpdateApsStatus (
663 Detect whether specified processor can find matching microcode patch and load it.
665 @param[in] CpuMpData The pointer to CPU MP Data structure.
666 @param[in] ProcessorNumber The handle number of the processor. The range is
667 from 0 to the total number of logical processors
672 IN CPU_MP_DATA
*CpuMpData
,
673 IN UINTN ProcessorNumber
677 Shadow the required microcode patches data into memory.
679 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
682 ShadowMicrocodeUpdatePatch (
683 IN OUT CPU_MP_DATA
*CpuMpData
687 Get the cached microcode patch base address and size from the microcode patch
688 information cache HOB.
690 @param[out] Address Base address of the microcode patches data.
691 It will be updated if the microcode patch
692 information cache HOB is found.
693 @param[out] RegionSize Size of the microcode patches data.
694 It will be updated if the microcode patch
695 information cache HOB is found.
697 @retval TRUE The microcode patch information cache HOB is found.
698 @retval FALSE The microcode patch information cache HOB is not found.
702 GetMicrocodePatchInfoFromHob (
708 Detect whether Mwait-monitor feature is supported.
710 @retval TRUE Mwait-monitor feature is supported.
711 @retval FALSE Mwait-monitor feature is not supported.
719 Enable Debug Agent to support source debugging on AP function.
728 Find the current Processor number by APIC ID.
730 @param[in] CpuMpData Pointer to PEI CPU MP Data
731 @param[out] ProcessorNumber Return the pocessor number found
733 @retval EFI_SUCCESS ProcessorNumber is found and returned.
734 @retval EFI_NOT_FOUND ProcessorNumber is not found.
738 IN CPU_MP_DATA
*CpuMpData
,
739 OUT UINTN
*ProcessorNumber
743 This funtion will try to invoke platform specific microcode shadow logic to
744 relocate microcode update patches into memory.
746 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
748 @retval EFI_SUCCESS Shadow microcode success.
749 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
750 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
754 PlatformShadowMicrocode (
755 IN OUT CPU_MP_DATA
*CpuMpData
759 Allocate the SEV-ES AP jump table buffer.
761 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
764 AllocateSevEsAPMemory (
765 IN OUT CPU_MP_DATA
*CpuMpData
769 Program the SEV-ES AP jump table buffer.
771 @param[in] SipiVector The SIPI vector used for the AP Reset
779 The function puts the AP in halt loop.
781 @param[in] CpuMpData The pointer to CPU MP Data structure.
785 CPU_MP_DATA
*CpuMpData
789 Check if the specified confidential computing attribute is active.
791 @retval TRUE The specified Attr is active.
792 @retval FALSE The specified Attr is not active.
796 ConfidentialComputingGuestHas (
797 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
801 The function fills the exchange data for the AP.
803 @param[in] ExchangeInfo The pointer to CPU Exchange Data structure
806 FillExchangeInfoDataSevEs (
807 IN
volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
811 Issue RMPADJUST to adjust the VMSA attribute of an SEV-SNP page.
813 @param[in] PageAddress
816 @return RMPADJUST return value
820 IN EFI_PHYSICAL_ADDRESS PageAddress
,
825 Create an SEV-SNP AP save area (VMSA) for use in running the vCPU.
827 @param[in] CpuMpData Pointer to CPU MP Data
828 @param[in] CpuData Pointer to CPU AP Data
829 @param[in] ApicId APIC ID of the vCPU
832 SevSnpCreateSaveArea (
833 IN CPU_MP_DATA
*CpuMpData
,
834 IN CPU_AP_DATA
*CpuData
,
841 @param[in] CpuMpData Pointer to CPU MP Data
842 @param[in] ProcessorNumber The handle number of specified processor
847 IN CPU_MP_DATA
*CpuMpData
,
848 IN INTN ProcessorNumber