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
;
226 } MP_CPU_EXCHANGE_INFO
;
231 // CPU MP Data save in memory
233 struct _CPU_MP_DATA
{
238 // The above fields data will be passed from PEI to DXE
239 // Please make sure the fields offset same in the different
244 UINTN CpuApStackSize
;
245 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
247 UINTN WakeupBufferHigh
;
249 UINTN BackupBufferSize
;
251 volatile UINT32 FinishedCount
;
253 BOOLEAN SingleThread
;
254 EFI_AP_PROCEDURE Procedure
;
261 UINTN
**FailedCpuList
;
263 AP_INIT_STATE InitFlag
;
264 BOOLEAN SwitchBspFlag
;
266 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
267 CPU_EXCHANGE_ROLE_INFO APInfo
;
268 MTRR_SETTINGS MtrrTable
;
270 UINT8 ApTargetCState
;
271 UINT16 PmCodeSegment
;
272 UINT16 Pm16CodeSegment
;
273 CPU_AP_DATA
*CpuData
;
274 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
276 UINT32 CurrentTimerCount
;
279 BOOLEAN PeriodicMode
;
280 BOOLEAN TimerInterruptState
;
281 UINT64 MicrocodePatchAddress
;
282 UINT64 MicrocodePatchRegionSize
;
285 // Whether need to use Init-Sipi-Sipi to wake up the APs.
286 // Two cases need to set this value to TRUE. One is in HLT
287 // loop mode, the other is resume from S3 which loop mode
288 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
291 BOOLEAN WakeUpByInitSipiSipi
;
293 BOOLEAN SevEsIsEnabled
;
295 UINTN SevEsAPResetStackStart
;
296 CPU_MP_DATA
*NewCpuMpData
;
301 #define AP_SAFE_STACK_SIZE 128
302 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
315 Assembly code to move an AP from long mode to real mode.
317 Move an AP from long mode to real mode in preparation to invoking
318 the reset vector. This is used for SEV-ES guests where a hypervisor
319 is not allowed to set the CS and RIP to point to the reset vector.
321 @param[in] BufferStart The reset vector target.
322 @param[in] Code16 16-bit protected mode code segment value.
323 @param[in] Code32 32-bit protected mode code segment value.
324 @param[in] StackStart The start of a stack to be used for transitioning
325 from long mode to real mode.
330 IN UINTN BufferStart
,
336 extern EFI_GUID mCpuInitMpLibHobGuid
;
339 Assembly code to place AP into safe loop mode.
341 Place AP into targeted C-State if MONITOR is supported, otherwise
342 place AP into hlt state.
343 Place AP in protected mode if the current is long mode. Due to AP maybe
344 wakeup by some hardware event. It could avoid accessing page table that
345 may not available during booting to OS.
347 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
348 FALSE indicates MONITOR is not supported.
349 @param[in] ApTargetCState Target C-State value.
350 @param[in] PmCodeSegment Protected mode code segment value.
354 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
355 IN BOOLEAN MwaitSupport
,
356 IN UINTN ApTargetCState
,
357 IN UINTN PmCodeSegment
,
358 IN UINTN TopOfApStack
,
359 IN UINTN NumberToFinish
,
360 IN UINTN Pm16CodeSegment
,
361 IN UINTN SevEsAPJumpTable
,
362 IN UINTN WakeupBuffer
366 Assembly code to get starting address and size of the rendezvous entry for APs.
367 Information for fixing a jump instruction in the code is also returned.
369 @param[out] AddressMap Output buffer for address map information.
374 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
378 This function is called by both the BSP and the AP which is to become the BSP to
379 Exchange execution context including stack between them. After return from this
380 function, the BSP becomes AP and the AP becomes the BSP.
382 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
383 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
389 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
390 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
394 Get the pointer to CPU MP Data structure.
396 @return The pointer to CPU MP Data structure.
404 Save the pointer to CPU MP Data structure.
406 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
410 IN CPU_MP_DATA
*CpuMpData
414 Get available system memory below 1MB by specified size.
416 @param[in] WakeupBufferSize Wakeup buffer size required
418 @retval other Return wakeup buffer address below 1MB.
419 @retval -1 Cannot find free memory below 1MB.
423 IN UINTN WakeupBufferSize
427 Get available EfiBootServicesCode memory below 4GB by specified size.
429 This buffer is required to safely transfer AP from real address mode to
430 protected mode or long mode, due to the fact that the buffer returned by
431 GetWakeupBuffer() may be marked as non-executable.
433 @param[in] BufferSize Wakeup transition buffer size.
435 @retval other Return wakeup transition buffer address below 4GB.
436 @retval 0 Cannot find free memory below 4GB.
439 GetModeTransitionBuffer (
444 Return the address of the SEV-ES AP jump table.
446 This buffer is required in order for an SEV-ES guest to transition from
449 @return Return SEV-ES AP jump table buffer
457 This function will be called by BSP to wakeup AP.
459 @param[in] CpuMpData Pointer to CPU MP Data
460 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
461 FALSE: Send IPI to AP by ApicId
462 @param[in] ProcessorNumber The handle number of specified processor
463 @param[in] Procedure The function to be invoked by AP
464 @param[in] ProcedureArgument The argument to be passed into AP function
465 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
469 IN CPU_MP_DATA
*CpuMpData
,
470 IN BOOLEAN Broadcast
,
471 IN UINTN ProcessorNumber
,
472 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
473 IN VOID
*ProcedureArgument OPTIONAL
,
474 IN BOOLEAN WakeUpDisabledAps OPTIONAL
478 Initialize global data for MP support.
480 @param[in] CpuMpData The pointer to CPU MP Data structure.
484 IN CPU_MP_DATA
*CpuMpData
488 Worker function to execute a caller provided function on all enabled APs.
490 @param[in] Procedure A pointer to the function to be run on
491 enabled APs of the system.
492 @param[in] SingleThread If TRUE, then all the enabled APs execute
493 the function specified by Procedure one by
494 one, in ascending order of processor handle
495 number. If FALSE, then all the enabled APs
496 execute the function specified by Procedure
498 @param[in] ExcludeBsp Whether let BSP also trig this task.
499 @param[in] WaitEvent The event created by the caller with CreateEvent()
501 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
502 APs to return from Procedure, either for
503 blocking or non-blocking mode.
504 @param[in] ProcedureArgument The parameter passed into Procedure for
506 @param[out] FailedCpuList If all APs finish successfully, then its
507 content is set to NULL. If not all APs
508 finish before timeout expires, then its
509 content is set to address of the buffer
510 holding handle numbers of the failed APs.
512 @retval EFI_SUCCESS In blocking mode, all APs have finished before
514 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
516 @retval others Failed to Startup all APs.
520 StartupAllCPUsWorker (
521 IN EFI_AP_PROCEDURE Procedure
,
522 IN BOOLEAN SingleThread
,
523 IN BOOLEAN ExcludeBsp
,
524 IN EFI_EVENT WaitEvent OPTIONAL
,
525 IN UINTN TimeoutInMicroseconds
,
526 IN VOID
*ProcedureArgument OPTIONAL
,
527 OUT UINTN
**FailedCpuList OPTIONAL
531 Worker function to let the caller get one enabled AP to execute a caller-provided
534 @param[in] Procedure A pointer to the function to be run on
535 enabled APs of the system.
536 @param[in] ProcessorNumber The handle number of the AP.
537 @param[in] WaitEvent The event created by the caller with CreateEvent()
539 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
540 APs to return from Procedure, either for
541 blocking or non-blocking mode.
542 @param[in] ProcedureArgument The parameter passed into Procedure for
544 @param[out] Finished If AP returns from Procedure before the
545 timeout expires, its content is set to TRUE.
546 Otherwise, the value is set to FALSE.
548 @retval EFI_SUCCESS In blocking mode, specified AP finished before
550 @retval others Failed to Startup AP.
554 StartupThisAPWorker (
555 IN EFI_AP_PROCEDURE Procedure
,
556 IN UINTN ProcessorNumber
,
557 IN EFI_EVENT WaitEvent OPTIONAL
,
558 IN UINTN TimeoutInMicroseconds
,
559 IN VOID
*ProcedureArgument OPTIONAL
,
560 OUT BOOLEAN
*Finished OPTIONAL
564 Worker function to switch the requested AP to be the BSP from that point onward.
566 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
567 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
568 enabled AP. Otherwise, it will be disabled.
570 @retval EFI_SUCCESS BSP successfully switched.
571 @retval others Failed to switch BSP.
576 IN UINTN ProcessorNumber
,
577 IN BOOLEAN EnableOldBSP
581 Worker function to let the caller enable or disable an AP from this point onward.
582 This service may only be called from the BSP.
584 @param[in] ProcessorNumber The handle number of AP.
585 @param[in] EnableAP Specifies the new state for the processor for
586 enabled, FALSE for disabled.
587 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
588 the new health status of the AP.
590 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
591 @retval others Failed to Enable/Disable AP.
595 EnableDisableApWorker (
596 IN UINTN ProcessorNumber
,
598 IN UINT32
*HealthFlag OPTIONAL
602 Get pointer to CPU MP Data structure from GUIDed HOB.
604 @return The pointer to CPU MP Data structure.
607 GetCpuMpDataFromGuidedHob (
611 /** Checks status of specified AP.
613 This function checks whether the specified AP has finished the task assigned
614 by StartupThisAP(), and whether timeout expires.
616 @param[in] ProcessorNumber The handle number of processor.
618 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
619 @retval EFI_TIMEOUT The timeout expires.
620 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
624 IN UINTN ProcessorNumber
628 Checks status of all APs.
630 This function checks whether all APs have finished task assigned by StartupAllAPs(),
631 and whether timeout expires.
633 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
634 @retval EFI_TIMEOUT The timeout expires.
635 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
643 Checks APs status and updates APs status if needed.
647 CheckAndUpdateApsStatus (
652 Detect whether specified processor can find matching microcode patch and load it.
654 @param[in] CpuMpData The pointer to CPU MP Data structure.
655 @param[in] ProcessorNumber The handle number of the processor. The range is
656 from 0 to the total number of logical processors
661 IN CPU_MP_DATA
*CpuMpData
,
662 IN UINTN ProcessorNumber
666 Shadow the required microcode patches data into memory.
668 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
671 ShadowMicrocodeUpdatePatch (
672 IN OUT CPU_MP_DATA
*CpuMpData
676 Get the cached microcode patch base address and size from the microcode patch
677 information cache HOB.
679 @param[out] Address Base address of the microcode patches data.
680 It will be updated if the microcode patch
681 information cache HOB is found.
682 @param[out] RegionSize Size of the microcode patches data.
683 It will be updated if the microcode patch
684 information cache HOB is found.
686 @retval TRUE The microcode patch information cache HOB is found.
687 @retval FALSE The microcode patch information cache HOB is not found.
691 GetMicrocodePatchInfoFromHob (
697 Detect whether Mwait-monitor feature is supported.
699 @retval TRUE Mwait-monitor feature is supported.
700 @retval FALSE Mwait-monitor feature is not supported.
708 Enable Debug Agent to support source debugging on AP function.
717 Find the current Processor number by APIC ID.
719 @param[in] CpuMpData Pointer to PEI CPU MP Data
720 @param[out] ProcessorNumber Return the pocessor number found
722 @retval EFI_SUCCESS ProcessorNumber is found and returned.
723 @retval EFI_NOT_FOUND ProcessorNumber is not found.
727 IN CPU_MP_DATA
*CpuMpData
,
728 OUT UINTN
*ProcessorNumber
732 This funtion will try to invoke platform specific microcode shadow logic to
733 relocate microcode update patches into memory.
735 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
737 @retval EFI_SUCCESS Shadow microcode success.
738 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
739 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
743 PlatformShadowMicrocode (
744 IN OUT CPU_MP_DATA
*CpuMpData
748 Allocate the SEV-ES AP jump table buffer.
750 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
753 AllocateSevEsAPMemory (
754 IN OUT CPU_MP_DATA
*CpuMpData
758 Program the SEV-ES AP jump table buffer.
760 @param[in] SipiVector The SIPI vector used for the AP Reset
768 The function puts the AP in halt loop.
770 @param[in] CpuMpData The pointer to CPU MP Data structure.
774 CPU_MP_DATA
*CpuMpData
778 Check if the specified confidential computing attribute is active.
780 @retval TRUE The specified Attr is active.
781 @retval FALSE The specified Attr is not active.
785 ConfidentialComputingGuestHas (
786 CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr