]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.h
UefiCpuPkg: Update code to include register definitions from MdePkg
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.h
1 /** @file
2 Common header file for MP Initialize Library.
3
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #ifndef _MP_LIB_H_
10 #define _MP_LIB_H_
11
12 #include <PiPei.h>
13
14 #include <Register/Intel/Cpuid.h>
15 #include <Register/Intel/Msr.h>
16 #include <Register/Intel/LocalApic.h>
17 #include <Register/Intel/Microcode.h>
18
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>
31
32 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
33
34 #define CPU_INIT_MP_LIB_HOB_GUID \
35 { \
36 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
37 }
38
39 //
40 // The MP data for switch BSP
41 //
42 #define CPU_SWITCH_STATE_IDLE 0
43 #define CPU_SWITCH_STATE_STORED 1
44 #define CPU_SWITCH_STATE_LOADED 2
45
46 //
47 // CPU exchange information for switch BSP
48 //
49 typedef struct {
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;
55
56 //
57 // AP loop state when APs are in idle state
58 // It's value is the same with PcdCpuApLoopMode
59 //
60 typedef enum {
61 ApInHltLoop = 1,
62 ApInMwaitLoop = 2,
63 ApInRunLoop = 3
64 } AP_LOOP_MODE;
65
66 //
67 // AP initialization state during APs wakeup
68 //
69 typedef enum {
70 ApInitConfig = 1,
71 ApInitReconfig = 2,
72 ApInitDone = 3
73 } AP_INIT_STATE;
74
75 //
76 // AP state
77 //
78 // The state transitions for an AP when it process a procedure are:
79 // Idle ----> Ready ----> Busy ----> Idle
80 // [BSP] [AP] [AP]
81 //
82 typedef enum {
83 CpuStateIdle,
84 CpuStateReady,
85 CpuStateBusy,
86 CpuStateFinished,
87 CpuStateDisabled
88 } CPU_STATE;
89
90 //
91 // CPU volatile registers around INIT-SIPI-SIPI
92 //
93 typedef struct {
94 UINTN Cr0;
95 UINTN Cr3;
96 UINTN Cr4;
97 UINTN Dr0;
98 UINTN Dr1;
99 UINTN Dr2;
100 UINTN Dr3;
101 UINTN Dr6;
102 UINTN Dr7;
103 IA32_DESCRIPTOR Gdtr;
104 IA32_DESCRIPTOR Idtr;
105 UINT16 Tr;
106 } CPU_VOLATILE_REGISTERS;
107
108 //
109 // AP related data
110 //
111 typedef struct {
112 SPIN_LOCK ApLock;
113 volatile UINT32 *StartupApSignal;
114 volatile UINTN ApFunction;
115 volatile UINTN ApFunctionArgument;
116 BOOLEAN CpuHealthy;
117 volatile CPU_STATE State;
118 CPU_VOLATILE_REGISTERS VolatileRegisters;
119 BOOLEAN Waiting;
120 BOOLEAN *Finished;
121 UINT64 ExpectedTime;
122 UINT64 CurrentTime;
123 UINT64 TotalTime;
124 EFI_EVENT WaitEvent;
125 } CPU_AP_DATA;
126
127 //
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
131 // architecture.
132 //
133 #pragma pack (1)
134 typedef struct {
135 UINT32 InitialApicId;
136 UINT32 ApicId;
137 UINT32 Health;
138 UINT64 ApTopOfStack;
139 } CPU_INFO_IN_HOB;
140 #pragma pack ()
141
142 //
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.
146 //
147 typedef struct {
148 UINT8 *RendezvousFunnelAddress;
149 UINTN ModeEntryOffset;
150 UINTN RendezvousFunnelSize;
151 UINT8 *RelocateApLoopFuncAddress;
152 UINTN RelocateApLoopFuncSize;
153 UINTN ModeTransitionOffset;
154 } MP_ASSEMBLY_ADDRESS_MAP;
155
156 typedef struct _CPU_MP_DATA CPU_MP_DATA;
157
158 #pragma pack(1)
159
160 //
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
164 //
165 typedef struct {
166 UINTN Lock;
167 UINTN StackStart;
168 UINTN StackSize;
169 UINTN CFunction;
170 IA32_DESCRIPTOR GdtrProfile;
171 IA32_DESCRIPTOR IdtrProfile;
172 UINTN BufferStart;
173 UINTN ModeOffset;
174 UINTN ApIndex;
175 UINTN CodeSegment;
176 UINTN DataSegment;
177 UINTN EnableExecuteDisable;
178 UINTN Cr3;
179 UINTN InitFlag;
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;
188 //
189 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
190 //
191 BOOLEAN Enable5LevelPaging;
192 } MP_CPU_EXCHANGE_INFO;
193
194 #pragma pack()
195
196 //
197 // CPU MP Data save in memory
198 //
199 struct _CPU_MP_DATA {
200 UINT64 CpuInfoInHob;
201 UINT32 CpuCount;
202 UINT32 BspNumber;
203 //
204 // The above fields data will be passed from PEI to DXE
205 // Please make sure the fields offset same in the different
206 // architecture.
207 //
208 SPIN_LOCK MpLock;
209 UINTN Buffer;
210 UINTN CpuApStackSize;
211 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
212 UINTN WakeupBuffer;
213 UINTN WakeupBufferHigh;
214 UINTN BackupBuffer;
215 UINTN BackupBufferSize;
216
217 volatile UINT32 FinishedCount;
218 UINT32 RunningCount;
219 BOOLEAN SingleThread;
220 EFI_AP_PROCEDURE Procedure;
221 VOID *ProcArguments;
222 BOOLEAN *Finished;
223 UINT64 ExpectedTime;
224 UINT64 CurrentTime;
225 UINT64 TotalTime;
226 EFI_EVENT WaitEvent;
227 UINTN **FailedCpuList;
228
229 AP_INIT_STATE InitFlag;
230 BOOLEAN X2ApicEnable;
231 BOOLEAN SwitchBspFlag;
232 UINTN NewBspNumber;
233 CPU_EXCHANGE_ROLE_INFO BSPInfo;
234 CPU_EXCHANGE_ROLE_INFO APInfo;
235 MTRR_SETTINGS MtrrTable;
236 UINT8 ApLoopMode;
237 UINT8 ApTargetCState;
238 UINT16 PmCodeSegment;
239 CPU_AP_DATA *CpuData;
240 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;
241
242 UINT32 CurrentTimerCount;
243 UINTN DivideValue;
244 UINT8 Vector;
245 BOOLEAN PeriodicMode;
246 BOOLEAN TimerInterruptState;
247 UINT64 MicrocodePatchAddress;
248 UINT64 MicrocodePatchRegionSize;
249
250 UINT32 ProcessorSignature;
251 UINT32 ProcessorFlags;
252 UINT64 MicrocodeDataAddress;
253 UINT32 MicrocodeRevision;
254
255 //
256 // Whether need to use Init-Sipi-Sipi to wake up the APs.
257 // Two cases need to set this value to TRUE. One is in HLT
258 // loop mode, the other is resume from S3 which loop mode
259 // will be hardcode change to HLT mode by PiSmmCpuDxeSmm
260 // driver.
261 //
262 BOOLEAN WakeUpByInitSipiSipi;
263 };
264
265 extern EFI_GUID mCpuInitMpLibHobGuid;
266
267 /**
268 Assembly code to place AP into safe loop mode.
269
270 Place AP into targeted C-State if MONITOR is supported, otherwise
271 place AP into hlt state.
272 Place AP in protected mode if the current is long mode. Due to AP maybe
273 wakeup by some hardware event. It could avoid accessing page table that
274 may not available during booting to OS.
275
276 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
277 FALSE indicates MONITOR is not supported.
278 @param[in] ApTargetCState Target C-State value.
279 @param[in] PmCodeSegment Protected mode code segment value.
280 **/
281 typedef
282 VOID
283 (EFIAPI * ASM_RELOCATE_AP_LOOP) (
284 IN BOOLEAN MwaitSupport,
285 IN UINTN ApTargetCState,
286 IN UINTN PmCodeSegment,
287 IN UINTN TopOfApStack,
288 IN UINTN NumberToFinish
289 );
290
291 /**
292 Assembly code to get starting address and size of the rendezvous entry for APs.
293 Information for fixing a jump instruction in the code is also returned.
294
295 @param[out] AddressMap Output buffer for address map information.
296 **/
297 VOID
298 EFIAPI
299 AsmGetAddressMap (
300 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap
301 );
302
303 /**
304 This function is called by both the BSP and the AP which is to become the BSP to
305 Exchange execution context including stack between them. After return from this
306 function, the BSP becomes AP and the AP becomes the BSP.
307
308 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
309 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
310
311 **/
312 VOID
313 EFIAPI
314 AsmExchangeRole (
315 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,
316 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo
317 );
318
319 /**
320 Get the pointer to CPU MP Data structure.
321
322 @return The pointer to CPU MP Data structure.
323 **/
324 CPU_MP_DATA *
325 GetCpuMpData (
326 VOID
327 );
328
329 /**
330 Save the pointer to CPU MP Data structure.
331
332 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
333 **/
334 VOID
335 SaveCpuMpData (
336 IN CPU_MP_DATA *CpuMpData
337 );
338
339
340 /**
341 Get available system memory below 1MB by specified size.
342
343 @param[in] WakeupBufferSize Wakeup buffer size required
344
345 @retval other Return wakeup buffer address below 1MB.
346 @retval -1 Cannot find free memory below 1MB.
347 **/
348 UINTN
349 GetWakeupBuffer (
350 IN UINTN WakeupBufferSize
351 );
352
353 /**
354 Get available EfiBootServicesCode memory below 4GB by specified size.
355
356 This buffer is required to safely transfer AP from real address mode to
357 protected mode or long mode, due to the fact that the buffer returned by
358 GetWakeupBuffer() may be marked as non-executable.
359
360 @param[in] BufferSize Wakeup transition buffer size.
361
362 @retval other Return wakeup transition buffer address below 4GB.
363 @retval 0 Cannot find free memory below 4GB.
364 **/
365 UINTN
366 GetModeTransitionBuffer (
367 IN UINTN BufferSize
368 );
369
370 /**
371 This function will be called by BSP to wakeup AP.
372
373 @param[in] CpuMpData Pointer to CPU MP Data
374 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
375 FALSE: Send IPI to AP by ApicId
376 @param[in] ProcessorNumber The handle number of specified processor
377 @param[in] Procedure The function to be invoked by AP
378 @param[in] ProcedureArgument The argument to be passed into AP function
379 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
380 **/
381 VOID
382 WakeUpAP (
383 IN CPU_MP_DATA *CpuMpData,
384 IN BOOLEAN Broadcast,
385 IN UINTN ProcessorNumber,
386 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
387 IN VOID *ProcedureArgument, OPTIONAL
388 IN BOOLEAN WakeUpDisabledAps OPTIONAL
389 );
390
391 /**
392 Initialize global data for MP support.
393
394 @param[in] CpuMpData The pointer to CPU MP Data structure.
395 **/
396 VOID
397 InitMpGlobalData (
398 IN CPU_MP_DATA *CpuMpData
399 );
400
401 /**
402 Worker function to execute a caller provided function on all enabled APs.
403
404 @param[in] Procedure A pointer to the function to be run on
405 enabled APs of the system.
406 @param[in] SingleThread If TRUE, then all the enabled APs execute
407 the function specified by Procedure one by
408 one, in ascending order of processor handle
409 number. If FALSE, then all the enabled APs
410 execute the function specified by Procedure
411 simultaneously.
412 @param[in] ExcludeBsp Whether let BSP also trig this task.
413 @param[in] WaitEvent The event created by the caller with CreateEvent()
414 service.
415 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
416 APs to return from Procedure, either for
417 blocking or non-blocking mode.
418 @param[in] ProcedureArgument The parameter passed into Procedure for
419 all APs.
420 @param[out] FailedCpuList If all APs finish successfully, then its
421 content is set to NULL. If not all APs
422 finish before timeout expires, then its
423 content is set to address of the buffer
424 holding handle numbers of the failed APs.
425
426 @retval EFI_SUCCESS In blocking mode, all APs have finished before
427 the timeout expired.
428 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
429 to all enabled APs.
430 @retval others Failed to Startup all APs.
431
432 **/
433 EFI_STATUS
434 StartupAllCPUsWorker (
435 IN EFI_AP_PROCEDURE Procedure,
436 IN BOOLEAN SingleThread,
437 IN BOOLEAN ExcludeBsp,
438 IN EFI_EVENT WaitEvent OPTIONAL,
439 IN UINTN TimeoutInMicroseconds,
440 IN VOID *ProcedureArgument OPTIONAL,
441 OUT UINTN **FailedCpuList OPTIONAL
442 );
443
444 /**
445 Worker function to let the caller get one enabled AP to execute a caller-provided
446 function.
447
448 @param[in] Procedure A pointer to the function to be run on
449 enabled APs of the system.
450 @param[in] ProcessorNumber The handle number of the AP.
451 @param[in] WaitEvent The event created by the caller with CreateEvent()
452 service.
453 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
454 APs to return from Procedure, either for
455 blocking or non-blocking mode.
456 @param[in] ProcedureArgument The parameter passed into Procedure for
457 all APs.
458 @param[out] Finished If AP returns from Procedure before the
459 timeout expires, its content is set to TRUE.
460 Otherwise, the value is set to FALSE.
461
462 @retval EFI_SUCCESS In blocking mode, specified AP finished before
463 the timeout expires.
464 @retval others Failed to Startup AP.
465
466 **/
467 EFI_STATUS
468 StartupThisAPWorker (
469 IN EFI_AP_PROCEDURE Procedure,
470 IN UINTN ProcessorNumber,
471 IN EFI_EVENT WaitEvent OPTIONAL,
472 IN UINTN TimeoutInMicroseconds,
473 IN VOID *ProcedureArgument OPTIONAL,
474 OUT BOOLEAN *Finished OPTIONAL
475 );
476
477 /**
478 Worker function to switch the requested AP to be the BSP from that point onward.
479
480 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
481 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
482 enabled AP. Otherwise, it will be disabled.
483
484 @retval EFI_SUCCESS BSP successfully switched.
485 @retval others Failed to switch BSP.
486
487 **/
488 EFI_STATUS
489 SwitchBSPWorker (
490 IN UINTN ProcessorNumber,
491 IN BOOLEAN EnableOldBSP
492 );
493
494 /**
495 Worker function to let the caller enable or disable an AP from this point onward.
496 This service may only be called from the BSP.
497
498 @param[in] ProcessorNumber The handle number of AP.
499 @param[in] EnableAP Specifies the new state for the processor for
500 enabled, FALSE for disabled.
501 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
502 the new health status of the AP.
503
504 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
505 @retval others Failed to Enable/Disable AP.
506
507 **/
508 EFI_STATUS
509 EnableDisableApWorker (
510 IN UINTN ProcessorNumber,
511 IN BOOLEAN EnableAP,
512 IN UINT32 *HealthFlag OPTIONAL
513 );
514
515 /**
516 Get pointer to CPU MP Data structure from GUIDed HOB.
517
518 @return The pointer to CPU MP Data structure.
519 **/
520 CPU_MP_DATA *
521 GetCpuMpDataFromGuidedHob (
522 VOID
523 );
524
525 /** Checks status of specified AP.
526
527 This function checks whether the specified AP has finished the task assigned
528 by StartupThisAP(), and whether timeout expires.
529
530 @param[in] ProcessorNumber The handle number of processor.
531
532 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
533 @retval EFI_TIMEOUT The timeout expires.
534 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
535 **/
536 EFI_STATUS
537 CheckThisAP (
538 IN UINTN ProcessorNumber
539 );
540
541 /**
542 Checks status of all APs.
543
544 This function checks whether all APs have finished task assigned by StartupAllAPs(),
545 and whether timeout expires.
546
547 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
548 @retval EFI_TIMEOUT The timeout expires.
549 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
550 **/
551 EFI_STATUS
552 CheckAllAPs (
553 VOID
554 );
555
556 /**
557 Checks APs status and updates APs status if needed.
558
559 **/
560 VOID
561 CheckAndUpdateApsStatus (
562 VOID
563 );
564
565 /**
566 Detect whether specified processor can find matching microcode patch and load it.
567
568 @param[in] CpuMpData The pointer to CPU MP Data structure.
569 @param[in] IsBspCallIn Indicate whether the caller is BSP or not.
570 **/
571 VOID
572 MicrocodeDetect (
573 IN CPU_MP_DATA *CpuMpData,
574 IN BOOLEAN IsBspCallIn
575 );
576
577 /**
578 Detect whether Mwait-monitor feature is supported.
579
580 @retval TRUE Mwait-monitor feature is supported.
581 @retval FALSE Mwait-monitor feature is not supported.
582 **/
583 BOOLEAN
584 IsMwaitSupport (
585 VOID
586 );
587
588 /**
589 Enable Debug Agent to support source debugging on AP function.
590
591 **/
592 VOID
593 EnableDebugAgent (
594 VOID
595 );
596
597 #endif
598