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>
35 #include <Library/MicrocodeLib.h>
36 #include <ConfidentialComputingGuestAttr.h>
38 #include <Register/Amd/Fam17Msr.h>
39 #include <Register/Amd/Ghcb.h>
41 #include <Guid/MicrocodePatchHob.h>
43 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
45 #define CPU_INIT_MP_LIB_HOB_GUID \
47 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
51 // The MP data for switch BSP
53 #define CPU_SWITCH_STATE_IDLE 0
54 #define CPU_SWITCH_STATE_STORED 1
55 #define CPU_SWITCH_STATE_LOADED 2
58 // Default maximum number of entries to store the microcode patches information
60 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
63 // Data structure for microcode patch information
68 } MICROCODE_PATCH_INFO
;
71 // CPU exchange information for switch BSP
74 UINT8 State
; // offset 0
75 UINTN StackPointer
; // offset 4 / 8
76 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
77 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
78 } CPU_EXCHANGE_ROLE_INFO
;
81 // AP loop state when APs are in idle state
82 // It's value is the same with PcdCpuApLoopMode
91 // AP initialization state during APs wakeup
102 // The state transitions for an AP when it process a procedure are:
103 // Idle ----> Ready ----> Busy ----> Idle
115 // CPU volatile registers around INIT-SIPI-SIPI
127 IA32_DESCRIPTOR Gdtr
;
128 IA32_DESCRIPTOR Idtr
;
130 } CPU_VOLATILE_REGISTERS
;
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
;
156 // Basic CPU information saved in Guided HOB.
157 // Because the contents will be shard between PEI and DXE,
158 // we need to make sure the each fields offset same in different
163 UINT32 InitialApicId
;
171 // AP reset code information including code address and size,
172 // this structure will be shared be C code and assembly code.
173 // It is natural aligned by design.
176 UINT8
*RendezvousFunnelAddress
;
177 UINTN ModeEntryOffset
;
178 UINTN RendezvousFunnelSize
;
179 UINT8
*RelocateApLoopFuncAddress
;
180 UINTN RelocateApLoopFuncSize
;
181 UINTN ModeTransitionOffset
;
182 UINTN SwitchToRealSize
;
183 UINTN SwitchToRealOffset
;
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 } 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
;
297 UINTN SevEsAPResetStackStart
;
298 CPU_MP_DATA
*NewCpuMpData
;
303 #define AP_SAFE_STACK_SIZE 128
304 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
317 Assembly code to move an AP from long mode to real mode.
319 Move an AP from long mode to real mode in preparation to invoking
320 the reset vector. This is used for SEV-ES guests where a hypervisor
321 is not allowed to set the CS and RIP to point to the reset vector.
323 @param[in] BufferStart The reset vector target.
324 @param[in] Code16 16-bit protected mode code segment value.
325 @param[in] Code32 32-bit protected mode code segment value.
326 @param[in] StackStart The start of a stack to be used for transitioning
327 from long mode to real mode.
332 IN UINTN BufferStart
,
338 extern EFI_GUID mCpuInitMpLibHobGuid
;
341 Assembly code to place AP into safe loop mode.
343 Place AP into targeted C-State if MONITOR is supported, otherwise
344 place AP into hlt state.
345 Place AP in protected mode if the current is long mode. Due to AP maybe
346 wakeup by some hardware event. It could avoid accessing page table that
347 may not available during booting to OS.
349 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
350 FALSE indicates MONITOR is not supported.
351 @param[in] ApTargetCState Target C-State value.
352 @param[in] PmCodeSegment Protected mode code segment value.
356 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
357 IN BOOLEAN MwaitSupport
,
358 IN UINTN ApTargetCState
,
359 IN UINTN PmCodeSegment
,
360 IN UINTN TopOfApStack
,
361 IN UINTN NumberToFinish
,
362 IN UINTN Pm16CodeSegment
,
363 IN UINTN SevEsAPJumpTable
,
364 IN UINTN WakeupBuffer
368 Assembly code to get starting address and size of the rendezvous entry for APs.
369 Information for fixing a jump instruction in the code is also returned.
371 @param[out] AddressMap Output buffer for address map information.
376 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
380 This function is called by both the BSP and the AP which is to become the BSP to
381 Exchange execution context including stack between them. After return from this
382 function, the BSP becomes AP and the AP becomes the BSP.
384 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
385 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
391 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
392 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
396 Get the pointer to CPU MP Data structure.
398 @return The pointer to CPU MP Data structure.
406 Save the pointer to CPU MP Data structure.
408 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
412 IN CPU_MP_DATA
*CpuMpData
416 Get available system memory below 1MB by specified size.
418 @param[in] WakeupBufferSize Wakeup buffer size required
420 @retval other Return wakeup buffer address below 1MB.
421 @retval -1 Cannot find free memory below 1MB.
425 IN UINTN WakeupBufferSize
429 Get available EfiBootServicesCode memory below 4GB by specified size.
431 This buffer is required to safely transfer AP from real address mode to
432 protected mode or long mode, due to the fact that the buffer returned by
433 GetWakeupBuffer() may be marked as non-executable.
435 @param[in] BufferSize Wakeup transition buffer size.
437 @retval other Return wakeup transition buffer address below 4GB.
438 @retval 0 Cannot find free memory below 4GB.
441 GetModeTransitionBuffer (
446 Return the address of the SEV-ES AP jump table.
448 This buffer is required in order for an SEV-ES guest to transition from
451 @return Return SEV-ES AP jump table buffer
459 This function will be called by BSP to wakeup AP.
461 @param[in] CpuMpData Pointer to CPU MP Data
462 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
463 FALSE: Send IPI to AP by ApicId
464 @param[in] ProcessorNumber The handle number of specified processor
465 @param[in] Procedure The function to be invoked by AP
466 @param[in] ProcedureArgument The argument to be passed into AP function
467 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
471 IN CPU_MP_DATA
*CpuMpData
,
472 IN BOOLEAN Broadcast
,
473 IN UINTN ProcessorNumber
,
474 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
475 IN VOID
*ProcedureArgument OPTIONAL
,
476 IN BOOLEAN WakeUpDisabledAps OPTIONAL
480 Initialize global data for MP support.
482 @param[in] CpuMpData The pointer to CPU MP Data structure.
486 IN CPU_MP_DATA
*CpuMpData
490 Worker function to execute a caller provided function on all enabled APs.
492 @param[in] Procedure A pointer to the function to be run on
493 enabled APs of the system.
494 @param[in] SingleThread If TRUE, then all the enabled APs execute
495 the function specified by Procedure one by
496 one, in ascending order of processor handle
497 number. If FALSE, then all the enabled APs
498 execute the function specified by Procedure
500 @param[in] ExcludeBsp Whether let BSP also trig this task.
501 @param[in] WaitEvent The event created by the caller with CreateEvent()
503 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
504 APs to return from Procedure, either for
505 blocking or non-blocking mode.
506 @param[in] ProcedureArgument The parameter passed into Procedure for
508 @param[out] FailedCpuList If all APs finish successfully, then its
509 content is set to NULL. If not all APs
510 finish before timeout expires, then its
511 content is set to address of the buffer
512 holding handle numbers of the failed APs.
514 @retval EFI_SUCCESS In blocking mode, all APs have finished before
516 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
518 @retval others Failed to Startup all APs.
522 StartupAllCPUsWorker (
523 IN EFI_AP_PROCEDURE Procedure
,
524 IN BOOLEAN SingleThread
,
525 IN BOOLEAN ExcludeBsp
,
526 IN EFI_EVENT WaitEvent OPTIONAL
,
527 IN UINTN TimeoutInMicroseconds
,
528 IN VOID
*ProcedureArgument OPTIONAL
,
529 OUT UINTN
**FailedCpuList OPTIONAL
533 Worker function to let the caller get one enabled AP to execute a caller-provided
536 @param[in] Procedure A pointer to the function to be run on
537 enabled APs of the system.
538 @param[in] ProcessorNumber The handle number of the AP.
539 @param[in] WaitEvent The event created by the caller with CreateEvent()
541 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
542 APs to return from Procedure, either for
543 blocking or non-blocking mode.
544 @param[in] ProcedureArgument The parameter passed into Procedure for
546 @param[out] Finished If AP returns from Procedure before the
547 timeout expires, its content is set to TRUE.
548 Otherwise, the value is set to FALSE.
550 @retval EFI_SUCCESS In blocking mode, specified AP finished before
552 @retval others Failed to Startup AP.
556 StartupThisAPWorker (
557 IN EFI_AP_PROCEDURE Procedure
,
558 IN UINTN ProcessorNumber
,
559 IN EFI_EVENT WaitEvent OPTIONAL
,
560 IN UINTN TimeoutInMicroseconds
,
561 IN VOID
*ProcedureArgument OPTIONAL
,
562 OUT BOOLEAN
*Finished OPTIONAL
566 Worker function to switch the requested AP to be the BSP from that point onward.
568 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
569 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
570 enabled AP. Otherwise, it will be disabled.
572 @retval EFI_SUCCESS BSP successfully switched.
573 @retval others Failed to switch BSP.
578 IN UINTN ProcessorNumber
,
579 IN BOOLEAN EnableOldBSP
583 Worker function to let the caller enable or disable an AP from this point onward.
584 This service may only be called from the BSP.
586 @param[in] ProcessorNumber The handle number of AP.
587 @param[in] EnableAP Specifies the new state for the processor for
588 enabled, FALSE for disabled.
589 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
590 the new health status of the AP.
592 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
593 @retval others Failed to Enable/Disable AP.
597 EnableDisableApWorker (
598 IN UINTN ProcessorNumber
,
600 IN UINT32
*HealthFlag OPTIONAL
604 Get pointer to CPU MP Data structure from GUIDed HOB.
606 @return The pointer to CPU MP Data structure.
609 GetCpuMpDataFromGuidedHob (
613 /** Checks status of specified AP.
615 This function checks whether the specified AP has finished the task assigned
616 by StartupThisAP(), and whether timeout expires.
618 @param[in] ProcessorNumber The handle number of processor.
620 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
621 @retval EFI_TIMEOUT The timeout expires.
622 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
626 IN UINTN ProcessorNumber
630 Checks status of all APs.
632 This function checks whether all APs have finished task assigned by StartupAllAPs(),
633 and whether timeout expires.
635 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
636 @retval EFI_TIMEOUT The timeout expires.
637 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
645 Checks APs status and updates APs status if needed.
649 CheckAndUpdateApsStatus (
654 Detect whether specified processor can find matching microcode patch and load it.
656 @param[in] CpuMpData The pointer to CPU MP Data structure.
657 @param[in] ProcessorNumber The handle number of the processor. The range is
658 from 0 to the total number of logical processors
663 IN CPU_MP_DATA
*CpuMpData
,
664 IN UINTN ProcessorNumber
668 Shadow the required microcode patches data into memory.
670 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
673 ShadowMicrocodeUpdatePatch (
674 IN OUT CPU_MP_DATA
*CpuMpData
678 Get the cached microcode patch base address and size from the microcode patch
679 information cache HOB.
681 @param[out] Address Base address of the microcode patches data.
682 It will be updated if the microcode patch
683 information cache HOB is found.
684 @param[out] RegionSize Size of the microcode patches data.
685 It will be updated if the microcode patch
686 information cache HOB is found.
688 @retval TRUE The microcode patch information cache HOB is found.
689 @retval FALSE The microcode patch information cache HOB is not found.
693 GetMicrocodePatchInfoFromHob (
699 Detect whether Mwait-monitor feature is supported.
701 @retval TRUE Mwait-monitor feature is supported.
702 @retval FALSE Mwait-monitor feature is not supported.
710 Enable Debug Agent to support source debugging on AP function.
719 Find the current Processor number by APIC ID.
721 @param[in] CpuMpData Pointer to PEI CPU MP Data
722 @param[out] ProcessorNumber Return the pocessor number found
724 @retval EFI_SUCCESS ProcessorNumber is found and returned.
725 @retval EFI_NOT_FOUND ProcessorNumber is not found.
729 IN CPU_MP_DATA
*CpuMpData
,
730 OUT UINTN
*ProcessorNumber
734 This funtion will try to invoke platform specific microcode shadow logic to
735 relocate microcode update patches into memory.
737 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
739 @retval EFI_SUCCESS Shadow microcode success.
740 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
741 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
745 PlatformShadowMicrocode (
746 IN OUT CPU_MP_DATA
*CpuMpData
750 Allocate the SEV-ES AP jump table buffer.
752 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
755 AllocateSevEsAPMemory (
756 IN OUT CPU_MP_DATA
*CpuMpData
760 Program the SEV-ES AP jump table buffer.
762 @param[in] SipiVector The SIPI vector used for the AP Reset
770 The function puts the AP in halt loop.
772 @param[in] CpuMpData The pointer to CPU MP Data structure.
776 CPU_MP_DATA
*CpuMpData
780 Check if the specified confidential computing attribute is active.
782 @retval TRUE The specified Attr is active.
783 @retval FALSE The specified Attr is not active.
787 ConfidentialComputingGuestHas (
788 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr