UefiCpuPkg/CpuDxe: implement Mp Services:StartupAllAPs()
[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
003973d9 33 NULL, // 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
fa7ce675
CF
894/**\r
895 This service lets the caller enable or disable an AP from this point onward.\r
896 This service may only be called from the BSP.\r
897\r
898 This service allows the caller enable or disable an AP from this point onward.\r
899 The caller can optionally specify the health status of the AP by Health. If\r
900 an AP is being disabled, then the state of the disabled AP is implementation\r
901 dependent. If an AP is enabled, then the implementation must guarantee that a\r
902 complete initialization sequence is performed on the AP, so the AP is in a state\r
903 that is compatible with an MP operating system. This service may not be supported\r
904 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
905\r
906 If the enable or disable AP operation cannot be completed prior to the return\r
907 from this service, then EFI_UNSUPPORTED must be returned.\r
908\r
909 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
910 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
911 BSP. The range is from 0 to the total number of\r
912 logical processors minus 1. The total number of\r
913 logical processors can be retrieved by\r
914 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
915 @param[in] EnableAP Specifies the new state for the processor for\r
916 enabled, FALSE for disabled.\r
917 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
918 the new health status of the AP. This flag\r
919 corresponds to StatusFlag defined in\r
920 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
921 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
922 bits are ignored. If it is NULL, this parameter\r
923 is ignored.\r
924\r
925 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
926 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
927 prior to this service returning.\r
928 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
929 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
930 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
931 does not exist.\r
932 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
933\r
934**/\r
935EFI_STATUS\r
936EFIAPI\r
937EnableDisableAP (\r
938 IN EFI_MP_SERVICES_PROTOCOL *This,\r
939 IN UINTN ProcessorNumber,\r
940 IN BOOLEAN EnableAP,\r
941 IN UINT32 *HealthFlag OPTIONAL\r
942 )\r
943{\r
944 CPU_DATA_BLOCK *CpuData;\r
945\r
946 if (!IsBSP ()) {\r
947 return EFI_DEVICE_ERROR;\r
948 }\r
949\r
950 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
951 return EFI_NOT_FOUND;\r
952 }\r
953\r
954 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
955 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
956 return EFI_INVALID_PARAMETER;\r
957 }\r
958\r
959 if (GetApState (CpuData) != CpuStateIdle) {\r
960 return EFI_UNSUPPORTED;\r
961 }\r
962\r
963 if (EnableAP) {\r
964 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
965 mMpSystemData.NumberOfEnabledProcessors++;\r
966 }\r
967 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
968 } else {\r
969 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
970 mMpSystemData.NumberOfEnabledProcessors--;\r
971 }\r
972 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
973 }\r
974\r
975 if (HealthFlag != NULL) {\r
976 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
977 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
978 }\r
979\r
980 return EFI_SUCCESS;\r
981}\r
982\r
cfa2fac1
CF
983/**\r
984 This return the handle number for the calling processor. This service may be\r
985 called from the BSP and APs.\r
986\r
987 This service returns the processor handle number for the calling processor.\r
988 The returned value is in the range from 0 to the total number of logical\r
989 processors minus 1. The total number of logical processors can be retrieved\r
990 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
991 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
992 is returned. Otherwise, the current processors handle number is returned in\r
993 ProcessorNumber, and EFI_SUCCESS is returned.\r
994\r
995 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
996 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
997 BSP. The range is from 0 to the total number of\r
998 logical processors minus 1. The total number of\r
999 logical processors can be retrieved by\r
1000 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
1001\r
1002 @retval EFI_SUCCESS The current processor handle number was returned\r
1003 in ProcessorNumber.\r
1004 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1005\r
1006**/\r
1007EFI_STATUS\r
1008EFIAPI\r
1009WhoAmI (\r
1010 IN EFI_MP_SERVICES_PROTOCOL *This,\r
1011 OUT UINTN *ProcessorNumber\r
1012 )\r
1013{\r
1014 UINTN Index;\r
1015 UINT32 ProcessorId;\r
1016\r
1017 if (ProcessorNumber == NULL) {\r
1018 return EFI_INVALID_PARAMETER;\r
1019 }\r
1020\r
1021 ProcessorId = GetApicId ();\r
1022 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
1023 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
1024 break;\r
1025 }\r
1026 }\r
1027\r
1028 *ProcessorNumber = Index;\r
1029 return EFI_SUCCESS;\r
1030}\r
1031\r
3f4f0af8
CF
1032/**\r
1033 Terminate AP's task and set it to idle state.\r
1034\r
1035 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1036 and sends it to idle state.\r
1037\r
1038 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
1039\r
1040**/\r
1041VOID\r
1042ResetProcessorToIdleState (\r
1043 IN CPU_DATA_BLOCK *CpuData\r
1044 )\r
1045{\r
1046}\r
1047\r
e343f8f7
CF
1048/**\r
1049 Application Processors do loop routine\r
1050 after switch to its own stack.\r
1051\r
1052 @param Context1 A pointer to the context to pass into the function.\r
1053 @param Context2 A pointer to the context to pass into the function.\r
1054\r
1055**/\r
1056VOID\r
1057ProcessorToIdleState (\r
1058 IN VOID *Context1, OPTIONAL\r
1059 IN VOID *Context2 OPTIONAL\r
1060 )\r
1061{\r
1062 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
1063\r
1064 AsmApDoneWithCommonStack ();\r
1065\r
1066 CpuSleep ();\r
1067 CpuDeadLoop ();\r
1068}\r
1069\r
3f4f0af8
CF
1070/**\r
1071 Checks AP' status periodically.\r
1072\r
1073 This function is triggerred by timer perodically to check the\r
1074 state of AP forStartupThisAP() executed in non-blocking mode.\r
1075\r
1076 @param Event Event triggered.\r
1077 @param Context Parameter passed with the event.\r
1078\r
1079**/\r
1080VOID\r
1081EFIAPI\r
1082CheckThisAPStatus (\r
1083 IN EFI_EVENT Event,\r
1084 IN VOID *Context\r
1085 )\r
1086{\r
1087 CPU_DATA_BLOCK *CpuData;\r
1088 CPU_STATE CpuState;\r
1089\r
1090 CpuData = (CPU_DATA_BLOCK *) Context;\r
1091 if (CpuData->TimeoutActive) {\r
1092 CpuData->Timeout -= gPollInterval;\r
1093 }\r
1094\r
1095 CpuState = GetApState (CpuData);\r
1096\r
1097 if (CpuState == CpuStateFinished) {\r
1098 if (CpuData->Finished) {\r
1099 *CpuData->Finished = TRUE;\r
1100 }\r
1101 SetApState (CpuData, CpuStateIdle);\r
1102 goto out;\r
1103 }\r
1104\r
1105 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
1106 if (CpuState != CpuStateIdle &&\r
1107 CpuData->Finished) {\r
1108 *CpuData->Finished = FALSE;\r
1109 }\r
1110 ResetProcessorToIdleState (CpuData);\r
1111 goto out;\r
1112 }\r
1113\r
1114 return;\r
1115\r
1116out:\r
1117 gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);\r
1118 if (CpuData->WaitEvent) {\r
1119 gBS->SignalEvent (CpuData->WaitEvent);\r
1120 CpuData->WaitEvent = NULL;\r
1121 }\r
1122}\r
1123\r
5fee172f
CF
1124/**\r
1125 Checks APs' status periodically.\r
1126\r
1127 This function is triggerred by timer perodically to check the\r
1128 state of APs for StartupAllAPs() executed in non-blocking mode.\r
1129\r
1130 @param Event Event triggered.\r
1131 @param Context Parameter passed with the event.\r
1132\r
1133**/\r
1134VOID\r
1135EFIAPI\r
1136CheckAllAPsStatus (\r
1137 IN EFI_EVENT Event,\r
1138 IN VOID *Context\r
1139 )\r
1140{\r
1141 if (mMpSystemData.TimeoutActive) {\r
1142 mMpSystemData.Timeout -= gPollInterval;\r
1143 }\r
1144\r
1145 CheckAndUpdateAllAPsToIdleState ();\r
1146\r
1147 //\r
1148 // task timeout\r
1149 //\r
1150 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
1151 ResetAllFailedAPs();\r
1152 //\r
1153 // force exit\r
1154 //\r
1155 mMpSystemData.FinishCount = mMpSystemData.StartCount;\r
1156 }\r
1157\r
1158 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {\r
1159 return;\r
1160 }\r
1161\r
1162 gBS->SetTimer (\r
1163 mMpSystemData.CheckAllAPsEvent,\r
1164 TimerCancel,\r
1165 0\r
1166 );\r
1167\r
1168 if (mMpSystemData.WaitEvent) {\r
1169 gBS->SignalEvent (mMpSystemData.WaitEvent);\r
1170 mMpSystemData.WaitEvent = NULL;\r
1171 }\r
1172}\r
1173\r
1535c888
JJ
1174/**\r
1175 Application Processor C code entry point.\r
1176\r
1177**/\r
1178VOID\r
1179EFIAPI\r
1180ApEntryPointInC (\r
1181 VOID\r
1182 )\r
1183{\r
03673ae1
CF
1184 VOID* TopOfApStack;\r
1185\r
1186 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
1187 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
1188 mApStackStart = TopOfApStack;\r
1189\r
1190 mMpSystemData.NumberOfProcessors++;\r
e343f8f7
CF
1191\r
1192 SwitchStack (\r
1193 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
1194 NULL,\r
1195 NULL,\r
03673ae1
CF
1196 TopOfApStack);\r
1197}\r
1198\r
1199/**\r
1200 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
1201\r
1202 @param Bsp TRUE if the CPU is BSP\r
1203 @param ProcessorNumber The specific processor number\r
1204\r
1205 @retval EFI_SUCCESS Data for the processor collected and filled in\r
1206\r
1207**/\r
1208EFI_STATUS\r
1209FillInProcessorInformation (\r
1210 IN BOOLEAN Bsp,\r
1211 IN UINTN ProcessorNumber\r
1212 )\r
1213{\r
1214 CPU_DATA_BLOCK *CpuData;\r
1215 UINT32 ProcessorId;\r
1216\r
1217 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1218 ProcessorId = GetApicId ();\r
1219 CpuData->Info.ProcessorId = ProcessorId;\r
1220 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
1221 if (Bsp) {\r
1222 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1223 }\r
1224 CpuData->Info.Location.Package = ProcessorId;\r
1225 CpuData->Info.Location.Core = 0;\r
1226 CpuData->Info.Location.Thread = 0;\r
1227 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
1228\r
1229 CpuData->Procedure = NULL;\r
1230 CpuData->Parameter = NULL;\r
1231 InitializeSpinLock (&CpuData->CpuDataLock);\r
1232\r
1233 return EFI_SUCCESS;\r
1535c888
JJ
1234}\r
1235\r
03673ae1
CF
1236/**\r
1237 Prepare the System Data.\r
1238\r
1239 @retval EFI_SUCCESS the System Data finished initilization.\r
1240\r
1241**/\r
1242EFI_STATUS\r
1243InitMpSystemData (\r
1244 VOID\r
1245 )\r
1246{\r
3f4f0af8
CF
1247 UINTN ProcessorNumber;\r
1248 CPU_DATA_BLOCK *CpuData;\r
1249 EFI_STATUS Status;\r
1250\r
03673ae1
CF
1251 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
1252\r
1253 mMpSystemData.NumberOfProcessors = 1;\r
1254 mMpSystemData.NumberOfEnabledProcessors = 1;\r
1255\r
1256 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
1257 ASSERT(mMpSystemData.CpuDatas != NULL);\r
1258\r
5fee172f
CF
1259 Status = gBS->CreateEvent (\r
1260 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1261 TPL_CALLBACK,\r
1262 CheckAllAPsStatus,\r
1263 NULL,\r
1264 &mMpSystemData.CheckAllAPsEvent\r
1265 );\r
1266 ASSERT_EFI_ERROR (Status);\r
1267\r
3f4f0af8
CF
1268 for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {\r
1269 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1270 Status = gBS->CreateEvent (\r
1271 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1272 TPL_CALLBACK,\r
1273 CheckThisAPStatus,\r
1274 (VOID *) CpuData,\r
1275 &CpuData->CheckThisAPEvent\r
1276 );\r
1277 ASSERT_EFI_ERROR (Status);\r
1278 }\r
1279\r
03673ae1
CF
1280 //\r
1281 // BSP\r
1282 //\r
1283 FillInProcessorInformation (TRUE, 0);\r
1284\r
1285 return EFI_SUCCESS;\r
1286}\r
1535c888 1287\r
6022e28c
JJ
1288/**\r
1289 Initialize Multi-processor support.\r
1290\r
1291**/\r
1292VOID\r
1293InitializeMpSupport (\r
1294 VOID\r
1295 )\r
1296{\r
6a26a597
CF
1297 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
1298 if (gMaxLogicalProcessorNumber < 1) {\r
1299 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
1300 return;\r
1301 }\r
1302\r
1303 if (gMaxLogicalProcessorNumber == 1) {\r
1304 return;\r
1305 }\r
1306\r
1307 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
1308 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
1309\r
1310 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1311 ASSERT (mApStackStart != NULL);\r
6022e28c 1312\r
6a26a597
CF
1313 //\r
1314 // the first buffer of stack size used for common stack, when the amount of AP\r
1315 // more than 1, we should never free the common stack which maybe used for AP reset.\r
1316 //\r
1317 mCommonStack = mApStackStart;\r
1318 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
1319 mApStackStart = mTopOfApCommonStack;\r
1320\r
03673ae1 1321 InitMpSystemData ();\r
6a26a597 1322\r
03673ae1 1323 if (mMpSystemData.NumberOfProcessors == 1) {\r
6a26a597
CF
1324 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1325 return;\r
1326 }\r
1327\r
03673ae1
CF
1328 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
1329 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
1330 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
1331 gApStackSize));\r
6a26a597
CF
1332 }\r
1333}\r