2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2019, 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 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
34 #define CPU_INIT_MP_LIB_HOB_GUID \
36 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
40 // The MP data for switch BSP
42 #define CPU_SWITCH_STATE_IDLE 0
43 #define CPU_SWITCH_STATE_STORED 1
44 #define CPU_SWITCH_STATE_LOADED 2
47 // CPU exchange information for switch BSP
50 UINT8 State
; // offset 0
51 UINTN StackPointer
; // offset 4 / 8
52 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
53 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
54 } CPU_EXCHANGE_ROLE_INFO
;
57 // AP loop state when APs are in idle state
58 // It's value is the same with PcdCpuApLoopMode
67 // AP initialization state during APs wakeup
78 // The state transitions for an AP when it process a procedure are:
79 // Idle ----> Ready ----> Busy ----> Idle
91 // CPU volatile registers around INIT-SIPI-SIPI
103 IA32_DESCRIPTOR Gdtr
;
104 IA32_DESCRIPTOR Idtr
;
106 } CPU_VOLATILE_REGISTERS
;
113 volatile UINT32
*StartupApSignal
;
114 volatile UINTN ApFunction
;
115 volatile UINTN ApFunctionArgument
;
117 volatile CPU_STATE State
;
118 CPU_VOLATILE_REGISTERS VolatileRegisters
;
128 // Basic CPU information saved in Guided HOB.
129 // Because the contents will be shard between PEI and DXE,
130 // we need to make sure the each fields offset same in different
135 UINT32 InitialApicId
;
143 // AP reset code information including code address and size,
144 // this structure will be shared be C code and assembly code.
145 // It is natural aligned by design.
148 UINT8
*RendezvousFunnelAddress
;
149 UINTN ModeEntryOffset
;
150 UINTN RendezvousFunnelSize
;
151 UINT8
*RelocateApLoopFuncAddress
;
152 UINTN RelocateApLoopFuncSize
;
153 UINTN ModeTransitionOffset
;
154 } MP_ASSEMBLY_ADDRESS_MAP
;
156 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
161 // MP CPU exchange information for AP reset code
162 // This structure is required to be packed because fixed field offsets
163 // into this structure are used in assembly code in this module
170 IA32_DESCRIPTOR GdtrProfile
;
171 IA32_DESCRIPTOR IdtrProfile
;
177 UINTN EnableExecuteDisable
;
180 CPU_INFO_IN_HOB
*CpuInfo
;
181 UINTN NumApsExecuting
;
182 CPU_MP_DATA
*CpuMpData
;
183 UINTN InitializeFloatingPointUnitsAddress
;
184 UINT32 ModeTransitionMemory
;
185 UINT16 ModeTransitionSegment
;
186 UINT32 ModeHighMemory
;
187 UINT16 ModeHighSegment
;
189 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
191 BOOLEAN Enable5LevelPaging
;
192 } MP_CPU_EXCHANGE_INFO
;
197 // CPU MP Data save in memory
199 struct _CPU_MP_DATA
{
204 // The above fields data will be passed from PEI to DXE
205 // Please make sure the fields offset same in the different
210 UINTN CpuApStackSize
;
211 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
213 UINTN WakeupBufferHigh
;
215 UINTN BackupBufferSize
;
217 volatile UINT32 FinishedCount
;
219 BOOLEAN SingleThread
;
220 EFI_AP_PROCEDURE Procedure
;
227 UINTN
**FailedCpuList
;
229 AP_INIT_STATE InitFlag
;
230 BOOLEAN SwitchBspFlag
;
232 CPU_EXCHANGE_ROLE_INFO BSPInfo
;
233 CPU_EXCHANGE_ROLE_INFO APInfo
;
234 MTRR_SETTINGS MtrrTable
;
236 UINT8 ApTargetCState
;
237 UINT16 PmCodeSegment
;
238 CPU_AP_DATA
*CpuData
;
239 volatile MP_CPU_EXCHANGE_INFO
*MpCpuExchangeInfo
;
241 UINT32 CurrentTimerCount
;
244 BOOLEAN PeriodicMode
;
245 BOOLEAN TimerInterruptState
;
246 UINT64 MicrocodePatchAddress
;
247 UINT64 MicrocodePatchRegionSize
;
249 UINT32 ProcessorSignature
;
250 UINT32 ProcessorFlags
;
251 UINT64 MicrocodeDataAddress
;
252 UINT32 MicrocodeRevision
;
255 // Whether need to use Init-Sipi-Sipi to wake up the APs.
256 // Two cases need to set this value to TRUE. One is in HLT
257 // loop mode, the other is resume from S3 which loop mode
258 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
261 BOOLEAN WakeUpByInitSipiSipi
;
264 extern EFI_GUID mCpuInitMpLibHobGuid
;
267 Assembly code to place AP into safe loop mode.
269 Place AP into targeted C-State if MONITOR is supported, otherwise
270 place AP into hlt state.
271 Place AP in protected mode if the current is long mode. Due to AP maybe
272 wakeup by some hardware event. It could avoid accessing page table that
273 may not available during booting to OS.
275 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
276 FALSE indicates MONITOR is not supported.
277 @param[in] ApTargetCState Target C-State value.
278 @param[in] PmCodeSegment Protected mode code segment value.
282 (EFIAPI
* ASM_RELOCATE_AP_LOOP
) (
283 IN BOOLEAN MwaitSupport
,
284 IN UINTN ApTargetCState
,
285 IN UINTN PmCodeSegment
,
286 IN UINTN TopOfApStack
,
287 IN UINTN NumberToFinish
291 Assembly code to get starting address and size of the rendezvous entry for APs.
292 Information for fixing a jump instruction in the code is also returned.
294 @param[out] AddressMap Output buffer for address map information.
299 OUT MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
303 This function is called by both the BSP and the AP which is to become the BSP to
304 Exchange execution context including stack between them. After return from this
305 function, the BSP becomes AP and the AP becomes the BSP.
307 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
308 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
314 IN CPU_EXCHANGE_ROLE_INFO
*MyInfo
,
315 IN CPU_EXCHANGE_ROLE_INFO
*OthersInfo
319 Get the pointer to CPU MP Data structure.
321 @return The pointer to CPU MP Data structure.
329 Save the pointer to CPU MP Data structure.
331 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
335 IN CPU_MP_DATA
*CpuMpData
340 Get available system memory below 1MB by specified size.
342 @param[in] WakeupBufferSize Wakeup buffer size required
344 @retval other Return wakeup buffer address below 1MB.
345 @retval -1 Cannot find free memory below 1MB.
349 IN UINTN WakeupBufferSize
353 Get available EfiBootServicesCode memory below 4GB by specified size.
355 This buffer is required to safely transfer AP from real address mode to
356 protected mode or long mode, due to the fact that the buffer returned by
357 GetWakeupBuffer() may be marked as non-executable.
359 @param[in] BufferSize Wakeup transition buffer size.
361 @retval other Return wakeup transition buffer address below 4GB.
362 @retval 0 Cannot find free memory below 4GB.
365 GetModeTransitionBuffer (
370 This function will be called by BSP to wakeup AP.
372 @param[in] CpuMpData Pointer to CPU MP Data
373 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
374 FALSE: Send IPI to AP by ApicId
375 @param[in] ProcessorNumber The handle number of specified processor
376 @param[in] Procedure The function to be invoked by AP
377 @param[in] ProcedureArgument The argument to be passed into AP function
378 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
382 IN CPU_MP_DATA
*CpuMpData
,
383 IN BOOLEAN Broadcast
,
384 IN UINTN ProcessorNumber
,
385 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
386 IN VOID
*ProcedureArgument
, OPTIONAL
387 IN BOOLEAN WakeUpDisabledAps OPTIONAL
391 Initialize global data for MP support.
393 @param[in] CpuMpData The pointer to CPU MP Data structure.
397 IN CPU_MP_DATA
*CpuMpData
401 Worker function to execute a caller provided function on all enabled APs.
403 @param[in] Procedure A pointer to the function to be run on
404 enabled APs of the system.
405 @param[in] SingleThread If TRUE, then all the enabled APs execute
406 the function specified by Procedure one by
407 one, in ascending order of processor handle
408 number. If FALSE, then all the enabled APs
409 execute the function specified by Procedure
411 @param[in] ExcludeBsp Whether let BSP also trig this task.
412 @param[in] WaitEvent The event created by the caller with CreateEvent()
414 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
415 APs to return from Procedure, either for
416 blocking or non-blocking mode.
417 @param[in] ProcedureArgument The parameter passed into Procedure for
419 @param[out] FailedCpuList If all APs finish successfully, then its
420 content is set to NULL. If not all APs
421 finish before timeout expires, then its
422 content is set to address of the buffer
423 holding handle numbers of the failed APs.
425 @retval EFI_SUCCESS In blocking mode, all APs have finished before
427 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
429 @retval others Failed to Startup all APs.
433 StartupAllCPUsWorker (
434 IN EFI_AP_PROCEDURE Procedure
,
435 IN BOOLEAN SingleThread
,
436 IN BOOLEAN ExcludeBsp
,
437 IN EFI_EVENT WaitEvent OPTIONAL
,
438 IN UINTN TimeoutInMicroseconds
,
439 IN VOID
*ProcedureArgument OPTIONAL
,
440 OUT UINTN
**FailedCpuList OPTIONAL
444 Worker function to let the caller get one enabled AP to execute a caller-provided
447 @param[in] Procedure A pointer to the function to be run on
448 enabled APs of the system.
449 @param[in] ProcessorNumber The handle number of the AP.
450 @param[in] WaitEvent The event created by the caller with CreateEvent()
452 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
453 APs to return from Procedure, either for
454 blocking or non-blocking mode.
455 @param[in] ProcedureArgument The parameter passed into Procedure for
457 @param[out] Finished If AP returns from Procedure before the
458 timeout expires, its content is set to TRUE.
459 Otherwise, the value is set to FALSE.
461 @retval EFI_SUCCESS In blocking mode, specified AP finished before
463 @retval others Failed to Startup AP.
467 StartupThisAPWorker (
468 IN EFI_AP_PROCEDURE Procedure
,
469 IN UINTN ProcessorNumber
,
470 IN EFI_EVENT WaitEvent OPTIONAL
,
471 IN UINTN TimeoutInMicroseconds
,
472 IN VOID
*ProcedureArgument OPTIONAL
,
473 OUT BOOLEAN
*Finished OPTIONAL
477 Worker function to switch the requested AP to be the BSP from that point onward.
479 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
480 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
481 enabled AP. Otherwise, it will be disabled.
483 @retval EFI_SUCCESS BSP successfully switched.
484 @retval others Failed to switch BSP.
489 IN UINTN ProcessorNumber
,
490 IN BOOLEAN EnableOldBSP
494 Worker function to let the caller enable or disable an AP from this point onward.
495 This service may only be called from the BSP.
497 @param[in] ProcessorNumber The handle number of AP.
498 @param[in] EnableAP Specifies the new state for the processor for
499 enabled, FALSE for disabled.
500 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
501 the new health status of the AP.
503 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
504 @retval others Failed to Enable/Disable AP.
508 EnableDisableApWorker (
509 IN UINTN ProcessorNumber
,
511 IN UINT32
*HealthFlag OPTIONAL
515 Get pointer to CPU MP Data structure from GUIDed HOB.
517 @return The pointer to CPU MP Data structure.
520 GetCpuMpDataFromGuidedHob (
524 /** Checks status of specified AP.
526 This function checks whether the specified AP has finished the task assigned
527 by StartupThisAP(), and whether timeout expires.
529 @param[in] ProcessorNumber The handle number of processor.
531 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
532 @retval EFI_TIMEOUT The timeout expires.
533 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
537 IN UINTN ProcessorNumber
541 Checks status of all APs.
543 This function checks whether all APs have finished task assigned by StartupAllAPs(),
544 and whether timeout expires.
546 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
547 @retval EFI_TIMEOUT The timeout expires.
548 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
556 Checks APs status and updates APs status if needed.
560 CheckAndUpdateApsStatus (
565 Detect whether specified processor can find matching microcode patch and load it.
567 @param[in] CpuMpData The pointer to CPU MP Data structure.
568 @param[in] IsBspCallIn Indicate whether the caller is BSP or not.
572 IN CPU_MP_DATA
*CpuMpData
,
573 IN BOOLEAN IsBspCallIn
577 Detect whether Mwait-monitor feature is supported.
579 @retval TRUE Mwait-monitor feature is supported.
580 @retval FALSE Mwait-monitor feature is not supported.
588 Enable Debug Agent to support source debugging on AP function.