]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.h
f73a469ae84f5831fb2d1931a3a83412eb149f16
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.h
1 /** @file
2 Common header file for MP Initialize Library.
3
4 Copyright (c) 2016, 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 typedef enum {
85 CpuStateIdle,
86 CpuStateReady,
87 CpuStateBusy,
88 CpuStateFinished,
89 CpuStateDisabled
90 } CPU_STATE;
91
92 //
93 // CPU volatile registers around INIT-SIPI-SIPI
94 //
95 typedef struct {
96 UINTN Cr0;
97 UINTN Cr3;
98 UINTN Cr4;
99 UINTN Dr0;
100 UINTN Dr1;
101 UINTN Dr2;
102 UINTN Dr3;
103 UINTN Dr6;
104 UINTN Dr7;
105 } CPU_VOLATILE_REGISTERS;
106
107 //
108 // AP related data
109 //
110 typedef struct {
111 SPIN_LOCK ApLock;
112 volatile UINT32 *StartupApSignal;
113 volatile UINTN ApFunction;
114 volatile UINTN ApFunctionArgument;
115 BOOLEAN CpuHealthy;
116 volatile CPU_STATE State;
117 CPU_VOLATILE_REGISTERS VolatileRegisters;
118 BOOLEAN Waiting;
119 BOOLEAN *Finished;
120 UINT64 ExpectedTime;
121 UINT64 CurrentTime;
122 UINT64 TotalTime;
123 EFI_EVENT WaitEvent;
124 } CPU_AP_DATA;
125
126 //
127 // Basic CPU information saved in Guided HOB.
128 // Because the contents will be shard between PEI and DXE,
129 // we need to make sure the each fields offset same in different
130 // architecture.
131 //
132 #pragma pack (1)
133 typedef struct {
134 UINT32 InitialApicId;
135 UINT32 ApicId;
136 UINT32 Health;
137 UINT64 ApTopOfStack;
138 } CPU_INFO_IN_HOB;
139 #pragma pack ()
140
141 //
142 // AP reset code information including code address and size,
143 // this structure will be shared be C code and assembly code.
144 // It is natural aligned by design.
145 //
146 typedef struct {
147 UINT8 *RendezvousFunnelAddress;
148 UINTN ModeEntryOffset;
149 UINTN RendezvousFunnelSize;
150 UINT8 *RelocateApLoopFuncAddress;
151 UINTN RelocateApLoopFuncSize;
152 } MP_ASSEMBLY_ADDRESS_MAP;
153
154 typedef struct _CPU_MP_DATA CPU_MP_DATA;
155
156 #pragma pack(1)
157
158 //
159 // MP CPU exchange information for AP reset code
160 // This structure is required to be packed because fixed field offsets
161 // into this structure are used in assembly code in this module
162 //
163 typedef struct {
164 UINTN Lock;
165 UINTN StackStart;
166 UINTN StackSize;
167 UINTN CFunction;
168 IA32_DESCRIPTOR GdtrProfile;
169 IA32_DESCRIPTOR IdtrProfile;
170 UINTN BufferStart;
171 UINTN ModeOffset;
172 UINTN NumApsExecuting;
173 UINTN CodeSegment;
174 UINTN DataSegment;
175 UINTN EnableExecuteDisable;
176 UINTN Cr3;
177 UINTN InitFlag;
178 CPU_INFO_IN_HOB *CpuInfo;
179 CPU_MP_DATA *CpuMpData;
180 } MP_CPU_EXCHANGE_INFO;
181
182 #pragma pack()
183
184 //
185 // CPU MP Data save in memory
186 //
187 struct _CPU_MP_DATA {
188 UINT64 CpuInfoInHob;
189 UINT32 CpuCount;
190 UINT32 BspNumber;
191 //
192 // The above fields data will be passed from PEI to DXE
193 // Please make sure the fields offset same in the different
194 // architecture.
195 //
196 SPIN_LOCK MpLock;
197 UINTN Buffer;
198 UINTN CpuApStackSize;
199 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
200 UINTN WakeupBuffer;
201 UINTN BackupBuffer;
202 UINTN BackupBufferSize;
203 BOOLEAN SaveRestoreFlag;
204
205 volatile UINT32 StartCount;
206 volatile UINT32 FinishedCount;
207 volatile UINT32 RunningCount;
208 BOOLEAN SingleThread;
209 EFI_AP_PROCEDURE Procedure;
210 VOID *ProcArguments;
211 BOOLEAN *Finished;
212 UINT64 ExpectedTime;
213 UINT64 CurrentTime;
214 UINT64 TotalTime;
215 EFI_EVENT WaitEvent;
216 UINTN **FailedCpuList;
217
218 AP_INIT_STATE InitFlag;
219 BOOLEAN X2ApicEnable;
220 BOOLEAN SwitchBspFlag;
221 UINTN NewBspNumber;
222 CPU_EXCHANGE_ROLE_INFO BSPInfo;
223 CPU_EXCHANGE_ROLE_INFO APInfo;
224 MTRR_SETTINGS MtrrTable;
225 UINT8 ApLoopMode;
226 UINT8 ApTargetCState;
227 UINT16 PmCodeSegment;
228 CPU_AP_DATA *CpuData;
229 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;
230 };
231
232 extern EFI_GUID mCpuInitMpLibHobGuid;
233
234 /**
235 Assembly code to place AP into safe loop mode.
236
237 Place AP into targeted C-State if MONITOR is supported, otherwise
238 place AP into hlt state.
239 Place AP in protected mode if the current is long mode. Due to AP maybe
240 wakeup by some hardware event. It could avoid accessing page table that
241 may not available during booting to OS.
242
243 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
244 FALSE indicates MONITOR is not supported.
245 @param[in] ApTargetCState Target C-State value.
246 @param[in] PmCodeSegment Protected mode code segment value.
247 **/
248 typedef
249 VOID
250 (EFIAPI * ASM_RELOCATE_AP_LOOP) (
251 IN BOOLEAN MwaitSupport,
252 IN UINTN ApTargetCState,
253 IN UINTN PmCodeSegment
254 );
255
256 /**
257 Assembly code to get starting address and size of the rendezvous entry for APs.
258 Information for fixing a jump instruction in the code is also returned.
259
260 @param[out] AddressMap Output buffer for address map information.
261 **/
262 VOID
263 EFIAPI
264 AsmGetAddressMap (
265 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap
266 );
267
268 /**
269 This function is called by both the BSP and the AP which is to become the BSP to
270 Exchange execution context including stack between them. After return from this
271 function, the BSP becomes AP and the AP becomes the BSP.
272
273 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
274 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
275
276 **/
277 VOID
278 EFIAPI
279 AsmExchangeRole (
280 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,
281 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo
282 );
283
284 /**
285 Get the pointer to CPU MP Data structure.
286
287 @return The pointer to CPU MP Data structure.
288 **/
289 CPU_MP_DATA *
290 GetCpuMpData (
291 VOID
292 );
293
294 /**
295 Save the pointer to CPU MP Data structure.
296
297 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
298 **/
299 VOID
300 SaveCpuMpData (
301 IN CPU_MP_DATA *CpuMpData
302 );
303
304 /**
305 Allocate reset vector buffer.
306
307 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
308 **/
309 VOID
310 AllocateResetVector (
311 IN OUT CPU_MP_DATA *CpuMpData
312 );
313
314 /**
315 Free AP reset vector buffer.
316
317 @param[in] CpuMpData The pointer to CPU MP Data structure.
318 **/
319 VOID
320 FreeResetVector (
321 IN CPU_MP_DATA *CpuMpData
322 );
323
324 /**
325 This function will be called by BSP to wakeup AP.
326
327 @param[in] CpuMpData Pointer to CPU MP Data
328 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
329 FALSE: Send IPI to AP by ApicId
330 @param[in] ProcessorNumber The handle number of specified processor
331 @param[in] Procedure The function to be invoked by AP
332 @param[in] ProcedureArgument The argument to be passed into AP function
333 **/
334 VOID
335 WakeUpAP (
336 IN CPU_MP_DATA *CpuMpData,
337 IN BOOLEAN Broadcast,
338 IN UINTN ProcessorNumber,
339 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
340 IN VOID *ProcedureArgument OPTIONAL
341 );
342
343 /**
344 Initialize global data for MP support.
345
346 @param[in] CpuMpData The pointer to CPU MP Data structure.
347 **/
348 VOID
349 InitMpGlobalData (
350 IN CPU_MP_DATA *CpuMpData
351 );
352
353 /**
354 Worker function to execute a caller provided function on all enabled APs.
355
356 @param[in] Procedure A pointer to the function to be run on
357 enabled APs of the system.
358 @param[in] SingleThread If TRUE, then all the enabled APs execute
359 the function specified by Procedure one by
360 one, in ascending order of processor handle
361 number. If FALSE, then all the enabled APs
362 execute the function specified by Procedure
363 simultaneously.
364 @param[in] WaitEvent The event created by the caller with CreateEvent()
365 service.
366 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
367 APs to return from Procedure, either for
368 blocking or non-blocking mode.
369 @param[in] ProcedureArgument The parameter passed into Procedure for
370 all APs.
371 @param[out] FailedCpuList If all APs finish successfully, then its
372 content is set to NULL. If not all APs
373 finish before timeout expires, then its
374 content is set to address of the buffer
375 holding handle numbers of the failed APs.
376
377 @retval EFI_SUCCESS In blocking mode, all APs have finished before
378 the timeout expired.
379 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
380 to all enabled APs.
381 @retval others Failed to Startup all APs.
382
383 **/
384 EFI_STATUS
385 StartupAllAPsWorker (
386 IN EFI_AP_PROCEDURE Procedure,
387 IN BOOLEAN SingleThread,
388 IN EFI_EVENT WaitEvent OPTIONAL,
389 IN UINTN TimeoutInMicroseconds,
390 IN VOID *ProcedureArgument OPTIONAL,
391 OUT UINTN **FailedCpuList OPTIONAL
392 );
393
394 /**
395 Worker function to let the caller get one enabled AP to execute a caller-provided
396 function.
397
398 @param[in] Procedure A pointer to the function to be run on
399 enabled APs of the system.
400 @param[in] ProcessorNumber The handle number of the AP.
401 @param[in] WaitEvent The event created by the caller with CreateEvent()
402 service.
403 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
404 APs to return from Procedure, either for
405 blocking or non-blocking mode.
406 @param[in] ProcedureArgument The parameter passed into Procedure for
407 all APs.
408 @param[out] Finished If AP returns from Procedure before the
409 timeout expires, its content is set to TRUE.
410 Otherwise, the value is set to FALSE.
411
412 @retval EFI_SUCCESS In blocking mode, specified AP finished before
413 the timeout expires.
414 @retval others Failed to Startup AP.
415
416 **/
417 EFI_STATUS
418 StartupThisAPWorker (
419 IN EFI_AP_PROCEDURE Procedure,
420 IN UINTN ProcessorNumber,
421 IN EFI_EVENT WaitEvent OPTIONAL,
422 IN UINTN TimeoutInMicroseconds,
423 IN VOID *ProcedureArgument OPTIONAL,
424 OUT BOOLEAN *Finished OPTIONAL
425 );
426
427 /**
428 Worker function to switch the requested AP to be the BSP from that point onward.
429
430 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
431 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
432 enabled AP. Otherwise, it will be disabled.
433
434 @retval EFI_SUCCESS BSP successfully switched.
435 @retval others Failed to switch BSP.
436
437 **/
438 EFI_STATUS
439 SwitchBSPWorker (
440 IN UINTN ProcessorNumber,
441 IN BOOLEAN EnableOldBSP
442 );
443
444 /**
445 Worker function to let the caller enable or disable an AP from this point onward.
446 This service may only be called from the BSP.
447
448 @param[in] ProcessorNumber The handle number of AP.
449 @param[in] EnableAP Specifies the new state for the processor for
450 enabled, FALSE for disabled.
451 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
452 the new health status of the AP.
453
454 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
455 @retval others Failed to Enable/Disable AP.
456
457 **/
458 EFI_STATUS
459 EnableDisableApWorker (
460 IN UINTN ProcessorNumber,
461 IN BOOLEAN EnableAP,
462 IN UINT32 *HealthFlag OPTIONAL
463 );
464
465 /**
466 Get pointer to CPU MP Data structure from GUIDed HOB.
467
468 @return The pointer to CPU MP Data structure.
469 **/
470 CPU_MP_DATA *
471 GetCpuMpDataFromGuidedHob (
472 VOID
473 );
474
475 /** Checks status of specified AP.
476
477 This function checks whether the specified AP has finished the task assigned
478 by StartupThisAP(), and whether timeout expires.
479
480 @param[in] ProcessorNumber The handle number of processor.
481
482 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
483 @retval EFI_TIMEOUT The timeout expires.
484 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
485 **/
486 EFI_STATUS
487 CheckThisAP (
488 IN UINTN ProcessorNumber
489 );
490
491 /**
492 Checks status of all APs.
493
494 This function checks whether all APs have finished task assigned by StartupAllAPs(),
495 and whether timeout expires.
496
497 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
498 @retval EFI_TIMEOUT The timeout expires.
499 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
500 **/
501 EFI_STATUS
502 CheckAllAPs (
503 VOID
504 );
505
506 /**
507 Checks APs status and updates APs status if needed.
508
509 **/
510 VOID
511 CheckAndUpdateApsStatus (
512 VOID
513 );
514
515 /**
516 Detect whether specified processor can find matching microcode patch and load it.
517
518 @param[in] CpuMpData The pointer to CPU MP Data structure.
519 **/
520 VOID
521 MicrocodeDetect (
522 IN CPU_MP_DATA *CpuMpData
523 );
524
525 /**
526 Detect whether Mwait-monitor feature is supported.
527
528 @retval TRUE Mwait-monitor feature is supported.
529 @retval FALSE Mwait-monitor feature is not supported.
530 **/
531 BOOLEAN
532 IsMwaitSupport (
533 VOID
534 );
535
536 /**
537 Notify function on End Of PEI PPI.
538
539 On S3 boot, this function will restore wakeup buffer data.
540 On normal boot, this function will flag wakeup buffer to be un-used type.
541
542 @param[in] PeiServices The pointer to the PEI Services Table.
543 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
544 @param[in] Ppi Address of the PPI that was installed.
545
546 @retval EFI_SUCCESS When everything is OK.
547 **/
548 EFI_STATUS
549 EFIAPI
550 CpuMpEndOfPeiCallback (
551 IN EFI_PEI_SERVICES **PeiServices,
552 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
553 IN VOID *Ppi
554 );
555
556 /**
557 Get available system memory below 1MB by specified size.
558
559 @param[in] CpuMpData The pointer to CPU MP Data structure.
560 **/
561 VOID
562 BackupAndPrepareWakeupBuffer(
563 IN CPU_MP_DATA *CpuMpData
564 );
565
566 /**
567 Restore wakeup buffer data.
568
569 @param[in] CpuMpData The pointer to CPU MP Data structure.
570 **/
571 VOID
572 RestoreWakeupBuffer(
573 IN CPU_MP_DATA *CpuMpData
574 );
575
576 #endif
577