2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2020, 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>
36 #include <Guid/MicrocodePatchHob.h>
38 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
40 #define CPU_INIT_MP_LIB_HOB_GUID \
42 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
46 // The MP data for switch BSP
48 #define CPU_SWITCH_STATE_IDLE 0
49 #define CPU_SWITCH_STATE_STORED 1
50 #define CPU_SWITCH_STATE_LOADED 2
53 // Default maximum number of entries to store the microcode patches information
55 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
58 // Data structure for microcode patch information
63 } MICROCODE_PATCH_INFO
;
66 // CPU exchange information for switch BSP
69 UINT8 State
; // offset 0
70 UINTN StackPointer
; // offset 4 / 8
71 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
72 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
73 } CPU_EXCHANGE_ROLE_INFO
;
76 // AP loop state when APs are in idle state
77 // It's value is the same with PcdCpuApLoopMode
86 // AP initialization state during APs wakeup
97 // The state transitions for an AP when it process a procedure are:
98 // Idle ----> Ready ----> Busy ----> Idle
110 // CPU volatile registers around INIT-SIPI-SIPI
122 IA32_DESCRIPTOR Gdtr
;
123 IA32_DESCRIPTOR Idtr
;
125 } CPU_VOLATILE_REGISTERS
;
132 volatile UINT32
*StartupApSignal
;
133 volatile UINTN ApFunction
;
134 volatile UINTN ApFunctionArgument
;
136 volatile CPU_STATE State
;
137 CPU_VOLATILE_REGISTERS VolatileRegisters
;
144 UINT32 ProcessorSignature
;
146 UINT64 MicrocodeEntryAddr
;
150 // Basic CPU information saved in Guided HOB.
151 // Because the contents will be shard between PEI and DXE,
152 // we need to make sure the each fields offset same in different
157 UINT32 InitialApicId
;
165 // AP reset code information including code address and size,
166 // this structure will be shared be C code and assembly code.
167 // It is natural aligned by design.
170 UINT8
*RendezvousFunnelAddress
;
171 UINTN ModeEntryOffset
;
172 UINTN RendezvousFunnelSize
;
173 UINT8
*RelocateApLoopFuncAddress
;
174 UINTN RelocateApLoopFuncSize
;
175 UINTN ModeTransitionOffset
;
176 UINTN SwitchToRealSize
;
177 UINTN SwitchToRealOffset
;
178 UINTN SwitchToRealNoNxOffset
;
179 UINTN SwitchToRealPM16ModeOffset
;
180 UINTN SwitchToRealPM16ModeSize
;
181 } MP_ASSEMBLY_ADDRESS_MAP
;
183 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
188 // MP CPU exchange information for AP reset code
189 // This structure is required to be packed because fixed field offsets
190 // into this structure are used in assembly code in this module
197 IA32_DESCRIPTOR GdtrProfile
;
198 IA32_DESCRIPTOR IdtrProfile
;
204 UINTN EnableExecuteDisable
;
207 CPU_INFO_IN_HOB
*CpuInfo
;
208 UINTN NumApsExecuting
;
209 CPU_MP_DATA
*CpuMpData
;
210 UINTN InitializeFloatingPointUnitsAddress
;
211 UINT32 ModeTransitionMemory
;
212 UINT16 ModeTransitionSegment
;
213 UINT32 ModeHighMemory
;
214 UINT16 ModeHighSegment
;
216 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
218 BOOLEAN Enable5LevelPaging
;
219 BOOLEAN SevEsIsEnabled
;
221 } MP_CPU_EXCHANGE_INFO
;
226 // CPU MP Data save in memory
228 struct _CPU_MP_DATA
{
233 // The above fields data will be passed from PEI to DXE
234 // Please make sure the fields offset same in the different
239 UINTN CpuApStackSize
;
240 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
242 UINTN WakeupBufferHigh
;
244 UINTN BackupBufferSize
;
246 volatile UINT32 FinishedCount
;
248 BOOLEAN SingleThread
;
249 EFI_AP_PROCEDURE Procedure
;
256 UINTN
**FailedCpuList
;
258 AP_INIT_STATE InitFlag
;
259 BOOLEAN SwitchBspFlag
;
261 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
262 CPU_EXCHANGE_ROLE_INFO APInfo
;
263 MTRR_SETTINGS MtrrTable
;
265 UINT8 ApTargetCState
;
266 UINT16 PmCodeSegment
;
267 UINT16 Pm16CodeSegment
;
268 CPU_AP_DATA
*CpuData
;
269 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
271 UINT32 CurrentTimerCount
;
274 BOOLEAN PeriodicMode
;
275 BOOLEAN TimerInterruptState
;
276 UINT64 MicrocodePatchAddress
;
277 UINT64 MicrocodePatchRegionSize
;
280 // Whether need to use Init-Sipi-Sipi to wake up the APs.
281 // Two cases need to set this value to TRUE. One is in HLT
282 // loop mode, the other is resume from S3 which loop mode
283 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
286 BOOLEAN WakeUpByInitSipiSipi
;
288 BOOLEAN SevEsIsEnabled
;
290 UINTN SevEsAPResetStackStart
;
291 CPU_MP_DATA
*NewCpuMpData
;
296 #define AP_RESET_STACK_SIZE 64
309 Assembly code to move an AP from long mode to real mode.
311 Move an AP from long mode to real mode in preparation to invoking
312 the reset vector. This is used for SEV-ES guests where a hypervisor
313 is not allowed to set the CS and RIP to point to the reset vector.
315 @param[in] BufferStart The reset vector target.
316 @param[in] Code16 16-bit protected mode code segment value.
317 @param[in] Code32 32-bit protected mode code segment value.
318 @param[in] StackStart The start of a stack to be used for transitioning
319 from long mode to real mode.
324 IN UINTN BufferStart
,
330 extern EFI_GUID mCpuInitMpLibHobGuid
;
333 Assembly code to place AP into safe loop mode.
335 Place AP into targeted C-State if MONITOR is supported, otherwise
336 place AP into hlt state.
337 Place AP in protected mode if the current is long mode. Due to AP maybe
338 wakeup by some hardware event. It could avoid accessing page table that
339 may not available during booting to OS.
341 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
342 FALSE indicates MONITOR is not supported.
343 @param[in] ApTargetCState Target C-State value.
344 @param[in] PmCodeSegment Protected mode code segment value.
348 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
349 IN BOOLEAN MwaitSupport
,
350 IN UINTN ApTargetCState
,
351 IN UINTN PmCodeSegment
,
352 IN UINTN TopOfApStack
,
353 IN UINTN NumberToFinish
357 Assembly code to get starting address and size of the rendezvous entry for APs.
358 Information for fixing a jump instruction in the code is also returned.
360 @param[out] AddressMap Output buffer for address map information.
365 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
369 This function is called by both the BSP and the AP which is to become the BSP to
370 Exchange execution context including stack between them. After return from this
371 function, the BSP becomes AP and the AP becomes the BSP.
373 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
374 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
380 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
381 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
385 Get the pointer to CPU MP Data structure.
387 @return The pointer to CPU MP Data structure.
395 Save the pointer to CPU MP Data structure.
397 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
401 IN CPU_MP_DATA
*CpuMpData
406 Get available system memory below 1MB by specified size.
408 @param[in] WakeupBufferSize Wakeup buffer size required
410 @retval other Return wakeup buffer address below 1MB.
411 @retval -1 Cannot find free memory below 1MB.
415 IN UINTN WakeupBufferSize
419 Get available EfiBootServicesCode memory below 4GB by specified size.
421 This buffer is required to safely transfer AP from real address mode to
422 protected mode or long mode, due to the fact that the buffer returned by
423 GetWakeupBuffer() may be marked as non-executable.
425 @param[in] BufferSize Wakeup transition buffer size.
427 @retval other Return wakeup transition buffer address below 4GB.
428 @retval 0 Cannot find free memory below 4GB.
431 GetModeTransitionBuffer (
436 Return the address of the SEV-ES AP jump table.
438 This buffer is required in order for an SEV-ES guest to transition from
441 @return Return SEV-ES AP jump table buffer
449 This function will be called by BSP to wakeup AP.
451 @param[in] CpuMpData Pointer to CPU MP Data
452 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
453 FALSE: Send IPI to AP by ApicId
454 @param[in] ProcessorNumber The handle number of specified processor
455 @param[in] Procedure The function to be invoked by AP
456 @param[in] ProcedureArgument The argument to be passed into AP function
457 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
461 IN CPU_MP_DATA
*CpuMpData
,
462 IN BOOLEAN Broadcast
,
463 IN UINTN ProcessorNumber
,
464 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
465 IN VOID
*ProcedureArgument
, OPTIONAL
466 IN BOOLEAN WakeUpDisabledAps OPTIONAL
470 Initialize global data for MP support.
472 @param[in] CpuMpData The pointer to CPU MP Data structure.
476 IN CPU_MP_DATA
*CpuMpData
480 Worker function to execute a caller provided function on all enabled APs.
482 @param[in] Procedure A pointer to the function to be run on
483 enabled APs of the system.
484 @param[in] SingleThread If TRUE, then all the enabled APs execute
485 the function specified by Procedure one by
486 one, in ascending order of processor handle
487 number. If FALSE, then all the enabled APs
488 execute the function specified by Procedure
490 @param[in] ExcludeBsp Whether let BSP also trig this task.
491 @param[in] WaitEvent The event created by the caller with CreateEvent()
493 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
494 APs to return from Procedure, either for
495 blocking or non-blocking mode.
496 @param[in] ProcedureArgument The parameter passed into Procedure for
498 @param[out] FailedCpuList If all APs finish successfully, then its
499 content is set to NULL. If not all APs
500 finish before timeout expires, then its
501 content is set to address of the buffer
502 holding handle numbers of the failed APs.
504 @retval EFI_SUCCESS In blocking mode, all APs have finished before
506 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
508 @retval others Failed to Startup all APs.
512 StartupAllCPUsWorker (
513 IN EFI_AP_PROCEDURE Procedure
,
514 IN BOOLEAN SingleThread
,
515 IN BOOLEAN ExcludeBsp
,
516 IN EFI_EVENT WaitEvent OPTIONAL
,
517 IN UINTN TimeoutInMicroseconds
,
518 IN VOID
*ProcedureArgument OPTIONAL
,
519 OUT UINTN
**FailedCpuList OPTIONAL
523 Worker function to let the caller get one enabled AP to execute a caller-provided
526 @param[in] Procedure A pointer to the function to be run on
527 enabled APs of the system.
528 @param[in] ProcessorNumber The handle number of the AP.
529 @param[in] WaitEvent The event created by the caller with CreateEvent()
531 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
532 APs to return from Procedure, either for
533 blocking or non-blocking mode.
534 @param[in] ProcedureArgument The parameter passed into Procedure for
536 @param[out] Finished If AP returns from Procedure before the
537 timeout expires, its content is set to TRUE.
538 Otherwise, the value is set to FALSE.
540 @retval EFI_SUCCESS In blocking mode, specified AP finished before
542 @retval others Failed to Startup AP.
546 StartupThisAPWorker (
547 IN EFI_AP_PROCEDURE Procedure
,
548 IN UINTN ProcessorNumber
,
549 IN EFI_EVENT WaitEvent OPTIONAL
,
550 IN UINTN TimeoutInMicroseconds
,
551 IN VOID
*ProcedureArgument OPTIONAL
,
552 OUT BOOLEAN
*Finished OPTIONAL
556 Worker function to switch the requested AP to be the BSP from that point onward.
558 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
559 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
560 enabled AP. Otherwise, it will be disabled.
562 @retval EFI_SUCCESS BSP successfully switched.
563 @retval others Failed to switch BSP.
568 IN UINTN ProcessorNumber
,
569 IN BOOLEAN EnableOldBSP
573 Worker function to let the caller enable or disable an AP from this point onward.
574 This service may only be called from the BSP.
576 @param[in] ProcessorNumber The handle number of AP.
577 @param[in] EnableAP Specifies the new state for the processor for
578 enabled, FALSE for disabled.
579 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
580 the new health status of the AP.
582 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
583 @retval others Failed to Enable/Disable AP.
587 EnableDisableApWorker (
588 IN UINTN ProcessorNumber
,
590 IN UINT32
*HealthFlag OPTIONAL
594 Get pointer to CPU MP Data structure from GUIDed HOB.
596 @return The pointer to CPU MP Data structure.
599 GetCpuMpDataFromGuidedHob (
603 /** Checks status of specified AP.
605 This function checks whether the specified AP has finished the task assigned
606 by StartupThisAP(), and whether timeout expires.
608 @param[in] ProcessorNumber The handle number of processor.
610 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
611 @retval EFI_TIMEOUT The timeout expires.
612 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
616 IN UINTN ProcessorNumber
620 Checks status of all APs.
622 This function checks whether all APs have finished task assigned by StartupAllAPs(),
623 and whether timeout expires.
625 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
626 @retval EFI_TIMEOUT The timeout expires.
627 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
635 Checks APs status and updates APs status if needed.
639 CheckAndUpdateApsStatus (
644 Detect whether specified processor can find matching microcode patch and load it.
646 @param[in] CpuMpData The pointer to CPU MP Data structure.
647 @param[in] ProcessorNumber The handle number of the processor. The range is
648 from 0 to the total number of logical processors
653 IN CPU_MP_DATA
*CpuMpData
,
654 IN UINTN ProcessorNumber
658 Shadow the required microcode patches data into memory.
660 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
663 ShadowMicrocodeUpdatePatch (
664 IN OUT CPU_MP_DATA
*CpuMpData
668 Get the cached microcode patch base address and size from the microcode patch
669 information cache HOB.
671 @param[out] Address Base address of the microcode patches data.
672 It will be updated if the microcode patch
673 information cache HOB is found.
674 @param[out] RegionSize Size of the microcode patches data.
675 It will be updated if the microcode patch
676 information cache HOB is found.
678 @retval TRUE The microcode patch information cache HOB is found.
679 @retval FALSE The microcode patch information cache HOB is not found.
683 GetMicrocodePatchInfoFromHob (
689 Detect whether Mwait-monitor feature is supported.
691 @retval TRUE Mwait-monitor feature is supported.
692 @retval FALSE Mwait-monitor feature is not supported.
700 Enable Debug Agent to support source debugging on AP function.
709 Find the current Processor number by APIC ID.
711 @param[in] CpuMpData Pointer to PEI CPU MP Data
712 @param[out] ProcessorNumber Return the pocessor number found
714 @retval EFI_SUCCESS ProcessorNumber is found and returned.
715 @retval EFI_NOT_FOUND ProcessorNumber is not found.
719 IN CPU_MP_DATA
*CpuMpData
,
720 OUT UINTN
*ProcessorNumber
724 This funtion will try to invoke platform specific microcode shadow logic to
725 relocate microcode update patches into memory.
727 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
729 @retval EFI_SUCCESS Shadow microcode success.
730 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
731 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
735 PlatformShadowMicrocode (
736 IN OUT CPU_MP_DATA
*CpuMpData