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