]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmulatorPkg/CpuRuntimeDxe/MpService.c
IntelFsp2WrapperPkg: Update gFspWrapperTokenSpaceGuid to gIntelFsp2WrapperTokenSpaceGuid.
[mirror_edk2.git] / EmulatorPkg / CpuRuntimeDxe / MpService.c
... / ...
CommitLineData
1/** @file\r
2 Construct MP Services Protocol on top of the EMU Thread protocol.\r
3 This code makes APs show up in the emulator. PcdEmuApCount is the\r
4 number of APs the emulator should produce.\r
5\r
6 The MP Services Protocol provides a generalized way of performing following tasks:\r
7 - Retrieving information of multi-processor environment and MP-related status of\r
8 specific processors.\r
9 - Dispatching user-provided function to APs.\r
10 - Maintain MP-related processor status.\r
11\r
12 The MP Services Protocol must be produced on any system with more than one logical\r
13 processor.\r
14\r
15 The Protocol is available only during boot time.\r
16\r
17 MP Services Protocol is hardware-independent. Most of the logic of this protocol\r
18 is architecturally neutral. It abstracts the multi-processor environment and\r
19 status of processors, and provides interfaces to retrieve information, maintain,\r
20 and dispatch.\r
21\r
22 MP Services Protocol may be consumed by ACPI module. The ACPI module may use this\r
23 protocol to retrieve data that are needed for an MP platform and report them to OS.\r
24 MP Services Protocol may also be used to program and configure processors, such\r
25 as MTRR synchronization for memory space attributes setting in DXE Services.\r
26 MP Services Protocol may be used by non-CPU DXE drivers to speed up platform boot\r
27 by taking advantage of the processing capabilities of the APs, for example, using\r
28 APs to help test system memory in parallel with other device initialization.\r
29 Diagnostics applications may also use this protocol for multi-processor.\r
30\r
31Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
32Portitions Copyright (c) 2011, Apple Inc. All rights reserved.\r
33This program and the accompanying materials are licensed and made available under\r
34the terms and conditions of the BSD License that accompanies this distribution.\r
35The full text of the license may be found at\r
36http://opensource.org/licenses/bsd-license.php.\r
37\r
38THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
39WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
40\r
41\r
42**/\r
43\r
44#include "CpuDriver.h"\r
45\r
46\r
47MP_SYSTEM_DATA gMPSystem;\r
48EMU_THREAD_THUNK_PROTOCOL *gThread = NULL;\r
49EFI_EVENT gReadToBootEvent;\r
50BOOLEAN gReadToBoot = FALSE;\r
51UINTN gPollInterval;\r
52\r
53\r
54BOOLEAN\r
55IsBSP (\r
56 VOID\r
57 )\r
58{\r
59 EFI_STATUS Status;\r
60 UINTN ProcessorNumber;\r
61\r
62 Status = CpuMpServicesWhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
63 if (EFI_ERROR (Status)) {\r
64 return FALSE;\r
65 }\r
66\r
67 return (gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0;\r
68}\r
69\r
70\r
71VOID\r
72SetApProcedure (\r
73 IN PROCESSOR_DATA_BLOCK *Processor,\r
74 IN EFI_AP_PROCEDURE Procedure,\r
75 IN VOID *ProcedureArgument\r
76 )\r
77{\r
78 gThread->MutexLock (Processor->ProcedureLock);\r
79 Processor->Parameter = ProcedureArgument;\r
80 Processor->Procedure = Procedure;\r
81 gThread->MutexUnlock (Processor->ProcedureLock);\r
82}\r
83\r
84\r
85EFI_STATUS\r
86GetNextBlockedNumber (\r
87 OUT UINTN *NextNumber\r
88 )\r
89{\r
90 UINTN Number;\r
91 PROCESSOR_STATE ProcessorState;\r
92 PROCESSOR_DATA_BLOCK *Data;\r
93\r
94 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
95 Data = &gMPSystem.ProcessorData[Number];\r
96 if ((Data->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
97 // Skip BSP\r
98 continue;\r
99 }\r
100\r
101 gThread->MutexLock (Data->StateLock);\r
102 ProcessorState = Data->State;\r
103 gThread->MutexUnlock (Data->StateLock);\r
104\r
105 if (ProcessorState == CPU_STATE_BLOCKED) {\r
106 *NextNumber = Number;\r
107 return EFI_SUCCESS;\r
108 }\r
109 }\r
110\r
111 return EFI_NOT_FOUND;\r
112}\r
113\r
114/**\r
115 * Calculated and stalled the interval time by BSP to check whether\r
116 * the APs have finished.\r
117 *\r
118 * @param[in] Timeout The time limit in microseconds for\r
119 * APs to return from Procedure.\r
120 *\r
121 * @retval StallTime Time of execution stall.\r
122**/\r
123UINTN\r
124CalculateAndStallInterval (\r
125 IN UINTN Timeout\r
126 )\r
127{\r
128 UINTN StallTime;\r
129\r
130 if (Timeout < gPollInterval && Timeout != 0) {\r
131 StallTime = Timeout;\r
132 } else {\r
133 StallTime = gPollInterval;\r
134 }\r
135 gBS->Stall (StallTime);\r
136\r
137 return StallTime;\r
138}\r
139\r
140/**\r
141 This service retrieves the number of logical processor in the platform\r
142 and the number of those logical processors that are enabled on this boot.\r
143 This service may only be called from the BSP.\r
144\r
145 This function is used to retrieve the following information:\r
146 - The number of logical processors that are present in the system.\r
147 - The number of enabled logical processors in the system at the instant\r
148 this call is made.\r
149\r
150 Because MP Service Protocol provides services to enable and disable processors\r
151 dynamically, the number of enabled logical processors may vary during the\r
152 course of a boot session.\r
153\r
154 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
155 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
156 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
157 is returned in NumberOfProcessors, the number of currently enabled processor\r
158 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
159\r
160 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
161 instance.\r
162 @param[out] NumberOfProcessors Pointer to the total number of logical\r
163 processors in the system, including the BSP\r
164 and disabled APs.\r
165 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
166 processors that exist in system, including\r
167 the BSP.\r
168\r
169 @retval EFI_SUCCESS The number of logical processors and enabled\r
170 logical processors was retrieved.\r
171 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
172 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
173 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
174\r
175**/\r
176EFI_STATUS\r
177EFIAPI\r
178CpuMpServicesGetNumberOfProcessors (\r
179 IN EFI_MP_SERVICES_PROTOCOL *This,\r
180 OUT UINTN *NumberOfProcessors,\r
181 OUT UINTN *NumberOfEnabledProcessors\r
182 )\r
183{\r
184 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
185 return EFI_INVALID_PARAMETER;\r
186 }\r
187\r
188 if (!IsBSP ()) {\r
189 return EFI_DEVICE_ERROR;\r
190 }\r
191\r
192 *NumberOfProcessors = gMPSystem.NumberOfProcessors;\r
193 *NumberOfEnabledProcessors = gMPSystem.NumberOfEnabledProcessors;\r
194 return EFI_SUCCESS;\r
195}\r
196\r
197\r
198\r
199/**\r
200 Gets detailed MP-related information on the requested processor at the\r
201 instant this call is made. This service may only be called from the BSP.\r
202\r
203 This service retrieves detailed MP-related information about any processor\r
204 on the platform. Note the following:\r
205 - The processor information may change during the course of a boot session.\r
206 - The information presented here is entirely MP related.\r
207\r
208 Information regarding the number of caches and their sizes, frequency of operation,\r
209 slot numbers is all considered platform-related information and is not provided\r
210 by this service.\r
211\r
212 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
213 instance.\r
214 @param[in] ProcessorNumber The handle number of processor.\r
215 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
216 the requested processor is deposited.\r
217\r
218 @retval EFI_SUCCESS Processor information was returned.\r
219 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
220 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
221 @retval EFI_NOT_FOUND The processor with the handle specified by\r
222 ProcessorNumber does not exist in the platform.\r
223\r
224**/\r
225EFI_STATUS\r
226EFIAPI\r
227CpuMpServicesGetProcessorInfo (\r
228 IN EFI_MP_SERVICES_PROTOCOL *This,\r
229 IN UINTN ProcessorNumber,\r
230 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
231 )\r
232{\r
233 if (ProcessorInfoBuffer == NULL) {\r
234 return EFI_INVALID_PARAMETER;\r
235 }\r
236\r
237 if (!IsBSP ()) {\r
238 return EFI_DEVICE_ERROR;\r
239 }\r
240\r
241 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
242 return EFI_NOT_FOUND;\r
243 }\r
244\r
245 CopyMem (ProcessorInfoBuffer, &gMPSystem.ProcessorData[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
246 return EFI_SUCCESS;\r
247}\r
248\r
249\r
250/**\r
251 This service executes a caller provided function on all enabled APs. APs can\r
252 run either simultaneously or one at a time in sequence. This service supports\r
253 both blocking and non-blocking requests. The non-blocking requests use EFI\r
254 events so the BSP can detect when the APs have finished. This service may only\r
255 be called from the BSP.\r
256\r
257 This function is used to dispatch all the enabled APs to the function specified\r
258 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
259 immediately and Procedure is not started on any AP.\r
260\r
261 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
262 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
263 all the enabled APs execute the function specified by Procedure simultaneously.\r
264\r
265 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all\r
266 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking\r
267 mode, and the BSP returns from this service without waiting for APs. If a\r
268 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
269 is signaled, then EFI_UNSUPPORTED must be returned.\r
270\r
271 If the timeout specified by TimeoutInMicroseconds expires before all APs return\r
272 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
273 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
274 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its\r
275 content points to the list of processor handle numbers in which Procedure was\r
276 terminated.\r
277\r
278 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
279 to make sure that the nature of the code that is executed on the BSP and the\r
280 dispatched APs is well controlled. The MP Services Protocol does not guarantee\r
281 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
282 parallel are limited to certain independent tasks and well-controlled exclusive\r
283 code. EFI services and protocols may not be called by APs unless otherwise\r
284 specified.\r
285\r
286 In blocking execution mode, BSP waits until all APs finish or\r
287 TimeoutInMicroseconds expires.\r
288\r
289 In non-blocking execution mode, BSP is freed to return to the caller and then\r
290 proceed to the next task without having to wait for APs. The following\r
291 sequence needs to occur in a non-blocking execution mode:\r
292\r
293 -# The caller that intends to use this MP Services Protocol in non-blocking\r
294 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller\r
295 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent\r
296 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests\r
297 the function specified by Procedure to be started on all the enabled APs,\r
298 and releases the BSP to continue with other tasks.\r
299 -# The caller can use the CheckEvent() and WaitForEvent() services to check\r
300 the state of the WaitEvent created in step 1.\r
301 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP\r
302 Service signals WaitEvent by calling the EFI SignalEvent() function. If\r
303 FailedCpuList is not NULL, its content is available when WaitEvent is\r
304 signaled. If all APs returned from Procedure prior to the timeout, then\r
305 FailedCpuList is set to NULL. If not all APs return from Procedure before\r
306 the timeout, then FailedCpuList is filled in with the list of the failed\r
307 APs. The buffer is allocated by MP Service Protocol using AllocatePool().\r
308 It is the caller's responsibility to free the buffer with FreePool() service.\r
309 -# This invocation of SignalEvent() function informs the caller that invoked\r
310 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed\r
311 the specified task or a timeout occurred. The contents of FailedCpuList\r
312 can be examined to determine which APs did not complete the specified task\r
313 prior to the timeout.\r
314\r
315 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
316 instance.\r
317 @param[in] Procedure A pointer to the function to be run on\r
318 enabled APs of the system. See type\r
319 EFI_AP_PROCEDURE.\r
320 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
321 the function specified by Procedure one by\r
322 one, in ascending order of processor handle\r
323 number. If FALSE, then all the enabled APs\r
324 execute the function specified by Procedure\r
325 simultaneously.\r
326 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
327 service. If it is NULL, then execute in\r
328 blocking mode. BSP waits until all APs finish\r
329 or TimeoutInMicroseconds expires. If it's\r
330 not NULL, then execute in non-blocking mode.\r
331 BSP requests the function specified by\r
332 Procedure to be started on all the enabled\r
333 APs, and go on executing immediately. If\r
334 all return from Procedure, or TimeoutInMicroseconds\r
335 expires, this event is signaled. The BSP\r
336 can use the CheckEvent() or WaitForEvent()\r
337 services to check the state of event. Type\r
338 EFI_EVENT is defined in CreateEvent() in\r
339 the Unified Extensible Firmware Interface\r
340 Specification.\r
341 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for\r
342 APs to return from Procedure, either for\r
343 blocking or non-blocking mode. Zero means\r
344 infinity. If the timeout expires before\r
345 all APs return from Procedure, then Procedure\r
346 on the failed APs is terminated. All enabled\r
347 APs are available for next function assigned\r
348 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
349 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
350 If the timeout expires in blocking mode,\r
351 BSP returns EFI_TIMEOUT. If the timeout\r
352 expires in non-blocking mode, WaitEvent\r
353 is signaled with SignalEvent().\r
354 @param[in] ProcedureArgument The parameter passed into Procedure for\r
355 all APs.\r
356 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
357 if all APs finish successfully, then its\r
358 content is set to NULL. If not all APs\r
359 finish before timeout expires, then its\r
360 content is set to address of the buffer\r
361 holding handle numbers of the failed APs.\r
362 The buffer is allocated by MP Service Protocol,\r
363 and it's the caller's responsibility to\r
364 free the buffer with FreePool() service.\r
365 In blocking mode, it is ready for consumption\r
366 when the call returns. In non-blocking mode,\r
367 it is ready when WaitEvent is signaled. The\r
368 list of failed CPU is terminated by\r
369 END_OF_CPU_LIST.\r
370\r
371 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
372 the timeout expired.\r
373 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
374 to all enabled APs.\r
375 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
376 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
377 signaled.\r
378 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
379 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
380 @retval EFI_NOT_READY Any enabled APs are busy.\r
381 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
382 all enabled APs have finished.\r
383 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
384\r
385**/\r
386EFI_STATUS\r
387EFIAPI\r
388CpuMpServicesStartupAllAps (\r
389 IN EFI_MP_SERVICES_PROTOCOL *This,\r
390 IN EFI_AP_PROCEDURE Procedure,\r
391 IN BOOLEAN SingleThread,\r
392 IN EFI_EVENT WaitEvent OPTIONAL,\r
393 IN UINTN TimeoutInMicroseconds,\r
394 IN VOID *ProcedureArgument OPTIONAL,\r
395 OUT UINTN **FailedCpuList OPTIONAL\r
396 )\r
397{\r
398 EFI_STATUS Status;\r
399 PROCESSOR_DATA_BLOCK *ProcessorData;\r
400 UINTN Number;\r
401 UINTN NextNumber;\r
402 PROCESSOR_STATE APInitialState;\r
403 PROCESSOR_STATE ProcessorState;\r
404 UINTN Timeout;\r
405\r
406\r
407 if (!IsBSP ()) {\r
408 return EFI_DEVICE_ERROR;\r
409 }\r
410\r
411 if (gMPSystem.NumberOfProcessors == 1) {\r
412 return EFI_NOT_STARTED;\r
413 }\r
414\r
415 if (Procedure == NULL) {\r
416 return EFI_INVALID_PARAMETER;\r
417 }\r
418\r
419 if ((WaitEvent != NULL) && gReadToBoot) {\r
420 return EFI_UNSUPPORTED;\r
421 }\r
422\r
423 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
424 ProcessorData = &gMPSystem.ProcessorData[Number];\r
425 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
426 // Skip BSP\r
427 continue;\r
428 }\r
429\r
430 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
431 // Skip Disabled processors\r
432 continue;\r
433 }\r
434 gThread->MutexLock(ProcessorData->StateLock);\r
435 if (ProcessorData->State != CPU_STATE_IDLE) {\r
436 gThread->MutexUnlock (ProcessorData->StateLock);\r
437 return EFI_NOT_READY;\r
438 }\r
439 gThread->MutexUnlock(ProcessorData->StateLock);\r
440 }\r
441\r
442 if (FailedCpuList != NULL) {\r
443 gMPSystem.FailedList = AllocatePool ((gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN));\r
444 if (gMPSystem.FailedList == NULL) {\r
445 return EFI_OUT_OF_RESOURCES;\r
446 }\r
447 SetMemN (gMPSystem.FailedList, (gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN), END_OF_CPU_LIST);\r
448 gMPSystem.FailedListIndex = 0;\r
449 *FailedCpuList = gMPSystem.FailedList;\r
450 }\r
451\r
452 Timeout = TimeoutInMicroseconds;\r
453\r
454 ProcessorData = NULL;\r
455\r
456 gMPSystem.FinishCount = 0;\r
457 gMPSystem.StartCount = 0;\r
458 gMPSystem.SingleThread = SingleThread;\r
459 APInitialState = CPU_STATE_READY;\r
460\r
461 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
462 ProcessorData = &gMPSystem.ProcessorData[Number];\r
463\r
464 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
465 // Skip BSP\r
466 continue;\r
467 }\r
468\r
469 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
470 // Skip Disabled processors\r
471 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Number;\r
472 continue;\r
473 }\r
474\r
475 //\r
476 // Get APs prepared, and put failing APs into FailedCpuList\r
477 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready\r
478 // state 1 by 1, until the previous 1 finished its task\r
479 // if not "SingleThread", all APs are put to ready state from the beginning\r
480 //\r
481 gThread->MutexLock(ProcessorData->StateLock);\r
482 ASSERT (ProcessorData->State == CPU_STATE_IDLE);\r
483 ProcessorData->State = APInitialState;\r
484 gThread->MutexUnlock (ProcessorData->StateLock);\r
485\r
486 gMPSystem.StartCount++;\r
487 if (SingleThread) {\r
488 APInitialState = CPU_STATE_BLOCKED;\r
489 }\r
490 }\r
491\r
492 if (WaitEvent != NULL) {\r
493 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
494 ProcessorData = &gMPSystem.ProcessorData[Number];\r
495 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
496 // Skip BSP\r
497 continue;\r
498 }\r
499\r
500 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
501 // Skip Disabled processors\r
502 continue;\r
503 }\r
504\r
505 gThread->MutexLock (ProcessorData->StateLock);\r
506 ProcessorState = ProcessorData->State;\r
507 gThread->MutexUnlock (ProcessorData->StateLock);\r
508\r
509 if (ProcessorState == CPU_STATE_READY) {\r
510 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);\r
511 }\r
512 }\r
513\r
514 //\r
515 // Save data into private data structure, and create timer to poll AP state before exiting\r
516 //\r
517 gMPSystem.Procedure = Procedure;\r
518 gMPSystem.ProcedureArgument = ProcedureArgument;\r
519 gMPSystem.WaitEvent = WaitEvent;\r
520 gMPSystem.Timeout = TimeoutInMicroseconds;\r
521 gMPSystem.TimeoutActive = (BOOLEAN)(TimeoutInMicroseconds != 0);\r
522 Status = gBS->SetTimer (\r
523 gMPSystem.CheckAllAPsEvent,\r
524 TimerPeriodic,\r
525 gPollInterval\r
526 );\r
527 return Status;\r
528\r
529 }\r
530\r
531 while (TRUE) {\r
532 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
533 ProcessorData = &gMPSystem.ProcessorData[Number];\r
534 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
535 // Skip BSP\r
536 continue;\r
537 }\r
538\r
539 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
540 // Skip Disabled processors\r
541 continue;\r
542 }\r
543\r
544 gThread->MutexLock (ProcessorData->StateLock);\r
545 ProcessorState = ProcessorData->State;\r
546 gThread->MutexUnlock (ProcessorData->StateLock);\r
547\r
548 switch (ProcessorState) {\r
549 case CPU_STATE_READY:\r
550 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);\r
551 break;\r
552\r
553 case CPU_STATE_FINISHED:\r
554 gMPSystem.FinishCount++;\r
555 if (SingleThread) {\r
556 Status = GetNextBlockedNumber (&NextNumber);\r
557 if (!EFI_ERROR (Status)) {\r
558 gThread->MutexLock (gMPSystem.ProcessorData[NextNumber].StateLock);\r
559 gMPSystem.ProcessorData[NextNumber].State = CPU_STATE_READY;\r
560 gThread->MutexUnlock (gMPSystem.ProcessorData[NextNumber].StateLock);\r
561 }\r
562 }\r
563\r
564 gThread->MutexLock (ProcessorData->StateLock);\r
565 ProcessorData->State = CPU_STATE_IDLE;\r
566 gThread->MutexUnlock (ProcessorData->StateLock);\r
567\r
568 break;\r
569\r
570 default:\r
571 break;\r
572 }\r
573 }\r
574\r
575 if (gMPSystem.FinishCount == gMPSystem.StartCount) {\r
576 Status = EFI_SUCCESS;\r
577 goto Done;\r
578 }\r
579\r
580 if ((TimeoutInMicroseconds != 0) && (Timeout == 0)) {\r
581 Status = EFI_TIMEOUT;\r
582 goto Done;\r
583 }\r
584\r
585 Timeout -= CalculateAndStallInterval (Timeout);\r
586 }\r
587\r
588Done:\r
589 if (FailedCpuList != NULL) {\r
590 if (gMPSystem.FailedListIndex == 0) {\r
591 FreePool (*FailedCpuList);\r
592 *FailedCpuList = NULL;\r
593 }\r
594 }\r
595\r
596 return EFI_SUCCESS;\r
597}\r
598\r
599\r
600/**\r
601 This service lets the caller get one enabled AP to execute a caller-provided\r
602 function. The caller can request the BSP to either wait for the completion\r
603 of the AP or just proceed with the next task by using the EFI event mechanism.\r
604 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
605 execution support. This service may only be called from the BSP.\r
606\r
607 This function is used to dispatch one enabled AP to the function specified by\r
608 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
609 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
610 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
611 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
612 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
613 then EFI_UNSUPPORTED must be returned.\r
614\r
615 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
616 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
617 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
618 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
619\r
620 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
621 instance.\r
622 @param[in] Procedure A pointer to the function to be run on\r
623 enabled APs of the system. See type\r
624 EFI_AP_PROCEDURE.\r
625 @param[in] ProcessorNumber The handle number of the AP. The range is\r
626 from 0 to the total number of logical\r
627 processors minus 1. The total number of\r
628 logical processors can be retrieved by\r
629 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
630 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
631 service. If it is NULL, then execute in\r
632 blocking mode. BSP waits until all APs finish\r
633 or TimeoutInMicroseconds expires. If it's\r
634 not NULL, then execute in non-blocking mode.\r
635 BSP requests the function specified by\r
636 Procedure to be started on all the enabled\r
637 APs, and go on executing immediately. If\r
638 all return from Procedure or TimeoutInMicroseconds\r
639 expires, this event is signaled. The BSP\r
640 can use the CheckEvent() or WaitForEvent()\r
641 services to check the state of event. Type\r
642 EFI_EVENT is defined in CreateEvent() in\r
643 the Unified Extensible Firmware Interface\r
644 Specification.\r
645 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for\r
646 APs to return from Procedure, either for\r
647 blocking or non-blocking mode. Zero means\r
648 infinity. If the timeout expires before\r
649 all APs return from Procedure, then Procedure\r
650 on the failed APs is terminated. All enabled\r
651 APs are available for next function assigned\r
652 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
653 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
654 If the timeout expires in blocking mode,\r
655 BSP returns EFI_TIMEOUT. If the timeout\r
656 expires in non-blocking mode, WaitEvent\r
657 is signaled with SignalEvent().\r
658 @param[in] ProcedureArgument The parameter passed into Procedure for\r
659 all APs.\r
660 @param[out] Finished If NULL, this parameter is ignored. In\r
661 blocking mode, this parameter is ignored.\r
662 In non-blocking mode, if AP returns from\r
663 Procedure before the timeout expires, its\r
664 content is set to TRUE. Otherwise, the\r
665 value is set to FALSE. The caller can\r
666 determine if the AP returned from Procedure\r
667 by evaluating this value.\r
668\r
669 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
670 the timeout expires.\r
671 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
672 dispatched to specified AP.\r
673 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
674 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
675 signaled.\r
676 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
677 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
678 the specified AP has finished.\r
679 @retval EFI_NOT_READY The specified AP is busy.\r
680 @retval EFI_NOT_FOUND The processor with the handle specified by\r
681 ProcessorNumber does not exist.\r
682 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
683 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
684\r
685**/\r
686EFI_STATUS\r
687EFIAPI\r
688CpuMpServicesStartupThisAP (\r
689 IN EFI_MP_SERVICES_PROTOCOL *This,\r
690 IN EFI_AP_PROCEDURE Procedure,\r
691 IN UINTN ProcessorNumber,\r
692 IN EFI_EVENT WaitEvent OPTIONAL,\r
693 IN UINTN TimeoutInMicroseconds,\r
694 IN VOID *ProcedureArgument OPTIONAL,\r
695 OUT BOOLEAN *Finished OPTIONAL\r
696 )\r
697{\r
698 UINTN Timeout;\r
699\r
700 if (!IsBSP ()) {\r
701 return EFI_DEVICE_ERROR;\r
702 }\r
703\r
704 if (Procedure == NULL) {\r
705 return EFI_INVALID_PARAMETER;\r
706 }\r
707\r
708 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
709 return EFI_NOT_FOUND;\r
710 }\r
711\r
712 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
713 return EFI_INVALID_PARAMETER;\r
714 }\r
715\r
716 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
717 return EFI_INVALID_PARAMETER;\r
718 }\r
719\r
720 gThread->MutexLock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
721 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
722 gThread->MutexUnlock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
723 return EFI_NOT_READY;\r
724 }\r
725 gThread->MutexUnlock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
726\r
727 if ((WaitEvent != NULL) && gReadToBoot) {\r
728 return EFI_UNSUPPORTED;\r
729 }\r
730\r
731 Timeout = TimeoutInMicroseconds;\r
732\r
733 gMPSystem.StartCount = 1;\r
734 gMPSystem.FinishCount = 0;\r
735\r
736 SetApProcedure (&gMPSystem.ProcessorData[ProcessorNumber], Procedure, ProcedureArgument);\r
737\r
738 if (WaitEvent != NULL) {\r
739 // Non Blocking\r
740 gMPSystem.WaitEvent = WaitEvent;\r
741 gBS->SetTimer (\r
742 gMPSystem.ProcessorData[ProcessorNumber].CheckThisAPEvent,\r
743 TimerPeriodic,\r
744 gPollInterval\r
745 );\r
746 return EFI_SUCCESS;\r
747 }\r
748\r
749 // Blocking\r
750 while (TRUE) {\r
751 gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
752 if (gMPSystem.ProcessorData[ProcessorNumber].State == CPU_STATE_FINISHED) {\r
753 gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;\r
754 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
755 break;\r
756 }\r
757\r
758 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
759\r
760 if ((TimeoutInMicroseconds != 0) && (Timeout == 0)) {\r
761 return EFI_TIMEOUT;\r
762 }\r
763\r
764 Timeout -= CalculateAndStallInterval (Timeout);\r
765 }\r
766\r
767 return EFI_SUCCESS;\r
768\r
769}\r
770\r
771\r
772/**\r
773 This service switches the requested AP to be the BSP from that point onward.\r
774 This service changes the BSP for all purposes. This call can only be performed\r
775 by the current BSP.\r
776\r
777 This service switches the requested AP to be the BSP from that point onward.\r
778 This service changes the BSP for all purposes. The new BSP can take over the\r
779 execution of the old BSP and continue seamlessly from where the old one left\r
780 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
781 is signaled.\r
782\r
783 If the BSP cannot be switched prior to the return from this service, then\r
784 EFI_UNSUPPORTED must be returned.\r
785\r
786 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
787 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
788 BSP. The range is from 0 to the total number of\r
789 logical processors minus 1. The total number of\r
790 logical processors can be retrieved by\r
791 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
792 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
793 enabled AP. Otherwise, it will be disabled.\r
794\r
795 @retval EFI_SUCCESS BSP successfully switched.\r
796 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
797 this service returning.\r
798 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
799 @retval EFI_SUCCESS The calling processor is an AP.\r
800 @retval EFI_NOT_FOUND The processor with the handle specified by\r
801 ProcessorNumber does not exist.\r
802 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
803 a disabled AP.\r
804 @retval EFI_NOT_READY The specified AP is busy.\r
805\r
806**/\r
807EFI_STATUS\r
808EFIAPI\r
809CpuMpServicesSwitchBSP (\r
810 IN EFI_MP_SERVICES_PROTOCOL *This,\r
811 IN UINTN ProcessorNumber,\r
812 IN BOOLEAN EnableOldBSP\r
813 )\r
814{\r
815 UINTN Index;\r
816\r
817 if (!IsBSP ()) {\r
818 return EFI_DEVICE_ERROR;\r
819 }\r
820\r
821 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
822 return EFI_NOT_FOUND;\r
823 }\r
824\r
825 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
826 return EFI_INVALID_PARAMETER;\r
827 }\r
828\r
829 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
830 return EFI_INVALID_PARAMETER;\r
831 }\r
832\r
833 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
834 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
835 break;\r
836 }\r
837 }\r
838 ASSERT (Index != gMPSystem.NumberOfProcessors);\r
839\r
840 gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
841 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
842 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
843 return EFI_NOT_READY;\r
844 }\r
845 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
846\r
847 // Skip for now as we need switch a bunch of stack stuff around and it's complex\r
848 // May not be worth it?\r
849 return EFI_NOT_READY;\r
850}\r
851\r
852\r
853/**\r
854 This service lets the caller enable or disable an AP from this point onward.\r
855 This service may only be called from the BSP.\r
856\r
857 This service allows the caller enable or disable an AP from this point onward.\r
858 The caller can optionally specify the health status of the AP by Health. If\r
859 an AP is being disabled, then the state of the disabled AP is implementation\r
860 dependent. If an AP is enabled, then the implementation must guarantee that a\r
861 complete initialization sequence is performed on the AP, so the AP is in a state\r
862 that is compatible with an MP operating system. This service may not be supported\r
863 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
864\r
865 If the enable or disable AP operation cannot be completed prior to the return\r
866 from this service, then EFI_UNSUPPORTED must be returned.\r
867\r
868 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
869 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
870 BSP. The range is from 0 to the total number of\r
871 logical processors minus 1. The total number of\r
872 logical processors can be retrieved by\r
873 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
874 @param[in] EnableAP Specifies the new state for the processor for\r
875 enabled, FALSE for disabled.\r
876 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
877 the new health status of the AP. This flag\r
878 corresponds to StatusFlag defined in\r
879 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
880 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
881 bits are ignored. If it is NULL, this parameter\r
882 is ignored.\r
883\r
884 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
885 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
886 prior to this service returning.\r
887 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
888 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
889 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
890 does not exist.\r
891 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
892\r
893**/\r
894EFI_STATUS\r
895EFIAPI\r
896CpuMpServicesEnableDisableAP (\r
897 IN EFI_MP_SERVICES_PROTOCOL *This,\r
898 IN UINTN ProcessorNumber,\r
899 IN BOOLEAN EnableAP,\r
900 IN UINT32 *HealthFlag OPTIONAL\r
901 )\r
902{\r
903 if (!IsBSP ()) {\r
904 return EFI_DEVICE_ERROR;\r
905 }\r
906\r
907 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
908 return EFI_NOT_FOUND;\r
909 }\r
910\r
911 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
912 return EFI_INVALID_PARAMETER;\r
913 }\r
914\r
915 gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
916 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
917 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
918 return EFI_UNSUPPORTED;\r
919 }\r
920 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
921\r
922 if (EnableAP) {\r
923 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0 ) {\r
924 gMPSystem.NumberOfEnabledProcessors++;\r
925 }\r
926 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_ENABLED_BIT;\r
927 } else {\r
928 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == PROCESSOR_ENABLED_BIT ) {\r
929 gMPSystem.NumberOfEnabledProcessors--;\r
930 }\r
931 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
932 }\r
933\r
934 if (HealthFlag != NULL) {\r
935 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;\r
936 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT);\r
937 }\r
938\r
939 return EFI_SUCCESS;\r
940}\r
941\r
942\r
943/**\r
944 This return the handle number for the calling processor. This service may be\r
945 called from the BSP and APs.\r
946\r
947 This service returns the processor handle number for the calling processor.\r
948 The returned value is in the range from 0 to the total number of logical\r
949 processors minus 1. The total number of logical processors can be retrieved\r
950 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
951 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
952 is returned. Otherwise, the current processors handle number is returned in\r
953 ProcessorNumber, and EFI_SUCCESS is returned.\r
954\r
955 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
956 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
957 BSP. The range is from 0 to the total number of\r
958 logical processors minus 1. The total number of\r
959 logical processors can be retrieved by\r
960 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
961\r
962 @retval EFI_SUCCESS The current processor handle number was returned\r
963 in ProcessorNumber.\r
964 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
965\r
966**/\r
967EFI_STATUS\r
968EFIAPI\r
969CpuMpServicesWhoAmI (\r
970 IN EFI_MP_SERVICES_PROTOCOL *This,\r
971 OUT UINTN *ProcessorNumber\r
972 )\r
973{\r
974 UINTN Index;\r
975 UINT64 ProcessorId;\r
976\r
977 if (ProcessorNumber == NULL) {\r
978 return EFI_INVALID_PARAMETER;\r
979 }\r
980\r
981 ProcessorId = gThread->Self ();\r
982 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
983 if (gMPSystem.ProcessorData[Index].Info.ProcessorId == ProcessorId) {\r
984 break;\r
985 }\r
986 }\r
987\r
988 *ProcessorNumber = Index;\r
989 return EFI_SUCCESS;\r
990}\r
991\r
992\r
993\r
994EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
995 CpuMpServicesGetNumberOfProcessors,\r
996 CpuMpServicesGetProcessorInfo,\r
997 CpuMpServicesStartupAllAps,\r
998 CpuMpServicesStartupThisAP,\r
999 CpuMpServicesSwitchBSP,\r
1000 CpuMpServicesEnableDisableAP,\r
1001 CpuMpServicesWhoAmI\r
1002};\r
1003\r
1004\r
1005\r
1006/*++\r
1007 If timeout occurs in StartupAllAps(), a timer is set, which invokes this\r
1008 procedure periodically to check whether all APs have finished.\r
1009\r
1010\r
1011--*/\r
1012VOID\r
1013EFIAPI\r
1014CpuCheckAllAPsStatus (\r
1015 IN EFI_EVENT Event,\r
1016 IN VOID *Context\r
1017 )\r
1018{\r
1019 UINTN ProcessorNumber;\r
1020 UINTN NextNumber;\r
1021 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1022 PROCESSOR_DATA_BLOCK *NextData;\r
1023 EFI_STATUS Status;\r
1024 PROCESSOR_STATE ProcessorState;\r
1025 UINTN Cpu;\r
1026 BOOLEAN Found;\r
1027\r
1028 if (gMPSystem.TimeoutActive) {\r
1029 gMPSystem.Timeout -= CalculateAndStallInterval (gMPSystem.Timeout);\r
1030 }\r
1031\r
1032 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {\r
1033 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];\r
1034 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
1035 // Skip BSP\r
1036 continue;\r
1037 }\r
1038\r
1039 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
1040 // Skip Disabled processors\r
1041 continue;\r
1042 }\r
1043\r
1044 // This is an Interrupt Service routine.\r
1045 // This can grab a lock that is held in a non-interrupt\r
1046 // context. Meaning deadlock. Which is a bad thing.\r
1047 // So, try lock it. If we can get it, cool, do our thing.\r
1048 // otherwise, just dump out & try again on the next iteration.\r
1049 Status = gThread->MutexTryLock (ProcessorData->StateLock);\r
1050 if (EFI_ERROR(Status)) {\r
1051 return;\r
1052 }\r
1053 ProcessorState = ProcessorData->State;\r
1054 gThread->MutexUnlock (ProcessorData->StateLock);\r
1055\r
1056 switch (ProcessorState) {\r
1057 case CPU_STATE_FINISHED:\r
1058 if (gMPSystem.SingleThread) {\r
1059 Status = GetNextBlockedNumber (&NextNumber);\r
1060 if (!EFI_ERROR (Status)) {\r
1061 NextData = &gMPSystem.ProcessorData[NextNumber];\r
1062\r
1063 gThread->MutexLock (NextData->StateLock);\r
1064 NextData->State = CPU_STATE_READY;\r
1065 gThread->MutexUnlock (NextData->StateLock);\r
1066\r
1067 SetApProcedure (NextData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);\r
1068 }\r
1069 }\r
1070\r
1071 gThread->MutexLock (ProcessorData->StateLock);\r
1072 ProcessorData->State = CPU_STATE_IDLE;\r
1073 gThread->MutexUnlock (ProcessorData->StateLock);\r
1074 gMPSystem.FinishCount++;\r
1075 break;\r
1076\r
1077 default:\r
1078 break;\r
1079 }\r
1080 }\r
1081\r
1082 if (gMPSystem.TimeoutActive && gMPSystem.Timeout == 0) {\r
1083 //\r
1084 // Timeout\r
1085 //\r
1086 if (gMPSystem.FailedList != NULL) {\r
1087 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {\r
1088 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];\r
1089 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
1090 // Skip BSP\r
1091 continue;\r
1092 }\r
1093\r
1094 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
1095 // Skip Disabled processors\r
1096 continue;\r
1097 }\r
1098\r
1099 // Mark the\r
1100 Status = gThread->MutexTryLock (ProcessorData->StateLock);\r
1101 if (EFI_ERROR(Status)) {\r
1102 return;\r
1103 }\r
1104 ProcessorState = ProcessorData->State;\r
1105 gThread->MutexUnlock (ProcessorData->StateLock);\r
1106\r
1107 if (ProcessorState != CPU_STATE_IDLE) {\r
1108 // If we are retrying make sure we don't double count\r
1109 for (Cpu = 0, Found = FALSE; Cpu < gMPSystem.NumberOfProcessors; Cpu++) {\r
1110 if (gMPSystem.FailedList[Cpu] == END_OF_CPU_LIST) {\r
1111 break;\r
1112 }\r
1113 if (gMPSystem.FailedList[ProcessorNumber] == Cpu) {\r
1114 Found = TRUE;\r
1115 break;\r
1116 }\r
1117 }\r
1118 if (!Found) {\r
1119 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Cpu;\r
1120 }\r
1121 }\r
1122 }\r
1123 }\r
1124 // Force terminal exit\r
1125 gMPSystem.FinishCount = gMPSystem.StartCount;\r
1126 }\r
1127\r
1128 if (gMPSystem.FinishCount != gMPSystem.StartCount) {\r
1129 return;\r
1130 }\r
1131\r
1132 gBS->SetTimer (\r
1133 gMPSystem.CheckAllAPsEvent,\r
1134 TimerCancel,\r
1135 0\r
1136 );\r
1137\r
1138 if (gMPSystem.FailedListIndex == 0) {\r
1139 if (gMPSystem.FailedList != NULL) {\r
1140 FreePool (gMPSystem.FailedList);\r
1141 gMPSystem.FailedList = NULL;\r
1142 }\r
1143 }\r
1144\r
1145 Status = gBS->SignalEvent (gMPSystem.WaitEvent);\r
1146\r
1147 return ;\r
1148}\r
1149\r
1150VOID\r
1151EFIAPI\r
1152CpuCheckThisAPStatus (\r
1153 IN EFI_EVENT Event,\r
1154 IN VOID *Context\r
1155 )\r
1156{\r
1157 EFI_STATUS Status;\r
1158 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1159 PROCESSOR_STATE ProcessorState;\r
1160\r
1161 ProcessorData = (PROCESSOR_DATA_BLOCK *) Context;\r
1162\r
1163 //\r
1164 // This is an Interrupt Service routine.\r
1165 // that can grab a lock that is held in a non-interrupt\r
1166 // context. Meaning deadlock. Which is a badddd thing.\r
1167 // So, try lock it. If we can get it, cool, do our thing.\r
1168 // otherwise, just dump out & try again on the next iteration.\r
1169 //\r
1170 Status = gThread->MutexTryLock (ProcessorData->StateLock);\r
1171 if (EFI_ERROR(Status)) {\r
1172 return;\r
1173 }\r
1174 ProcessorState = ProcessorData->State;\r
1175 gThread->MutexUnlock (ProcessorData->StateLock);\r
1176\r
1177 if (ProcessorState == CPU_STATE_FINISHED) {\r
1178 Status = gBS->SetTimer (ProcessorData->CheckThisAPEvent, TimerCancel, 0);\r
1179 ASSERT_EFI_ERROR (Status);\r
1180\r
1181 Status = gBS->SignalEvent (gMPSystem.WaitEvent);\r
1182 ASSERT_EFI_ERROR (Status);\r
1183\r
1184 gThread->MutexLock (ProcessorData->StateLock);\r
1185 ProcessorData->State = CPU_STATE_IDLE;\r
1186 gThread->MutexUnlock (ProcessorData->StateLock);\r
1187 }\r
1188\r
1189 return ;\r
1190}\r
1191\r
1192\r
1193/*++\r
1194 This function is called by all processors (both BSP and AP) once and collects MP related data\r
1195\r
1196 MPSystemData - Pointer to the data structure containing MP related data\r
1197 BSP - TRUE if the CPU is BSP\r
1198\r
1199 EFI_SUCCESS - Data for the processor collected and filled in\r
1200\r
1201--*/\r
1202EFI_STATUS\r
1203FillInProcessorInformation (\r
1204 IN BOOLEAN BSP,\r
1205 IN UINTN ProcessorNumber\r
1206 )\r
1207{\r
1208 gMPSystem.ProcessorData[ProcessorNumber].Info.ProcessorId = gThread->Self ();\r
1209 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
1210 if (BSP) {\r
1211 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1212 }\r
1213\r
1214 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Package = (UINT32) ProcessorNumber;\r
1215 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Core = 0;\r
1216 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Thread = 0;\r
1217 gMPSystem.ProcessorData[ProcessorNumber].State = BSP ? CPU_STATE_BUSY : CPU_STATE_IDLE;\r
1218\r
1219 gMPSystem.ProcessorData[ProcessorNumber].Procedure = NULL;\r
1220 gMPSystem.ProcessorData[ProcessorNumber].Parameter = NULL;\r
1221 gMPSystem.ProcessorData[ProcessorNumber].StateLock = gThread->MutexInit ();\r
1222 gMPSystem.ProcessorData[ProcessorNumber].ProcedureLock = gThread->MutexInit ();\r
1223\r
1224 return EFI_SUCCESS;\r
1225}\r
1226\r
1227VOID *\r
1228EFIAPI\r
1229CpuDriverApIdolLoop (\r
1230 VOID *Context\r
1231 )\r
1232{\r
1233 EFI_AP_PROCEDURE Procedure;\r
1234 VOID *Parameter;\r
1235 UINTN ProcessorNumber;\r
1236 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1237\r
1238 ProcessorNumber = (UINTN)Context;\r
1239 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];\r
1240\r
1241 ProcessorData->Info.ProcessorId = gThread->Self ();\r
1242\r
1243 while (TRUE) {\r
1244 //\r
1245 // Make a local copy on the stack to be extra safe\r
1246 //\r
1247 gThread->MutexLock (ProcessorData->ProcedureLock);\r
1248 Procedure = ProcessorData->Procedure;\r
1249 Parameter = ProcessorData->Parameter;\r
1250 gThread->MutexUnlock (ProcessorData->ProcedureLock);\r
1251\r
1252 if (Procedure != NULL) {\r
1253 gThread->MutexLock (ProcessorData->StateLock);\r
1254 ProcessorData->State = CPU_STATE_BUSY;\r
1255 gThread->MutexUnlock (ProcessorData->StateLock);\r
1256\r
1257 Procedure (Parameter);\r
1258\r
1259 gThread->MutexLock (ProcessorData->ProcedureLock);\r
1260 ProcessorData->Procedure = NULL;\r
1261 gThread->MutexUnlock (ProcessorData->ProcedureLock);\r
1262\r
1263 gThread->MutexLock (ProcessorData->StateLock);\r
1264 ProcessorData->State = CPU_STATE_FINISHED;\r
1265 gThread->MutexUnlock (ProcessorData->StateLock);\r
1266 }\r
1267\r
1268 // Poll 5 times a seconds, 200ms\r
1269 // Don't want to burn too many system resources doing nothing.\r
1270 gEmuThunk->Sleep (200 * 1000);\r
1271 }\r
1272\r
1273 return 0;\r
1274}\r
1275\r
1276\r
1277EFI_STATUS\r
1278InitializeMpSystemData (\r
1279 IN UINTN NumberOfProcessors\r
1280 )\r
1281{\r
1282 EFI_STATUS Status;\r
1283 UINTN Index;\r
1284\r
1285\r
1286 //\r
1287 // Clear the data structure area first.\r
1288 //\r
1289 ZeroMem (&gMPSystem, sizeof (MP_SYSTEM_DATA));\r
1290\r
1291 //\r
1292 // First BSP fills and inits all known values, including it's own records.\r
1293 //\r
1294 gMPSystem.NumberOfProcessors = NumberOfProcessors;\r
1295 gMPSystem.NumberOfEnabledProcessors = NumberOfProcessors;\r
1296\r
1297 gMPSystem.ProcessorData = AllocateZeroPool (gMPSystem.NumberOfProcessors * sizeof (PROCESSOR_DATA_BLOCK));\r
1298 ASSERT (gMPSystem.ProcessorData != NULL);\r
1299\r
1300 FillInProcessorInformation (TRUE, 0);\r
1301\r
1302 Status = gBS->CreateEvent (\r
1303 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1304 TPL_CALLBACK,\r
1305 CpuCheckAllAPsStatus,\r
1306 NULL,\r
1307 &gMPSystem.CheckAllAPsEvent\r
1308 );\r
1309 ASSERT_EFI_ERROR (Status);\r
1310\r
1311\r
1312 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
1313 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
1314 // Skip BSP\r
1315 continue;\r
1316 }\r
1317\r
1318 FillInProcessorInformation (FALSE, Index);\r
1319\r
1320 Status = gThread->CreateThread (\r
1321 (VOID *)&gMPSystem.ProcessorData[Index].Info.ProcessorId,\r
1322 NULL,\r
1323 CpuDriverApIdolLoop,\r
1324 (VOID *)Index\r
1325 );\r
1326\r
1327\r
1328 Status = gBS->CreateEvent (\r
1329 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1330 TPL_CALLBACK,\r
1331 CpuCheckThisAPStatus,\r
1332 (VOID *) &gMPSystem.ProcessorData[Index],\r
1333 &gMPSystem.ProcessorData[Index].CheckThisAPEvent\r
1334 );\r
1335 }\r
1336\r
1337 return EFI_SUCCESS;\r
1338}\r
1339\r
1340\r
1341\r
1342/**\r
1343 Invoke a notification event\r
1344\r
1345 @param Event Event whose notification function is being invoked.\r
1346 @param Context The pointer to the notification function's context,\r
1347 which is implementation-dependent.\r
1348\r
1349**/\r
1350VOID\r
1351EFIAPI\r
1352CpuReadToBootFunction (\r
1353 IN EFI_EVENT Event,\r
1354 IN VOID *Context\r
1355 )\r
1356{\r
1357 gReadToBoot = TRUE;\r
1358}\r
1359\r
1360\r
1361\r
1362EFI_STATUS\r
1363CpuMpServicesInit (\r
1364 OUT UINTN *MaxCpus\r
1365 )\r
1366{\r
1367 EFI_STATUS Status;\r
1368 EFI_HANDLE Handle;\r
1369 EMU_IO_THUNK_PROTOCOL *IoThunk;\r
1370\r
1371 *MaxCpus = 1; // BSP\r
1372 IoThunk = GetIoThunkInstance (&gEmuThreadThunkProtocolGuid, 0);\r
1373 if (IoThunk != NULL) {\r
1374 Status = IoThunk->Open (IoThunk);\r
1375 if (!EFI_ERROR (Status)) {\r
1376 if (IoThunk->ConfigString != NULL) {\r
1377 *MaxCpus += StrDecimalToUintn (IoThunk->ConfigString);\r
1378 gThread = IoThunk->Interface;\r
1379 }\r
1380 }\r
1381 }\r
1382\r
1383 if (*MaxCpus == 1) {\r
1384 // We are not MP so nothing to do\r
1385 return EFI_SUCCESS;\r
1386 }\r
1387\r
1388 gPollInterval = (UINTN) PcdGet64 (PcdEmuMpServicesPollingInterval);\r
1389\r
1390 Status = InitializeMpSystemData (*MaxCpus);\r
1391 if (EFI_ERROR (Status)) {\r
1392 return Status;\r
1393 }\r
1394\r
1395 Status = EfiCreateEventReadyToBootEx (TPL_CALLBACK, CpuReadToBootFunction, NULL, &gReadToBootEvent);\r
1396 ASSERT_EFI_ERROR (Status);\r
1397\r
1398 //\r
1399 // Now install the MP services protocol.\r
1400 //\r
1401 Handle = NULL;\r
1402 Status = gBS->InstallMultipleProtocolInterfaces (\r
1403 &Handle,\r
1404 &gEfiMpServiceProtocolGuid, &mMpServicesTemplate,\r
1405 NULL\r
1406 );\r
1407 return Status;\r
1408}\r
1409\r
1410\r