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