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 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 SwitchToRealNoNxOffset
;
185 UINTN SwitchToRealPM16ModeOffset
;
186 UINTN SwitchToRealPM16ModeSize
;
187 } MP_ASSEMBLY_ADDRESS_MAP
;
189 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
194 // MP CPU exchange information for AP reset code
195 // This structure is required to be packed because fixed field offsets
196 // into this structure are used in assembly code in this module
202 IA32_DESCRIPTOR GdtrProfile
;
203 IA32_DESCRIPTOR IdtrProfile
;
209 UINTN EnableExecuteDisable
;
212 CPU_INFO_IN_HOB
*CpuInfo
;
213 UINTN NumApsExecuting
;
214 CPU_MP_DATA
*CpuMpData
;
215 UINTN InitializeFloatingPointUnitsAddress
;
216 UINT32 ModeTransitionMemory
;
217 UINT16 ModeTransitionSegment
;
218 UINT32 ModeHighMemory
;
219 UINT16 ModeHighSegment
;
221 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
223 BOOLEAN Enable5LevelPaging
;
224 BOOLEAN SevEsIsEnabled
;
225 BOOLEAN SevSnpIsEnabled
;
227 BOOLEAN ExtTopoAvail
;
228 } MP_CPU_EXCHANGE_INFO
;
233 // CPU MP Data save in memory
235 struct _CPU_MP_DATA
{
240 // The above fields data will be passed from PEI to DXE
241 // Please make sure the fields offset same in the different
246 UINTN CpuApStackSize
;
247 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
249 UINTN WakeupBufferHigh
;
251 UINTN BackupBufferSize
;
253 volatile UINT32 FinishedCount
;
255 BOOLEAN SingleThread
;
256 EFI_AP_PROCEDURE Procedure
;
263 UINTN
**FailedCpuList
;
265 AP_INIT_STATE InitFlag
;
266 BOOLEAN SwitchBspFlag
;
268 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
269 CPU_EXCHANGE_ROLE_INFO APInfo
;
270 MTRR_SETTINGS MtrrTable
;
272 UINT8 ApTargetCState
;
273 UINT16 PmCodeSegment
;
274 UINT16 Pm16CodeSegment
;
275 CPU_AP_DATA
*CpuData
;
276 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
278 UINT32 CurrentTimerCount
;
281 BOOLEAN PeriodicMode
;
282 BOOLEAN TimerInterruptState
;
283 UINT64 MicrocodePatchAddress
;
284 UINT64 MicrocodePatchRegionSize
;
287 // Whether need to use Init-Sipi-Sipi to wake up the APs.
288 // Two cases need to set this value to TRUE. One is in HLT
289 // loop mode, the other is resume from S3 which loop mode
290 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
293 BOOLEAN WakeUpByInitSipiSipi
;
295 BOOLEAN SevEsIsEnabled
;
296 BOOLEAN SevSnpIsEnabled
;
297 BOOLEAN UseSevEsAPMethod
;
299 UINTN SevEsAPResetStackStart
;
300 CPU_MP_DATA
*NewCpuMpData
;
305 #define AP_SAFE_STACK_SIZE 128
306 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
319 Assembly code to move an AP from long mode to real mode.
321 Move an AP from long mode to real mode in preparation to invoking
322 the reset vector. This is used for SEV-ES guests where a hypervisor
323 is not allowed to set the CS and RIP to point to the reset vector.
325 @param[in] BufferStart The reset vector target.
326 @param[in] Code16 16-bit protected mode code segment value.
327 @param[in] Code32 32-bit protected mode code segment value.
328 @param[in] StackStart The start of a stack to be used for transitioning
329 from long mode to real mode.
334 IN UINTN BufferStart
,
340 extern EFI_GUID mCpuInitMpLibHobGuid
;
343 Assembly code to place AP into safe loop mode.
345 Place AP into targeted C-State if MONITOR is supported, otherwise
346 place AP into hlt state.
347 Place AP in protected mode if the current is long mode. Due to AP maybe
348 wakeup by some hardware event. It could avoid accessing page table that
349 may not available during booting to OS.
351 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
352 FALSE indicates MONITOR is not supported.
353 @param[in] ApTargetCState Target C-State value.
354 @param[in] PmCodeSegment Protected mode code segment value.
358 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
359 IN BOOLEAN MwaitSupport
,
360 IN UINTN ApTargetCState
,
361 IN UINTN PmCodeSegment
,
362 IN UINTN TopOfApStack
,
363 IN UINTN NumberToFinish
,
364 IN UINTN Pm16CodeSegment
,
365 IN UINTN SevEsAPJumpTable
,
366 IN UINTN WakeupBuffer
370 Assembly code to get starting address and size of the rendezvous entry for APs.
371 Information for fixing a jump instruction in the code is also returned.
373 @param[out] AddressMap Output buffer for address map information.
378 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
382 This function is called by both the BSP and the AP which is to become the BSP to
383 Exchange execution context including stack between them. After return from this
384 function, the BSP becomes AP and the AP becomes the BSP.
386 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
387 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
393 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
394 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
398 Get the pointer to CPU MP Data structure.
400 @return The pointer to CPU MP Data structure.
408 Save the pointer to CPU MP Data structure.
410 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
414 IN CPU_MP_DATA
*CpuMpData
418 Get available system memory below 1MB by specified size.
420 @param[in] WakeupBufferSize Wakeup buffer size required
422 @retval other Return wakeup buffer address below 1MB.
423 @retval -1 Cannot find free memory below 1MB.
427 IN UINTN WakeupBufferSize
431 Get available EfiBootServicesCode memory below 4GB by specified size.
433 This buffer is required to safely transfer AP from real address mode to
434 protected mode or long mode, due to the fact that the buffer returned by
435 GetWakeupBuffer() may be marked as non-executable.
437 @param[in] BufferSize Wakeup transition buffer size.
439 @retval other Return wakeup transition buffer address below 4GB.
440 @retval 0 Cannot find free memory below 4GB.
448 Return the address of the SEV-ES AP jump table.
450 This buffer is required in order for an SEV-ES guest to transition from
453 @return Return SEV-ES AP jump table buffer
461 This function will be called by BSP to wakeup AP.
463 @param[in] CpuMpData Pointer to CPU MP Data
464 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
465 FALSE: Send IPI to AP by ApicId
466 @param[in] ProcessorNumber The handle number of specified processor
467 @param[in] Procedure The function to be invoked by AP
468 @param[in] ProcedureArgument The argument to be passed into AP function
469 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
473 IN CPU_MP_DATA
*CpuMpData
,
474 IN BOOLEAN Broadcast
,
475 IN UINTN ProcessorNumber
,
476 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
477 IN VOID
*ProcedureArgument OPTIONAL
,
478 IN BOOLEAN WakeUpDisabledAps
482 Initialize global data for MP support.
484 @param[in] CpuMpData The pointer to CPU MP Data structure.
488 IN CPU_MP_DATA
*CpuMpData
492 Worker function to execute a caller provided function on all enabled APs.
494 @param[in] Procedure A pointer to the function to be run on
495 enabled APs of the system.
496 @param[in] SingleThread If TRUE, then all the enabled APs execute
497 the function specified by Procedure one by
498 one, in ascending order of processor handle
499 number. If FALSE, then all the enabled APs
500 execute the function specified by Procedure
502 @param[in] ExcludeBsp Whether let BSP also trig this task.
503 @param[in] WaitEvent The event created by the caller with CreateEvent()
505 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
506 APs to return from Procedure, either for
507 blocking or non-blocking mode.
508 @param[in] ProcedureArgument The parameter passed into Procedure for
510 @param[out] FailedCpuList If all APs finish successfully, then its
511 content is set to NULL. If not all APs
512 finish before timeout expires, then its
513 content is set to address of the buffer
514 holding handle numbers of the failed APs.
516 @retval EFI_SUCCESS In blocking mode, all APs have finished before
518 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
520 @retval others Failed to Startup all APs.
524 StartupAllCPUsWorker (
525 IN EFI_AP_PROCEDURE Procedure
,
526 IN BOOLEAN SingleThread
,
527 IN BOOLEAN ExcludeBsp
,
528 IN EFI_EVENT WaitEvent OPTIONAL
,
529 IN UINTN TimeoutInMicroseconds
,
530 IN VOID
*ProcedureArgument OPTIONAL
,
531 OUT UINTN
**FailedCpuList OPTIONAL
535 Worker function to let the caller get one enabled AP to execute a caller-provided
538 @param[in] Procedure A pointer to the function to be run on
539 enabled APs of the system.
540 @param[in] ProcessorNumber The handle number of the AP.
541 @param[in] WaitEvent The event created by the caller with CreateEvent()
543 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
544 APs to return from Procedure, either for
545 blocking or non-blocking mode.
546 @param[in] ProcedureArgument The parameter passed into Procedure for
548 @param[out] Finished If AP returns from Procedure before the
549 timeout expires, its content is set to TRUE.
550 Otherwise, the value is set to FALSE.
552 @retval EFI_SUCCESS In blocking mode, specified AP finished before
554 @retval others Failed to Startup AP.
558 StartupThisAPWorker (
559 IN EFI_AP_PROCEDURE Procedure
,
560 IN UINTN ProcessorNumber
,
561 IN EFI_EVENT WaitEvent OPTIONAL
,
562 IN UINTN TimeoutInMicroseconds
,
563 IN VOID
*ProcedureArgument OPTIONAL
,
564 OUT BOOLEAN
*Finished OPTIONAL
568 Worker function to switch the requested AP to be the BSP from that point onward.
570 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
571 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
572 enabled AP. Otherwise, it will be disabled.
574 @retval EFI_SUCCESS BSP successfully switched.
575 @retval others Failed to switch BSP.
580 IN UINTN ProcessorNumber
,
581 IN BOOLEAN EnableOldBSP
585 Worker function to let the caller enable or disable an AP from this point onward.
586 This service may only be called from the BSP.
588 @param[in] ProcessorNumber The handle number of AP.
589 @param[in] EnableAP Specifies the new state for the processor for
590 enabled, FALSE for disabled.
591 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
592 the new health status of the AP.
594 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
595 @retval others Failed to Enable/Disable AP.
599 EnableDisableApWorker (
600 IN UINTN ProcessorNumber
,
602 IN UINT32
*HealthFlag OPTIONAL
606 Get pointer to CPU MP Data structure from GUIDed HOB.
608 @return The pointer to CPU MP Data structure.
611 GetCpuMpDataFromGuidedHob (
615 /** Checks status of specified AP.
617 This function checks whether the specified AP has finished the task assigned
618 by StartupThisAP(), and whether timeout expires.
620 @param[in] ProcessorNumber The handle number of processor.
622 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
623 @retval EFI_TIMEOUT The timeout expires.
624 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
628 IN UINTN ProcessorNumber
632 Checks status of all APs.
634 This function checks whether all APs have finished task assigned by StartupAllAPs(),
635 and whether timeout expires.
637 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
638 @retval EFI_TIMEOUT The timeout expires.
639 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
647 Checks APs status and updates APs status if needed.
651 CheckAndUpdateApsStatus (
656 Detect whether specified processor can find matching microcode patch and load it.
658 @param[in] CpuMpData The pointer to CPU MP Data structure.
659 @param[in] ProcessorNumber The handle number of the processor. The range is
660 from 0 to the total number of logical processors
665 IN CPU_MP_DATA
*CpuMpData
,
666 IN UINTN ProcessorNumber
670 Shadow the required microcode patches data into memory.
672 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
675 ShadowMicrocodeUpdatePatch (
676 IN OUT CPU_MP_DATA
*CpuMpData
680 Get the cached microcode patch base address and size from the microcode patch
681 information cache HOB.
683 @param[out] Address Base address of the microcode patches data.
684 It will be updated if the microcode patch
685 information cache HOB is found.
686 @param[out] RegionSize Size of the microcode patches data.
687 It will be updated if the microcode patch
688 information cache HOB is found.
690 @retval TRUE The microcode patch information cache HOB is found.
691 @retval FALSE The microcode patch information cache HOB is not found.
695 GetMicrocodePatchInfoFromHob (
701 Detect whether Mwait-monitor feature is supported.
703 @retval TRUE Mwait-monitor feature is supported.
704 @retval FALSE Mwait-monitor feature is not supported.
712 Enable Debug Agent to support source debugging on AP function.
721 Find the current Processor number by APIC ID.
723 @param[in] CpuMpData Pointer to PEI CPU MP Data
724 @param[out] ProcessorNumber Return the pocessor number found
726 @retval EFI_SUCCESS ProcessorNumber is found and returned.
727 @retval EFI_NOT_FOUND ProcessorNumber is not found.
731 IN CPU_MP_DATA
*CpuMpData
,
732 OUT UINTN
*ProcessorNumber
736 This funtion will try to invoke platform specific microcode shadow logic to
737 relocate microcode update patches into memory.
739 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
741 @retval EFI_SUCCESS Shadow microcode success.
742 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
743 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
747 PlatformShadowMicrocode (
748 IN OUT CPU_MP_DATA
*CpuMpData
752 Allocate the SEV-ES AP jump table buffer.
754 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
757 AllocateSevEsAPMemory (
758 IN OUT CPU_MP_DATA
*CpuMpData
762 Program the SEV-ES AP jump table buffer.
764 @param[in] SipiVector The SIPI vector used for the AP Reset
772 The function puts the AP in halt loop.
774 @param[in] CpuMpData The pointer to CPU MP Data structure.
778 CPU_MP_DATA
*CpuMpData
782 Check if the specified confidential computing attribute is active.
784 @retval TRUE The specified Attr is active.
785 @retval FALSE The specified Attr is not active.
789 ConfidentialComputingGuestHas (
790 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
794 The function fills the exchange data for the AP.
796 @param[in] ExchangeInfo The pointer to CPU Exchange Data structure
799 FillExchangeInfoDataSevEs (
800 IN
volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
804 Issue RMPADJUST to adjust the VMSA attribute of an SEV-SNP page.
806 @param[in] PageAddress
809 @return RMPADJUST return value
813 IN EFI_PHYSICAL_ADDRESS PageAddress
,
818 Create an SEV-SNP AP save area (VMSA) for use in running the vCPU.
820 @param[in] CpuMpData Pointer to CPU MP Data
821 @param[in] CpuData Pointer to CPU AP Data
822 @param[in] ApicId APIC ID of the vCPU
825 SevSnpCreateSaveArea (
826 IN CPU_MP_DATA
*CpuMpData
,
827 IN CPU_AP_DATA
*CpuData
,
834 @param[in] CpuMpData Pointer to CPU MP Data
835 @param[in] ProcessorNumber The handle number of specified processor
840 IN CPU_MP_DATA
*CpuMpData
,
841 IN INTN ProcessorNumber