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