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