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