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