]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuDxe/CpuMp.c
BaseTools: Extend support for Windows hosted gcc to versions 4.4-4.7.
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
CommitLineData
6022e28c
JJ
1/** @file\r
2 CPU DXE Module.\r
3\r
4 Copyright (c) 2008 - 2014, 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#include "CpuDxe.h"\r
16#include "CpuMp.h"\r
17\r
6a26a597
CF
18UINTN gMaxLogicalProcessorNumber;\r
19UINTN gApStackSize;\r
3f4f0af8 20UINTN gPollInterval = 100; // 100 microseconds\r
6a26a597 21\r
03673ae1 22MP_SYSTEM_DATA mMpSystemData;\r
9840b129
CF
23EFI_HANDLE mMpServiceHandle = NULL;\r
24EFI_EVENT mExitBootServicesEvent = (EFI_EVENT)NULL;\r
03673ae1 25\r
fab82c18
JJ
26VOID *mCommonStack = 0;\r
27VOID *mTopOfApCommonStack = 0;\r
6a26a597 28VOID *mApStackStart = 0;\r
fab82c18 29\r
232eb4c8 30volatile BOOLEAN mAPsAlreadyInitFinished = FALSE;\r
acb2172d
CF
31volatile BOOLEAN mStopCheckAllAPsStatus = TRUE;\r
32\r
003973d9 33EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
d894d8b7 34 GetNumberOfProcessors,\r
e7938b5a 35 GetProcessorInfo,\r
5fee172f 36 StartupAllAPs,\r
3f4f0af8 37 StartupThisAP,\r
b7c05ba5 38 SwitchBSP,\r
fa7ce675 39 EnableDisableAP,\r
cfa2fac1 40 WhoAmI\r
003973d9
CF
41};\r
42\r
d16cf36d
CF
43/**\r
44 Get Mp Service Lock.\r
45\r
46 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor\r
47\r
48**/\r
49VOID\r
50GetMpSpinLock (\r
51 IN CPU_DATA_BLOCK *CpuData\r
52 )\r
53{\r
54 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
55 CpuPause ();\r
56 }\r
0e724fc1 57 CpuData->LockSelf = GetApicId ();\r
d16cf36d
CF
58}\r
59\r
60/**\r
61 Release Mp Service Lock.\r
62\r
63 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor\r
64\r
65**/\r
66VOID\r
67ReleaseMpSpinLock (\r
68 IN CPU_DATA_BLOCK *CpuData\r
69 )\r
70{\r
71 ReleaseSpinLock (&CpuData->CpuDataLock);\r
72}\r
73\r
d894d8b7
CF
74/**\r
75 Check whether caller processor is BSP.\r
76\r
77 @retval TRUE the caller is BSP\r
78 @retval FALSE the caller is AP\r
79\r
80**/\r
81BOOLEAN\r
82IsBSP (\r
83 VOID\r
84 )\r
85{\r
86 UINTN CpuIndex;\r
87 CPU_DATA_BLOCK *CpuData;\r
88\r
89 CpuData = NULL;\r
90\r
91 WhoAmI (&mMpServicesTemplate, &CpuIndex);\r
92 CpuData = &mMpSystemData.CpuDatas[CpuIndex];\r
93\r
94 return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;\r
95}\r
96\r
fa7ce675
CF
97/**\r
98 Get the Application Processors state.\r
99\r
100 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
101\r
102 @retval CPU_STATE the AP status\r
103\r
104**/\r
105CPU_STATE\r
106GetApState (\r
107 IN CPU_DATA_BLOCK *CpuData\r
108 )\r
109{\r
110 CPU_STATE State;\r
111\r
d16cf36d 112 GetMpSpinLock (CpuData);\r
fa7ce675 113 State = CpuData->State;\r
d16cf36d 114 ReleaseMpSpinLock (CpuData);\r
fa7ce675
CF
115\r
116 return State;\r
117}\r
118\r
3f4f0af8
CF
119/**\r
120 Set the Application Processors state.\r
121\r
122 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP\r
123 @param State The AP status\r
124\r
125**/\r
126VOID\r
127SetApState (\r
128 IN CPU_DATA_BLOCK *CpuData,\r
129 IN CPU_STATE State\r
130 )\r
131{\r
d16cf36d 132 GetMpSpinLock (CpuData);\r
3f4f0af8 133 CpuData->State = State;\r
d16cf36d 134 ReleaseMpSpinLock (CpuData);\r
3f4f0af8
CF
135}\r
136\r
137/**\r
138 Set the Application Processor prepare to run a function specified\r
139 by Params.\r
140\r
141 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
142 @param Procedure A pointer to the function to be run on enabled APs of the system\r
143 @param ProcedureArgument Pointer to the optional parameter of the assigned function\r
144\r
145**/\r
146VOID\r
147SetApProcedure (\r
148 IN CPU_DATA_BLOCK *CpuData,\r
149 IN EFI_AP_PROCEDURE Procedure,\r
150 IN VOID *ProcedureArgument\r
151 )\r
152{\r
d16cf36d 153 GetMpSpinLock (CpuData);\r
3f4f0af8
CF
154 CpuData->Parameter = ProcedureArgument;\r
155 CpuData->Procedure = Procedure;\r
d16cf36d 156 ReleaseMpSpinLock (CpuData);\r
3f4f0af8
CF
157}\r
158\r
fa7ce675
CF
159/**\r
160 Check the Application Processors Status whether contains the Flags.\r
161\r
162 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
163 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
164\r
165 @retval TRUE the AP status includes the StatusFlag\r
166 @retval FALSE the AP status excludes the StatusFlag\r
167\r
168**/\r
169BOOLEAN\r
170TestCpuStatusFlag (\r
171 IN CPU_DATA_BLOCK *CpuData,\r
172 IN UINT32 Flags\r
173 )\r
174{\r
175 UINT32 Ret;\r
176\r
d16cf36d 177 GetMpSpinLock (CpuData);\r
fa7ce675 178 Ret = CpuData->Info.StatusFlag & Flags;\r
d16cf36d 179 ReleaseMpSpinLock (CpuData);\r
fa7ce675 180\r
3a5d9a36 181 return (Ret != 0);\r
fa7ce675
CF
182}\r
183\r
184/**\r
185 Bitwise-Or of the Application Processors Status with the Flags.\r
186\r
187 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
188 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
189\r
190**/\r
191VOID\r
192CpuStatusFlagOr (\r
193 IN CPU_DATA_BLOCK *CpuData,\r
194 IN UINT32 Flags\r
195 )\r
196{\r
d16cf36d 197 GetMpSpinLock (CpuData);\r
fa7ce675 198 CpuData->Info.StatusFlag |= Flags;\r
d16cf36d 199 ReleaseMpSpinLock (CpuData);\r
fa7ce675
CF
200}\r
201\r
202/**\r
203 Bitwise-AndNot of the Application Processors Status with the Flags.\r
204\r
205 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
206 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
207\r
208**/\r
209VOID\r
210CpuStatusFlagAndNot (\r
211 IN CPU_DATA_BLOCK *CpuData,\r
212 IN UINT32 Flags\r
213 )\r
214{\r
d16cf36d 215 GetMpSpinLock (CpuData);\r
fa7ce675 216 CpuData->Info.StatusFlag &= ~Flags;\r
d16cf36d 217 ReleaseMpSpinLock (CpuData);\r
fa7ce675
CF
218}\r
219\r
3f4f0af8
CF
220/**\r
221 Searches for the next blocking AP.\r
222\r
223 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().\r
224\r
225 @param NextNumber Pointer to the processor number of the next blocking AP.\r
226\r
227 @retval EFI_SUCCESS The next blocking AP has been found.\r
228 @retval EFI_NOT_FOUND No blocking AP exists.\r
229\r
230**/\r
231EFI_STATUS\r
232GetNextBlockedNumber (\r
233 OUT UINTN *NextNumber\r
234 )\r
235{\r
236 UINTN Number;\r
237 CPU_STATE CpuState;\r
238 CPU_DATA_BLOCK *CpuData;\r
239\r
240 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
241 CpuData = &mMpSystemData.CpuDatas[Number];\r
242 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
243 //\r
244 // Skip BSP\r
245 //\r
246 continue;\r
247 }\r
248\r
249 CpuState = GetApState (CpuData);\r
250 if (CpuState == CpuStateBlocked) {\r
251 *NextNumber = Number;\r
252 return EFI_SUCCESS;\r
253 }\r
254 }\r
255\r
256 return EFI_NOT_FOUND;\r
257}\r
258\r
5fee172f
CF
259/**\r
260 Check if the APs state are finished, and update them to idle state\r
261 by StartupAllAPs().\r
262\r
263**/\r
264VOID\r
265CheckAndUpdateAllAPsToIdleState (\r
266 VOID\r
267 )\r
268{\r
269 UINTN ProcessorNumber;\r
270 UINTN NextNumber;\r
271 CPU_DATA_BLOCK *CpuData;\r
272 EFI_STATUS Status;\r
273 CPU_STATE CpuState;\r
274\r
275 for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) {\r
276 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
277 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
278 //\r
279 // Skip BSP\r
280 //\r
281 continue;\r
282 }\r
283\r
284 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
285 //\r
286 // Skip Disabled processors\r
287 //\r
288 continue;\r
289 }\r
290\r
291 CpuState = GetApState (CpuData);\r
292 if (CpuState == CpuStateFinished) {\r
293 mMpSystemData.FinishCount++;\r
294 if (mMpSystemData.SingleThread) {\r
295 Status = GetNextBlockedNumber (&NextNumber);\r
296 if (!EFI_ERROR (Status)) {\r
297 SetApState (&mMpSystemData.CpuDatas[NextNumber], CpuStateReady);\r
298 SetApProcedure (&mMpSystemData.CpuDatas[NextNumber],\r
299 mMpSystemData.Procedure,\r
300 mMpSystemData.ProcedureArgument);\r
301 }\r
302 }\r
303\r
304 SetApState (CpuData, CpuStateIdle);\r
305 }\r
306 }\r
307}\r
308\r
309/**\r
310 If the timeout expires before all APs returns from Procedure,\r
311 we should forcibly terminate the executing AP and fill FailedList back\r
312 by StartupAllAPs().\r
313\r
314**/\r
315VOID\r
316ResetAllFailedAPs (\r
317 VOID\r
318 )\r
319{\r
320 CPU_DATA_BLOCK *CpuData;\r
321 UINTN Number;\r
322 CPU_STATE CpuState;\r
323\r
324 if (mMpSystemData.FailedList != NULL) {\r
325 *mMpSystemData.FailedList = AllocatePool ((mMpSystemData.StartCount - mMpSystemData.FinishCount + 1) * sizeof(UINTN));\r
326 ASSERT (*mMpSystemData.FailedList != NULL);\r
327 }\r
328\r
329 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
330 CpuData = &mMpSystemData.CpuDatas[Number];\r
331 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
332 //\r
333 // Skip BSP\r
334 //\r
335 continue;\r
336 }\r
337\r
338 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
339 //\r
340 // Skip Disabled processors\r
341 //\r
342 continue;\r
343 }\r
344\r
345 CpuState = GetApState (CpuData);\r
346 if (CpuState != CpuStateIdle) {\r
347 if (mMpSystemData.FailedList != NULL) {\r
348 (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number;\r
349 }\r
350 ResetProcessorToIdleState (CpuData);\r
351 }\r
352 }\r
353\r
354 if (mMpSystemData.FailedList != NULL) {\r
355 (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex] = END_OF_CPU_LIST;\r
356 }\r
357}\r
358\r
d894d8b7
CF
359/**\r
360 This service retrieves the number of logical processor in the platform\r
361 and the number of those logical processors that are enabled on this boot.\r
362 This service may only be called from the BSP.\r
363\r
364 This function is used to retrieve the following information:\r
365 - The number of logical processors that are present in the system.\r
366 - The number of enabled logical processors in the system at the instant\r
367 this call is made.\r
368\r
369 Because MP Service Protocol provides services to enable and disable processors\r
370 dynamically, the number of enabled logical processors may vary during the\r
371 course of a boot session.\r
372\r
373 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
374 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
375 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
376 is returned in NumberOfProcessors, the number of currently enabled processor\r
377 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
378\r
379 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
380 instance.\r
381 @param[out] NumberOfProcessors Pointer to the total number of logical\r
382 processors in the system, including the BSP\r
383 and disabled APs.\r
384 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
385 processors that exist in system, including\r
386 the BSP.\r
387\r
388 @retval EFI_SUCCESS The number of logical processors and enabled\r
389 logical processors was retrieved.\r
390 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
391 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
392 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
393\r
394**/\r
395EFI_STATUS\r
396EFIAPI\r
397GetNumberOfProcessors (\r
398 IN EFI_MP_SERVICES_PROTOCOL *This,\r
399 OUT UINTN *NumberOfProcessors,\r
400 OUT UINTN *NumberOfEnabledProcessors\r
401 )\r
402{\r
403 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
404 return EFI_INVALID_PARAMETER;\r
405 }\r
406\r
407 if (!IsBSP ()) {\r
408 return EFI_DEVICE_ERROR;\r
409 }\r
410\r
411 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;\r
412 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\r
413 return EFI_SUCCESS;\r
414}\r
415\r
e7938b5a
CF
416/**\r
417 Gets detailed MP-related information on the requested processor at the\r
418 instant this call is made. This service may only be called from the BSP.\r
419\r
420 This service retrieves detailed MP-related information about any processor\r
421 on the platform. Note the following:\r
422 - The processor information may change during the course of a boot session.\r
423 - The information presented here is entirely MP related.\r
424\r
425 Information regarding the number of caches and their sizes, frequency of operation,\r
426 slot numbers is all considered platform-related information and is not provided\r
427 by this service.\r
428\r
429 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
430 instance.\r
431 @param[in] ProcessorNumber The handle number of processor.\r
432 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
433 the requested processor is deposited.\r
434\r
435 @retval EFI_SUCCESS Processor information was returned.\r
436 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
437 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
438 @retval EFI_NOT_FOUND The processor with the handle specified by\r
439 ProcessorNumber does not exist in the platform.\r
440\r
441**/\r
442EFI_STATUS\r
443EFIAPI\r
444GetProcessorInfo (\r
445 IN EFI_MP_SERVICES_PROTOCOL *This,\r
446 IN UINTN ProcessorNumber,\r
447 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
448 )\r
449{\r
450 if (ProcessorInfoBuffer == NULL) {\r
451 return EFI_INVALID_PARAMETER;\r
452 }\r
453\r
454 if (!IsBSP ()) {\r
455 return EFI_DEVICE_ERROR;\r
456 }\r
457\r
458 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
459 return EFI_NOT_FOUND;\r
460 }\r
461\r
462 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
463 return EFI_SUCCESS;\r
464}\r
465\r
5fee172f
CF
466/**\r
467 This service executes a caller provided function on all enabled APs. APs can\r
468 run either simultaneously or one at a time in sequence. This service supports\r
469 both blocking and non-blocking requests. The non-blocking requests use EFI\r
470 events so the BSP can detect when the APs have finished. This service may only\r
471 be called from the BSP.\r
472\r
473 This function is used to dispatch all the enabled APs to the function specified\r
474 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
475 immediately and Procedure is not started on any AP.\r
476\r
477 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
478 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
479 all the enabled APs execute the function specified by Procedure simultaneously.\r
480\r
481 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all\r
482 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking\r
483 mode, and the BSP returns from this service without waiting for APs. If a\r
484 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
485 is signaled, then EFI_UNSUPPORTED must be returned.\r
486\r
487 If the timeout specified by TimeoutInMicroseconds expires before all APs return\r
488 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
489 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
490 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its\r
491 content points to the list of processor handle numbers in which Procedure was\r
492 terminated.\r
493\r
494 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
495 to make sure that the nature of the code that is executed on the BSP and the\r
496 dispatched APs is well controlled. The MP Services Protocol does not guarantee\r
497 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
498 parallel are limited to certain independent tasks and well-controlled exclusive\r
499 code. EFI services and protocols may not be called by APs unless otherwise\r
500 specified.\r
501\r
502 In blocking execution mode, BSP waits until all APs finish or\r
503 TimeoutInMicroseconds expires.\r
504\r
505 In non-blocking execution mode, BSP is freed to return to the caller and then\r
506 proceed to the next task without having to wait for APs. The following\r
507 sequence needs to occur in a non-blocking execution mode:\r
508\r
509 -# The caller that intends to use this MP Services Protocol in non-blocking\r
510 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller\r
511 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent\r
512 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests\r
513 the function specified by Procedure to be started on all the enabled APs,\r
514 and releases the BSP to continue with other tasks.\r
515 -# The caller can use the CheckEvent() and WaitForEvent() services to check\r
516 the state of the WaitEvent created in step 1.\r
517 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP\r
518 Service signals WaitEvent by calling the EFI SignalEvent() function. If\r
519 FailedCpuList is not NULL, its content is available when WaitEvent is\r
520 signaled. If all APs returned from Procedure prior to the timeout, then\r
521 FailedCpuList is set to NULL. If not all APs return from Procedure before\r
522 the timeout, then FailedCpuList is filled in with the list of the failed\r
523 APs. The buffer is allocated by MP Service Protocol using AllocatePool().\r
524 It is the caller's responsibility to free the buffer with FreePool() service.\r
525 -# This invocation of SignalEvent() function informs the caller that invoked\r
526 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed\r
527 the specified task or a timeout occurred. The contents of FailedCpuList\r
528 can be examined to determine which APs did not complete the specified task\r
529 prior to the timeout.\r
530\r
531 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
532 instance.\r
533 @param[in] Procedure A pointer to the function to be run on\r
534 enabled APs of the system. See type\r
535 EFI_AP_PROCEDURE.\r
536 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
537 the function specified by Procedure one by\r
538 one, in ascending order of processor handle\r
539 number. If FALSE, then all the enabled APs\r
540 execute the function specified by Procedure\r
541 simultaneously.\r
542 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
543 service. If it is NULL, then execute in\r
544 blocking mode. BSP waits until all APs finish\r
545 or TimeoutInMicroseconds expires. If it's\r
546 not NULL, then execute in non-blocking mode.\r
547 BSP requests the function specified by\r
548 Procedure to be started on all the enabled\r
549 APs, and go on executing immediately. If\r
550 all return from Procedure, or TimeoutInMicroseconds\r
551 expires, this event is signaled. The BSP\r
552 can use the CheckEvent() or WaitForEvent()\r
553 services to check the state of event. Type\r
554 EFI_EVENT is defined in CreateEvent() in\r
555 the Unified Extensible Firmware Interface\r
556 Specification.\r
557 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
558 APs to return from Procedure, either for\r
559 blocking or non-blocking mode. Zero means\r
560 infinity. If the timeout expires before\r
561 all APs return from Procedure, then Procedure\r
562 on the failed APs is terminated. All enabled\r
563 APs are available for next function assigned\r
564 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
565 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
566 If the timeout expires in blocking mode,\r
567 BSP returns EFI_TIMEOUT. If the timeout\r
568 expires in non-blocking mode, WaitEvent\r
569 is signaled with SignalEvent().\r
570 @param[in] ProcedureArgument The parameter passed into Procedure for\r
571 all APs.\r
572 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
573 if all APs finish successfully, then its\r
574 content is set to NULL. If not all APs\r
575 finish before timeout expires, then its\r
576 content is set to address of the buffer\r
577 holding handle numbers of the failed APs.\r
578 The buffer is allocated by MP Service Protocol,\r
579 and it's the caller's responsibility to\r
580 free the buffer with FreePool() service.\r
581 In blocking mode, it is ready for consumption\r
582 when the call returns. In non-blocking mode,\r
583 it is ready when WaitEvent is signaled. The\r
584 list of failed CPU is terminated by\r
585 END_OF_CPU_LIST.\r
586\r
587 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
588 the timeout expired.\r
589 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
590 to all enabled APs.\r
591 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
592 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
593 signaled.\r
594 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
595 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
596 @retval EFI_NOT_READY Any enabled APs are busy.\r
597 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
598 all enabled APs have finished.\r
599 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
600\r
601**/\r
602EFI_STATUS\r
603EFIAPI\r
604StartupAllAPs (\r
605 IN EFI_MP_SERVICES_PROTOCOL *This,\r
606 IN EFI_AP_PROCEDURE Procedure,\r
607 IN BOOLEAN SingleThread,\r
608 IN EFI_EVENT WaitEvent OPTIONAL,\r
609 IN UINTN TimeoutInMicroseconds,\r
610 IN VOID *ProcedureArgument OPTIONAL,\r
611 OUT UINTN **FailedCpuList OPTIONAL\r
612 )\r
613{\r
614 EFI_STATUS Status;\r
615 CPU_DATA_BLOCK *CpuData;\r
616 UINTN Number;\r
617 CPU_STATE APInitialState;\r
618\r
619 CpuData = NULL;\r
620\r
621 if (FailedCpuList != NULL) {\r
622 *FailedCpuList = NULL;\r
623 }\r
624\r
625 if (!IsBSP ()) {\r
626 return EFI_DEVICE_ERROR;\r
627 }\r
628\r
629 if (mMpSystemData.NumberOfProcessors == 1) {\r
630 return EFI_NOT_STARTED;\r
631 }\r
632\r
633 if (Procedure == NULL) {\r
634 return EFI_INVALID_PARAMETER;\r
635 }\r
636\r
cd8c700b
CF
637 //\r
638 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
639 //\r
640 mStopCheckAllAPsStatus = TRUE;\r
641\r
5fee172f
CF
642 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
643 CpuData = &mMpSystemData.CpuDatas[Number];\r
644 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
645 //\r
646 // Skip BSP\r
647 //\r
648 continue;\r
649 }\r
650\r
651 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
652 //\r
653 // Skip Disabled processors\r
654 //\r
655 continue;\r
656 }\r
657\r
658 if (GetApState (CpuData) != CpuStateIdle) {\r
659 return EFI_NOT_READY;\r
660 }\r
661 }\r
662\r
663 mMpSystemData.Procedure = Procedure;\r
664 mMpSystemData.ProcedureArgument = ProcedureArgument;\r
665 mMpSystemData.WaitEvent = WaitEvent;\r
666 mMpSystemData.Timeout = TimeoutInMicroseconds;\r
3a5d9a36 667 mMpSystemData.TimeoutActive = (TimeoutInMicroseconds != 0);\r
5fee172f
CF
668 mMpSystemData.FinishCount = 0;\r
669 mMpSystemData.StartCount = 0;\r
670 mMpSystemData.SingleThread = SingleThread;\r
671 mMpSystemData.FailedList = FailedCpuList;\r
672 mMpSystemData.FailedListIndex = 0;\r
673 APInitialState = CpuStateReady;\r
674\r
675 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
676 CpuData = &mMpSystemData.CpuDatas[Number];\r
677 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
678 //\r
679 // Skip BSP\r
680 //\r
681 continue;\r
682 }\r
683\r
684 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
685 //\r
686 // Skip Disabled processors\r
687 //\r
688 continue;\r
689 }\r
690\r
691 //\r
692 // Get APs prepared, and put failing APs into FailedCpuList\r
693 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready\r
694 // state 1 by 1, until the previous 1 finished its task\r
695 // if not "SingleThread", all APs are put to ready state from the beginning\r
696 //\r
697 if (GetApState (CpuData) == CpuStateIdle) {\r
698 mMpSystemData.StartCount++;\r
699\r
700 SetApState (CpuData, APInitialState);\r
701\r
702 if (APInitialState == CpuStateReady) {\r
703 SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
704 }\r
705\r
706 if (SingleThread) {\r
707 APInitialState = CpuStateBlocked;\r
708 }\r
709 }\r
710 }\r
711\r
acb2172d
CF
712 mStopCheckAllAPsStatus = FALSE;\r
713\r
5fee172f 714 if (WaitEvent != NULL) {\r
acb2172d
CF
715 //\r
716 // non blocking\r
717 //\r
718 return EFI_SUCCESS;\r
5fee172f
CF
719 }\r
720\r
cd8c700b
CF
721 //\r
722 // Blocking temporarily stop CheckAllAPsStatus()\r
723 //\r
724 mStopCheckAllAPsStatus = TRUE;\r
725\r
5fee172f
CF
726 while (TRUE) {\r
727 CheckAndUpdateAllAPsToIdleState ();\r
728 if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {\r
729 Status = EFI_SUCCESS;\r
730 goto Done;\r
731 }\r
732\r
733 //\r
734 // task timeout\r
735 //\r
736 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
737 ResetAllFailedAPs();\r
738 Status = EFI_TIMEOUT;\r
739 goto Done;\r
740 }\r
741\r
742 gBS->Stall (gPollInterval);\r
743 mMpSystemData.Timeout -= gPollInterval;\r
744 }\r
745\r
746Done:\r
747\r
748 return Status;\r
749}\r
750\r
3f4f0af8
CF
751/**\r
752 This service lets the caller get one enabled AP to execute a caller-provided\r
753 function. The caller can request the BSP to either wait for the completion\r
754 of the AP or just proceed with the next task by using the EFI event mechanism.\r
755 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
756 execution support. This service may only be called from the BSP.\r
757\r
758 This function is used to dispatch one enabled AP to the function specified by\r
759 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
760 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
761 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
762 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
763 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
764 then EFI_UNSUPPORTED must be returned.\r
765\r
766 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
767 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
768 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
769 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
770\r
771 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
772 instance.\r
773 @param[in] Procedure A pointer to the function to be run on\r
774 enabled APs of the system. See type\r
775 EFI_AP_PROCEDURE.\r
776 @param[in] ProcessorNumber The handle number of the AP. The range is\r
777 from 0 to the total number of logical\r
778 processors minus 1. The total number of\r
779 logical processors can be retrieved by\r
780 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
781 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
782 service. If it is NULL, then execute in\r
783 blocking mode. BSP waits until all APs finish\r
784 or TimeoutInMicroseconds expires. If it's\r
785 not NULL, then execute in non-blocking mode.\r
786 BSP requests the function specified by\r
787 Procedure to be started on all the enabled\r
788 APs, and go on executing immediately. If\r
789 all return from Procedure or TimeoutInMicroseconds\r
790 expires, this event is signaled. The BSP\r
791 can use the CheckEvent() or WaitForEvent()\r
792 services to check the state of event. Type\r
793 EFI_EVENT is defined in CreateEvent() in\r
794 the Unified Extensible Firmware Interface\r
795 Specification.\r
796 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
797 APs to return from Procedure, either for\r
798 blocking or non-blocking mode. Zero means\r
799 infinity. If the timeout expires before\r
800 all APs return from Procedure, then Procedure\r
801 on the failed APs is terminated. All enabled\r
802 APs are available for next function assigned\r
803 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
804 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
805 If the timeout expires in blocking mode,\r
806 BSP returns EFI_TIMEOUT. If the timeout\r
807 expires in non-blocking mode, WaitEvent\r
808 is signaled with SignalEvent().\r
809 @param[in] ProcedureArgument The parameter passed into Procedure for\r
810 all APs.\r
811 @param[out] Finished If NULL, this parameter is ignored. In\r
812 blocking mode, this parameter is ignored.\r
813 In non-blocking mode, if AP returns from\r
814 Procedure before the timeout expires, its\r
815 content is set to TRUE. Otherwise, the\r
816 value is set to FALSE. The caller can\r
817 determine if the AP returned from Procedure\r
818 by evaluating this value.\r
819\r
820 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
821 the timeout expires.\r
822 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
823 dispatched to specified AP.\r
824 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
825 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
826 signaled.\r
827 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
828 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
829 the specified AP has finished.\r
830 @retval EFI_NOT_READY The specified AP is busy.\r
831 @retval EFI_NOT_FOUND The processor with the handle specified by\r
832 ProcessorNumber does not exist.\r
833 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
834 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
835\r
836**/\r
837EFI_STATUS\r
838EFIAPI\r
839StartupThisAP (\r
840 IN EFI_MP_SERVICES_PROTOCOL *This,\r
841 IN EFI_AP_PROCEDURE Procedure,\r
842 IN UINTN ProcessorNumber,\r
843 IN EFI_EVENT WaitEvent OPTIONAL,\r
844 IN UINTN TimeoutInMicroseconds,\r
845 IN VOID *ProcedureArgument OPTIONAL,\r
846 OUT BOOLEAN *Finished OPTIONAL\r
847 )\r
848{\r
849 CPU_DATA_BLOCK *CpuData;\r
3f4f0af8
CF
850\r
851 CpuData = NULL;\r
852\r
853 if (Finished != NULL) {\r
854 *Finished = FALSE;\r
855 }\r
856\r
857 if (!IsBSP ()) {\r
858 return EFI_DEVICE_ERROR;\r
859 }\r
860\r
861 if (Procedure == NULL) {\r
862 return EFI_INVALID_PARAMETER;\r
863 }\r
864\r
865 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
866 return EFI_NOT_FOUND;\r
867 }\r
868\r
cd8c700b
CF
869 //\r
870 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
871 //\r
872 mStopCheckAllAPsStatus = TRUE;\r
873\r
3f4f0af8
CF
874 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
875 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
876 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
877 return EFI_INVALID_PARAMETER;\r
878 }\r
879\r
880 if (GetApState (CpuData) != CpuStateIdle) {\r
881 return EFI_NOT_READY;\r
882 }\r
883\r
884 SetApState (CpuData, CpuStateReady);\r
885\r
886 SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
887\r
888 CpuData->Timeout = TimeoutInMicroseconds;\r
889 CpuData->WaitEvent = WaitEvent;\r
3a5d9a36 890 CpuData->TimeoutActive = (TimeoutInMicroseconds != 0);\r
3f4f0af8
CF
891 CpuData->Finished = Finished;\r
892\r
acb2172d
CF
893 mStopCheckAllAPsStatus = FALSE;\r
894\r
3f4f0af8
CF
895 if (WaitEvent != NULL) {\r
896 //\r
897 // Non Blocking\r
898 //\r
acb2172d 899 return EFI_SUCCESS;\r
3f4f0af8
CF
900 }\r
901\r
902 //\r
903 // Blocking\r
904 //\r
905 while (TRUE) {\r
906 if (GetApState (CpuData) == CpuStateFinished) {\r
907 SetApState (CpuData, CpuStateIdle);\r
908 break;\r
909 }\r
910\r
911 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
912 ResetProcessorToIdleState (CpuData);\r
913 return EFI_TIMEOUT;\r
914 }\r
915\r
916 gBS->Stall (gPollInterval);\r
917 CpuData->Timeout -= gPollInterval;\r
918 }\r
919\r
920 return EFI_SUCCESS;\r
921}\r
922\r
b7c05ba5
CF
923/**\r
924 This service switches the requested AP to be the BSP from that point onward.\r
925 This service changes the BSP for all purposes. This call can only be performed\r
926 by the current BSP.\r
927\r
928 This service switches the requested AP to be the BSP from that point onward.\r
929 This service changes the BSP for all purposes. The new BSP can take over the\r
930 execution of the old BSP and continue seamlessly from where the old one left\r
931 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
932 is signaled.\r
933\r
934 If the BSP cannot be switched prior to the return from this service, then\r
935 EFI_UNSUPPORTED must be returned.\r
936\r
937 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
938 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
939 BSP. The range is from 0 to the total number of\r
940 logical processors minus 1. The total number of\r
941 logical processors can be retrieved by\r
942 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
943 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
944 enabled AP. Otherwise, it will be disabled.\r
945\r
946 @retval EFI_SUCCESS BSP successfully switched.\r
947 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
948 this service returning.\r
949 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
950 @retval EFI_SUCCESS The calling processor is an AP.\r
951 @retval EFI_NOT_FOUND The processor with the handle specified by\r
952 ProcessorNumber does not exist.\r
953 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
954 a disabled AP.\r
955 @retval EFI_NOT_READY The specified AP is busy.\r
956\r
957**/\r
958EFI_STATUS\r
959EFIAPI\r
960SwitchBSP (\r
961 IN EFI_MP_SERVICES_PROTOCOL *This,\r
962 IN UINTN ProcessorNumber,\r
963 IN BOOLEAN EnableOldBSP\r
964 )\r
965{\r
966 //\r
967 // Current always return unsupported.\r
968 //\r
969 return EFI_UNSUPPORTED;\r
970}\r
971\r
fa7ce675
CF
972/**\r
973 This service lets the caller enable or disable an AP from this point onward.\r
974 This service may only be called from the BSP.\r
975\r
976 This service allows the caller enable or disable an AP from this point onward.\r
977 The caller can optionally specify the health status of the AP by Health. If\r
978 an AP is being disabled, then the state of the disabled AP is implementation\r
979 dependent. If an AP is enabled, then the implementation must guarantee that a\r
980 complete initialization sequence is performed on the AP, so the AP is in a state\r
981 that is compatible with an MP operating system. This service may not be supported\r
982 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
983\r
984 If the enable or disable AP operation cannot be completed prior to the return\r
985 from this service, then EFI_UNSUPPORTED must be returned.\r
986\r
987 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
988 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
989 BSP. The range is from 0 to the total number of\r
990 logical processors minus 1. The total number of\r
991 logical processors can be retrieved by\r
992 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
993 @param[in] EnableAP Specifies the new state for the processor for\r
994 enabled, FALSE for disabled.\r
995 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
996 the new health status of the AP. This flag\r
997 corresponds to StatusFlag defined in\r
998 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
999 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
1000 bits are ignored. If it is NULL, this parameter\r
1001 is ignored.\r
1002\r
1003 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1004 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
1005 prior to this service returning.\r
1006 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
1007 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1008 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
1009 does not exist.\r
1010 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
1011\r
1012**/\r
1013EFI_STATUS\r
1014EFIAPI\r
1015EnableDisableAP (\r
1016 IN EFI_MP_SERVICES_PROTOCOL *This,\r
1017 IN UINTN ProcessorNumber,\r
1018 IN BOOLEAN EnableAP,\r
1019 IN UINT32 *HealthFlag OPTIONAL\r
1020 )\r
1021{\r
1022 CPU_DATA_BLOCK *CpuData;\r
cd8c700b
CF
1023 BOOLEAN TempStopCheckState;\r
1024\r
1025 CpuData = NULL;\r
1026 TempStopCheckState = FALSE;\r
fa7ce675
CF
1027\r
1028 if (!IsBSP ()) {\r
1029 return EFI_DEVICE_ERROR;\r
1030 }\r
1031\r
1032 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
1033 return EFI_NOT_FOUND;\r
1034 }\r
1035\r
cd8c700b
CF
1036 //\r
1037 // temporarily stop checkAllAPsStatus for initialize parameters.\r
1038 //\r
1039 if (!mStopCheckAllAPsStatus) {\r
1040 mStopCheckAllAPsStatus = TRUE;\r
1041 TempStopCheckState = TRUE;\r
1042 }\r
1043\r
fa7ce675
CF
1044 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1045 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
1046 return EFI_INVALID_PARAMETER;\r
1047 }\r
1048\r
1049 if (GetApState (CpuData) != CpuStateIdle) {\r
1050 return EFI_UNSUPPORTED;\r
1051 }\r
1052\r
1053 if (EnableAP) {\r
1054 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
1055 mMpSystemData.NumberOfEnabledProcessors++;\r
1056 }\r
1057 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
1058 } else {\r
1059 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
1060 mMpSystemData.NumberOfEnabledProcessors--;\r
1061 }\r
1062 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
1063 }\r
1064\r
1065 if (HealthFlag != NULL) {\r
1066 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
1067 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
1068 }\r
1069\r
cd8c700b
CF
1070 if (TempStopCheckState) {\r
1071 mStopCheckAllAPsStatus = FALSE;\r
1072 }\r
1073\r
fa7ce675
CF
1074 return EFI_SUCCESS;\r
1075}\r
1076\r
cfa2fac1
CF
1077/**\r
1078 This return the handle number for the calling processor. This service may be\r
1079 called from the BSP and APs.\r
1080\r
1081 This service returns the processor handle number for the calling processor.\r
1082 The returned value is in the range from 0 to the total number of logical\r
1083 processors minus 1. The total number of logical processors can be retrieved\r
1084 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
1085 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
1086 is returned. Otherwise, the current processors handle number is returned in\r
1087 ProcessorNumber, and EFI_SUCCESS is returned.\r
1088\r
1089 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
1090 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
1091 BSP. The range is from 0 to the total number of\r
1092 logical processors minus 1. The total number of\r
1093 logical processors can be retrieved by\r
1094 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
1095\r
1096 @retval EFI_SUCCESS The current processor handle number was returned\r
1097 in ProcessorNumber.\r
1098 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1099\r
1100**/\r
1101EFI_STATUS\r
1102EFIAPI\r
1103WhoAmI (\r
1104 IN EFI_MP_SERVICES_PROTOCOL *This,\r
1105 OUT UINTN *ProcessorNumber\r
1106 )\r
1107{\r
1108 UINTN Index;\r
1109 UINT32 ProcessorId;\r
1110\r
1111 if (ProcessorNumber == NULL) {\r
1112 return EFI_INVALID_PARAMETER;\r
1113 }\r
1114\r
1115 ProcessorId = GetApicId ();\r
1116 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
1117 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
1118 break;\r
1119 }\r
1120 }\r
1121\r
1122 *ProcessorNumber = Index;\r
1123 return EFI_SUCCESS;\r
1124}\r
1125\r
3f4f0af8
CF
1126/**\r
1127 Terminate AP's task and set it to idle state.\r
1128\r
1129 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1130 and sends it to idle state.\r
1131\r
1132 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
1133\r
1134**/\r
1135VOID\r
1136ResetProcessorToIdleState (\r
1137 IN CPU_DATA_BLOCK *CpuData\r
1138 )\r
1139{\r
ac9dbb3b 1140 ResetApStackless ((UINT32)CpuData->Info.ProcessorId);\r
3f4f0af8
CF
1141}\r
1142\r
e343f8f7
CF
1143/**\r
1144 Application Processors do loop routine\r
1145 after switch to its own stack.\r
1146\r
1147 @param Context1 A pointer to the context to pass into the function.\r
1148 @param Context2 A pointer to the context to pass into the function.\r
1149\r
1150**/\r
1151VOID\r
1152ProcessorToIdleState (\r
1153 IN VOID *Context1, OPTIONAL\r
1154 IN VOID *Context2 OPTIONAL\r
1155 )\r
1156{\r
9908a829
CF
1157 UINTN ProcessorNumber;\r
1158 CPU_DATA_BLOCK *CpuData;\r
1159 EFI_AP_PROCEDURE Procedure;\r
1160 VOID *ProcedureArgument;\r
1161\r
232eb4c8
CF
1162 AsmApDoneWithCommonStack ();\r
1163\r
1164 while (!mAPsAlreadyInitFinished) {\r
1165 CpuPause ();\r
1166 }\r
1167\r
9908a829
CF
1168 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
1169 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
e343f8f7 1170\r
0e724fc1
CF
1171 //\r
1172 // Avoid forcibly reset AP caused the AP got lock not release.\r
1173 //\r
1174 if (CpuData->LockSelf == (INTN) GetApicId ()) {\r
1175 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1176 }\r
1177\r
ac9dbb3b
CF
1178 //\r
1179 // Avoid forcibly reset AP caused the AP State is not updated.\r
1180 //\r
1181 GetMpSpinLock (CpuData);\r
1182 CpuData->State = CpuStateIdle;\r
1183 CpuData->Procedure = NULL;\r
1184 ReleaseMpSpinLock (CpuData);\r
1185\r
9908a829 1186 while (TRUE) {\r
d16cf36d 1187 GetMpSpinLock (CpuData);\r
9908a829
CF
1188 ProcedureArgument = CpuData->Parameter;\r
1189 Procedure = CpuData->Procedure;\r
d16cf36d 1190 ReleaseMpSpinLock (CpuData);\r
9908a829
CF
1191\r
1192 if (Procedure != NULL) {\r
1193 Procedure (ProcedureArgument);\r
1194\r
d16cf36d 1195 GetMpSpinLock (CpuData);\r
9908a829 1196 CpuData->Procedure = NULL;\r
d16cf36d
CF
1197 CpuData->State = CpuStateFinished;\r
1198 ReleaseMpSpinLock (CpuData);\r
9908a829
CF
1199 }\r
1200\r
1201 CpuPause ();\r
1202 }\r
1203\r
e343f8f7
CF
1204 CpuSleep ();\r
1205 CpuDeadLoop ();\r
1206}\r
1207\r
3f4f0af8
CF
1208/**\r
1209 Checks AP' status periodically.\r
1210\r
1211 This function is triggerred by timer perodically to check the\r
1212 state of AP forStartupThisAP() executed in non-blocking mode.\r
1213\r
1214 @param Event Event triggered.\r
1215 @param Context Parameter passed with the event.\r
1216\r
1217**/\r
1218VOID\r
1219EFIAPI\r
1220CheckThisAPStatus (\r
1221 IN EFI_EVENT Event,\r
1222 IN VOID *Context\r
1223 )\r
1224{\r
1225 CPU_DATA_BLOCK *CpuData;\r
1226 CPU_STATE CpuState;\r
1227\r
1228 CpuData = (CPU_DATA_BLOCK *) Context;\r
1229 if (CpuData->TimeoutActive) {\r
1230 CpuData->Timeout -= gPollInterval;\r
1231 }\r
1232\r
1233 CpuState = GetApState (CpuData);\r
1234\r
1235 if (CpuState == CpuStateFinished) {\r
1236 if (CpuData->Finished) {\r
1237 *CpuData->Finished = TRUE;\r
1238 }\r
1239 SetApState (CpuData, CpuStateIdle);\r
1240 goto out;\r
1241 }\r
1242\r
1243 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
1244 if (CpuState != CpuStateIdle &&\r
1245 CpuData->Finished) {\r
1246 *CpuData->Finished = FALSE;\r
1247 }\r
1248 ResetProcessorToIdleState (CpuData);\r
1249 goto out;\r
1250 }\r
1251\r
1252 return;\r
1253\r
1254out:\r
acb2172d
CF
1255 CpuData->TimeoutActive = FALSE;\r
1256 gBS->SignalEvent (CpuData->WaitEvent);\r
1257 CpuData->WaitEvent = NULL;\r
3f4f0af8
CF
1258}\r
1259\r
5fee172f
CF
1260/**\r
1261 Checks APs' status periodically.\r
1262\r
1263 This function is triggerred by timer perodically to check the\r
1264 state of APs for StartupAllAPs() executed in non-blocking mode.\r
1265\r
1266 @param Event Event triggered.\r
1267 @param Context Parameter passed with the event.\r
1268\r
1269**/\r
1270VOID\r
1271EFIAPI\r
1272CheckAllAPsStatus (\r
1273 IN EFI_EVENT Event,\r
1274 IN VOID *Context\r
1275 )\r
1276{\r
acb2172d
CF
1277 CPU_DATA_BLOCK *CpuData;\r
1278 UINTN Number;\r
e4aaf764 1279 EFI_STATUS Status;\r
acb2172d 1280\r
5fee172f
CF
1281 if (mMpSystemData.TimeoutActive) {\r
1282 mMpSystemData.Timeout -= gPollInterval;\r
1283 }\r
1284\r
acb2172d
CF
1285 if (mStopCheckAllAPsStatus) {\r
1286 return;\r
1287 }\r
5fee172f 1288\r
e4aaf764
CF
1289 //\r
1290 // avoid next timer enter.\r
1291 //\r
1292 Status = gBS->SetTimer (\r
1293 mMpSystemData.CheckAllAPsEvent,\r
1294 TimerCancel,\r
1295 0\r
1296 );\r
1297 ASSERT_EFI_ERROR (Status);\r
1298\r
acb2172d
CF
1299 if (mMpSystemData.WaitEvent != NULL) {\r
1300 CheckAndUpdateAllAPsToIdleState ();\r
5fee172f 1301 //\r
acb2172d 1302 // task timeout\r
5fee172f 1303 //\r
acb2172d
CF
1304 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
1305 ResetAllFailedAPs();\r
1306 //\r
1307 // force exit\r
1308 //\r
1309 mMpSystemData.FinishCount = mMpSystemData.StartCount;\r
1310 }\r
5fee172f 1311\r
acb2172d 1312 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {\r
e4aaf764 1313 goto EXIT;\r
acb2172d 1314 }\r
5fee172f 1315\r
acb2172d 1316 mMpSystemData.TimeoutActive = FALSE;\r
5fee172f
CF
1317 gBS->SignalEvent (mMpSystemData.WaitEvent);\r
1318 mMpSystemData.WaitEvent = NULL;\r
acb2172d 1319 mStopCheckAllAPsStatus = TRUE;\r
e4aaf764
CF
1320\r
1321 goto EXIT;\r
acb2172d
CF
1322 }\r
1323\r
1324 //\r
1325 // check each AP status for StartupThisAP\r
1326 //\r
1327 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
1328 CpuData = &mMpSystemData.CpuDatas[Number];\r
acb2172d
CF
1329 if (CpuData->WaitEvent) {\r
1330 CheckThisAPStatus (NULL, (VOID *)CpuData);\r
1331 }\r
5fee172f 1332 }\r
e4aaf764
CF
1333\r
1334EXIT:\r
1335 Status = gBS->SetTimer (\r
1336 mMpSystemData.CheckAllAPsEvent,\r
1337 TimerPeriodic,\r
1338 EFI_TIMER_PERIOD_MICROSECONDS (100)\r
1339 );\r
1340 ASSERT_EFI_ERROR (Status);\r
5fee172f
CF
1341}\r
1342\r
1535c888
JJ
1343/**\r
1344 Application Processor C code entry point.\r
1345\r
1346**/\r
1347VOID\r
1348EFIAPI\r
1349ApEntryPointInC (\r
1350 VOID\r
1351 )\r
1352{\r
ac9dbb3b
CF
1353 VOID* TopOfApStack;\r
1354 UINTN ProcessorNumber;\r
03673ae1 1355\r
ac9dbb3b
CF
1356 if (!mAPsAlreadyInitFinished) {\r
1357 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
1358 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
1359 mApStackStart = TopOfApStack;\r
03673ae1 1360\r
ac9dbb3b
CF
1361 //\r
1362 // Store the Stack address, when reset the AP, We can found the original address.\r
1363 //\r
1364 mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack;\r
1365 mMpSystemData.NumberOfProcessors++;\r
1366 mMpSystemData.NumberOfEnabledProcessors++;\r
1367 } else {\r
1368 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
1369 //\r
1370 // Get the original stack address.\r
1371 //\r
1372 TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack;\r
1373 }\r
e343f8f7
CF
1374\r
1375 SwitchStack (\r
1376 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
1377 NULL,\r
1378 NULL,\r
03673ae1
CF
1379 TopOfApStack);\r
1380}\r
1381\r
1382/**\r
1383 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
1384\r
1385 @param Bsp TRUE if the CPU is BSP\r
1386 @param ProcessorNumber The specific processor number\r
1387\r
1388 @retval EFI_SUCCESS Data for the processor collected and filled in\r
1389\r
1390**/\r
1391EFI_STATUS\r
1392FillInProcessorInformation (\r
1393 IN BOOLEAN Bsp,\r
1394 IN UINTN ProcessorNumber\r
1395 )\r
1396{\r
1397 CPU_DATA_BLOCK *CpuData;\r
1398 UINT32 ProcessorId;\r
1399\r
1400 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1401 ProcessorId = GetApicId ();\r
1402 CpuData->Info.ProcessorId = ProcessorId;\r
1403 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
1404 if (Bsp) {\r
1405 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1406 }\r
1407 CpuData->Info.Location.Package = ProcessorId;\r
1408 CpuData->Info.Location.Core = 0;\r
1409 CpuData->Info.Location.Thread = 0;\r
1410 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
1411\r
1412 CpuData->Procedure = NULL;\r
1413 CpuData->Parameter = NULL;\r
1414 InitializeSpinLock (&CpuData->CpuDataLock);\r
0e724fc1 1415 CpuData->LockSelf = -1;\r
03673ae1
CF
1416\r
1417 return EFI_SUCCESS;\r
1535c888
JJ
1418}\r
1419\r
03673ae1
CF
1420/**\r
1421 Prepare the System Data.\r
1422\r
1423 @retval EFI_SUCCESS the System Data finished initilization.\r
1424\r
1425**/\r
1426EFI_STATUS\r
1427InitMpSystemData (\r
1428 VOID\r
1429 )\r
1430{\r
3f4f0af8
CF
1431 EFI_STATUS Status;\r
1432\r
03673ae1
CF
1433 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
1434\r
1435 mMpSystemData.NumberOfProcessors = 1;\r
1436 mMpSystemData.NumberOfEnabledProcessors = 1;\r
1437\r
1438 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
1439 ASSERT(mMpSystemData.CpuDatas != NULL);\r
1440\r
5fee172f
CF
1441 Status = gBS->CreateEvent (\r
1442 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1443 TPL_CALLBACK,\r
1444 CheckAllAPsStatus,\r
1445 NULL,\r
1446 &mMpSystemData.CheckAllAPsEvent\r
1447 );\r
1448 ASSERT_EFI_ERROR (Status);\r
1449\r
acb2172d
CF
1450 //\r
1451 // Set timer to check all APs status.\r
1452 //\r
1453 Status = gBS->SetTimer (\r
1454 mMpSystemData.CheckAllAPsEvent,\r
1455 TimerPeriodic,\r
1456 EFI_TIMER_PERIOD_MICROSECONDS (100)\r
1457 );\r
1458 ASSERT_EFI_ERROR (Status);\r
3f4f0af8 1459\r
03673ae1
CF
1460 //\r
1461 // BSP\r
1462 //\r
1463 FillInProcessorInformation (TRUE, 0);\r
1464\r
1465 return EFI_SUCCESS;\r
1466}\r
1535c888 1467\r
9840b129
CF
1468/**\r
1469 Callback function for ExitBootServices.\r
1470\r
1471 @param Event Event whose notification function is being invoked.\r
1472 @param Context The pointer to the notification function's context,\r
1473 which is implementation-dependent.\r
1474\r
1475**/\r
1476VOID\r
1477EFIAPI\r
1478ExitBootServicesCallback (\r
1479 IN EFI_EVENT Event,\r
1480 IN VOID *Context\r
1481 )\r
1482{\r
1483 //\r
1484 // Avoid APs access invalid buff datas which allocated by BootServices,\r
1485 // so we send INIT IPI to APs to let them wait for SIPI state.\r
1486 //\r
1487 SendInitIpiAllExcludingSelf ();\r
1488}\r
1489\r
6022e28c
JJ
1490/**\r
1491 Initialize Multi-processor support.\r
1492\r
1493**/\r
1494VOID\r
1495InitializeMpSupport (\r
1496 VOID\r
1497 )\r
1498{\r
4a50c272
CF
1499 EFI_STATUS Status;\r
1500\r
6a26a597
CF
1501 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
1502 if (gMaxLogicalProcessorNumber < 1) {\r
1503 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
1504 return;\r
1505 }\r
1506\r
1507 if (gMaxLogicalProcessorNumber == 1) {\r
1508 return;\r
1509 }\r
1510\r
1511 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
1512 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
1513\r
1514 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1515 ASSERT (mApStackStart != NULL);\r
6022e28c 1516\r
6a26a597
CF
1517 //\r
1518 // the first buffer of stack size used for common stack, when the amount of AP\r
1519 // more than 1, we should never free the common stack which maybe used for AP reset.\r
1520 //\r
1521 mCommonStack = mApStackStart;\r
1522 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
1523 mApStackStart = mTopOfApCommonStack;\r
1524\r
03673ae1 1525 InitMpSystemData ();\r
6a26a597 1526\r
fe078dd5
CF
1527 PrepareAPStartupCode ();\r
1528\r
dee9376f
JJ
1529 StartApsStackless ();\r
1530\r
1531 DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mMpSystemData.NumberOfProcessors));\r
03673ae1 1532 if (mMpSystemData.NumberOfProcessors == 1) {\r
fe078dd5 1533 FreeApStartupCode ();\r
6a26a597
CF
1534 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1535 return;\r
1536 }\r
1537\r
232eb4c8
CF
1538 mMpSystemData.CpuDatas = ReallocatePool (\r
1539 sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber,\r
1540 sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,\r
1541 mMpSystemData.CpuDatas);\r
1542\r
ac9dbb3b
CF
1543 mAPsAlreadyInitFinished = TRUE;\r
1544\r
4a50c272
CF
1545 Status = gBS->InstallMultipleProtocolInterfaces (\r
1546 &mMpServiceHandle,\r
1547 &gEfiMpServiceProtocolGuid, &mMpServicesTemplate,\r
1548 NULL\r
1549 );\r
1550 ASSERT_EFI_ERROR (Status);\r
1551\r
03673ae1
CF
1552 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
1553 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
1554 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
1555 gApStackSize));\r
6a26a597 1556 }\r
9840b129
CF
1557\r
1558 Status = gBS->CreateEvent (\r
1559 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
1560 TPL_CALLBACK,\r
1561 ExitBootServicesCallback,\r
1562 NULL,\r
1563 &mExitBootServicesEvent\r
1564 );\r
1565 ASSERT_EFI_ERROR (Status);\r
6a26a597 1566}\r