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