]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: Startup APs
[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
cd8c700b
CF
635 //\r
636 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
637 //\r
638 mStopCheckAllAPsStatus = TRUE;\r
639\r
5fee172f
CF
640 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
641 CpuData = &mMpSystemData.CpuDatas[Number];\r
642 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
643 //\r
644 // Skip BSP\r
645 //\r
646 continue;\r
647 }\r
648\r
649 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
650 //\r
651 // Skip Disabled processors\r
652 //\r
653 continue;\r
654 }\r
655\r
656 if (GetApState (CpuData) != CpuStateIdle) {\r
657 return EFI_NOT_READY;\r
658 }\r
659 }\r
660\r
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
cd8c700b
CF
719 //\r
720 // Blocking temporarily stop CheckAllAPsStatus()\r
721 //\r
722 mStopCheckAllAPsStatus = TRUE;\r
723\r
5fee172f
CF
724 while (TRUE) {\r
725 CheckAndUpdateAllAPsToIdleState ();\r
726 if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {\r
727 Status = EFI_SUCCESS;\r
728 goto Done;\r
729 }\r
730\r
731 //\r
732 // task timeout\r
733 //\r
734 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
735 ResetAllFailedAPs();\r
736 Status = EFI_TIMEOUT;\r
737 goto Done;\r
738 }\r
739\r
740 gBS->Stall (gPollInterval);\r
741 mMpSystemData.Timeout -= gPollInterval;\r
742 }\r
743\r
744Done:\r
745\r
746 return Status;\r
747}\r
748\r
3f4f0af8
CF
749/**\r
750 This service lets the caller get one enabled AP to execute a caller-provided\r
751 function. The caller can request the BSP to either wait for the completion\r
752 of the AP or just proceed with the next task by using the EFI event mechanism.\r
753 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
754 execution support. This service may only be called from the BSP.\r
755\r
756 This function is used to dispatch one enabled AP to the function specified by\r
757 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
758 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
759 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
760 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
761 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
762 then EFI_UNSUPPORTED must be returned.\r
763\r
764 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
765 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
766 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
767 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
768\r
769 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
770 instance.\r
771 @param[in] Procedure A pointer to the function to be run on\r
772 enabled APs of the system. See type\r
773 EFI_AP_PROCEDURE.\r
774 @param[in] ProcessorNumber The handle number of the AP. The range is\r
775 from 0 to the total number of logical\r
776 processors minus 1. The total number of\r
777 logical processors can be retrieved by\r
778 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
779 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
780 service. If it is NULL, then execute in\r
781 blocking mode. BSP waits until all APs finish\r
782 or TimeoutInMicroseconds expires. If it's\r
783 not NULL, then execute in non-blocking mode.\r
784 BSP requests the function specified by\r
785 Procedure to be started on all the enabled\r
786 APs, and go on executing immediately. If\r
787 all return from Procedure or TimeoutInMicroseconds\r
788 expires, this event is signaled. The BSP\r
789 can use the CheckEvent() or WaitForEvent()\r
790 services to check the state of event. Type\r
791 EFI_EVENT is defined in CreateEvent() in\r
792 the Unified Extensible Firmware Interface\r
793 Specification.\r
794 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
795 APs to return from Procedure, either for\r
796 blocking or non-blocking mode. Zero means\r
797 infinity. If the timeout expires before\r
798 all APs return from Procedure, then Procedure\r
799 on the failed APs is terminated. All enabled\r
800 APs are available for next function assigned\r
801 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
802 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
803 If the timeout expires in blocking mode,\r
804 BSP returns EFI_TIMEOUT. If the timeout\r
805 expires in non-blocking mode, WaitEvent\r
806 is signaled with SignalEvent().\r
807 @param[in] ProcedureArgument The parameter passed into Procedure for\r
808 all APs.\r
809 @param[out] Finished If NULL, this parameter is ignored. In\r
810 blocking mode, this parameter is ignored.\r
811 In non-blocking mode, if AP returns from\r
812 Procedure before the timeout expires, its\r
813 content is set to TRUE. Otherwise, the\r
814 value is set to FALSE. The caller can\r
815 determine if the AP returned from Procedure\r
816 by evaluating this value.\r
817\r
818 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
819 the timeout expires.\r
820 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
821 dispatched to specified AP.\r
822 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
823 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
824 signaled.\r
825 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
826 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
827 the specified AP has finished.\r
828 @retval EFI_NOT_READY The specified AP is busy.\r
829 @retval EFI_NOT_FOUND The processor with the handle specified by\r
830 ProcessorNumber does not exist.\r
831 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
832 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
833\r
834**/\r
835EFI_STATUS\r
836EFIAPI\r
837StartupThisAP (\r
838 IN EFI_MP_SERVICES_PROTOCOL *This,\r
839 IN EFI_AP_PROCEDURE Procedure,\r
840 IN UINTN ProcessorNumber,\r
841 IN EFI_EVENT WaitEvent OPTIONAL,\r
842 IN UINTN TimeoutInMicroseconds,\r
843 IN VOID *ProcedureArgument OPTIONAL,\r
844 OUT BOOLEAN *Finished OPTIONAL\r
845 )\r
846{\r
847 CPU_DATA_BLOCK *CpuData;\r
3f4f0af8
CF
848\r
849 CpuData = NULL;\r
850\r
851 if (Finished != NULL) {\r
852 *Finished = FALSE;\r
853 }\r
854\r
855 if (!IsBSP ()) {\r
856 return EFI_DEVICE_ERROR;\r
857 }\r
858\r
859 if (Procedure == NULL) {\r
860 return EFI_INVALID_PARAMETER;\r
861 }\r
862\r
863 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
864 return EFI_NOT_FOUND;\r
865 }\r
866\r
cd8c700b
CF
867 //\r
868 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
869 //\r
870 mStopCheckAllAPsStatus = TRUE;\r
871\r
3f4f0af8
CF
872 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
873 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
874 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
875 return EFI_INVALID_PARAMETER;\r
876 }\r
877\r
878 if (GetApState (CpuData) != CpuStateIdle) {\r
879 return EFI_NOT_READY;\r
880 }\r
881\r
882 SetApState (CpuData, CpuStateReady);\r
883\r
884 SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
885\r
886 CpuData->Timeout = TimeoutInMicroseconds;\r
887 CpuData->WaitEvent = WaitEvent;\r
888 CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
889 CpuData->Finished = Finished;\r
890\r
acb2172d
CF
891 mStopCheckAllAPsStatus = FALSE;\r
892\r
3f4f0af8
CF
893 if (WaitEvent != NULL) {\r
894 //\r
895 // Non Blocking\r
896 //\r
acb2172d 897 return EFI_SUCCESS;\r
3f4f0af8
CF
898 }\r
899\r
900 //\r
901 // Blocking\r
902 //\r
903 while (TRUE) {\r
904 if (GetApState (CpuData) == CpuStateFinished) {\r
905 SetApState (CpuData, CpuStateIdle);\r
906 break;\r
907 }\r
908\r
909 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
910 ResetProcessorToIdleState (CpuData);\r
911 return EFI_TIMEOUT;\r
912 }\r
913\r
914 gBS->Stall (gPollInterval);\r
915 CpuData->Timeout -= gPollInterval;\r
916 }\r
917\r
918 return EFI_SUCCESS;\r
919}\r
920\r
b7c05ba5
CF
921/**\r
922 This service switches the requested AP to be the BSP from that point onward.\r
923 This service changes the BSP for all purposes. This call can only be performed\r
924 by the current BSP.\r
925\r
926 This service switches the requested AP to be the BSP from that point onward.\r
927 This service changes the BSP for all purposes. The new BSP can take over the\r
928 execution of the old BSP and continue seamlessly from where the old one left\r
929 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
930 is signaled.\r
931\r
932 If the BSP cannot be switched prior to the return from this service, then\r
933 EFI_UNSUPPORTED must be returned.\r
934\r
935 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
936 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
937 BSP. The range is from 0 to the total number of\r
938 logical processors minus 1. The total number of\r
939 logical processors can be retrieved by\r
940 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
941 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
942 enabled AP. Otherwise, it will be disabled.\r
943\r
944 @retval EFI_SUCCESS BSP successfully switched.\r
945 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
946 this service returning.\r
947 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
948 @retval EFI_SUCCESS The calling processor is an AP.\r
949 @retval EFI_NOT_FOUND The processor with the handle specified by\r
950 ProcessorNumber does not exist.\r
951 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
952 a disabled AP.\r
953 @retval EFI_NOT_READY The specified AP is busy.\r
954\r
955**/\r
956EFI_STATUS\r
957EFIAPI\r
958SwitchBSP (\r
959 IN EFI_MP_SERVICES_PROTOCOL *This,\r
960 IN UINTN ProcessorNumber,\r
961 IN BOOLEAN EnableOldBSP\r
962 )\r
963{\r
964 //\r
965 // Current always return unsupported.\r
966 //\r
967 return EFI_UNSUPPORTED;\r
968}\r
969\r
fa7ce675
CF
970/**\r
971 This service lets the caller enable or disable an AP from this point onward.\r
972 This service may only be called from the BSP.\r
973\r
974 This service allows the caller enable or disable an AP from this point onward.\r
975 The caller can optionally specify the health status of the AP by Health. If\r
976 an AP is being disabled, then the state of the disabled AP is implementation\r
977 dependent. If an AP is enabled, then the implementation must guarantee that a\r
978 complete initialization sequence is performed on the AP, so the AP is in a state\r
979 that is compatible with an MP operating system. This service may not be supported\r
980 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
981\r
982 If the enable or disable AP operation cannot be completed prior to the return\r
983 from this service, then EFI_UNSUPPORTED must be returned.\r
984\r
985 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
986 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
987 BSP. The range is from 0 to the total number of\r
988 logical processors minus 1. The total number of\r
989 logical processors can be retrieved by\r
990 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
991 @param[in] EnableAP Specifies the new state for the processor for\r
992 enabled, FALSE for disabled.\r
993 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
994 the new health status of the AP. This flag\r
995 corresponds to StatusFlag defined in\r
996 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
997 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
998 bits are ignored. If it is NULL, this parameter\r
999 is ignored.\r
1000\r
1001 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1002 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
1003 prior to this service returning.\r
1004 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
1005 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1006 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
1007 does not exist.\r
1008 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
1009\r
1010**/\r
1011EFI_STATUS\r
1012EFIAPI\r
1013EnableDisableAP (\r
1014 IN EFI_MP_SERVICES_PROTOCOL *This,\r
1015 IN UINTN ProcessorNumber,\r
1016 IN BOOLEAN EnableAP,\r
1017 IN UINT32 *HealthFlag OPTIONAL\r
1018 )\r
1019{\r
1020 CPU_DATA_BLOCK *CpuData;\r
cd8c700b
CF
1021 BOOLEAN TempStopCheckState;\r
1022\r
1023 CpuData = NULL;\r
1024 TempStopCheckState = FALSE;\r
fa7ce675
CF
1025\r
1026 if (!IsBSP ()) {\r
1027 return EFI_DEVICE_ERROR;\r
1028 }\r
1029\r
1030 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
1031 return EFI_NOT_FOUND;\r
1032 }\r
1033\r
cd8c700b
CF
1034 //\r
1035 // temporarily stop checkAllAPsStatus for initialize parameters.\r
1036 //\r
1037 if (!mStopCheckAllAPsStatus) {\r
1038 mStopCheckAllAPsStatus = TRUE;\r
1039 TempStopCheckState = TRUE;\r
1040 }\r
1041\r
fa7ce675
CF
1042 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1043 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
1044 return EFI_INVALID_PARAMETER;\r
1045 }\r
1046\r
1047 if (GetApState (CpuData) != CpuStateIdle) {\r
1048 return EFI_UNSUPPORTED;\r
1049 }\r
1050\r
1051 if (EnableAP) {\r
1052 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
1053 mMpSystemData.NumberOfEnabledProcessors++;\r
1054 }\r
1055 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
1056 } else {\r
1057 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
1058 mMpSystemData.NumberOfEnabledProcessors--;\r
1059 }\r
1060 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
1061 }\r
1062\r
1063 if (HealthFlag != NULL) {\r
1064 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
1065 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
1066 }\r
1067\r
cd8c700b
CF
1068 if (TempStopCheckState) {\r
1069 mStopCheckAllAPsStatus = FALSE;\r
1070 }\r
1071\r
fa7ce675
CF
1072 return EFI_SUCCESS;\r
1073}\r
1074\r
cfa2fac1
CF
1075/**\r
1076 This return the handle number for the calling processor. This service may be\r
1077 called from the BSP and APs.\r
1078\r
1079 This service returns the processor handle number for the calling processor.\r
1080 The returned value is in the range from 0 to the total number of logical\r
1081 processors minus 1. The total number of logical processors can be retrieved\r
1082 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
1083 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
1084 is returned. Otherwise, the current processors handle number is returned in\r
1085 ProcessorNumber, and EFI_SUCCESS is returned.\r
1086\r
1087 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
1088 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
1089 BSP. The range is from 0 to the total number of\r
1090 logical processors minus 1. The total number of\r
1091 logical processors can be retrieved by\r
1092 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
1093\r
1094 @retval EFI_SUCCESS The current processor handle number was returned\r
1095 in ProcessorNumber.\r
1096 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1097\r
1098**/\r
1099EFI_STATUS\r
1100EFIAPI\r
1101WhoAmI (\r
1102 IN EFI_MP_SERVICES_PROTOCOL *This,\r
1103 OUT UINTN *ProcessorNumber\r
1104 )\r
1105{\r
1106 UINTN Index;\r
1107 UINT32 ProcessorId;\r
1108\r
1109 if (ProcessorNumber == NULL) {\r
1110 return EFI_INVALID_PARAMETER;\r
1111 }\r
1112\r
1113 ProcessorId = GetApicId ();\r
1114 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
1115 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
1116 break;\r
1117 }\r
1118 }\r
1119\r
1120 *ProcessorNumber = Index;\r
1121 return EFI_SUCCESS;\r
1122}\r
1123\r
3f4f0af8
CF
1124/**\r
1125 Terminate AP's task and set it to idle state.\r
1126\r
1127 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
1128 and sends it to idle state.\r
1129\r
1130 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
1131\r
1132**/\r
1133VOID\r
1134ResetProcessorToIdleState (\r
1135 IN CPU_DATA_BLOCK *CpuData\r
1136 )\r
1137{\r
ac9dbb3b 1138 ResetApStackless ((UINT32)CpuData->Info.ProcessorId);\r
3f4f0af8
CF
1139}\r
1140\r
e343f8f7
CF
1141/**\r
1142 Application Processors do loop routine\r
1143 after switch to its own stack.\r
1144\r
1145 @param Context1 A pointer to the context to pass into the function.\r
1146 @param Context2 A pointer to the context to pass into the function.\r
1147\r
1148**/\r
1149VOID\r
1150ProcessorToIdleState (\r
1151 IN VOID *Context1, OPTIONAL\r
1152 IN VOID *Context2 OPTIONAL\r
1153 )\r
1154{\r
9908a829
CF
1155 UINTN ProcessorNumber;\r
1156 CPU_DATA_BLOCK *CpuData;\r
1157 EFI_AP_PROCEDURE Procedure;\r
1158 VOID *ProcedureArgument;\r
1159\r
232eb4c8
CF
1160 AsmApDoneWithCommonStack ();\r
1161\r
1162 while (!mAPsAlreadyInitFinished) {\r
1163 CpuPause ();\r
1164 }\r
1165\r
9908a829
CF
1166 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
1167 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
e343f8f7 1168\r
0e724fc1
CF
1169 //\r
1170 // Avoid forcibly reset AP caused the AP got lock not release.\r
1171 //\r
1172 if (CpuData->LockSelf == (INTN) GetApicId ()) {\r
1173 ReleaseSpinLock (&CpuData->CpuDataLock);\r
1174 }\r
1175\r
ac9dbb3b
CF
1176 //\r
1177 // Avoid forcibly reset AP caused the AP State is not updated.\r
1178 //\r
1179 GetMpSpinLock (CpuData);\r
1180 CpuData->State = CpuStateIdle;\r
1181 CpuData->Procedure = NULL;\r
1182 ReleaseMpSpinLock (CpuData);\r
1183\r
9908a829 1184 while (TRUE) {\r
d16cf36d 1185 GetMpSpinLock (CpuData);\r
9908a829
CF
1186 ProcedureArgument = CpuData->Parameter;\r
1187 Procedure = CpuData->Procedure;\r
d16cf36d 1188 ReleaseMpSpinLock (CpuData);\r
9908a829
CF
1189\r
1190 if (Procedure != NULL) {\r
1191 Procedure (ProcedureArgument);\r
1192\r
d16cf36d 1193 GetMpSpinLock (CpuData);\r
9908a829 1194 CpuData->Procedure = NULL;\r
d16cf36d
CF
1195 CpuData->State = CpuStateFinished;\r
1196 ReleaseMpSpinLock (CpuData);\r
9908a829
CF
1197 }\r
1198\r
1199 CpuPause ();\r
1200 }\r
1201\r
e343f8f7
CF
1202 CpuSleep ();\r
1203 CpuDeadLoop ();\r
1204}\r
1205\r
3f4f0af8
CF
1206/**\r
1207 Checks AP' status periodically.\r
1208\r
1209 This function is triggerred by timer perodically to check the\r
1210 state of AP forStartupThisAP() executed in non-blocking mode.\r
1211\r
1212 @param Event Event triggered.\r
1213 @param Context Parameter passed with the event.\r
1214\r
1215**/\r
1216VOID\r
1217EFIAPI\r
1218CheckThisAPStatus (\r
1219 IN EFI_EVENT Event,\r
1220 IN VOID *Context\r
1221 )\r
1222{\r
1223 CPU_DATA_BLOCK *CpuData;\r
1224 CPU_STATE CpuState;\r
1225\r
1226 CpuData = (CPU_DATA_BLOCK *) Context;\r
1227 if (CpuData->TimeoutActive) {\r
1228 CpuData->Timeout -= gPollInterval;\r
1229 }\r
1230\r
1231 CpuState = GetApState (CpuData);\r
1232\r
1233 if (CpuState == CpuStateFinished) {\r
1234 if (CpuData->Finished) {\r
1235 *CpuData->Finished = TRUE;\r
1236 }\r
1237 SetApState (CpuData, CpuStateIdle);\r
1238 goto out;\r
1239 }\r
1240\r
1241 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
1242 if (CpuState != CpuStateIdle &&\r
1243 CpuData->Finished) {\r
1244 *CpuData->Finished = FALSE;\r
1245 }\r
1246 ResetProcessorToIdleState (CpuData);\r
1247 goto out;\r
1248 }\r
1249\r
1250 return;\r
1251\r
1252out:\r
acb2172d
CF
1253 CpuData->TimeoutActive = FALSE;\r
1254 gBS->SignalEvent (CpuData->WaitEvent);\r
1255 CpuData->WaitEvent = NULL;\r
3f4f0af8
CF
1256}\r
1257\r
5fee172f
CF
1258/**\r
1259 Checks APs' status periodically.\r
1260\r
1261 This function is triggerred by timer perodically to check the\r
1262 state of APs for StartupAllAPs() executed in non-blocking mode.\r
1263\r
1264 @param Event Event triggered.\r
1265 @param Context Parameter passed with the event.\r
1266\r
1267**/\r
1268VOID\r
1269EFIAPI\r
1270CheckAllAPsStatus (\r
1271 IN EFI_EVENT Event,\r
1272 IN VOID *Context\r
1273 )\r
1274{\r
acb2172d
CF
1275 CPU_DATA_BLOCK *CpuData;\r
1276 UINTN Number;\r
e4aaf764 1277 EFI_STATUS Status;\r
acb2172d 1278\r
5fee172f
CF
1279 if (mMpSystemData.TimeoutActive) {\r
1280 mMpSystemData.Timeout -= gPollInterval;\r
1281 }\r
1282\r
acb2172d
CF
1283 if (mStopCheckAllAPsStatus) {\r
1284 return;\r
1285 }\r
5fee172f 1286\r
e4aaf764
CF
1287 //\r
1288 // avoid next timer enter.\r
1289 //\r
1290 Status = gBS->SetTimer (\r
1291 mMpSystemData.CheckAllAPsEvent,\r
1292 TimerCancel,\r
1293 0\r
1294 );\r
1295 ASSERT_EFI_ERROR (Status);\r
1296\r
acb2172d
CF
1297 if (mMpSystemData.WaitEvent != NULL) {\r
1298 CheckAndUpdateAllAPsToIdleState ();\r
5fee172f 1299 //\r
acb2172d 1300 // task timeout\r
5fee172f 1301 //\r
acb2172d
CF
1302 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
1303 ResetAllFailedAPs();\r
1304 //\r
1305 // force exit\r
1306 //\r
1307 mMpSystemData.FinishCount = mMpSystemData.StartCount;\r
1308 }\r
5fee172f 1309\r
acb2172d 1310 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {\r
e4aaf764 1311 goto EXIT;\r
acb2172d 1312 }\r
5fee172f 1313\r
acb2172d 1314 mMpSystemData.TimeoutActive = FALSE;\r
5fee172f
CF
1315 gBS->SignalEvent (mMpSystemData.WaitEvent);\r
1316 mMpSystemData.WaitEvent = NULL;\r
acb2172d 1317 mStopCheckAllAPsStatus = TRUE;\r
e4aaf764
CF
1318\r
1319 goto EXIT;\r
acb2172d
CF
1320 }\r
1321\r
1322 //\r
1323 // check each AP status for StartupThisAP\r
1324 //\r
1325 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
1326 CpuData = &mMpSystemData.CpuDatas[Number];\r
acb2172d
CF
1327 if (CpuData->WaitEvent) {\r
1328 CheckThisAPStatus (NULL, (VOID *)CpuData);\r
1329 }\r
5fee172f 1330 }\r
e4aaf764
CF
1331\r
1332EXIT:\r
1333 Status = gBS->SetTimer (\r
1334 mMpSystemData.CheckAllAPsEvent,\r
1335 TimerPeriodic,\r
1336 EFI_TIMER_PERIOD_MICROSECONDS (100)\r
1337 );\r
1338 ASSERT_EFI_ERROR (Status);\r
5fee172f
CF
1339}\r
1340\r
1535c888
JJ
1341/**\r
1342 Application Processor C code entry point.\r
1343\r
1344**/\r
1345VOID\r
1346EFIAPI\r
1347ApEntryPointInC (\r
1348 VOID\r
1349 )\r
1350{\r
ac9dbb3b
CF
1351 VOID* TopOfApStack;\r
1352 UINTN ProcessorNumber;\r
03673ae1 1353\r
ac9dbb3b
CF
1354 if (!mAPsAlreadyInitFinished) {\r
1355 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
1356 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
1357 mApStackStart = TopOfApStack;\r
03673ae1 1358\r
ac9dbb3b
CF
1359 //\r
1360 // Store the Stack address, when reset the AP, We can found the original address.\r
1361 //\r
1362 mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack;\r
1363 mMpSystemData.NumberOfProcessors++;\r
1364 mMpSystemData.NumberOfEnabledProcessors++;\r
1365 } else {\r
1366 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
1367 //\r
1368 // Get the original stack address.\r
1369 //\r
1370 TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack;\r
1371 }\r
e343f8f7
CF
1372\r
1373 SwitchStack (\r
1374 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
1375 NULL,\r
1376 NULL,\r
03673ae1
CF
1377 TopOfApStack);\r
1378}\r
1379\r
1380/**\r
1381 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
1382\r
1383 @param Bsp TRUE if the CPU is BSP\r
1384 @param ProcessorNumber The specific processor number\r
1385\r
1386 @retval EFI_SUCCESS Data for the processor collected and filled in\r
1387\r
1388**/\r
1389EFI_STATUS\r
1390FillInProcessorInformation (\r
1391 IN BOOLEAN Bsp,\r
1392 IN UINTN ProcessorNumber\r
1393 )\r
1394{\r
1395 CPU_DATA_BLOCK *CpuData;\r
1396 UINT32 ProcessorId;\r
1397\r
1398 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
1399 ProcessorId = GetApicId ();\r
1400 CpuData->Info.ProcessorId = ProcessorId;\r
1401 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
1402 if (Bsp) {\r
1403 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1404 }\r
1405 CpuData->Info.Location.Package = ProcessorId;\r
1406 CpuData->Info.Location.Core = 0;\r
1407 CpuData->Info.Location.Thread = 0;\r
1408 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
1409\r
1410 CpuData->Procedure = NULL;\r
1411 CpuData->Parameter = NULL;\r
1412 InitializeSpinLock (&CpuData->CpuDataLock);\r
0e724fc1 1413 CpuData->LockSelf = -1;\r
03673ae1
CF
1414\r
1415 return EFI_SUCCESS;\r
1535c888
JJ
1416}\r
1417\r
03673ae1
CF
1418/**\r
1419 Prepare the System Data.\r
1420\r
1421 @retval EFI_SUCCESS the System Data finished initilization.\r
1422\r
1423**/\r
1424EFI_STATUS\r
1425InitMpSystemData (\r
1426 VOID\r
1427 )\r
1428{\r
3f4f0af8
CF
1429 EFI_STATUS Status;\r
1430\r
03673ae1
CF
1431 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
1432\r
1433 mMpSystemData.NumberOfProcessors = 1;\r
1434 mMpSystemData.NumberOfEnabledProcessors = 1;\r
1435\r
1436 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
1437 ASSERT(mMpSystemData.CpuDatas != NULL);\r
1438\r
5fee172f
CF
1439 Status = gBS->CreateEvent (\r
1440 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1441 TPL_CALLBACK,\r
1442 CheckAllAPsStatus,\r
1443 NULL,\r
1444 &mMpSystemData.CheckAllAPsEvent\r
1445 );\r
1446 ASSERT_EFI_ERROR (Status);\r
1447\r
acb2172d
CF
1448 //\r
1449 // Set timer to check all APs status.\r
1450 //\r
1451 Status = gBS->SetTimer (\r
1452 mMpSystemData.CheckAllAPsEvent,\r
1453 TimerPeriodic,\r
1454 EFI_TIMER_PERIOD_MICROSECONDS (100)\r
1455 );\r
1456 ASSERT_EFI_ERROR (Status);\r
3f4f0af8 1457\r
03673ae1
CF
1458 //\r
1459 // BSP\r
1460 //\r
1461 FillInProcessorInformation (TRUE, 0);\r
1462\r
1463 return EFI_SUCCESS;\r
1464}\r
1535c888 1465\r
6022e28c
JJ
1466/**\r
1467 Initialize Multi-processor support.\r
1468\r
1469**/\r
1470VOID\r
1471InitializeMpSupport (\r
1472 VOID\r
1473 )\r
1474{\r
6a26a597
CF
1475 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
1476 if (gMaxLogicalProcessorNumber < 1) {\r
1477 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
1478 return;\r
1479 }\r
1480\r
1481 if (gMaxLogicalProcessorNumber == 1) {\r
1482 return;\r
1483 }\r
1484\r
1485 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
1486 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
1487\r
1488 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1489 ASSERT (mApStackStart != NULL);\r
6022e28c 1490\r
6a26a597
CF
1491 //\r
1492 // the first buffer of stack size used for common stack, when the amount of AP\r
1493 // more than 1, we should never free the common stack which maybe used for AP reset.\r
1494 //\r
1495 mCommonStack = mApStackStart;\r
1496 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
1497 mApStackStart = mTopOfApCommonStack;\r
1498\r
03673ae1 1499 InitMpSystemData ();\r
6a26a597 1500\r
fe078dd5
CF
1501 PrepareAPStartupCode ();\r
1502\r
dee9376f
JJ
1503 StartApsStackless ();\r
1504\r
1505 DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mMpSystemData.NumberOfProcessors));\r
03673ae1 1506 if (mMpSystemData.NumberOfProcessors == 1) {\r
fe078dd5 1507 FreeApStartupCode ();\r
6a26a597
CF
1508 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
1509 return;\r
1510 }\r
1511\r
232eb4c8
CF
1512 mMpSystemData.CpuDatas = ReallocatePool (\r
1513 sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber,\r
1514 sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,\r
1515 mMpSystemData.CpuDatas);\r
1516\r
ac9dbb3b
CF
1517 mAPsAlreadyInitFinished = TRUE;\r
1518\r
03673ae1
CF
1519 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
1520 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
1521 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
1522 gApStackSize));\r
6a26a597
CF
1523 }\r
1524}\r