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