UefiCpuPkg/MpLib: Add GDTR, IDTR and TR in saved AP data
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.h
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 Common header file for MP Initialize Library.\r
3\r
c934a0a5 4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
3e8ad6bd
JF
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
b8b04307
JF
38#define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')\r
39\r
93ca4c0f
JF
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
41be0da5
JF
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
9ebcf0f4
JF
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
e59f8f6b
JF
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
03a1a925
JF
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
68cb9330
JF
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
e9415e48
JW
105 IA32_DESCRIPTOR Gdtr;\r
106 IA32_DESCRIPTOR Idtr;\r
107 UINT16 Tr;\r
68cb9330
JF
108} CPU_VOLATILE_REGISTERS;\r
109\r
e59f8f6b
JF
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
e59f8f6b 118 BOOLEAN CpuHealthy;\r
03a1a925 119 volatile CPU_STATE State;\r
68cb9330 120 CPU_VOLATILE_REGISTERS VolatileRegisters;\r
e59f8f6b
JF
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
dd3fa0cd 135#pragma pack (1)\r
e59f8f6b
JF
136typedef struct {\r
137 UINT32 InitialApicId;\r
138 UINT32 ApicId;\r
139 UINT32 Health;\r
dd3fa0cd 140 UINT64 ApTopOfStack;\r
e59f8f6b 141} CPU_INFO_IN_HOB;\r
dd3fa0cd 142#pragma pack ()\r
e59f8f6b 143\r
f7f85d83
JF
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} MP_ASSEMBLY_ADDRESS_MAP;\r
3e8ad6bd 156\r
e59f8f6b
JF
157typedef struct _CPU_MP_DATA CPU_MP_DATA;\r
158\r
d94e5f67
JF
159#pragma pack(1)\r
160\r
161//\r
162// MP CPU exchange information for AP reset code\r
163// This structure is required to be packed because fixed field offsets\r
164// into this structure are used in assembly code in this module\r
165//\r
166typedef struct {\r
167 UINTN Lock;\r
168 UINTN StackStart;\r
169 UINTN StackSize;\r
170 UINTN CFunction;\r
171 IA32_DESCRIPTOR GdtrProfile;\r
172 IA32_DESCRIPTOR IdtrProfile;\r
173 UINTN BufferStart;\r
174 UINTN ModeOffset;\r
37676b9f 175 UINTN ApIndex;\r
d94e5f67
JF
176 UINTN CodeSegment;\r
177 UINTN DataSegment;\r
5c66d125 178 UINTN EnableExecuteDisable;\r
d94e5f67 179 UINTN Cr3;\r
46d4b885
JF
180 UINTN InitFlag;\r
181 CPU_INFO_IN_HOB *CpuInfo;\r
0594ec41 182 UINTN NumApsExecuting;\r
e59f8f6b 183 CPU_MP_DATA *CpuMpData;\r
3b2928b4 184 UINTN InitializeFloatingPointUnitsAddress;\r
d94e5f67
JF
185} MP_CPU_EXCHANGE_INFO;\r
186\r
187#pragma pack()\r
e59f8f6b
JF
188\r
189//\r
190// CPU MP Data save in memory\r
191//\r
192struct _CPU_MP_DATA {\r
193 UINT64 CpuInfoInHob;\r
194 UINT32 CpuCount;\r
195 UINT32 BspNumber;\r
196 //\r
197 // The above fields data will be passed from PEI to DXE\r
198 // Please make sure the fields offset same in the different\r
199 // architecture.\r
200 //\r
201 SPIN_LOCK MpLock;\r
202 UINTN Buffer;\r
203 UINTN CpuApStackSize;\r
204 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
205 UINTN WakeupBuffer;\r
206 UINTN BackupBuffer;\r
207 UINTN BackupBufferSize;\r
e59f8f6b
JF
208\r
209 volatile UINT32 StartCount;\r
210 volatile UINT32 FinishedCount;\r
211 volatile UINT32 RunningCount;\r
212 BOOLEAN SingleThread;\r
213 EFI_AP_PROCEDURE Procedure;\r
214 VOID *ProcArguments;\r
215 BOOLEAN *Finished;\r
216 UINT64 ExpectedTime;\r
217 UINT64 CurrentTime;\r
218 UINT64 TotalTime;\r
219 EFI_EVENT WaitEvent;\r
220 UINTN **FailedCpuList;\r
221\r
222 AP_INIT_STATE InitFlag;\r
223 BOOLEAN X2ApicEnable;\r
41be0da5 224 BOOLEAN SwitchBspFlag;\r
b3775af2 225 UINTN NewBspNumber;\r
41be0da5
JF
226 CPU_EXCHANGE_ROLE_INFO BSPInfo;\r
227 CPU_EXCHANGE_ROLE_INFO APInfo;\r
e59f8f6b
JF
228 MTRR_SETTINGS MtrrTable;\r
229 UINT8 ApLoopMode;\r
230 UINT8 ApTargetCState;\r
231 UINT16 PmCodeSegment;\r
232 CPU_AP_DATA *CpuData;\r
233 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;\r
ffab2442
JF
234\r
235 UINT32 CurrentTimerCount;\r
236 UINTN DivideValue;\r
237 UINT8 Vector;\r
238 BOOLEAN PeriodicMode;\r
239 BOOLEAN TimerInterruptState;\r
1e3f7a37
ED
240 UINT64 MicrocodePatchAddress;\r
241 UINT64 MicrocodePatchRegionSize;\r
e59f8f6b 242};\r
93ca4c0f
JF
243\r
244extern EFI_GUID mCpuInitMpLibHobGuid;\r
245\r
76157021
JF
246/**\r
247 Assembly code to place AP into safe loop mode.\r
248\r
249 Place AP into targeted C-State if MONITOR is supported, otherwise\r
250 place AP into hlt state.\r
251 Place AP in protected mode if the current is long mode. Due to AP maybe\r
252 wakeup by some hardware event. It could avoid accessing page table that\r
253 may not available during booting to OS.\r
254\r
255 @param[in] MwaitSupport TRUE indicates MONITOR is supported.\r
256 FALSE indicates MONITOR is not supported.\r
257 @param[in] ApTargetCState Target C-State value.\r
258 @param[in] PmCodeSegment Protected mode code segment value.\r
259**/\r
260typedef\r
261VOID\r
262(EFIAPI * ASM_RELOCATE_AP_LOOP) (\r
263 IN BOOLEAN MwaitSupport,\r
264 IN UINTN ApTargetCState,\r
bf2786dc 265 IN UINTN PmCodeSegment,\r
9f91cb01
JF
266 IN UINTN TopOfApStack,\r
267 IN UINTN NumberToFinish\r
76157021 268 );\r
f7f85d83
JF
269\r
270/**\r
271 Assembly code to get starting address and size of the rendezvous entry for APs.\r
272 Information for fixing a jump instruction in the code is also returned.\r
273\r
274 @param[out] AddressMap Output buffer for address map information.\r
275**/\r
276VOID\r
277EFIAPI\r
278AsmGetAddressMap (\r
279 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap\r
280 );\r
281\r
41be0da5
JF
282/**\r
283 This function is called by both the BSP and the AP which is to become the BSP to\r
284 Exchange execution context including stack between them. After return from this\r
285 function, the BSP becomes AP and the AP becomes the BSP.\r
286\r
287 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.\r
288 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.\r
289\r
290**/\r
291VOID\r
292EFIAPI\r
293AsmExchangeRole (\r
294 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,\r
295 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo\r
296 );\r
297\r
93ca4c0f
JF
298/**\r
299 Get the pointer to CPU MP Data structure.\r
300\r
301 @return The pointer to CPU MP Data structure.\r
302**/\r
303CPU_MP_DATA *\r
304GetCpuMpData (\r
305 VOID\r
306 );\r
307\r
308/**\r
309 Save the pointer to CPU MP Data structure.\r
310\r
311 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
312**/\r
313VOID\r
314SaveCpuMpData (\r
315 IN CPU_MP_DATA *CpuMpData\r
316 );\r
317\r
ed66e0e3
JF
318\r
319/**\r
a6b3d753 320 Get available system memory below 1MB by specified size.\r
ed66e0e3 321\r
a6b3d753
SZ
322 @param[in] WakeupBufferSize Wakeup buffer size required\r
323\r
324 @retval other Return wakeup buffer address below 1MB.\r
325 @retval -1 Cannot find free memory below 1MB.\r
ed66e0e3 326**/\r
a6b3d753
SZ
327UINTN\r
328GetWakeupBuffer (\r
329 IN UINTN WakeupBufferSize\r
ed66e0e3
JF
330 );\r
331\r
96f5920d
JF
332/**\r
333 This function will be called by BSP to wakeup AP.\r
334\r
335 @param[in] CpuMpData Pointer to CPU MP Data\r
336 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
337 FALSE: Send IPI to AP by ApicId\r
338 @param[in] ProcessorNumber The handle number of specified processor\r
339 @param[in] Procedure The function to be invoked by AP\r
340 @param[in] ProcedureArgument The argument to be passed into AP function\r
341**/\r
342VOID\r
343WakeUpAP (\r
344 IN CPU_MP_DATA *CpuMpData,\r
345 IN BOOLEAN Broadcast,\r
346 IN UINTN ProcessorNumber,\r
347 IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
348 IN VOID *ProcedureArgument OPTIONAL\r
349 );\r
350\r
93ca4c0f
JF
351/**\r
352 Initialize global data for MP support.\r
353\r
354 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
355**/\r
356VOID\r
357InitMpGlobalData (\r
358 IN CPU_MP_DATA *CpuMpData\r
359 );\r
360\r
86efe976
JF
361/**\r
362 Worker function to execute a caller provided function on all enabled APs.\r
363\r
364 @param[in] Procedure A pointer to the function to be run on\r
365 enabled APs of the system.\r
366 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
367 the function specified by Procedure one by\r
368 one, in ascending order of processor handle\r
369 number. If FALSE, then all the enabled APs\r
370 execute the function specified by Procedure\r
371 simultaneously.\r
372 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
373 service.\r
367284e7 374 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
86efe976
JF
375 APs to return from Procedure, either for\r
376 blocking or non-blocking mode.\r
377 @param[in] ProcedureArgument The parameter passed into Procedure for\r
378 all APs.\r
379 @param[out] FailedCpuList If all APs finish successfully, then its\r
380 content is set to NULL. If not all APs\r
381 finish before timeout expires, then its\r
382 content is set to address of the buffer\r
383 holding handle numbers of the failed APs.\r
384\r
385 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
386 the timeout expired.\r
387 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
388 to all enabled APs.\r
389 @retval others Failed to Startup all APs.\r
390\r
391**/\r
392EFI_STATUS\r
393StartupAllAPsWorker (\r
394 IN EFI_AP_PROCEDURE Procedure,\r
395 IN BOOLEAN SingleThread,\r
396 IN EFI_EVENT WaitEvent OPTIONAL,\r
397 IN UINTN TimeoutInMicroseconds,\r
398 IN VOID *ProcedureArgument OPTIONAL,\r
399 OUT UINTN **FailedCpuList OPTIONAL\r
400 );\r
401\r
20ae5774
JF
402/**\r
403 Worker function to let the caller get one enabled AP to execute a caller-provided\r
404 function.\r
405\r
406 @param[in] Procedure A pointer to the function to be run on\r
407 enabled APs of the system.\r
408 @param[in] ProcessorNumber The handle number of the AP.\r
409 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
410 service.\r
367284e7 411 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
20ae5774
JF
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] Finished If AP returns from Procedure before the\r
417 timeout expires, its content is set to TRUE.\r
418 Otherwise, the value is set to FALSE.\r
419\r
420 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
421 the timeout expires.\r
422 @retval others Failed to Startup AP.\r
423\r
424**/\r
425EFI_STATUS\r
426StartupThisAPWorker (\r
427 IN EFI_AP_PROCEDURE Procedure,\r
428 IN UINTN ProcessorNumber,\r
429 IN EFI_EVENT WaitEvent OPTIONAL,\r
430 IN UINTN TimeoutInMicroseconds,\r
431 IN VOID *ProcedureArgument OPTIONAL,\r
432 OUT BOOLEAN *Finished OPTIONAL\r
433 );\r
434\r
41be0da5
JF
435/**\r
436 Worker function to switch the requested AP to be the BSP from that point onward.\r
437\r
438 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
439 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
440 enabled AP. Otherwise, it will be disabled.\r
441\r
442 @retval EFI_SUCCESS BSP successfully switched.\r
443 @retval others Failed to switch BSP. \r
444\r
445**/\r
446EFI_STATUS\r
447SwitchBSPWorker (\r
448 IN UINTN ProcessorNumber,\r
449 IN BOOLEAN EnableOldBSP\r
450 );\r
451\r
e37109bc
JF
452/**\r
453 Worker function to let the caller enable or disable an AP from this point onward.\r
454 This service may only be called from the BSP.\r
455\r
456 @param[in] ProcessorNumber The handle number of AP.\r
457 @param[in] EnableAP Specifies the new state for the processor for\r
458 enabled, FALSE for disabled.\r
459 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
460 the new health status of the AP.\r
461\r
462 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
463 @retval others Failed to Enable/Disable AP.\r
464\r
465**/\r
466EFI_STATUS\r
467EnableDisableApWorker (\r
468 IN UINTN ProcessorNumber,\r
469 IN BOOLEAN EnableAP,\r
470 IN UINT32 *HealthFlag OPTIONAL\r
471 );\r
472\r
93ca4c0f
JF
473/**\r
474 Get pointer to CPU MP Data structure from GUIDed HOB.\r
475\r
476 @return The pointer to CPU MP Data structure.\r
477**/\r
478CPU_MP_DATA *\r
479GetCpuMpDataFromGuidedHob (\r
480 VOID\r
481 );\r
08085f08
JF
482\r
483/** Checks status of specified AP.\r
484\r
485 This function checks whether the specified AP has finished the task assigned\r
486 by StartupThisAP(), and whether timeout expires.\r
487\r
488 @param[in] ProcessorNumber The handle number of processor.\r
489\r
490 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
491 @retval EFI_TIMEOUT The timeout expires.\r
492 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
493**/\r
494EFI_STATUS\r
495CheckThisAP (\r
496 IN UINTN ProcessorNumber\r
497 );\r
498\r
499/**\r
500 Checks status of all APs.\r
501\r
502 This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
503 and whether timeout expires.\r
504\r
505 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
506 @retval EFI_TIMEOUT The timeout expires.\r
507 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
508**/\r
509EFI_STATUS\r
510CheckAllAPs (\r
511 VOID\r
512 );\r
513\r
514/**\r
515 Checks APs status and updates APs status if needed.\r
516\r
517**/\r
518VOID\r
519CheckAndUpdateApsStatus (\r
520 VOID\r
521 );\r
522\r
94f63c76
JF
523/**\r
524 Detect whether specified processor can find matching microcode patch and load it.\r
525\r
b31c1ad1 526 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
94f63c76
JF
527**/\r
528VOID\r
529MicrocodeDetect (\r
530 IN CPU_MP_DATA *CpuMpData\r
531 );\r
532\r
4d3314f6
JF
533/**\r
534 Detect whether Mwait-monitor feature is supported.\r
535\r
536 @retval TRUE Mwait-monitor feature is supported.\r
537 @retval FALSE Mwait-monitor feature is not supported.\r
538**/\r
539BOOLEAN\r
540IsMwaitSupport (\r
541 VOID\r
542 );\r
543\r
43c9fdcc
JF
544/**\r
545 Enable Debug Agent to support source debugging on AP function.\r
546\r
547**/\r
548VOID\r
549EnableDebugAgent (\r
550 VOID\r
551 );\r
552\r
3e8ad6bd
JF
553#endif\r
554\r