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>
37 #include <Register/Amd/Fam17Msr.h>
38 #include <Register/Amd/Ghcb.h>
40 #include <Guid/MicrocodePatchHob.h>
42 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
44 #define CPU_INIT_MP_LIB_HOB_GUID \
46 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
50 // The MP data for switch BSP
52 #define CPU_SWITCH_STATE_IDLE 0
53 #define CPU_SWITCH_STATE_STORED 1
54 #define CPU_SWITCH_STATE_LOADED 2
57 // Default maximum number of entries to store the microcode patches information
59 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
62 // Data structure for microcode patch information
67 } MICROCODE_PATCH_INFO
;
70 // CPU exchange information for switch BSP
73 UINT8 State
; // offset 0
74 UINTN StackPointer
; // offset 4 / 8
75 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
76 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
77 } CPU_EXCHANGE_ROLE_INFO
;
80 // AP loop state when APs are in idle state
81 // It's value is the same with PcdCpuApLoopMode
90 // AP initialization state during APs wakeup
101 // The state transitions for an AP when it process a procedure are:
102 // Idle ----> Ready ----> Busy ----> Idle
114 // CPU volatile registers around INIT-SIPI-SIPI
126 IA32_DESCRIPTOR Gdtr
;
127 IA32_DESCRIPTOR Idtr
;
129 } CPU_VOLATILE_REGISTERS
;
136 volatile UINT32
*StartupApSignal
;
137 volatile UINTN ApFunction
;
138 volatile UINTN ApFunctionArgument
;
140 volatile CPU_STATE State
;
141 CPU_VOLATILE_REGISTERS VolatileRegisters
;
148 UINT32 ProcessorSignature
;
150 UINT64 MicrocodeEntryAddr
;
151 UINT32 MicrocodeRevision
;
155 // Basic CPU information saved in Guided HOB.
156 // Because the contents will be shard between PEI and DXE,
157 // we need to make sure the each fields offset same in different
162 UINT32 InitialApicId
;
170 // AP reset code information including code address and size,
171 // this structure will be shared be C code and assembly code.
172 // It is natural aligned by design.
175 UINT8
*RendezvousFunnelAddress
;
176 UINTN ModeEntryOffset
;
177 UINTN RendezvousFunnelSize
;
178 UINT8
*RelocateApLoopFuncAddress
;
179 UINTN RelocateApLoopFuncSize
;
180 UINTN ModeTransitionOffset
;
181 UINTN SwitchToRealSize
;
182 UINTN SwitchToRealOffset
;
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
;
225 } MP_CPU_EXCHANGE_INFO
;
230 // CPU MP Data save in memory
232 struct _CPU_MP_DATA
{
237 // The above fields data will be passed from PEI to DXE
238 // Please make sure the fields offset same in the different
243 UINTN CpuApStackSize
;
244 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
246 UINTN WakeupBufferHigh
;
248 UINTN BackupBufferSize
;
250 volatile UINT32 FinishedCount
;
252 BOOLEAN SingleThread
;
253 EFI_AP_PROCEDURE Procedure
;
260 UINTN
**FailedCpuList
;
262 AP_INIT_STATE InitFlag
;
263 BOOLEAN SwitchBspFlag
;
265 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
266 CPU_EXCHANGE_ROLE_INFO APInfo
;
267 MTRR_SETTINGS MtrrTable
;
269 UINT8 ApTargetCState
;
270 UINT16 PmCodeSegment
;
271 UINT16 Pm16CodeSegment
;
272 CPU_AP_DATA
*CpuData
;
273 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
275 UINT32 CurrentTimerCount
;
278 BOOLEAN PeriodicMode
;
279 BOOLEAN TimerInterruptState
;
280 UINT64 MicrocodePatchAddress
;
281 UINT64 MicrocodePatchRegionSize
;
284 // Whether need to use Init-Sipi-Sipi to wake up the APs.
285 // Two cases need to set this value to TRUE. One is in HLT
286 // loop mode, the other is resume from S3 which loop mode
287 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
290 BOOLEAN WakeUpByInitSipiSipi
;
292 BOOLEAN SevEsIsEnabled
;
294 UINTN SevEsAPResetStackStart
;
295 CPU_MP_DATA
*NewCpuMpData
;
300 #define AP_SAFE_STACK_SIZE 128
301 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE
314 Assembly code to move an AP from long mode to real mode.
316 Move an AP from long mode to real mode in preparation to invoking
317 the reset vector. This is used for SEV-ES guests where a hypervisor
318 is not allowed to set the CS and RIP to point to the reset vector.
320 @param[in] BufferStart The reset vector target.
321 @param[in] Code16 16-bit protected mode code segment value.
322 @param[in] Code32 32-bit protected mode code segment value.
323 @param[in] StackStart The start of a stack to be used for transitioning
324 from long mode to real mode.
329 IN UINTN BufferStart
,
335 extern EFI_GUID mCpuInitMpLibHobGuid
;
338 Assembly code to place AP into safe loop mode.
340 Place AP into targeted C-State if MONITOR is supported, otherwise
341 place AP into hlt state.
342 Place AP in protected mode if the current is long mode. Due to AP maybe
343 wakeup by some hardware event. It could avoid accessing page table that
344 may not available during booting to OS.
346 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
347 FALSE indicates MONITOR is not supported.
348 @param[in] ApTargetCState Target C-State value.
349 @param[in] PmCodeSegment Protected mode code segment value.
353 (EFIAPI
*ASM_RELOCATE_AP_LOOP
)(
354 IN BOOLEAN MwaitSupport
,
355 IN UINTN ApTargetCState
,
356 IN UINTN PmCodeSegment
,
357 IN UINTN TopOfApStack
,
358 IN UINTN NumberToFinish
,
359 IN UINTN Pm16CodeSegment
,
360 IN UINTN SevEsAPJumpTable
,
361 IN UINTN WakeupBuffer
365 Assembly code to get starting address and size of the rendezvous entry for APs.
366 Information for fixing a jump instruction in the code is also returned.
368 @param[out] AddressMap Output buffer for address map information.
373 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
377 This function is called by both the BSP and the AP which is to become the BSP to
378 Exchange execution context including stack between them. After return from this
379 function, the BSP becomes AP and the AP becomes the BSP.
381 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
382 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
388 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
389 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
393 Get the pointer to CPU MP Data structure.
395 @return The pointer to CPU MP Data structure.
403 Save the pointer to CPU MP Data structure.
405 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
409 IN CPU_MP_DATA
*CpuMpData
413 Get available system memory below 1MB by specified size.
415 @param[in] WakeupBufferSize Wakeup buffer size required
417 @retval other Return wakeup buffer address below 1MB.
418 @retval -1 Cannot find free memory below 1MB.
422 IN UINTN WakeupBufferSize
426 Get available EfiBootServicesCode memory below 4GB by specified size.
428 This buffer is required to safely transfer AP from real address mode to
429 protected mode or long mode, due to the fact that the buffer returned by
430 GetWakeupBuffer() may be marked as non-executable.
432 @param[in] BufferSize Wakeup transition buffer size.
434 @retval other Return wakeup transition buffer address below 4GB.
435 @retval 0 Cannot find free memory below 4GB.
438 GetModeTransitionBuffer (
443 Return the address of the SEV-ES AP jump table.
445 This buffer is required in order for an SEV-ES guest to transition from
448 @return Return SEV-ES AP jump table buffer
456 This function will be called by BSP to wakeup AP.
458 @param[in] CpuMpData Pointer to CPU MP Data
459 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
460 FALSE: Send IPI to AP by ApicId
461 @param[in] ProcessorNumber The handle number of specified processor
462 @param[in] Procedure The function to be invoked by AP
463 @param[in] ProcedureArgument The argument to be passed into AP function
464 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
468 IN CPU_MP_DATA
*CpuMpData
,
469 IN BOOLEAN Broadcast
,
470 IN UINTN ProcessorNumber
,
471 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
472 IN VOID
*ProcedureArgument OPTIONAL
,
473 IN BOOLEAN WakeUpDisabledAps OPTIONAL
477 Initialize global data for MP support.
479 @param[in] CpuMpData The pointer to CPU MP Data structure.
483 IN CPU_MP_DATA
*CpuMpData
487 Worker function to execute a caller provided function on all enabled APs.
489 @param[in] Procedure A pointer to the function to be run on
490 enabled APs of the system.
491 @param[in] SingleThread If TRUE, then all the enabled APs execute
492 the function specified by Procedure one by
493 one, in ascending order of processor handle
494 number. If FALSE, then all the enabled APs
495 execute the function specified by Procedure
497 @param[in] ExcludeBsp Whether let BSP also trig this task.
498 @param[in] WaitEvent The event created by the caller with CreateEvent()
500 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
501 APs to return from Procedure, either for
502 blocking or non-blocking mode.
503 @param[in] ProcedureArgument The parameter passed into Procedure for
505 @param[out] FailedCpuList If all APs finish successfully, then its
506 content is set to NULL. If not all APs
507 finish before timeout expires, then its
508 content is set to address of the buffer
509 holding handle numbers of the failed APs.
511 @retval EFI_SUCCESS In blocking mode, all APs have finished before
513 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
515 @retval others Failed to Startup all APs.
519 StartupAllCPUsWorker (
520 IN EFI_AP_PROCEDURE Procedure
,
521 IN BOOLEAN SingleThread
,
522 IN BOOLEAN ExcludeBsp
,
523 IN EFI_EVENT WaitEvent OPTIONAL
,
524 IN UINTN TimeoutInMicroseconds
,
525 IN VOID
*ProcedureArgument OPTIONAL
,
526 OUT UINTN
**FailedCpuList OPTIONAL
530 Worker function to let the caller get one enabled AP to execute a caller-provided
533 @param[in] Procedure A pointer to the function to be run on
534 enabled APs of the system.
535 @param[in] ProcessorNumber The handle number of the AP.
536 @param[in] WaitEvent The event created by the caller with CreateEvent()
538 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
539 APs to return from Procedure, either for
540 blocking or non-blocking mode.
541 @param[in] ProcedureArgument The parameter passed into Procedure for
543 @param[out] Finished If AP returns from Procedure before the
544 timeout expires, its content is set to TRUE.
545 Otherwise, the value is set to FALSE.
547 @retval EFI_SUCCESS In blocking mode, specified AP finished before
549 @retval others Failed to Startup AP.
553 StartupThisAPWorker (
554 IN EFI_AP_PROCEDURE Procedure
,
555 IN UINTN ProcessorNumber
,
556 IN EFI_EVENT WaitEvent OPTIONAL
,
557 IN UINTN TimeoutInMicroseconds
,
558 IN VOID
*ProcedureArgument OPTIONAL
,
559 OUT BOOLEAN
*Finished OPTIONAL
563 Worker function to switch the requested AP to be the BSP from that point onward.
565 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
566 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
567 enabled AP. Otherwise, it will be disabled.
569 @retval EFI_SUCCESS BSP successfully switched.
570 @retval others Failed to switch BSP.
575 IN UINTN ProcessorNumber
,
576 IN BOOLEAN EnableOldBSP
580 Worker function to let the caller enable or disable an AP from this point onward.
581 This service may only be called from the BSP.
583 @param[in] ProcessorNumber The handle number of AP.
584 @param[in] EnableAP Specifies the new state for the processor for
585 enabled, FALSE for disabled.
586 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
587 the new health status of the AP.
589 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
590 @retval others Failed to Enable/Disable AP.
594 EnableDisableApWorker (
595 IN UINTN ProcessorNumber
,
597 IN UINT32
*HealthFlag OPTIONAL
601 Get pointer to CPU MP Data structure from GUIDed HOB.
603 @return The pointer to CPU MP Data structure.
606 GetCpuMpDataFromGuidedHob (
610 /** Checks status of specified AP.
612 This function checks whether the specified AP has finished the task assigned
613 by StartupThisAP(), and whether timeout expires.
615 @param[in] ProcessorNumber The handle number of processor.
617 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
618 @retval EFI_TIMEOUT The timeout expires.
619 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
623 IN UINTN ProcessorNumber
627 Checks status of all APs.
629 This function checks whether all APs have finished task assigned by StartupAllAPs(),
630 and whether timeout expires.
632 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
633 @retval EFI_TIMEOUT The timeout expires.
634 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
642 Checks APs status and updates APs status if needed.
646 CheckAndUpdateApsStatus (
651 Detect whether specified processor can find matching microcode patch and load it.
653 @param[in] CpuMpData The pointer to CPU MP Data structure.
654 @param[in] ProcessorNumber The handle number of the processor. The range is
655 from 0 to the total number of logical processors
660 IN CPU_MP_DATA
*CpuMpData
,
661 IN UINTN ProcessorNumber
665 Shadow the required microcode patches data into memory.
667 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
670 ShadowMicrocodeUpdatePatch (
671 IN OUT CPU_MP_DATA
*CpuMpData
675 Get the cached microcode patch base address and size from the microcode patch
676 information cache HOB.
678 @param[out] Address Base address of the microcode patches data.
679 It will be updated if the microcode patch
680 information cache HOB is found.
681 @param[out] RegionSize Size of the microcode patches data.
682 It will be updated if the microcode patch
683 information cache HOB is found.
685 @retval TRUE The microcode patch information cache HOB is found.
686 @retval FALSE The microcode patch information cache HOB is not found.
690 GetMicrocodePatchInfoFromHob (
696 Detect whether Mwait-monitor feature is supported.
698 @retval TRUE Mwait-monitor feature is supported.
699 @retval FALSE Mwait-monitor feature is not supported.
707 Enable Debug Agent to support source debugging on AP function.
716 Find the current Processor number by APIC ID.
718 @param[in] CpuMpData Pointer to PEI CPU MP Data
719 @param[out] ProcessorNumber Return the pocessor number found
721 @retval EFI_SUCCESS ProcessorNumber is found and returned.
722 @retval EFI_NOT_FOUND ProcessorNumber is not found.
726 IN CPU_MP_DATA
*CpuMpData
,
727 OUT UINTN
*ProcessorNumber
731 This funtion will try to invoke platform specific microcode shadow logic to
732 relocate microcode update patches into memory.
734 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
736 @retval EFI_SUCCESS Shadow microcode success.
737 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
738 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
742 PlatformShadowMicrocode (
743 IN OUT CPU_MP_DATA
*CpuMpData
747 Allocate the SEV-ES AP jump table buffer.
749 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
752 AllocateSevEsAPMemory (
753 IN OUT CPU_MP_DATA
*CpuMpData
757 Program the SEV-ES AP jump table buffer.
759 @param[in] SipiVector The SIPI vector used for the AP Reset
767 The function puts the AP in halt loop.
769 @param[in] CpuMpData The pointer to CPU MP Data structure.
773 CPU_MP_DATA
*CpuMpData