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