2 Common header file for MP Initialize Library.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Register/Cpuid.h>
21 #include <Register/Msr.h>
22 #include <Register/LocalApic.h>
23 #include <Register/Microcode.h>
25 #include <Library/MpInitLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/LocalApicLib.h>
31 #include <Library/CpuLib.h>
32 #include <Library/UefiCpuLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/MtrrLib.h>
36 #include <Library/HobLib.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 // CPU exchange information for switch BSP
56 UINT8 State
; // offset 0
57 UINTN StackPointer
; // offset 4 / 8
58 IA32_DESCRIPTOR Gdtr
; // offset 8 / 16
59 IA32_DESCRIPTOR Idtr
; // offset 14 / 26
60 } CPU_EXCHANGE_ROLE_INFO
;
63 // AP loop state when APs are in idle state
64 // It's value is the same with PcdCpuApLoopMode
73 // AP initialization state during APs wakeup
93 // CPU volatile registers around INIT-SIPI-SIPI
105 IA32_DESCRIPTOR Gdtr
;
106 IA32_DESCRIPTOR Idtr
;
108 } CPU_VOLATILE_REGISTERS
;
115 volatile UINT32
*StartupApSignal
;
116 volatile UINTN ApFunction
;
117 volatile UINTN ApFunctionArgument
;
119 volatile CPU_STATE State
;
120 CPU_VOLATILE_REGISTERS VolatileRegisters
;
130 // Basic CPU information saved in Guided HOB.
131 // Because the contents will be shard between PEI and DXE,
132 // we need to make sure the each fields offset same in different
137 UINT32 InitialApicId
;
145 // AP reset code information including code address and size,
146 // this structure will be shared be C code and assembly code.
147 // It is natural aligned by design.
150 UINT8
*RendezvousFunnelAddress
;
151 UINTN ModeEntryOffset
;
152 UINTN RendezvousFunnelSize
;
153 UINT8
*RelocateApLoopFuncAddress
;
154 UINTN RelocateApLoopFuncSize
;
155 UINTN ModeTransitionOffset
;
156 } MP_ASSEMBLY_ADDRESS_MAP
;
158 typedef struct _CPU_MP_DATA CPU_MP_DATA
;
163 // MP CPU exchange information for AP reset code
164 // This structure is required to be packed because fixed field offsets
165 // into this structure are used in assembly code in this module
172 IA32_DESCRIPTOR GdtrProfile
;
173 IA32_DESCRIPTOR IdtrProfile
;
179 UINTN EnableExecuteDisable
;
182 CPU_INFO_IN_HOB
*CpuInfo
;
183 UINTN NumApsExecuting
;
184 CPU_MP_DATA
*CpuMpData
;
185 UINTN InitializeFloatingPointUnitsAddress
;
186 UINT32 ModeTransitionMemory
;
187 UINT16 ModeTransitionSegment
;
188 UINT32 ModeHighMemory
;
189 UINT16 ModeHighSegment
;
190 } MP_CPU_EXCHANGE_INFO
;
195 // CPU MP Data save in memory
197 struct _CPU_MP_DATA
{
202 // The above fields data will be passed from PEI to DXE
203 // Please make sure the fields offset same in the different
208 UINTN CpuApStackSize
;
209 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
211 UINTN WakeupBufferHigh
;
213 UINTN BackupBufferSize
;
215 volatile UINT32 StartCount
;
216 volatile UINT32 FinishedCount
;
217 volatile UINT32 RunningCount
;
218 BOOLEAN SingleThread
;
219 EFI_AP_PROCEDURE Procedure
;
226 UINTN
**FailedCpuList
;
228 AP_INIT_STATE InitFlag
;
229 BOOLEAN X2ApicEnable
;
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
381 IN CPU_MP_DATA
*CpuMpData
,
382 IN BOOLEAN Broadcast
,
383 IN UINTN ProcessorNumber
,
384 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
385 IN VOID
*ProcedureArgument OPTIONAL
389 Initialize global data for MP support.
391 @param[in] CpuMpData The pointer to CPU MP Data structure.
395 IN CPU_MP_DATA
*CpuMpData
399 Worker function to execute a caller provided function on all enabled APs.
401 @param[in] Procedure A pointer to the function to be run on
402 enabled APs of the system.
403 @param[in] SingleThread If TRUE, then all the enabled APs execute
404 the function specified by Procedure one by
405 one, in ascending order of processor handle
406 number. If FALSE, then all the enabled APs
407 execute the function specified by Procedure
409 @param[in] WaitEvent The event created by the caller with CreateEvent()
411 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
412 APs to return from Procedure, either for
413 blocking or non-blocking mode.
414 @param[in] ProcedureArgument The parameter passed into Procedure for
416 @param[out] FailedCpuList If all APs finish successfully, then its
417 content is set to NULL. If not all APs
418 finish before timeout expires, then its
419 content is set to address of the buffer
420 holding handle numbers of the failed APs.
422 @retval EFI_SUCCESS In blocking mode, all APs have finished before
424 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
426 @retval others Failed to Startup all APs.
430 StartupAllAPsWorker (
431 IN EFI_AP_PROCEDURE Procedure
,
432 IN BOOLEAN SingleThread
,
433 IN EFI_EVENT WaitEvent OPTIONAL
,
434 IN UINTN TimeoutInMicroseconds
,
435 IN VOID
*ProcedureArgument OPTIONAL
,
436 OUT UINTN
**FailedCpuList OPTIONAL
440 Worker function to let the caller get one enabled AP to execute a caller-provided
443 @param[in] Procedure A pointer to the function to be run on
444 enabled APs of the system.
445 @param[in] ProcessorNumber The handle number of the AP.
446 @param[in] WaitEvent The event created by the caller with CreateEvent()
448 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
449 APs to return from Procedure, either for
450 blocking or non-blocking mode.
451 @param[in] ProcedureArgument The parameter passed into Procedure for
453 @param[out] Finished If AP returns from Procedure before the
454 timeout expires, its content is set to TRUE.
455 Otherwise, the value is set to FALSE.
457 @retval EFI_SUCCESS In blocking mode, specified AP finished before
459 @retval others Failed to Startup AP.
463 StartupThisAPWorker (
464 IN EFI_AP_PROCEDURE Procedure
,
465 IN UINTN ProcessorNumber
,
466 IN EFI_EVENT WaitEvent OPTIONAL
,
467 IN UINTN TimeoutInMicroseconds
,
468 IN VOID
*ProcedureArgument OPTIONAL
,
469 OUT BOOLEAN
*Finished OPTIONAL
473 Worker function to switch the requested AP to be the BSP from that point onward.
475 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
476 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
477 enabled AP. Otherwise, it will be disabled.
479 @retval EFI_SUCCESS BSP successfully switched.
480 @retval others Failed to switch BSP.
485 IN UINTN ProcessorNumber
,
486 IN BOOLEAN EnableOldBSP
490 Worker function to let the caller enable or disable an AP from this point onward.
491 This service may only be called from the BSP.
493 @param[in] ProcessorNumber The handle number of AP.
494 @param[in] EnableAP Specifies the new state for the processor for
495 enabled, FALSE for disabled.
496 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
497 the new health status of the AP.
499 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
500 @retval others Failed to Enable/Disable AP.
504 EnableDisableApWorker (
505 IN UINTN ProcessorNumber
,
507 IN UINT32
*HealthFlag OPTIONAL
511 Get pointer to CPU MP Data structure from GUIDed HOB.
513 @return The pointer to CPU MP Data structure.
516 GetCpuMpDataFromGuidedHob (
520 /** Checks status of specified AP.
522 This function checks whether the specified AP has finished the task assigned
523 by StartupThisAP(), and whether timeout expires.
525 @param[in] ProcessorNumber The handle number of processor.
527 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
528 @retval EFI_TIMEOUT The timeout expires.
529 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
533 IN UINTN ProcessorNumber
537 Checks status of all APs.
539 This function checks whether all APs have finished task assigned by StartupAllAPs(),
540 and whether timeout expires.
542 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
543 @retval EFI_TIMEOUT The timeout expires.
544 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
552 Checks APs status and updates APs status if needed.
556 CheckAndUpdateApsStatus (
561 Detect whether specified processor can find matching microcode patch and load it.
563 @param[in] CpuMpData The pointer to CPU MP Data structure.
564 @param[in] IsBspCallIn Indicate whether the caller is BSP or not.
568 IN CPU_MP_DATA
*CpuMpData
,
569 IN BOOLEAN IsBspCallIn
573 Detect whether Mwait-monitor feature is supported.
575 @retval TRUE Mwait-monitor feature is supported.
576 @retval FALSE Mwait-monitor feature is not supported.
584 Enable Debug Agent to support source debugging on AP function.