2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Register/Intel/Cpuid.h>
15 #include <Register/Intel/Msr.h>
16 #include <Register/Intel/LocalApic.h>
17 #include <Register/Intel/Microcode.h>
19 #include <Library/MpInitLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/LocalApicLib.h>
25 #include <Library/CpuLib.h>
26 #include <Library/UefiCpuLib.h>
27 #include <Library/TimerLib.h>
28 #include <Library/SynchronizationLib.h>
29 #include <Library/MtrrLib.h>
30 #include <Library/HobLib.h>
32 #include <Guid/MicrocodePatchHob.h>
34 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
36 #define CPU_INIT_MP_LIB_HOB_GUID \
38 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
42 // The MP data for switch BSP
44 #define CPU_SWITCH_STATE_IDLE 0
45 #define CPU_SWITCH_STATE_STORED 1
46 #define CPU_SWITCH_STATE_LOADED 2
49 // Default maximum number of entries to store the microcode patches information
51 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
54 // Data structure for microcode patch information
59 } MICROCODE_PATCH_INFO
;
62 // CPU exchange information for switch BSP
65 UINT8 State
; // offset 0
66 UINTN StackPointer
; // offset 4 / 8
67 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
68 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
69 } CPU_EXCHANGE_ROLE_INFO
;
72 // AP loop state when APs are in idle state
73 // It's value is the same with PcdCpuApLoopMode
82 // AP initialization state during APs wakeup
93 // The state transitions for an AP when it process a procedure are:
94 // Idle ----> Ready ----> Busy ----> Idle
106 // CPU volatile registers around INIT-SIPI-SIPI
118 IA32_DESCRIPTOR Gdtr
;
119 IA32_DESCRIPTOR Idtr
;
121 } CPU_VOLATILE_REGISTERS
;
128 volatile UINT32
*StartupApSignal
;
129 volatile UINTN ApFunction
;
130 volatile UINTN ApFunctionArgument
;
132 volatile CPU_STATE State
;
133 CPU_VOLATILE_REGISTERS VolatileRegisters
;
140 UINT32 ProcessorSignature
;
142 UINT64 MicrocodeEntryAddr
;
146 // Basic CPU information saved in Guided HOB.
147 // Because the contents will be shard between PEI and DXE,
148 // we need to make sure the each fields offset same in different
153 UINT32 InitialApicId
;
161 // AP reset code information including code address and size,
162 // this structure will be shared be C code and assembly code.
163 // It is natural aligned by design.
166 UINT8
*RendezvousFunnelAddress
;
167 UINTN ModeEntryOffset
;
168 UINTN RendezvousFunnelSize
;
169 UINT8
*RelocateApLoopFuncAddress
;
170 UINTN RelocateApLoopFuncSize
;
171 UINTN ModeTransitionOffset
;
172 } MP_ASSEMBLY_ADDRESS_MAP
;
174 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
179 // MP CPU exchange information for AP reset code
180 // This structure is required to be packed because fixed field offsets
181 // into this structure are used in assembly code in this module
188 IA32_DESCRIPTOR GdtrProfile
;
189 IA32_DESCRIPTOR IdtrProfile
;
195 UINTN EnableExecuteDisable
;
198 CPU_INFO_IN_HOB
*CpuInfo
;
199 UINTN NumApsExecuting
;
200 CPU_MP_DATA
*CpuMpData
;
201 UINTN InitializeFloatingPointUnitsAddress
;
202 UINT32 ModeTransitionMemory
;
203 UINT16 ModeTransitionSegment
;
204 UINT32 ModeHighMemory
;
205 UINT16 ModeHighSegment
;
207 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
209 BOOLEAN Enable5LevelPaging
;
210 } MP_CPU_EXCHANGE_INFO
;
215 // CPU MP Data save in memory
217 struct _CPU_MP_DATA
{
222 // The above fields data will be passed from PEI to DXE
223 // Please make sure the fields offset same in the different
228 UINTN CpuApStackSize
;
229 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
231 UINTN WakeupBufferHigh
;
233 UINTN BackupBufferSize
;
235 volatile UINT32 FinishedCount
;
237 BOOLEAN SingleThread
;
238 EFI_AP_PROCEDURE Procedure
;
245 UINTN
**FailedCpuList
;
247 AP_INIT_STATE InitFlag
;
248 BOOLEAN SwitchBspFlag
;
250 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
251 CPU_EXCHANGE_ROLE_INFO APInfo
;
252 MTRR_SETTINGS MtrrTable
;
254 UINT8 ApTargetCState
;
255 UINT16 PmCodeSegment
;
256 CPU_AP_DATA
*CpuData
;
257 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
259 UINT32 CurrentTimerCount
;
262 BOOLEAN PeriodicMode
;
263 BOOLEAN TimerInterruptState
;
264 UINT64 MicrocodePatchAddress
;
265 UINT64 MicrocodePatchRegionSize
;
268 // Whether need to use Init-Sipi-Sipi to wake up the APs.
269 // Two cases need to set this value to TRUE. One is in HLT
270 // loop mode, the other is resume from S3 which loop mode
271 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
274 BOOLEAN WakeUpByInitSipiSipi
;
277 extern EFI_GUID mCpuInitMpLibHobGuid
;
280 Assembly code to place AP into safe loop mode.
282 Place AP into targeted C-State if MONITOR is supported, otherwise
283 place AP into hlt state.
284 Place AP in protected mode if the current is long mode. Due to AP maybe
285 wakeup by some hardware event. It could avoid accessing page table that
286 may not available during booting to OS.
288 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
289 FALSE indicates MONITOR is not supported.
290 @param[in] ApTargetCState Target C-State value.
291 @param[in] PmCodeSegment Protected mode code segment value.
295 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
296 IN BOOLEAN MwaitSupport
,
297 IN UINTN ApTargetCState
,
298 IN UINTN PmCodeSegment
,
299 IN UINTN TopOfApStack
,
300 IN UINTN NumberToFinish
304 Assembly code to get starting address and size of the rendezvous entry for APs.
305 Information for fixing a jump instruction in the code is also returned.
307 @param[out] AddressMap Output buffer for address map information.
312 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
316 This function is called by both the BSP and the AP which is to become the BSP to
317 Exchange execution context including stack between them. After return from this
318 function, the BSP becomes AP and the AP becomes the BSP.
320 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
321 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
327 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
328 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
332 Get the pointer to CPU MP Data structure.
334 @return The pointer to CPU MP Data structure.
342 Save the pointer to CPU MP Data structure.
344 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
348 IN CPU_MP_DATA
*CpuMpData
353 Get available system memory below 1MB by specified size.
355 @param[in] WakeupBufferSize Wakeup buffer size required
357 @retval other Return wakeup buffer address below 1MB.
358 @retval -1 Cannot find free memory below 1MB.
362 IN UINTN WakeupBufferSize
366 Get available EfiBootServicesCode memory below 4GB by specified size.
368 This buffer is required to safely transfer AP from real address mode to
369 protected mode or long mode, due to the fact that the buffer returned by
370 GetWakeupBuffer() may be marked as non-executable.
372 @param[in] BufferSize Wakeup transition buffer size.
374 @retval other Return wakeup transition buffer address below 4GB.
375 @retval 0 Cannot find free memory below 4GB.
378 GetModeTransitionBuffer (
383 This function will be called by BSP to wakeup AP.
385 @param[in] CpuMpData Pointer to CPU MP Data
386 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
387 FALSE: Send IPI to AP by ApicId
388 @param[in] ProcessorNumber The handle number of specified processor
389 @param[in] Procedure The function to be invoked by AP
390 @param[in] ProcedureArgument The argument to be passed into AP function
391 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
395 IN CPU_MP_DATA
*CpuMpData
,
396 IN BOOLEAN Broadcast
,
397 IN UINTN ProcessorNumber
,
398 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
399 IN VOID
*ProcedureArgument
, OPTIONAL
400 IN BOOLEAN WakeUpDisabledAps OPTIONAL
404 Initialize global data for MP support.
406 @param[in] CpuMpData The pointer to CPU MP Data structure.
410 IN CPU_MP_DATA
*CpuMpData
414 Worker function to execute a caller provided function on all enabled APs.
416 @param[in] Procedure A pointer to the function to be run on
417 enabled APs of the system.
418 @param[in] SingleThread If TRUE, then all the enabled APs execute
419 the function specified by Procedure one by
420 one, in ascending order of processor handle
421 number. If FALSE, then all the enabled APs
422 execute the function specified by Procedure
424 @param[in] ExcludeBsp Whether let BSP also trig this task.
425 @param[in] WaitEvent The event created by the caller with CreateEvent()
427 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
428 APs to return from Procedure, either for
429 blocking or non-blocking mode.
430 @param[in] ProcedureArgument The parameter passed into Procedure for
432 @param[out] FailedCpuList If all APs finish successfully, then its
433 content is set to NULL. If not all APs
434 finish before timeout expires, then its
435 content is set to address of the buffer
436 holding handle numbers of the failed APs.
438 @retval EFI_SUCCESS In blocking mode, all APs have finished before
440 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
442 @retval others Failed to Startup all APs.
446 StartupAllCPUsWorker (
447 IN EFI_AP_PROCEDURE Procedure
,
448 IN BOOLEAN SingleThread
,
449 IN BOOLEAN ExcludeBsp
,
450 IN EFI_EVENT WaitEvent OPTIONAL
,
451 IN UINTN TimeoutInMicroseconds
,
452 IN VOID
*ProcedureArgument OPTIONAL
,
453 OUT UINTN
**FailedCpuList OPTIONAL
457 Worker function to let the caller get one enabled AP to execute a caller-provided
460 @param[in] Procedure A pointer to the function to be run on
461 enabled APs of the system.
462 @param[in] ProcessorNumber The handle number of the AP.
463 @param[in] WaitEvent The event created by the caller with CreateEvent()
465 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
466 APs to return from Procedure, either for
467 blocking or non-blocking mode.
468 @param[in] ProcedureArgument The parameter passed into Procedure for
470 @param[out] Finished If AP returns from Procedure before the
471 timeout expires, its content is set to TRUE.
472 Otherwise, the value is set to FALSE.
474 @retval EFI_SUCCESS In blocking mode, specified AP finished before
476 @retval others Failed to Startup AP.
480 StartupThisAPWorker (
481 IN EFI_AP_PROCEDURE Procedure
,
482 IN UINTN ProcessorNumber
,
483 IN EFI_EVENT WaitEvent OPTIONAL
,
484 IN UINTN TimeoutInMicroseconds
,
485 IN VOID
*ProcedureArgument OPTIONAL
,
486 OUT BOOLEAN
*Finished OPTIONAL
490 Worker function to switch the requested AP to be the BSP from that point onward.
492 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
493 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
494 enabled AP. Otherwise, it will be disabled.
496 @retval EFI_SUCCESS BSP successfully switched.
497 @retval others Failed to switch BSP.
502 IN UINTN ProcessorNumber
,
503 IN BOOLEAN EnableOldBSP
507 Worker function to let the caller enable or disable an AP from this point onward.
508 This service may only be called from the BSP.
510 @param[in] ProcessorNumber The handle number of AP.
511 @param[in] EnableAP Specifies the new state for the processor for
512 enabled, FALSE for disabled.
513 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
514 the new health status of the AP.
516 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
517 @retval others Failed to Enable/Disable AP.
521 EnableDisableApWorker (
522 IN UINTN ProcessorNumber
,
524 IN UINT32
*HealthFlag OPTIONAL
528 Get pointer to CPU MP Data structure from GUIDed HOB.
530 @return The pointer to CPU MP Data structure.
533 GetCpuMpDataFromGuidedHob (
537 /** Checks status of specified AP.
539 This function checks whether the specified AP has finished the task assigned
540 by StartupThisAP(), and whether timeout expires.
542 @param[in] ProcessorNumber The handle number of processor.
544 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
545 @retval EFI_TIMEOUT The timeout expires.
546 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
550 IN UINTN ProcessorNumber
554 Checks status of all APs.
556 This function checks whether all APs have finished task assigned by StartupAllAPs(),
557 and whether timeout expires.
559 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
560 @retval EFI_TIMEOUT The timeout expires.
561 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
569 Checks APs status and updates APs status if needed.
573 CheckAndUpdateApsStatus (
578 Detect whether specified processor can find matching microcode patch and load it.
580 @param[in] CpuMpData The pointer to CPU MP Data structure.
581 @param[in] ProcessorNumber The handle number of the processor. The range is
582 from 0 to the total number of logical processors
587 IN CPU_MP_DATA
*CpuMpData
,
588 IN UINTN ProcessorNumber
592 Shadow the required microcode patches data into memory.
594 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
597 ShadowMicrocodeUpdatePatch (
598 IN OUT CPU_MP_DATA
*CpuMpData
602 Get the cached microcode patch base address and size from the microcode patch
603 information cache HOB.
605 @param[out] Address Base address of the microcode patches data.
606 It will be updated if the microcode patch
607 information cache HOB is found.
608 @param[out] RegionSize Size of the microcode patches data.
609 It will be updated if the microcode patch
610 information cache HOB is found.
612 @retval TRUE The microcode patch information cache HOB is found.
613 @retval FALSE The microcode patch information cache HOB is not found.
617 GetMicrocodePatchInfoFromHob (
623 Detect whether Mwait-monitor feature is supported.
625 @retval TRUE Mwait-monitor feature is supported.
626 @retval FALSE Mwait-monitor feature is not supported.
634 Enable Debug Agent to support source debugging on AP function.
643 Find the current Processor number by APIC ID.
645 @param[in] CpuMpData Pointer to PEI CPU MP Data
646 @param[out] ProcessorNumber Return the pocessor number found
648 @retval EFI_SUCCESS ProcessorNumber is found and returned.
649 @retval EFI_NOT_FOUND ProcessorNumber is not found.
653 IN CPU_MP_DATA
*CpuMpData
,
654 OUT UINTN
*ProcessorNumber
658 This funtion will try to invoke platform specific microcode shadow logic to
659 relocate microcode update patches into memory.
661 @param[in] CpuMpData The pointer to CPU MP Data structure.
663 @retval EFI_SUCCESS Shadow microcode success.
664 @retval EFI_OUT_OF_RESOURCES No enough resource to complete the operation.
665 @retval EFI_UNSUPPORTED Can't find platform specific microcode shadow
669 PlatformShadowMicrocode (
670 IN OUT CPU_MP_DATA
*CpuMpData