]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Protocol:StartupThisAP()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
... / ...
CommitLineData
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
18UINTN gMaxLogicalProcessorNumber;\r
19UINTN gApStackSize;\r
20UINTN gPollInterval = 100; // 100 microseconds\r
21\r
22MP_SYSTEM_DATA mMpSystemData;\r
23\r
24VOID *mCommonStack = 0;\r
25VOID *mTopOfApCommonStack = 0;\r
26VOID *mApStackStart = 0;\r
27\r
28EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
29 GetNumberOfProcessors,\r
30 GetProcessorInfo,\r
31 NULL, // StartupAllAPs,\r
32 StartupThisAP,\r
33 NULL, // SwitchBSP,\r
34 EnableDisableAP,\r
35 WhoAmI\r
36};\r
37\r
38/**\r
39 Check whether caller processor is BSP.\r
40\r
41 @retval TRUE the caller is BSP\r
42 @retval FALSE the caller is AP\r
43\r
44**/\r
45BOOLEAN\r
46IsBSP (\r
47 VOID\r
48 )\r
49{\r
50 UINTN CpuIndex;\r
51 CPU_DATA_BLOCK *CpuData;\r
52\r
53 CpuData = NULL;\r
54\r
55 WhoAmI (&mMpServicesTemplate, &CpuIndex);\r
56 CpuData = &mMpSystemData.CpuDatas[CpuIndex];\r
57\r
58 return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;\r
59}\r
60\r
61/**\r
62 Get the Application Processors state.\r
63\r
64 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
65\r
66 @retval CPU_STATE the AP status\r
67\r
68**/\r
69CPU_STATE\r
70GetApState (\r
71 IN CPU_DATA_BLOCK *CpuData\r
72 )\r
73{\r
74 CPU_STATE State;\r
75\r
76 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
77 CpuPause ();\r
78 }\r
79\r
80 State = CpuData->State;\r
81 ReleaseSpinLock (&CpuData->CpuDataLock);\r
82\r
83 return State;\r
84}\r
85\r
86/**\r
87 Set the Application Processors state.\r
88\r
89 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP\r
90 @param State The AP status\r
91\r
92**/\r
93VOID\r
94SetApState (\r
95 IN CPU_DATA_BLOCK *CpuData,\r
96 IN CPU_STATE State\r
97 )\r
98{\r
99 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
100 CpuPause ();\r
101 }\r
102\r
103 CpuData->State = State;\r
104 ReleaseSpinLock (&CpuData->CpuDataLock);\r
105}\r
106\r
107/**\r
108 Set the Application Processor prepare to run a function specified\r
109 by Params.\r
110\r
111 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
112 @param Procedure A pointer to the function to be run on enabled APs of the system\r
113 @param ProcedureArgument Pointer to the optional parameter of the assigned function\r
114\r
115**/\r
116VOID\r
117SetApProcedure (\r
118 IN CPU_DATA_BLOCK *CpuData,\r
119 IN EFI_AP_PROCEDURE Procedure,\r
120 IN VOID *ProcedureArgument\r
121 )\r
122{\r
123 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
124 CpuPause ();\r
125 }\r
126\r
127 CpuData->Parameter = ProcedureArgument;\r
128 CpuData->Procedure = Procedure;\r
129 ReleaseSpinLock (&CpuData->CpuDataLock);\r
130}\r
131\r
132/**\r
133 Check the Application Processors Status whether contains the Flags.\r
134\r
135 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
136 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
137\r
138 @retval TRUE the AP status includes the StatusFlag\r
139 @retval FALSE the AP status excludes the StatusFlag\r
140\r
141**/\r
142BOOLEAN\r
143TestCpuStatusFlag (\r
144 IN CPU_DATA_BLOCK *CpuData,\r
145 IN UINT32 Flags\r
146 )\r
147{\r
148 UINT32 Ret;\r
149\r
150 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
151 CpuPause ();\r
152 }\r
153\r
154 Ret = CpuData->Info.StatusFlag & Flags;\r
155 ReleaseSpinLock (&CpuData->CpuDataLock);\r
156\r
157 return !!(Ret);\r
158}\r
159\r
160/**\r
161 Bitwise-Or of the Application Processors Status with the Flags.\r
162\r
163 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
164 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
165\r
166**/\r
167VOID\r
168CpuStatusFlagOr (\r
169 IN CPU_DATA_BLOCK *CpuData,\r
170 IN UINT32 Flags\r
171 )\r
172{\r
173 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
174 CpuPause ();\r
175 }\r
176\r
177 CpuData->Info.StatusFlag |= Flags;\r
178 ReleaseSpinLock (&CpuData->CpuDataLock);\r
179}\r
180\r
181/**\r
182 Bitwise-AndNot of the Application Processors Status with the Flags.\r
183\r
184 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
185 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
186\r
187**/\r
188VOID\r
189CpuStatusFlagAndNot (\r
190 IN CPU_DATA_BLOCK *CpuData,\r
191 IN UINT32 Flags\r
192 )\r
193{\r
194 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
195 CpuPause ();\r
196 }\r
197\r
198 CpuData->Info.StatusFlag &= ~Flags;\r
199 ReleaseSpinLock (&CpuData->CpuDataLock);\r
200}\r
201\r
202/**\r
203 Searches for the next blocking AP.\r
204\r
205 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().\r
206\r
207 @param NextNumber Pointer to the processor number of the next blocking AP.\r
208\r
209 @retval EFI_SUCCESS The next blocking AP has been found.\r
210 @retval EFI_NOT_FOUND No blocking AP exists.\r
211\r
212**/\r
213EFI_STATUS\r
214GetNextBlockedNumber (\r
215 OUT UINTN *NextNumber\r
216 )\r
217{\r
218 UINTN Number;\r
219 CPU_STATE CpuState;\r
220 CPU_DATA_BLOCK *CpuData;\r
221\r
222 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
223 CpuData = &mMpSystemData.CpuDatas[Number];\r
224 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
225 //\r
226 // Skip BSP\r
227 //\r
228 continue;\r
229 }\r
230\r
231 CpuState = GetApState (CpuData);\r
232 if (CpuState == CpuStateBlocked) {\r
233 *NextNumber = Number;\r
234 return EFI_SUCCESS;\r
235 }\r
236 }\r
237\r
238 return EFI_NOT_FOUND;\r
239}\r
240\r
241/**\r
242 This service retrieves the number of logical processor in the platform\r
243 and the number of those logical processors that are enabled on this boot.\r
244 This service may only be called from the BSP.\r
245\r
246 This function is used to retrieve the following information:\r
247 - The number of logical processors that are present in the system.\r
248 - The number of enabled logical processors in the system at the instant\r
249 this call is made.\r
250\r
251 Because MP Service Protocol provides services to enable and disable processors\r
252 dynamically, the number of enabled logical processors may vary during the\r
253 course of a boot session.\r
254\r
255 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
256 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
257 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
258 is returned in NumberOfProcessors, the number of currently enabled processor\r
259 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
260\r
261 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
262 instance.\r
263 @param[out] NumberOfProcessors Pointer to the total number of logical\r
264 processors in the system, including the BSP\r
265 and disabled APs.\r
266 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
267 processors that exist in system, including\r
268 the BSP.\r
269\r
270 @retval EFI_SUCCESS The number of logical processors and enabled\r
271 logical processors was retrieved.\r
272 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
273 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
274 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
275\r
276**/\r
277EFI_STATUS\r
278EFIAPI\r
279GetNumberOfProcessors (\r
280 IN EFI_MP_SERVICES_PROTOCOL *This,\r
281 OUT UINTN *NumberOfProcessors,\r
282 OUT UINTN *NumberOfEnabledProcessors\r
283 )\r
284{\r
285 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
286 return EFI_INVALID_PARAMETER;\r
287 }\r
288\r
289 if (!IsBSP ()) {\r
290 return EFI_DEVICE_ERROR;\r
291 }\r
292\r
293 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;\r
294 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\r
295 return EFI_SUCCESS;\r
296}\r
297\r
298/**\r
299 Gets detailed MP-related information on the requested processor at the\r
300 instant this call is made. This service may only be called from the BSP.\r
301\r
302 This service retrieves detailed MP-related information about any processor\r
303 on the platform. Note the following:\r
304 - The processor information may change during the course of a boot session.\r
305 - The information presented here is entirely MP related.\r
306\r
307 Information regarding the number of caches and their sizes, frequency of operation,\r
308 slot numbers is all considered platform-related information and is not provided\r
309 by this service.\r
310\r
311 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
312 instance.\r
313 @param[in] ProcessorNumber The handle number of processor.\r
314 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
315 the requested processor is deposited.\r
316\r
317 @retval EFI_SUCCESS Processor information was returned.\r
318 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
319 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
320 @retval EFI_NOT_FOUND The processor with the handle specified by\r
321 ProcessorNumber does not exist in the platform.\r
322\r
323**/\r
324EFI_STATUS\r
325EFIAPI\r
326GetProcessorInfo (\r
327 IN EFI_MP_SERVICES_PROTOCOL *This,\r
328 IN UINTN ProcessorNumber,\r
329 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
330 )\r
331{\r
332 if (ProcessorInfoBuffer == NULL) {\r
333 return EFI_INVALID_PARAMETER;\r
334 }\r
335\r
336 if (!IsBSP ()) {\r
337 return EFI_DEVICE_ERROR;\r
338 }\r
339\r
340 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
341 return EFI_NOT_FOUND;\r
342 }\r
343\r
344 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
345 return EFI_SUCCESS;\r
346}\r
347\r
348/**\r
349 This service lets the caller get one enabled AP to execute a caller-provided\r
350 function. The caller can request the BSP to either wait for the completion\r
351 of the AP or just proceed with the next task by using the EFI event mechanism.\r
352 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
353 execution support. This service may only be called from the BSP.\r
354\r
355 This function is used to dispatch one enabled AP to the function specified by\r
356 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
357 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
358 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
359 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
360 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
361 then EFI_UNSUPPORTED must be returned.\r
362\r
363 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
364 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
365 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
366 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
367\r
368 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
369 instance.\r
370 @param[in] Procedure A pointer to the function to be run on\r
371 enabled APs of the system. See type\r
372 EFI_AP_PROCEDURE.\r
373 @param[in] ProcessorNumber The handle number of the AP. The range is\r
374 from 0 to the total number of logical\r
375 processors minus 1. The total number of\r
376 logical processors can be retrieved by\r
377 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
378 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
379 service. If it is NULL, then execute in\r
380 blocking mode. BSP waits until all APs finish\r
381 or TimeoutInMicroseconds expires. If it's\r
382 not NULL, then execute in non-blocking mode.\r
383 BSP requests the function specified by\r
384 Procedure to be started on all the enabled\r
385 APs, and go on executing immediately. If\r
386 all return from Procedure or TimeoutInMicroseconds\r
387 expires, this event is signaled. The BSP\r
388 can use the CheckEvent() or WaitForEvent()\r
389 services to check the state of event. Type\r
390 EFI_EVENT is defined in CreateEvent() in\r
391 the Unified Extensible Firmware Interface\r
392 Specification.\r
393 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
394 APs to return from Procedure, either for\r
395 blocking or non-blocking mode. Zero means\r
396 infinity. If the timeout expires before\r
397 all APs return from Procedure, then Procedure\r
398 on the failed APs is terminated. All enabled\r
399 APs are available for next function assigned\r
400 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
401 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
402 If the timeout expires in blocking mode,\r
403 BSP returns EFI_TIMEOUT. If the timeout\r
404 expires in non-blocking mode, WaitEvent\r
405 is signaled with SignalEvent().\r
406 @param[in] ProcedureArgument The parameter passed into Procedure for\r
407 all APs.\r
408 @param[out] Finished If NULL, this parameter is ignored. In\r
409 blocking mode, this parameter is ignored.\r
410 In non-blocking mode, if AP returns from\r
411 Procedure before the timeout expires, its\r
412 content is set to TRUE. Otherwise, the\r
413 value is set to FALSE. The caller can\r
414 determine if the AP returned from Procedure\r
415 by evaluating this value.\r
416\r
417 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
418 the timeout expires.\r
419 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
420 dispatched to specified AP.\r
421 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
422 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
423 signaled.\r
424 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
425 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
426 the specified AP has finished.\r
427 @retval EFI_NOT_READY The specified AP is busy.\r
428 @retval EFI_NOT_FOUND The processor with the handle specified by\r
429 ProcessorNumber does not exist.\r
430 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
431 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
432\r
433**/\r
434EFI_STATUS\r
435EFIAPI\r
436StartupThisAP (\r
437 IN EFI_MP_SERVICES_PROTOCOL *This,\r
438 IN EFI_AP_PROCEDURE Procedure,\r
439 IN UINTN ProcessorNumber,\r
440 IN EFI_EVENT WaitEvent OPTIONAL,\r
441 IN UINTN TimeoutInMicroseconds,\r
442 IN VOID *ProcedureArgument OPTIONAL,\r
443 OUT BOOLEAN *Finished OPTIONAL\r
444 )\r
445{\r
446 CPU_DATA_BLOCK *CpuData;\r
447 EFI_STATUS Status;\r
448\r
449 CpuData = NULL;\r
450\r
451 if (Finished != NULL) {\r
452 *Finished = FALSE;\r
453 }\r
454\r
455 if (!IsBSP ()) {\r
456 return EFI_DEVICE_ERROR;\r
457 }\r
458\r
459 if (Procedure == NULL) {\r
460 return EFI_INVALID_PARAMETER;\r
461 }\r
462\r
463 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
464 return EFI_NOT_FOUND;\r
465 }\r
466\r
467 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
468 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
469 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
470 return EFI_INVALID_PARAMETER;\r
471 }\r
472\r
473 if (GetApState (CpuData) != CpuStateIdle) {\r
474 return EFI_NOT_READY;\r
475 }\r
476\r
477 SetApState (CpuData, CpuStateReady);\r
478\r
479 SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
480\r
481 CpuData->Timeout = TimeoutInMicroseconds;\r
482 CpuData->WaitEvent = WaitEvent;\r
483 CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
484 CpuData->Finished = Finished;\r
485\r
486 if (WaitEvent != NULL) {\r
487 //\r
488 // Non Blocking\r
489 //\r
490 Status = gBS->SetTimer (\r
491 CpuData->CheckThisAPEvent,\r
492 TimerPeriodic,\r
493 EFI_TIMER_PERIOD_MICROSECONDS (100)\r
494 );\r
495 return Status;\r
496 }\r
497\r
498 //\r
499 // Blocking\r
500 //\r
501 while (TRUE) {\r
502 if (GetApState (CpuData) == CpuStateFinished) {\r
503 SetApState (CpuData, CpuStateIdle);\r
504 break;\r
505 }\r
506\r
507 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
508 ResetProcessorToIdleState (CpuData);\r
509 return EFI_TIMEOUT;\r
510 }\r
511\r
512 gBS->Stall (gPollInterval);\r
513 CpuData->Timeout -= gPollInterval;\r
514 }\r
515\r
516 return EFI_SUCCESS;\r
517}\r
518\r
519/**\r
520 This service lets the caller enable or disable an AP from this point onward.\r
521 This service may only be called from the BSP.\r
522\r
523 This service allows the caller enable or disable an AP from this point onward.\r
524 The caller can optionally specify the health status of the AP by Health. If\r
525 an AP is being disabled, then the state of the disabled AP is implementation\r
526 dependent. If an AP is enabled, then the implementation must guarantee that a\r
527 complete initialization sequence is performed on the AP, so the AP is in a state\r
528 that is compatible with an MP operating system. This service may not be supported\r
529 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
530\r
531 If the enable or disable AP operation cannot be completed prior to the return\r
532 from this service, then EFI_UNSUPPORTED must be returned.\r
533\r
534 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
535 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
536 BSP. The range is from 0 to the total number of\r
537 logical processors minus 1. The total number of\r
538 logical processors can be retrieved by\r
539 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
540 @param[in] EnableAP Specifies the new state for the processor for\r
541 enabled, FALSE for disabled.\r
542 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
543 the new health status of the AP. This flag\r
544 corresponds to StatusFlag defined in\r
545 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
546 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
547 bits are ignored. If it is NULL, this parameter\r
548 is ignored.\r
549\r
550 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
551 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
552 prior to this service returning.\r
553 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
554 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
555 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
556 does not exist.\r
557 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
558\r
559**/\r
560EFI_STATUS\r
561EFIAPI\r
562EnableDisableAP (\r
563 IN EFI_MP_SERVICES_PROTOCOL *This,\r
564 IN UINTN ProcessorNumber,\r
565 IN BOOLEAN EnableAP,\r
566 IN UINT32 *HealthFlag OPTIONAL\r
567 )\r
568{\r
569 CPU_DATA_BLOCK *CpuData;\r
570\r
571 if (!IsBSP ()) {\r
572 return EFI_DEVICE_ERROR;\r
573 }\r
574\r
575 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
576 return EFI_NOT_FOUND;\r
577 }\r
578\r
579 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
580 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
581 return EFI_INVALID_PARAMETER;\r
582 }\r
583\r
584 if (GetApState (CpuData) != CpuStateIdle) {\r
585 return EFI_UNSUPPORTED;\r
586 }\r
587\r
588 if (EnableAP) {\r
589 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
590 mMpSystemData.NumberOfEnabledProcessors++;\r
591 }\r
592 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
593 } else {\r
594 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
595 mMpSystemData.NumberOfEnabledProcessors--;\r
596 }\r
597 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
598 }\r
599\r
600 if (HealthFlag != NULL) {\r
601 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
602 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
603 }\r
604\r
605 return EFI_SUCCESS;\r
606}\r
607\r
608/**\r
609 This return the handle number for the calling processor. This service may be\r
610 called from the BSP and APs.\r
611\r
612 This service returns the processor handle number for the calling processor.\r
613 The returned value is in the range from 0 to the total number of logical\r
614 processors minus 1. The total number of logical processors can be retrieved\r
615 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
616 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
617 is returned. Otherwise, the current processors handle number is returned in\r
618 ProcessorNumber, and EFI_SUCCESS is returned.\r
619\r
620 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
621 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
622 BSP. The range is from 0 to the total number of\r
623 logical processors minus 1. The total number of\r
624 logical processors can be retrieved by\r
625 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
626\r
627 @retval EFI_SUCCESS The current processor handle number was returned\r
628 in ProcessorNumber.\r
629 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
630\r
631**/\r
632EFI_STATUS\r
633EFIAPI\r
634WhoAmI (\r
635 IN EFI_MP_SERVICES_PROTOCOL *This,\r
636 OUT UINTN *ProcessorNumber\r
637 )\r
638{\r
639 UINTN Index;\r
640 UINT32 ProcessorId;\r
641\r
642 if (ProcessorNumber == NULL) {\r
643 return EFI_INVALID_PARAMETER;\r
644 }\r
645\r
646 ProcessorId = GetApicId ();\r
647 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
648 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
649 break;\r
650 }\r
651 }\r
652\r
653 *ProcessorNumber = Index;\r
654 return EFI_SUCCESS;\r
655}\r
656\r
657/**\r
658 Terminate AP's task and set it to idle state.\r
659\r
660 This function terminates AP's task due to timeout by sending INIT-SIPI,\r
661 and sends it to idle state.\r
662\r
663 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
664\r
665**/\r
666VOID\r
667ResetProcessorToIdleState (\r
668 IN CPU_DATA_BLOCK *CpuData\r
669 )\r
670{\r
671}\r
672\r
673/**\r
674 Application Processors do loop routine\r
675 after switch to its own stack.\r
676\r
677 @param Context1 A pointer to the context to pass into the function.\r
678 @param Context2 A pointer to the context to pass into the function.\r
679\r
680**/\r
681VOID\r
682ProcessorToIdleState (\r
683 IN VOID *Context1, OPTIONAL\r
684 IN VOID *Context2 OPTIONAL\r
685 )\r
686{\r
687 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
688\r
689 AsmApDoneWithCommonStack ();\r
690\r
691 CpuSleep ();\r
692 CpuDeadLoop ();\r
693}\r
694\r
695/**\r
696 Checks AP' status periodically.\r
697\r
698 This function is triggerred by timer perodically to check the\r
699 state of AP forStartupThisAP() executed in non-blocking mode.\r
700\r
701 @param Event Event triggered.\r
702 @param Context Parameter passed with the event.\r
703\r
704**/\r
705VOID\r
706EFIAPI\r
707CheckThisAPStatus (\r
708 IN EFI_EVENT Event,\r
709 IN VOID *Context\r
710 )\r
711{\r
712 CPU_DATA_BLOCK *CpuData;\r
713 CPU_STATE CpuState;\r
714\r
715 CpuData = (CPU_DATA_BLOCK *) Context;\r
716 if (CpuData->TimeoutActive) {\r
717 CpuData->Timeout -= gPollInterval;\r
718 }\r
719\r
720 CpuState = GetApState (CpuData);\r
721\r
722 if (CpuState == CpuStateFinished) {\r
723 if (CpuData->Finished) {\r
724 *CpuData->Finished = TRUE;\r
725 }\r
726 SetApState (CpuData, CpuStateIdle);\r
727 goto out;\r
728 }\r
729\r
730 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
731 if (CpuState != CpuStateIdle &&\r
732 CpuData->Finished) {\r
733 *CpuData->Finished = FALSE;\r
734 }\r
735 ResetProcessorToIdleState (CpuData);\r
736 goto out;\r
737 }\r
738\r
739 return;\r
740\r
741out:\r
742 gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);\r
743 if (CpuData->WaitEvent) {\r
744 gBS->SignalEvent (CpuData->WaitEvent);\r
745 CpuData->WaitEvent = NULL;\r
746 }\r
747}\r
748\r
749/**\r
750 Application Processor C code entry point.\r
751\r
752**/\r
753VOID\r
754EFIAPI\r
755ApEntryPointInC (\r
756 VOID\r
757 )\r
758{\r
759 VOID* TopOfApStack;\r
760\r
761 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
762 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
763 mApStackStart = TopOfApStack;\r
764\r
765 mMpSystemData.NumberOfProcessors++;\r
766\r
767 SwitchStack (\r
768 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
769 NULL,\r
770 NULL,\r
771 TopOfApStack);\r
772}\r
773\r
774/**\r
775 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
776\r
777 @param Bsp TRUE if the CPU is BSP\r
778 @param ProcessorNumber The specific processor number\r
779\r
780 @retval EFI_SUCCESS Data for the processor collected and filled in\r
781\r
782**/\r
783EFI_STATUS\r
784FillInProcessorInformation (\r
785 IN BOOLEAN Bsp,\r
786 IN UINTN ProcessorNumber\r
787 )\r
788{\r
789 CPU_DATA_BLOCK *CpuData;\r
790 UINT32 ProcessorId;\r
791\r
792 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
793 ProcessorId = GetApicId ();\r
794 CpuData->Info.ProcessorId = ProcessorId;\r
795 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
796 if (Bsp) {\r
797 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
798 }\r
799 CpuData->Info.Location.Package = ProcessorId;\r
800 CpuData->Info.Location.Core = 0;\r
801 CpuData->Info.Location.Thread = 0;\r
802 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
803\r
804 CpuData->Procedure = NULL;\r
805 CpuData->Parameter = NULL;\r
806 InitializeSpinLock (&CpuData->CpuDataLock);\r
807\r
808 return EFI_SUCCESS;\r
809}\r
810\r
811/**\r
812 Prepare the System Data.\r
813\r
814 @retval EFI_SUCCESS the System Data finished initilization.\r
815\r
816**/\r
817EFI_STATUS\r
818InitMpSystemData (\r
819 VOID\r
820 )\r
821{\r
822 UINTN ProcessorNumber;\r
823 CPU_DATA_BLOCK *CpuData;\r
824 EFI_STATUS Status;\r
825\r
826 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
827\r
828 mMpSystemData.NumberOfProcessors = 1;\r
829 mMpSystemData.NumberOfEnabledProcessors = 1;\r
830\r
831 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
832 ASSERT(mMpSystemData.CpuDatas != NULL);\r
833\r
834 for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {\r
835 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
836 Status = gBS->CreateEvent (\r
837 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
838 TPL_CALLBACK,\r
839 CheckThisAPStatus,\r
840 (VOID *) CpuData,\r
841 &CpuData->CheckThisAPEvent\r
842 );\r
843 ASSERT_EFI_ERROR (Status);\r
844 }\r
845\r
846 //\r
847 // BSP\r
848 //\r
849 FillInProcessorInformation (TRUE, 0);\r
850\r
851 return EFI_SUCCESS;\r
852}\r
853\r
854/**\r
855 Initialize Multi-processor support.\r
856\r
857**/\r
858VOID\r
859InitializeMpSupport (\r
860 VOID\r
861 )\r
862{\r
863 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
864 if (gMaxLogicalProcessorNumber < 1) {\r
865 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
866 return;\r
867 }\r
868\r
869 if (gMaxLogicalProcessorNumber == 1) {\r
870 return;\r
871 }\r
872\r
873 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
874 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
875\r
876 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
877 ASSERT (mApStackStart != NULL);\r
878\r
879 //\r
880 // the first buffer of stack size used for common stack, when the amount of AP\r
881 // more than 1, we should never free the common stack which maybe used for AP reset.\r
882 //\r
883 mCommonStack = mApStackStart;\r
884 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
885 mApStackStart = mTopOfApCommonStack;\r
886\r
887 InitMpSystemData ();\r
888\r
889 if (mMpSystemData.NumberOfProcessors == 1) {\r
890 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
891 return;\r
892 }\r
893\r
894 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
895 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
896 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
897 gApStackSize));\r
898 }\r
899}\r