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