]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.h
UefiCpuPkg/MpInitLib: Implementation of MpInitLibSwitchBSP()
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.h
1 /** @file
2 Common header file for MP Initialize Library.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #ifndef _MP_LIB_H_
16 #define _MP_LIB_H_
17
18 #include <PiPei.h>
19
20 #include <Register/Cpuid.h>
21 #include <Register/Msr.h>
22 #include <Register/LocalApic.h>
23 #include <Register/Microcode.h>
24
25 #include <Library/MpInitLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/LocalApicLib.h>
31 #include <Library/CpuLib.h>
32 #include <Library/UefiCpuLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/SynchronizationLib.h>
35 #include <Library/MtrrLib.h>
36 #include <Library/HobLib.h>
37
38 #define WAKEUP_AP_SIGNAL SIGNATURE_32 ('S', 'T', 'A', 'P')
39
40 #define CPU_INIT_MP_LIB_HOB_GUID \
41 { \
42 0x58eb6a19, 0x3699, 0x4c68, { 0xa8, 0x36, 0xda, 0xcd, 0x8e, 0xdc, 0xad, 0x4a } \
43 }
44
45 //
46 // The MP data for switch BSP
47 //
48 #define CPU_SWITCH_STATE_IDLE 0
49 #define CPU_SWITCH_STATE_STORED 1
50 #define CPU_SWITCH_STATE_LOADED 2
51
52 //
53 // CPU exchange information for switch BSP
54 //
55 typedef struct {
56 UINT8 State; // offset 0
57 UINTN StackPointer; // offset 4 / 8
58 IA32_DESCRIPTOR Gdtr; // offset 8 / 16
59 IA32_DESCRIPTOR Idtr; // offset 14 / 26
60 } CPU_EXCHANGE_ROLE_INFO;
61
62 //
63 // AP loop state when APs are in idle state
64 // It's value is the same with PcdCpuApLoopMode
65 //
66 typedef enum {
67 ApInHltLoop = 1,
68 ApInMwaitLoop = 2,
69 ApInRunLoop = 3
70 } AP_LOOP_MODE;
71
72 //
73 // AP initialization state during APs wakeup
74 //
75 typedef enum {
76 ApInitConfig = 1,
77 ApInitReconfig = 2,
78 ApInitDone = 3
79 } AP_INIT_STATE;
80
81 //
82 // AP state
83 //
84 typedef enum {
85 CpuStateIdle,
86 CpuStateReady,
87 CpuStateBusy,
88 CpuStateFinished,
89 CpuStateDisabled
90 } CPU_STATE;
91
92 //
93 // CPU volatile registers around INIT-SIPI-SIPI
94 //
95 typedef struct {
96 UINTN Cr0;
97 UINTN Cr3;
98 UINTN Cr4;
99 UINTN Dr0;
100 UINTN Dr1;
101 UINTN Dr2;
102 UINTN Dr3;
103 UINTN Dr6;
104 UINTN Dr7;
105 } CPU_VOLATILE_REGISTERS;
106
107 //
108 // AP related data
109 //
110 typedef struct {
111 SPIN_LOCK ApLock;
112 volatile UINT32 *StartupApSignal;
113 volatile UINTN ApFunction;
114 volatile UINTN ApFunctionArgument;
115 UINT32 InitialApicId;
116 UINT32 ApicId;
117 UINT32 Health;
118 BOOLEAN CpuHealthy;
119 volatile CPU_STATE State;
120 CPU_VOLATILE_REGISTERS VolatileRegisters;
121 BOOLEAN Waiting;
122 BOOLEAN *Finished;
123 UINT64 ExpectedTime;
124 UINT64 CurrentTime;
125 UINT64 TotalTime;
126 EFI_EVENT WaitEvent;
127 } CPU_AP_DATA;
128
129 //
130 // Basic CPU information saved in Guided HOB.
131 // Because the contents will be shard between PEI and DXE,
132 // we need to make sure the each fields offset same in different
133 // architecture.
134 //
135 typedef struct {
136 UINT32 InitialApicId;
137 UINT32 ApicId;
138 UINT32 Health;
139 } CPU_INFO_IN_HOB;
140
141 //
142 // AP reset code information including code address and size,
143 // this structure will be shared be C code and assembly code.
144 // It is natural aligned by design.
145 //
146 typedef struct {
147 UINT8 *RendezvousFunnelAddress;
148 UINTN ModeEntryOffset;
149 UINTN RendezvousFunnelSize;
150 UINT8 *RelocateApLoopFuncAddress;
151 UINTN RelocateApLoopFuncSize;
152 } MP_ASSEMBLY_ADDRESS_MAP;
153
154 typedef struct _CPU_MP_DATA CPU_MP_DATA;
155
156 #pragma pack(1)
157
158 //
159 // MP CPU exchange information for AP reset code
160 // This structure is required to be packed because fixed field offsets
161 // into this structure are used in assembly code in this module
162 //
163 typedef struct {
164 UINTN Lock;
165 UINTN StackStart;
166 UINTN StackSize;
167 UINTN CFunction;
168 IA32_DESCRIPTOR GdtrProfile;
169 IA32_DESCRIPTOR IdtrProfile;
170 UINTN BufferStart;
171 UINTN ModeOffset;
172 UINTN NumApsExecuting;
173 UINTN CodeSegment;
174 UINTN DataSegment;
175 UINTN EnableExecuteDisable;
176 UINTN Cr3;
177 CPU_MP_DATA *CpuMpData;
178 } MP_CPU_EXCHANGE_INFO;
179
180 #pragma pack()
181
182 //
183 // CPU MP Data save in memory
184 //
185 struct _CPU_MP_DATA {
186 UINT64 CpuInfoInHob;
187 UINT32 CpuCount;
188 UINT32 BspNumber;
189 //
190 // The above fields data will be passed from PEI to DXE
191 // Please make sure the fields offset same in the different
192 // architecture.
193 //
194 SPIN_LOCK MpLock;
195 UINTN Buffer;
196 UINTN CpuApStackSize;
197 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
198 UINTN WakeupBuffer;
199 UINTN BackupBuffer;
200 UINTN BackupBufferSize;
201 BOOLEAN EndOfPeiFlag;
202
203 volatile UINT32 StartCount;
204 volatile UINT32 FinishedCount;
205 volatile UINT32 RunningCount;
206 BOOLEAN SingleThread;
207 EFI_AP_PROCEDURE Procedure;
208 VOID *ProcArguments;
209 BOOLEAN *Finished;
210 UINT64 ExpectedTime;
211 UINT64 CurrentTime;
212 UINT64 TotalTime;
213 EFI_EVENT WaitEvent;
214 UINTN **FailedCpuList;
215
216 AP_INIT_STATE InitFlag;
217 BOOLEAN X2ApicEnable;
218 BOOLEAN SwitchBspFlag;
219 CPU_EXCHANGE_ROLE_INFO BSPInfo;
220 CPU_EXCHANGE_ROLE_INFO APInfo;
221 MTRR_SETTINGS MtrrTable;
222 UINT8 ApLoopMode;
223 UINT8 ApTargetCState;
224 UINT16 PmCodeSegment;
225 CPU_AP_DATA *CpuData;
226 volatile MP_CPU_EXCHANGE_INFO *MpCpuExchangeInfo;
227 };
228
229 extern EFI_GUID mCpuInitMpLibHobGuid;
230
231 /**
232 Assembly code to place AP into safe loop mode.
233
234 Place AP into targeted C-State if MONITOR is supported, otherwise
235 place AP into hlt state.
236 Place AP in protected mode if the current is long mode. Due to AP maybe
237 wakeup by some hardware event. It could avoid accessing page table that
238 may not available during booting to OS.
239
240 @param[in] MwaitSupport TRUE indicates MONITOR is supported.
241 FALSE indicates MONITOR is not supported.
242 @param[in] ApTargetCState Target C-State value.
243 @param[in] PmCodeSegment Protected mode code segment value.
244 **/
245 typedef
246 VOID
247 (EFIAPI * ASM_RELOCATE_AP_LOOP) (
248 IN BOOLEAN MwaitSupport,
249 IN UINTN ApTargetCState,
250 IN UINTN PmCodeSegment
251 );
252
253 /**
254 Assembly code to get starting address and size of the rendezvous entry for APs.
255 Information for fixing a jump instruction in the code is also returned.
256
257 @param[out] AddressMap Output buffer for address map information.
258 **/
259 VOID
260 EFIAPI
261 AsmGetAddressMap (
262 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap
263 );
264
265 /**
266 This function is called by both the BSP and the AP which is to become the BSP to
267 Exchange execution context including stack between them. After return from this
268 function, the BSP becomes AP and the AP becomes the BSP.
269
270 @param[in] MyInfo Pointer to buffer holding the exchanging information for the executing processor.
271 @param[in] OthersInfo Pointer to buffer holding the exchanging information for the peer.
272
273 **/
274 VOID
275 EFIAPI
276 AsmExchangeRole (
277 IN CPU_EXCHANGE_ROLE_INFO *MyInfo,
278 IN CPU_EXCHANGE_ROLE_INFO *OthersInfo
279 );
280
281 /**
282 Get the pointer to CPU MP Data structure.
283
284 @return The pointer to CPU MP Data structure.
285 **/
286 CPU_MP_DATA *
287 GetCpuMpData (
288 VOID
289 );
290
291 /**
292 Save the pointer to CPU MP Data structure.
293
294 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
295 **/
296 VOID
297 SaveCpuMpData (
298 IN CPU_MP_DATA *CpuMpData
299 );
300
301 /**
302 Allocate reset vector buffer.
303
304 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
305 **/
306 VOID
307 AllocateResetVector (
308 IN OUT CPU_MP_DATA *CpuMpData
309 );
310
311 /**
312 Free AP reset vector buffer.
313
314 @param[in] CpuMpData The pointer to CPU MP Data structure.
315 **/
316 VOID
317 FreeResetVector (
318 IN CPU_MP_DATA *CpuMpData
319 );
320
321 /**
322 This function will be called by BSP to wakeup AP.
323
324 @param[in] CpuMpData Pointer to CPU MP Data
325 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
326 FALSE: Send IPI to AP by ApicId
327 @param[in] ProcessorNumber The handle number of specified processor
328 @param[in] Procedure The function to be invoked by AP
329 @param[in] ProcedureArgument The argument to be passed into AP function
330 **/
331 VOID
332 WakeUpAP (
333 IN CPU_MP_DATA *CpuMpData,
334 IN BOOLEAN Broadcast,
335 IN UINTN ProcessorNumber,
336 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
337 IN VOID *ProcedureArgument OPTIONAL
338 );
339
340 /**
341 Initialize global data for MP support.
342
343 @param[in] CpuMpData The pointer to CPU MP Data structure.
344 **/
345 VOID
346 InitMpGlobalData (
347 IN CPU_MP_DATA *CpuMpData
348 );
349
350 /**
351 Worker function to switch the requested AP to be the BSP from that point onward.
352
353 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
354 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
355 enabled AP. Otherwise, it will be disabled.
356
357 @retval EFI_SUCCESS BSP successfully switched.
358 @retval others Failed to switch BSP.
359
360 **/
361 EFI_STATUS
362 SwitchBSPWorker (
363 IN UINTN ProcessorNumber,
364 IN BOOLEAN EnableOldBSP
365 );
366
367 /**
368 Get pointer to CPU MP Data structure from GUIDed HOB.
369
370 @return The pointer to CPU MP Data structure.
371 **/
372 CPU_MP_DATA *
373 GetCpuMpDataFromGuidedHob (
374 VOID
375 );
376
377 /**
378 Detect whether specified processor can find matching microcode patch and load it.
379
380 @param[in] PeiCpuMpData Pointer to PEI CPU MP Data
381 **/
382 VOID
383 MicrocodeDetect (
384 IN CPU_MP_DATA *CpuMpData
385 );
386
387 /**
388 Notify function on End Of PEI PPI.
389
390 On S3 boot, this function will restore wakeup buffer data.
391 On normal boot, this function will flag wakeup buffer to be un-used type.
392
393 @param[in] PeiServices The pointer to the PEI Services Table.
394 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
395 @param[in] Ppi Address of the PPI that was installed.
396
397 @retval EFI_SUCCESS When everything is OK.
398 **/
399 EFI_STATUS
400 EFIAPI
401 CpuMpEndOfPeiCallback (
402 IN EFI_PEI_SERVICES **PeiServices,
403 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
404 IN VOID *Ppi
405 );
406
407 #endif
408