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