]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.h
UefiCpuPkg/MpInitLib: Implementation of MpInitLibEnableDisableAP()
[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
115 UINT32 InitialApicId;\r
116 UINT32 ApicId;\r
117 UINT32 Health;\r
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
135typedef struct {\r
136 UINT32 InitialApicId;\r
137 UINT32 ApicId;\r
138 UINT32 Health;\r
139} CPU_INFO_IN_HOB;\r
140\r
f7f85d83
JF
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
3e8ad6bd 153\r
e59f8f6b
JF
154typedef struct _CPU_MP_DATA CPU_MP_DATA;\r
155\r
d94e5f67
JF
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
5c66d125 175 UINTN EnableExecuteDisable;\r
d94e5f67 176 UINTN Cr3;\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
201 BOOLEAN EndOfPeiFlag;\r
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
JF
218 BOOLEAN SwitchBspFlag;\r
219 CPU_EXCHANGE_ROLE_INFO BSPInfo;\r
220 CPU_EXCHANGE_ROLE_INFO APInfo;\r
e59f8f6b
JF
221 MTRR_SETTINGS MtrrTable;\r
222 UINT8 ApLoopMode;\r
223 UINT8 ApTargetCState;\r
224 UINT16 PmCodeSegment;\r
225 CPU_AP_DATA *CpuData;\r
226 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;\r
227};\r
93ca4c0f
JF
228\r
229extern EFI_GUID mCpuInitMpLibHobGuid;\r
230\r
76157021
JF
231/**\r
232 Assembly code to place AP into safe loop mode.\r
233\r
234 Place AP into targeted C-State if MONITOR is supported, otherwise\r
235 place AP into hlt state.\r
236 Place AP in protected mode if the current is long mode. Due to AP maybe\r
237 wakeup by some hardware event. It could avoid accessing page table that\r
238 may not available during booting to OS.\r
239\r
240 @param[in] MwaitSupport TRUE indicates MONITOR is supported.\r
241 FALSE indicates MONITOR is not supported.\r
242 @param[in] ApTargetCState Target C-State value.\r
243 @param[in] PmCodeSegment Protected mode code segment value.\r
244**/\r
245typedef\r
246VOID\r
247(EFIAPI * ASM_RELOCATE_AP_LOOP) (\r
248 IN BOOLEAN MwaitSupport,\r
249 IN UINTN ApTargetCState,\r
250 IN UINTN PmCodeSegment\r
251 );\r
f7f85d83
JF
252\r
253/**\r
254 Assembly code to get starting address and size of the rendezvous entry for APs.\r
255 Information for fixing a jump instruction in the code is also returned.\r
256\r
257 @param[out] AddressMap Output buffer for address map information.\r
258**/\r
259VOID\r
260EFIAPI\r
261AsmGetAddressMap (\r
262 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap\r
263 );\r
264\r
41be0da5
JF
265/**\r
266 This function is called by both the BSP and the AP which is to become the BSP to\r
267 Exchange execution context including stack between them. After return from this\r
268 function, the BSP becomes AP and the AP becomes the BSP.\r
269\r
270 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.\r
271 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.\r
272\r
273**/\r
274VOID\r
275EFIAPI\r
276AsmExchangeRole (\r
277 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,\r
278 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo\r
279 );\r
280\r
93ca4c0f
JF
281/**\r
282 Get the pointer to CPU MP Data structure.\r
283\r
284 @return The pointer to CPU MP Data structure.\r
285**/\r
286CPU_MP_DATA *\r
287GetCpuMpData (\r
288 VOID\r
289 );\r
290\r
291/**\r
292 Save the pointer to CPU MP Data structure.\r
293\r
294 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.\r
295**/\r
296VOID\r
297SaveCpuMpData (\r
298 IN CPU_MP_DATA *CpuMpData\r
299 );\r
300\r
ed66e0e3
JF
301/**\r
302 Allocate reset vector buffer.\r
303\r
304 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
305**/\r
306VOID\r
307AllocateResetVector (\r
308 IN OUT CPU_MP_DATA *CpuMpData\r
309 );\r
310\r
311/**\r
312 Free AP reset vector buffer.\r
313\r
314 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
315**/\r
316VOID\r
317FreeResetVector (\r
318 IN CPU_MP_DATA *CpuMpData\r
319 );\r
320\r
96f5920d
JF
321/**\r
322 This function will be called by BSP to wakeup AP.\r
323\r
324 @param[in] CpuMpData Pointer to CPU MP Data\r
325 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
326 FALSE: Send IPI to AP by ApicId\r
327 @param[in] ProcessorNumber The handle number of specified processor\r
328 @param[in] Procedure The function to be invoked by AP\r
329 @param[in] ProcedureArgument The argument to be passed into AP function\r
330**/\r
331VOID\r
332WakeUpAP (\r
333 IN CPU_MP_DATA *CpuMpData,\r
334 IN BOOLEAN Broadcast,\r
335 IN UINTN ProcessorNumber,\r
336 IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
337 IN VOID *ProcedureArgument OPTIONAL\r
338 );\r
339\r
93ca4c0f
JF
340/**\r
341 Initialize global data for MP support.\r
342\r
343 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
344**/\r
345VOID\r
346InitMpGlobalData (\r
347 IN CPU_MP_DATA *CpuMpData\r
348 );\r
349\r
41be0da5
JF
350/**\r
351 Worker function to switch the requested AP to be the BSP from that point onward.\r
352\r
353 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
354 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
355 enabled AP. Otherwise, it will be disabled.\r
356\r
357 @retval EFI_SUCCESS BSP successfully switched.\r
358 @retval others Failed to switch BSP. \r
359\r
360**/\r
361EFI_STATUS\r
362SwitchBSPWorker (\r
363 IN UINTN ProcessorNumber,\r
364 IN BOOLEAN EnableOldBSP\r
365 );\r
366\r
e37109bc
JF
367/**\r
368 Worker function to let the caller enable or disable an AP from this point onward.\r
369 This service may only be called from the BSP.\r
370\r
371 @param[in] ProcessorNumber The handle number of AP.\r
372 @param[in] EnableAP Specifies the new state for the processor for\r
373 enabled, FALSE for disabled.\r
374 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
375 the new health status of the AP.\r
376\r
377 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
378 @retval others Failed to Enable/Disable AP.\r
379\r
380**/\r
381EFI_STATUS\r
382EnableDisableApWorker (\r
383 IN UINTN ProcessorNumber,\r
384 IN BOOLEAN EnableAP,\r
385 IN UINT32 *HealthFlag OPTIONAL\r
386 );\r
387\r
93ca4c0f
JF
388/**\r
389 Get pointer to CPU MP Data structure from GUIDed HOB.\r
390\r
391 @return The pointer to CPU MP Data structure.\r
392**/\r
393CPU_MP_DATA *\r
394GetCpuMpDataFromGuidedHob (\r
395 VOID\r
396 );\r
397 \r
94f63c76
JF
398/**\r
399 Detect whether specified processor can find matching microcode patch and load it.\r
400\r
401 @param[in] PeiCpuMpData Pointer to PEI CPU MP Data\r
402**/\r
403VOID\r
404MicrocodeDetect (\r
405 IN CPU_MP_DATA *CpuMpData\r
406 );\r
407\r
6dc05093
JF
408/**\r
409 Notify function on End Of PEI PPI.\r
410\r
411 On S3 boot, this function will restore wakeup buffer data.\r
412 On normal boot, this function will flag wakeup buffer to be un-used type.\r
413\r
414 @param[in] PeiServices The pointer to the PEI Services Table.\r
415 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
416 @param[in] Ppi Address of the PPI that was installed.\r
417\r
418 @retval EFI_SUCCESS When everything is OK.\r
419**/\r
420EFI_STATUS\r
421EFIAPI\r
422CpuMpEndOfPeiCallback (\r
423 IN EFI_PEI_SERVICES **PeiServices,\r
424 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
425 IN VOID *Ppi\r
426 );\r
427\r
3e8ad6bd
JF
428#endif\r
429\r