]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.h
455cb3f09acb28f0f0c6747067b4201ab982a9aa
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.h
1 /** @file
2 Common header file for MP Initialize Library.
3
4 Copyright (c) 2016 - 2020, 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 #include <Guid/MicrocodePatchHob.h>
33
34 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
35
36 #define CPU_INIT_MP_LIB_HOB_GUID \
37 { \
38 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
39 }
40
41 //
42 // The MP data for switch BSP
43 //
44 #define CPU_SWITCH_STATE_IDLE 0
45 #define CPU_SWITCH_STATE_STORED 1
46 #define CPU_SWITCH_STATE_LOADED 2
47
48 //
49 // Default maximum number of entries to store the microcode patches information
50 //
51 #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8
52
53 //
54 // Data structure for microcode patch information
55 //
56 typedef struct {
57 UINTN Address;
58 UINTN Size;
59 } MICROCODE_PATCH_INFO;
60
61 //
62 // CPU exchange information for switch BSP
63 //
64 typedef struct {
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;
70
71 //
72 // AP loop state when APs are in idle state
73 // It's value is the same with PcdCpuApLoopMode
74 //
75 typedef enum {
76 ApInHltLoop = 1,
77 ApInMwaitLoop = 2,
78 ApInRunLoop = 3
79 } AP_LOOP_MODE;
80
81 //
82 // AP initialization state during APs wakeup
83 //
84 typedef enum {
85 ApInitConfig = 1,
86 ApInitReconfig = 2,
87 ApInitDone = 3
88 } AP_INIT_STATE;
89
90 //
91 // AP state
92 //
93 // The state transitions for an AP when it process a procedure are:
94 // Idle ----> Ready ----> Busy ----> Idle
95 // [BSP] [AP] [AP]
96 //
97 typedef enum {
98 CpuStateIdle,
99 CpuStateReady,
100 CpuStateBusy,
101 CpuStateFinished,
102 CpuStateDisabled
103 } CPU_STATE;
104
105 //
106 // CPU volatile registers around INIT-SIPI-SIPI
107 //
108 typedef struct {
109 UINTN Cr0;
110 UINTN Cr3;
111 UINTN Cr4;
112 UINTN Dr0;
113 UINTN Dr1;
114 UINTN Dr2;
115 UINTN Dr3;
116 UINTN Dr6;
117 UINTN Dr7;
118 IA32_DESCRIPTOR Gdtr;
119 IA32_DESCRIPTOR Idtr;
120 UINT16 Tr;
121 } CPU_VOLATILE_REGISTERS;
122
123 //
124 // AP related data
125 //
126 typedef struct {
127 SPIN_LOCK ApLock;
128 volatile UINT32 *StartupApSignal;
129 volatile UINTN ApFunction;
130 volatile UINTN ApFunctionArgument;
131 BOOLEAN CpuHealthy;
132 volatile CPU_STATE State;
133 CPU_VOLATILE_REGISTERS VolatileRegisters;
134 BOOLEAN Waiting;
135 BOOLEAN *Finished;
136 UINT64 ExpectedTime;
137 UINT64 CurrentTime;
138 UINT64 TotalTime;
139 EFI_EVENT WaitEvent;
140 UINT32 ProcessorSignature;
141 UINT8 PlatformId;
142 UINT64 MicrocodeEntryAddr;
143 } CPU_AP_DATA;
144
145 //
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
149 // architecture.
150 //
151 #pragma pack (1)
152 typedef struct {
153 UINT32 InitialApicId;
154 UINT32 ApicId;
155 UINT32 Health;
156 UINT64 ApTopOfStack;
157 } CPU_INFO_IN_HOB;
158 #pragma pack ()
159
160 //
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.
164 //
165 typedef struct {
166 UINT8 *RendezvousFunnelAddress;
167 UINTN ModeEntryOffset;
168 UINTN RendezvousFunnelSize;
169 UINT8 *RelocateApLoopFuncAddress;
170 UINTN RelocateApLoopFuncSize;
171 UINTN ModeTransitionOffset;
172 } MP_ASSEMBLY_ADDRESS_MAP;
173
174 typedef struct _CPU_MP_DATA CPU_MP_DATA;
175
176 #pragma pack(1)
177
178 //
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
182 //
183 typedef struct {
184 UINTN Lock;
185 UINTN StackStart;
186 UINTN StackSize;
187 UINTN CFunction;
188 IA32_DESCRIPTOR GdtrProfile;
189 IA32_DESCRIPTOR IdtrProfile;
190 UINTN BufferStart;
191 UINTN ModeOffset;
192 UINTN ApIndex;
193 UINTN CodeSegment;
194 UINTN DataSegment;
195 UINTN EnableExecuteDisable;
196 UINTN Cr3;
197 UINTN InitFlag;
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;
206 //
207 // Enable5LevelPaging indicates whether 5-level paging is enabled in long mode.
208 //
209 BOOLEAN Enable5LevelPaging;
210 } MP_CPU_EXCHANGE_INFO;
211
212 #pragma pack()
213
214 //
215 // CPU MP Data save in memory
216 //
217 struct _CPU_MP_DATA {
218 UINT64 CpuInfoInHob;
219 UINT32 CpuCount;
220 UINT32 BspNumber;
221 //
222 // The above fields data will be passed from PEI to DXE
223 // Please make sure the fields offset same in the different
224 // architecture.
225 //
226 SPIN_LOCK MpLock;
227 UINTN Buffer;
228 UINTN CpuApStackSize;
229 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
230 UINTN WakeupBuffer;
231 UINTN WakeupBufferHigh;
232 UINTN BackupBuffer;
233 UINTN BackupBufferSize;
234
235 volatile UINT32 FinishedCount;
236 UINT32 RunningCount;
237 BOOLEAN SingleThread;
238 EFI_AP_PROCEDURE Procedure;
239 VOID *ProcArguments;
240 BOOLEAN *Finished;
241 UINT64 ExpectedTime;
242 UINT64 CurrentTime;
243 UINT64 TotalTime;
244 EFI_EVENT WaitEvent;
245 UINTN **FailedCpuList;
246
247 AP_INIT_STATE InitFlag;
248 BOOLEAN SwitchBspFlag;
249 UINTN NewBspNumber;
250 CPU_EXCHANGE_ROLE_INFO BSPInfo;
251 CPU_EXCHANGE_ROLE_INFO APInfo;
252 MTRR_SETTINGS MtrrTable;
253 UINT8 ApLoopMode;
254 UINT8 ApTargetCState;
255 UINT16 PmCodeSegment;
256 CPU_AP_DATA *CpuData;
257 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;
258
259 UINT32 CurrentTimerCount;
260 UINTN DivideValue;
261 UINT8 Vector;
262 BOOLEAN PeriodicMode;
263 BOOLEAN TimerInterruptState;
264 UINT64 MicrocodePatchAddress;
265 UINT64 MicrocodePatchRegionSize;
266
267 //
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
272 // driver.
273 //
274 BOOLEAN WakeUpByInitSipiSipi;
275 };
276
277 extern EFI_GUID mCpuInitMpLibHobGuid;
278
279 /**
280 Assembly code to place AP into safe loop mode.
281
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.
287
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.
292 **/
293 typedef
294 VOID
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
301 );
302
303 /**
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.
306
307 @param[out] AddressMap Output buffer for address map information.
308 **/
309 VOID
310 EFIAPI
311 AsmGetAddressMap (
312 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap
313 );
314
315 /**
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.
319
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.
322
323 **/
324 VOID
325 EFIAPI
326 AsmExchangeRole (
327 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,
328 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo
329 );
330
331 /**
332 Get the pointer to CPU MP Data structure.
333
334 @return The pointer to CPU MP Data structure.
335 **/
336 CPU_MP_DATA *
337 GetCpuMpData (
338 VOID
339 );
340
341 /**
342 Save the pointer to CPU MP Data structure.
343
344 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
345 **/
346 VOID
347 SaveCpuMpData (
348 IN CPU_MP_DATA *CpuMpData
349 );
350
351
352 /**
353 Get available system memory below 1MB by specified size.
354
355 @param[in] WakeupBufferSize Wakeup buffer size required
356
357 @retval other Return wakeup buffer address below 1MB.
358 @retval -1 Cannot find free memory below 1MB.
359 **/
360 UINTN
361 GetWakeupBuffer (
362 IN UINTN WakeupBufferSize
363 );
364
365 /**
366 Get available EfiBootServicesCode memory below 4GB by specified size.
367
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.
371
372 @param[in] BufferSize Wakeup transition buffer size.
373
374 @retval other Return wakeup transition buffer address below 4GB.
375 @retval 0 Cannot find free memory below 4GB.
376 **/
377 UINTN
378 GetModeTransitionBuffer (
379 IN UINTN BufferSize
380 );
381
382 /**
383 This function will be called by BSP to wakeup AP.
384
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.
392 **/
393 VOID
394 WakeUpAP (
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
401 );
402
403 /**
404 Initialize global data for MP support.
405
406 @param[in] CpuMpData The pointer to CPU MP Data structure.
407 **/
408 VOID
409 InitMpGlobalData (
410 IN CPU_MP_DATA *CpuMpData
411 );
412
413 /**
414 Worker function to execute a caller provided function on all enabled APs.
415
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
423 simultaneously.
424 @param[in] ExcludeBsp Whether let BSP also trig this task.
425 @param[in] WaitEvent The event created by the caller with CreateEvent()
426 service.
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
431 all APs.
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.
437
438 @retval EFI_SUCCESS In blocking mode, all APs have finished before
439 the timeout expired.
440 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
441 to all enabled APs.
442 @retval others Failed to Startup all APs.
443
444 **/
445 EFI_STATUS
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
454 );
455
456 /**
457 Worker function to let the caller get one enabled AP to execute a caller-provided
458 function.
459
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()
464 service.
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
469 all APs.
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.
473
474 @retval EFI_SUCCESS In blocking mode, specified AP finished before
475 the timeout expires.
476 @retval others Failed to Startup AP.
477
478 **/
479 EFI_STATUS
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
487 );
488
489 /**
490 Worker function to switch the requested AP to be the BSP from that point onward.
491
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.
495
496 @retval EFI_SUCCESS BSP successfully switched.
497 @retval others Failed to switch BSP.
498
499 **/
500 EFI_STATUS
501 SwitchBSPWorker (
502 IN UINTN ProcessorNumber,
503 IN BOOLEAN EnableOldBSP
504 );
505
506 /**
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.
509
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.
515
516 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
517 @retval others Failed to Enable/Disable AP.
518
519 **/
520 EFI_STATUS
521 EnableDisableApWorker (
522 IN UINTN ProcessorNumber,
523 IN BOOLEAN EnableAP,
524 IN UINT32 *HealthFlag OPTIONAL
525 );
526
527 /**
528 Get pointer to CPU MP Data structure from GUIDed HOB.
529
530 @return The pointer to CPU MP Data structure.
531 **/
532 CPU_MP_DATA *
533 GetCpuMpDataFromGuidedHob (
534 VOID
535 );
536
537 /** Checks status of specified AP.
538
539 This function checks whether the specified AP has finished the task assigned
540 by StartupThisAP(), and whether timeout expires.
541
542 @param[in] ProcessorNumber The handle number of processor.
543
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.
547 **/
548 EFI_STATUS
549 CheckThisAP (
550 IN UINTN ProcessorNumber
551 );
552
553 /**
554 Checks status of all APs.
555
556 This function checks whether all APs have finished task assigned by StartupAllAPs(),
557 and whether timeout expires.
558
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.
562 **/
563 EFI_STATUS
564 CheckAllAPs (
565 VOID
566 );
567
568 /**
569 Checks APs status and updates APs status if needed.
570
571 **/
572 VOID
573 CheckAndUpdateApsStatus (
574 VOID
575 );
576
577 /**
578 Detect whether specified processor can find matching microcode patch and load it.
579
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
583 minus 1.
584 **/
585 VOID
586 MicrocodeDetect (
587 IN CPU_MP_DATA *CpuMpData,
588 IN UINTN ProcessorNumber
589 );
590
591 /**
592 Shadow the required microcode patches data into memory.
593
594 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
595 **/
596 VOID
597 ShadowMicrocodeUpdatePatch (
598 IN OUT CPU_MP_DATA *CpuMpData
599 );
600
601 /**
602 Get the cached microcode patch base address and size from the microcode patch
603 information cache HOB.
604
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.
611
612 @retval TRUE The microcode patch information cache HOB is found.
613 @retval FALSE The microcode patch information cache HOB is not found.
614
615 **/
616 BOOLEAN
617 GetMicrocodePatchInfoFromHob (
618 UINT64 *Address,
619 UINT64 *RegionSize
620 );
621
622 /**
623 Detect whether Mwait-monitor feature is supported.
624
625 @retval TRUE Mwait-monitor feature is supported.
626 @retval FALSE Mwait-monitor feature is not supported.
627 **/
628 BOOLEAN
629 IsMwaitSupport (
630 VOID
631 );
632
633 /**
634 Enable Debug Agent to support source debugging on AP function.
635
636 **/
637 VOID
638 EnableDebugAgent (
639 VOID
640 );
641
642 /**
643 Find the current Processor number by APIC ID.
644
645 @param[in] CpuMpData Pointer to PEI CPU MP Data
646 @param[out] ProcessorNumber Return the pocessor number found
647
648 @retval EFI_SUCCESS ProcessorNumber is found and returned.
649 @retval EFI_NOT_FOUND ProcessorNumber is not found.
650 **/
651 EFI_STATUS
652 GetProcessorNumber (
653 IN CPU_MP_DATA *CpuMpData,
654 OUT UINTN *ProcessorNumber
655 );
656
657 /**
658 This funtion will try to invoke platform specific microcode shadow logic to
659 relocate microcode update patches into memory.
660
661 @param[in] CpuMpData The pointer to CPU MP Data structure.
662
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
666 PPI/Protocol.
667 **/
668 EFI_STATUS
669 PlatformShadowMicrocode (
670 IN OUT CPU_MP_DATA *CpuMpData
671 );
672
673 #endif
674