]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.h
Fix the risk of AP stack conflict.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / MpServicesOnFrameworkMpServicesThunk / MpServicesOnFrameworkMpServicesThunk.h
1 /** @file
2 Include file for PI MP Services Protocol Thunk.
3
4 Copyright (c) 2009 - 2010, 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 Module Name:
13
14 **/
15
16 #ifndef _MP_SERVICES_ON_FRAMEWORK_MP_SERVICES_THUNK_
17 #define _MP_SERVICES_ON_FRAMEWORK_MP_SERVICES_THUNK_
18
19 #include <Protocol/MpService.h>
20 #include <Protocol/FrameworkMpService.h>
21 #include <Protocol/GenericMemoryTest.h>
22
23 #include <Library/BaseLib.h>
24 #include <Library/SynchronizationLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/UefiLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/MemoryAllocationLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/DxeServicesTableLib.h>
32 #include <Library/IoLib.h>
33 #include <Library/TimerLib.h>
34 #include <Library/DebugAgentLib.h>
35
36 #define AP_STACK_SIZE 0x8000
37 #define MAX_CPU_NUMBER 256
38
39 //
40 // Bit definition for IPI
41 //
42 #define BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT 0xC0000
43 #define SPECIFY_CPU_MODE_BIT 0x00000
44 #define TRIGGER_MODE_LEVEL_BIT 0x08000
45 #define ASSERT_BIT 0x04000
46
47 //
48 // Local APIC register definition for IPI.
49 //
50 #define APIC_REGISTER_SPURIOUS_VECTOR_OFFSET 0xF0
51 #define APIC_REGISTER_ICR_LOW_OFFSET 0x300
52 #define APIC_REGISTER_ICR_HIGH_OFFSET 0x310
53 #define APIC_REGISTER_LVT_TIMER 0x320
54 #define APIC_REGISTER_TIMER_INIT_COUNT 0x380
55 #define APIC_REGISTER_LINT0_VECTOR_OFFSET 0x350
56 #define APIC_REGISTER_LINT1_VECTOR_OFFSET 0x360
57 #define APIC_REGISTER_TIMER_COUNT 0x390
58 #define APIC_REGISTER_TIMER_DIVIDE 0x3E0
59
60 //
61 // Definition for MSR address
62 //
63 #define MSR_IA32_TIME_STAMP_COUNTER 0x10
64 #define MSR_IA32_APIC_BASE 0x1B
65
66 typedef struct {
67 UINTN Lock;
68 VOID *StackStart;
69 UINTN StackSize;
70 VOID *ApFunction;
71 IA32_DESCRIPTOR GdtrProfile;
72 IA32_DESCRIPTOR IdtrProfile;
73 UINT32 BufferStart;
74 UINT32 Cr3;
75 UINT32 ProcessorNumber[MAX_CPU_NUMBER];
76 } MP_CPU_EXCHANGE_INFO;
77
78 typedef struct {
79 UINT8 *RendezvousFunnelAddress;
80 UINTN PModeEntryOffset;
81 UINTN FlatJumpOffset;
82 UINTN LModeEntryOffset;
83 UINTN LongJumpOffset;
84 UINTN Size;
85 } MP_ASSEMBLY_ADDRESS_MAP;
86
87 typedef enum {
88 CpuStateIdle,
89 CpuStateReady,
90 CpuStateBusy,
91 CpuStateFinished,
92 CpuStateDisabled
93 } CPU_STATE;
94
95 //
96 // Define Individual Processor Data block.
97 //
98 typedef struct {
99 EFI_AP_PROCEDURE volatile Procedure;
100 VOID* volatile Parameter;
101
102 EFI_EVENT WaitEvent;
103 BOOLEAN *Finished;
104 UINT64 ExpectedTime;
105 UINT64 CurrentTime;
106 UINT64 TotalTime;
107
108 SPIN_LOCK CpuDataLock;
109 CPU_STATE volatile State;
110
111 } CPU_DATA_BLOCK;
112
113 //
114 // Define MP data block which consumes individual processor block.
115 //
116 typedef struct {
117 SPIN_LOCK APSerializeLock;
118
119 EFI_EVENT CheckAPsEvent;
120
121 UINTN FinishCount;
122 UINTN StartCount;
123
124 BOOLEAN CpuList[MAX_CPU_NUMBER];
125
126 EFI_AP_PROCEDURE Procedure;
127 VOID *ProcArguments;
128 BOOLEAN SingleThread;
129 EFI_EVENT WaitEvent;
130 UINTN **FailedCpuList;
131 UINT64 ExpectedTime;
132 UINT64 CurrentTime;
133 UINT64 TotalTime;
134
135 CPU_DATA_BLOCK CpuData[MAX_CPU_NUMBER];
136 } MP_SYSTEM_DATA;
137
138 /**
139 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
140
141 This service retrieves the number of logical processor in the platform
142 and the number of those logical processors that are enabled on this boot.
143 This service may only be called from the BSP.
144
145 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
146 @param NumberOfProcessors Pointer to the total number of logical processors in the system,
147 including the BSP and disabled APs.
148 @param NumberOfEnabledProcessors Pointer to the number of enabled logical processors that exist
149 in system, including the BSP.
150
151 @retval EFI_SUCCESS Number of logical processors and enabled logical processors retrieved..
152 @retval EFI_DEVICE_ERROR Caller processor is AP.
153 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL
154 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL
155
156 **/
157 EFI_STATUS
158 EFIAPI
159 GetNumberOfProcessors (
160 IN EFI_MP_SERVICES_PROTOCOL *This,
161 OUT UINTN *NumberOfProcessors,
162 OUT UINTN *NumberOfEnabledProcessors
163 );
164
165 /**
166 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
167
168 Gets detailed MP-related information on the requested processor at the
169 instant this call is made. This service may only be called from the BSP.
170
171 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
172 @param ProcessorNumber The handle number of processor.
173 @param ProcessorInfoBuffer A pointer to the buffer where information for the requested processor is deposited.
174
175 @retval EFI_SUCCESS Processor information successfully returned.
176 @retval EFI_DEVICE_ERROR Caller processor is AP.
177 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL
178 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 GetProcessorInfo (
184 IN EFI_MP_SERVICES_PROTOCOL *This,
185 IN UINTN ProcessorNumber,
186 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
187 );
188
189 /**
190 Implementation of StartupAllAPs() service of MP Services Protocol.
191
192 This service lets the caller get all enabled APs to execute a caller-provided function.
193 This service may only be called from the BSP.
194
195 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
196 @param Procedure A pointer to the function to be run on enabled APs of the system.
197 @param SingleThread Indicates whether to execute the function simultaneously or one by one..
198 @param WaitEvent The event created by the caller.
199 If it is NULL, then execute in blocking mode.
200 If it is not NULL, then execute in non-blocking mode.
201 @param TimeoutInMicroSeconds The time limit in microseconds for this AP to finish the function.
202 Zero means infinity.
203 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
204 @param FailedCpuList The list of processor numbers that fail to finish the function before
205 TimeoutInMicrosecsond expires.
206
207 @retval EFI_SUCCESS In blocking mode, all APs have finished before the timeout expired.
208 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to all enabled APs.
209 @retval EFI_DEVICE_ERROR Caller processor is AP.
210 @retval EFI_NOT_STARTED No enabled AP exists in the system.
211 @retval EFI_NOT_READY Any enabled AP is busy.
212 @retval EFI_TIMEOUT In blocking mode, The timeout expired before all enabled APs have finished.
213 @retval EFI_INVALID_PARAMETER Procedure is NULL.
214
215 **/
216 EFI_STATUS
217 EFIAPI
218 StartupAllAPs (
219 IN EFI_MP_SERVICES_PROTOCOL *This,
220 IN EFI_AP_PROCEDURE Procedure,
221 IN BOOLEAN SingleThread,
222 IN EFI_EVENT WaitEvent OPTIONAL,
223 IN UINTN TimeoutInMicroSeconds,
224 IN VOID *ProcedureArgument OPTIONAL,
225 OUT UINTN **FailedCpuList OPTIONAL
226 );
227
228 /**
229 Implementation of StartupThisAP() service of MP Services Protocol.
230
231 This service lets the caller get one enabled AP to execute a caller-provided function.
232 This service may only be called from the BSP.
233
234 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
235 @param Procedure A pointer to the function to be run on the designated AP.
236 @param ProcessorNumber The handle number of AP..
237 @param WaitEvent The event created by the caller.
238 If it is NULL, then execute in blocking mode.
239 If it is not NULL, then execute in non-blocking mode.
240 @param TimeoutInMicroseconds The time limit in microseconds for this AP to finish the function.
241 Zero means infinity.
242 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
243 @param Finished Indicates whether AP has finished assigned function.
244 In blocking mode, it is ignored.
245
246 @retval EFI_SUCCESS In blocking mode, specified AP has finished before the timeout expires.
247 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to specified AP.
248 @retval EFI_DEVICE_ERROR Caller processor is AP.
249 @retval EFI_TIMEOUT In blocking mode, the timeout expires before specified AP has finished.
250 @retval EFI_NOT_READY Specified AP is busy.
251 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
252 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
253 @retval EFI_INVALID_PARAMETER Procedure is NULL.
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 StartupThisAP (
259 IN EFI_MP_SERVICES_PROTOCOL *This,
260 IN EFI_AP_PROCEDURE Procedure,
261 IN UINTN ProcessorNumber,
262 IN EFI_EVENT WaitEvent OPTIONAL,
263 IN UINTN TimeoutInMicroseconds,
264 IN VOID *ProcedureArgument OPTIONAL,
265 OUT BOOLEAN *Finished OPTIONAL
266 );
267
268 /**
269 Implementation of SwitchBSP() service of MP Services Protocol.
270
271 This service switches the requested AP to be the BSP from that point onward.
272 This service may only be called from the current BSP.
273
274 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
275 @param ProcessorNumber The handle number of processor.
276 @param EnableOldBSP Whether to enable or disable the original BSP.
277
278 @retval EFI_SUCCESS BSP successfully switched.
279 @retval EFI_DEVICE_ERROR Caller processor is AP.
280 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
281 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
282 @retval EFI_NOT_READY Specified AP is busy.
283
284 **/
285 EFI_STATUS
286 EFIAPI
287 SwitchBSP (
288 IN EFI_MP_SERVICES_PROTOCOL *This,
289 IN UINTN ProcessorNumber,
290 IN BOOLEAN EnableOldBSP
291 );
292
293 /**
294 Implementation of EnableDisableAP() service of MP Services Protocol.
295
296 This service lets the caller enable or disable an AP.
297 This service may only be called from the BSP.
298
299 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
300 @param ProcessorNumber The handle number of processor.
301 @param EnableAP Indicates whether the newstate of the AP is enabled or disabled.
302 @param HealthFlag Indicates new health state of the AP..
303
304 @retval EFI_SUCCESS AP successfully enabled or disabled.
305 @retval EFI_DEVICE_ERROR Caller processor is AP.
306 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
307 @retval EFI_INVALID_PARAMETERS ProcessorNumber specifies the BSP.
308
309 **/
310 EFI_STATUS
311 EFIAPI
312 EnableDisableAP (
313 IN EFI_MP_SERVICES_PROTOCOL *This,
314 IN UINTN ProcessorNumber,
315 IN BOOLEAN EnableAP,
316 IN UINT32 *HealthFlag OPTIONAL
317 );
318
319 /**
320 Implementation of WhoAmI() service of MP Services Protocol.
321
322 This service lets the caller processor get its handle number.
323 This service may be called from the BSP and APs.
324
325 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
326 @param ProcessorNumber Pointer to the handle number of AP.
327
328 @retval EFI_SUCCESS Processor number successfully returned.
329 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL
330
331 **/
332 EFI_STATUS
333 EFIAPI
334 WhoAmI (
335 IN EFI_MP_SERVICES_PROTOCOL *This,
336 OUT UINTN *ProcessorNumber
337 );
338
339 /**
340 Checks APs' status periodically.
341
342 This function is triggerred by timer perodically to check the
343 state of APs for StartupAllAPs() and StartupThisAP() executed
344 in non-blocking mode.
345
346 @param Event Event triggered.
347 @param Context Parameter passed with the event.
348
349 **/
350 VOID
351 EFIAPI
352 CheckAPsStatus (
353 IN EFI_EVENT Event,
354 IN VOID *Context
355 );
356
357 /**
358 Checks status of all APs.
359
360 This function checks whether all APs have finished task assigned by StartupAllAPs(),
361 and whether timeout expires.
362
363 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
364 @retval EFI_TIMEOUT The timeout expires.
365 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
366
367 **/
368 EFI_STATUS
369 CheckAllAPs (
370 VOID
371 );
372
373 /**
374 Checks status of specified AP.
375
376 This function checks whether specified AP has finished task assigned by StartupThisAP(),
377 and whether timeout expires.
378
379 @param ProcessorNumber The handle number of processor.
380
381 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
382 @retval EFI_TIMEOUT The timeout expires.
383 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
384
385 **/
386 EFI_STATUS
387 CheckThisAP (
388 UINTN ProcessorNumber
389 );
390
391 /**
392 Calculate timeout value and return the current performance counter value.
393
394 Calculate the number of performance counter ticks required for a timeout.
395 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
396 as infinity.
397
398 @param TimeoutInMicroseconds Timeout value in microseconds.
399 @param CurrentTime Returns the current value of the performance counter.
400
401 @return Expected timestamp counter for timeout.
402 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
403 as infinity.
404
405 **/
406 UINT64
407 CalculateTimeout (
408 IN UINTN TimeoutInMicroseconds,
409 OUT UINT64 *CurrentTime
410 );
411
412 /**
413 Checks whether timeout expires.
414
415 Check whether the number of ellapsed performance counter ticks required for a timeout condition
416 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.
417
418 @param PreviousTime On input, the value of the performance counter when it was last read.
419 On output, the current value of the performance counter
420 @param TotalTime The total amount of ellapsed time in performance counter ticks.
421 @param Timeout The number of performance counter ticks required to reach a timeout condition.
422
423 @retval TRUE A timeout condition has been reached.
424 @retval FALSE A timeout condition has not been reached.
425
426 **/
427 BOOLEAN
428 CheckTimeout (
429 IN OUT UINT64 *PreviousTime,
430 IN UINT64 *TotalTime,
431 IN UINT64 Timeout
432 );
433
434 /**
435 Searches for the next waiting AP.
436
437 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
438
439 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.
440
441 @retval EFI_SUCCESS The next waiting AP has been found.
442 @retval EFI_NOT_FOUND No waiting AP exists.
443
444 **/
445 EFI_STATUS
446 GetNextWaitingProcessorNumber (
447 OUT UINTN *NextProcessorNumber
448 );
449
450 /**
451 Wrapper function for all procedures assigned to AP.
452
453 Wrapper function for all procedures assigned to AP via MP service protocol.
454 It controls states of AP and invokes assigned precedure.
455
456 **/
457 VOID
458 ApProcWrapper (
459 VOID
460 );
461
462 /**
463 Function to wake up a specified AP and assign procedure to it.
464
465 @param ProcessorNumber Handle number of the specified processor.
466 @param Procedure Procedure to assign.
467 @param ProcArguments Argument for Procedure.
468
469 **/
470 VOID
471 WakeUpAp (
472 IN UINTN ProcessorNumber,
473 IN EFI_AP_PROCEDURE Procedure,
474 IN VOID *ProcArguments
475 );
476
477 /**
478 Terminate AP's task and set it to idle state.
479
480 This function terminates AP's task due to timeout by sending INIT-SIPI,
481 and sends it to idle state.
482
483 @param ProcessorNumber Handle number of the specified processor.
484
485 **/
486 VOID
487 ResetProcessorToIdleState (
488 UINTN ProcessorNumber
489 );
490
491 /**
492 Worker function of EnableDisableAP ()
493
494 Worker function of EnableDisableAP (). Changes state of specified processor.
495
496 @param ProcessorNumber Processor number of specified AP.
497 @param NewState Desired state of the specified AP.
498
499 @retval EFI_SUCCESS AP's state successfully changed.
500
501 **/
502 EFI_STATUS
503 ChangeCpuState (
504 IN UINTN ProcessorNumber,
505 IN BOOLEAN NewState
506 );
507
508 /**
509 Gets the processor number of BSP.
510
511 @return The processor number of BSP.
512
513 **/
514 UINTN
515 GetBspNumber (
516 VOID
517 );
518
519 /**
520 Get address map of RendezvousFunnelProc.
521
522 This function gets address map of RendezvousFunnelProc.
523
524 @param AddressMap Output buffer for address map information
525
526 **/
527 VOID
528 AsmGetAddressMap (
529 OUT MP_ASSEMBLY_ADDRESS_MAP *AddressMap
530 );
531
532 #endif