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