]> git.proxmox.com Git - mirror_edk2.git/blame - InOsEmuPkg/CpuRuntimeDxe/MpService.c
InOsEmuPkg: Add TimerLib for PEI, DXE_CORE, and DXE/EFI drivers/applications.
[mirror_edk2.git] / InOsEmuPkg / CpuRuntimeDxe / MpService.c
CommitLineData
c4671a67 1/** @file\r
10d1be3e 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
c4671a67 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
224e1333 31Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
c4671a67 32Portitions Copyright (c) 2011, Apple Inc. All rights reserved.\r
224e1333 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
c4671a67 35The full text of the license may be found at\r
224e1333 36http://opensource.org/licenses/bsd-license.php.\r
37\r
38THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
c4671a67 39WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
40\r
224e1333 41\r
c4671a67 42**/\r
43\r
44#include "CpuDriver.h"\r
45\r
46\r
47MP_SYSTEM_DATA gMPSystem;\r
10d1be3e 48EMU_THREAD_THUNK_PROTOCOL *gThread = NULL; \r
c4671a67 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 (&mMpSercicesTemplate, &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
10d1be3e 78 gThread->MutexLock (Processor->ProcedureLock);\r
c4671a67 79 Processor->Parameter = ProcedureArgument;\r
80 Processor->Procedure = Procedure;\r
10d1be3e 81 gThread->MutexUnlock (Processor->ProcedureLock);\r
c4671a67 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
10d1be3e 101 gThread->MutexLock (Data->StateLock);\r
c4671a67 102 ProcessorState = Data->State;\r
10d1be3e 103 gThread->MutexUnlock (Data->StateLock);\r
c4671a67 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
c4671a67 377 UINTN ListIndex;\r
378 UINTN Number;\r
379 UINTN NextNumber;\r
380 PROCESSOR_STATE APInitialState;\r
381 PROCESSOR_STATE ProcessorState;\r
382 INTN Timeout;\r
383\r
384\r
385 if (!IsBSP ()) {\r
386 return EFI_DEVICE_ERROR;\r
387 }\r
388 \r
389 if (gMPSystem.NumberOfProcessors == 1) {\r
390 return EFI_NOT_STARTED;\r
391 }\r
392\r
393 if (Procedure == NULL) {\r
394 return EFI_INVALID_PARAMETER;\r
395 }\r
396 \r
397 if ((WaitEvent != NULL) && gReadToBoot) {\r
398 return EFI_UNSUPPORTED;\r
399 }\r
400 \r
401 \r
402 if (FailedCpuList != NULL) {\r
8b6d0c05 403 gMPSystem.FailedList = AllocatePool ((gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN));\r
404 if (gMPSystem.FailedList == NULL) {\r
405 return EFI_OUT_OF_RESOURCES;\r
406 }\r
407 SetMemN (gMPSystem.FailedList, (gMPSystem.NumberOfProcessors + 1) * sizeof (UINTN), END_OF_CPU_LIST);\r
408 gMPSystem.FailedListIndex = 0;\r
409 *FailedCpuList = gMPSystem.FailedList;\r
c4671a67 410 }\r
411\r
412 Timeout = TimeoutInMicroseconds;\r
413\r
414 ListIndex = 0;\r
8b6d0c05 415 ProcessorData = NULL;\r
c4671a67 416\r
8b6d0c05 417 gMPSystem.FinishCount = 0;\r
418 gMPSystem.StartCount = 0;\r
419 gMPSystem.SingleThread = SingleThread;\r
420 APInitialState = CPU_STATE_READY;\r
c4671a67 421\r
422 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
423 ProcessorData = &gMPSystem.ProcessorData[Number];\r
424\r
425 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
426 // Skip BSP\r
427 continue;\r
428 }\r
429\r
8b6d0c05 430 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
431 // Skip Disabled processors\r
432 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Number;\r
433 continue;\r
434 }\r
435\r
c4671a67 436 //\r
437 // Get APs prepared, and put failing APs into FailedCpuList\r
438 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready\r
439 // state 1 by 1, until the previous 1 finished its task\r
440 // if not "SingleThread", all APs are put to ready state from the beginning\r
441 //\r
442 if (ProcessorData->State == CPU_STATE_IDLE) {\r
443 gMPSystem.StartCount++;\r
444\r
10d1be3e 445 gThread->MutexLock (&ProcessorData->StateLock);\r
c4671a67 446 ProcessorData->State = APInitialState;\r
10d1be3e 447 gThread->MutexUnlock (&ProcessorData->StateLock);\r
c4671a67 448\r
449 if (SingleThread) {\r
450 APInitialState = CPU_STATE_BLOCKED;\r
451 }\r
8b6d0c05 452 } else {\r
453 return EFI_NOT_READY;\r
c4671a67 454 }\r
455 }\r
456 \r
8b6d0c05 457 if (WaitEvent != NULL) {\r
458 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
459 ProcessorData = &gMPSystem.ProcessorData[Number]; \r
460 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
461 // Skip BSP\r
462 continue;\r
463 }\r
464\r
465 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
466 // Skip Disabled processors\r
467 continue;\r
468 }\r
469 \r
470 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);\r
c4671a67 471 }\r
8b6d0c05 472\r
473 //\r
474 // Save data into private data structure, and create timer to poll AP state before exiting\r
475 //\r
476 gMPSystem.Procedure = Procedure;\r
477 gMPSystem.ProcedureArgument = ProcedureArgument;\r
478 gMPSystem.WaitEvent = WaitEvent;\r
479 gMPSystem.Timeout = TimeoutInMicroseconds;\r
480 gMPSystem.TimeoutActive = (BOOLEAN)(TimeoutInMicroseconds != 0);\r
481 Status = gBS->SetTimer (\r
482 gMPSystem.CheckAllAPsEvent,\r
483 TimerPeriodic,\r
484 gPollInterval\r
485 );\r
486 return Status;\r
487\r
c4671a67 488 }\r
489\r
490 while (TRUE) {\r
491 for (Number = 0; Number < gMPSystem.NumberOfProcessors; Number++) {\r
492 ProcessorData = &gMPSystem.ProcessorData[Number]; \r
493 if ((ProcessorData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
494 // Skip BSP\r
495 continue;\r
496 }\r
497\r
8b6d0c05 498 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
499 // Skip Disabled processors\r
500 continue;\r
501 }\r
502\r
10d1be3e 503 gThread->MutexLock (ProcessorData->StateLock);\r
c4671a67 504 ProcessorState = ProcessorData->State;\r
10d1be3e 505 gThread->MutexUnlock (ProcessorData->StateLock);\r
c4671a67 506\r
507 switch (ProcessorState) {\r
508 case CPU_STATE_READY:\r
509 SetApProcedure (ProcessorData, Procedure, ProcedureArgument);\r
510 break;\r
511\r
512 case CPU_STATE_FINISHED:\r
513 gMPSystem.FinishCount++;\r
514 if (SingleThread) {\r
515 Status = GetNextBlockedNumber (&NextNumber);\r
516 if (!EFI_ERROR (Status)) {\r
517 gMPSystem.ProcessorData[NextNumber].State = CPU_STATE_READY;\r
518 }\r
519 }\r
520\r
521 ProcessorData->State = CPU_STATE_IDLE;\r
522 break;\r
523\r
524 default:\r
525 break;\r
526 }\r
527 }\r
528\r
529 if (gMPSystem.FinishCount == gMPSystem.StartCount) {\r
8b6d0c05 530 Status = EFI_SUCCESS;\r
531 goto Done;\r
c4671a67 532 }\r
533\r
534 if ((TimeoutInMicroseconds != 0) && (Timeout < 0)) {\r
8b6d0c05 535 Status = EFI_TIMEOUT;\r
536 goto Done;\r
c4671a67 537 }\r
538\r
539 gBS->Stall (gPollInterval);\r
540 Timeout -= gPollInterval;\r
541 }\r
542\r
8b6d0c05 543Done:\r
544 if (FailedCpuList != NULL) {\r
545 if (gMPSystem.FailedListIndex == 0) {\r
546 FreePool (*FailedCpuList);\r
547 *FailedCpuList = NULL;\r
548 }\r
549 }\r
550\r
c4671a67 551 return EFI_SUCCESS;\r
552}\r
553\r
554\r
555/**\r
556 This service lets the caller get one enabled AP to execute a caller-provided \r
557 function. The caller can request the BSP to either wait for the completion \r
558 of the AP or just proceed with the next task by using the EFI event mechanism. \r
559 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking \r
560 execution support. This service may only be called from the BSP.\r
561\r
562 This function is used to dispatch one enabled AP to the function specified by \r
563 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent \r
564 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or \r
565 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode. \r
566 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode \r
567 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled, \r
568 then EFI_UNSUPPORTED must be returned.\r
569 \r
570 If the timeout specified by TimeoutInMicroseconds expires before the AP returns \r
571 from Procedure, then execution of Procedure by the AP is terminated. The AP is \r
572 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and \r
573 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
574\r
575 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
576 instance.\r
577 @param[in] Procedure A pointer to the function to be run on \r
578 enabled APs of the system. See type\r
579 EFI_AP_PROCEDURE.\r
580 @param[in] ProcessorNumber The handle number of the AP. The range is \r
581 from 0 to the total number of logical\r
582 processors minus 1. The total number of \r
583 logical processors can be retrieved by\r
584 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
585 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
586 service. If it is NULL, then execute in \r
587 blocking mode. BSP waits until all APs finish \r
588 or TimeoutInMicroseconds expires. If it's \r
589 not NULL, then execute in non-blocking mode. \r
590 BSP requests the function specified by \r
591 Procedure to be started on all the enabled \r
592 APs, and go on executing immediately. If \r
593 all return from Procedure or TimeoutInMicroseconds\r
594 expires, this event is signaled. The BSP \r
595 can use the CheckEvent() or WaitForEvent() \r
596 services to check the state of event. Type \r
597 EFI_EVENT is defined in CreateEvent() in \r
598 the Unified Extensible Firmware Interface \r
599 Specification. \r
600 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for \r
601 APs to return from Procedure, either for \r
602 blocking or non-blocking mode. Zero means \r
603 infinity. If the timeout expires before \r
604 all APs return from Procedure, then Procedure\r
605 on the failed APs is terminated. All enabled \r
606 APs are available for next function assigned \r
607 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() \r
608 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
609 If the timeout expires in blocking mode, \r
610 BSP returns EFI_TIMEOUT. If the timeout \r
611 expires in non-blocking mode, WaitEvent \r
612 is signaled with SignalEvent().\r
613 @param[in] ProcedureArgument The parameter passed into Procedure for \r
614 all APs.\r
615 @param[out] Finished If NULL, this parameter is ignored. In \r
616 blocking mode, this parameter is ignored.\r
617 In non-blocking mode, if AP returns from \r
618 Procedure before the timeout expires, its\r
619 content is set to TRUE. Otherwise, the \r
620 value is set to FALSE. The caller can\r
621 determine if the AP returned from Procedure \r
622 by evaluating this value.\r
623\r
624 @retval EFI_SUCCESS In blocking mode, specified AP finished before \r
625 the timeout expires.\r
626 @retval EFI_SUCCESS In non-blocking mode, the function has been \r
627 dispatched to specified AP.\r
628 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the \r
629 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was \r
630 signaled.\r
631 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
632 @retval EFI_TIMEOUT In blocking mode, the timeout expired before \r
633 the specified AP has finished.\r
634 @retval EFI_NOT_READY The specified AP is busy.\r
635 @retval EFI_NOT_FOUND The processor with the handle specified by \r
636 ProcessorNumber does not exist.\r
637 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
638 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
639\r
640**/\r
641EFI_STATUS\r
642EFIAPI\r
643CpuMpServicesStartupThisAP (\r
644 IN EFI_MP_SERVICES_PROTOCOL *This,\r
645 IN EFI_AP_PROCEDURE Procedure,\r
646 IN UINTN ProcessorNumber,\r
647 IN EFI_EVENT WaitEvent OPTIONAL,\r
648 IN UINTN TimeoutInMicroseconds,\r
649 IN VOID *ProcedureArgument OPTIONAL,\r
650 OUT BOOLEAN *Finished OPTIONAL\r
651 )\r
652{\r
653 EFI_STATUS Status;\r
654 INTN Timeout;\r
655 \r
656 if (!IsBSP ()) {\r
657 return EFI_DEVICE_ERROR;\r
658 }\r
659 \r
660 if (Procedure == NULL) {\r
661 return EFI_INVALID_PARAMETER;\r
662 }\r
663 \r
664 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
665 return EFI_NOT_FOUND;\r
666 }\r
667 \r
668 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
669 return EFI_INVALID_PARAMETER;\r
670 }\r
671\r
672 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
673 return EFI_NOT_READY;\r
674 }\r
675\r
676 if ((WaitEvent != NULL) && gReadToBoot) {\r
677 return EFI_UNSUPPORTED;\r
678 }\r
679\r
680 Timeout = TimeoutInMicroseconds;\r
681\r
682 gMPSystem.StartCount = 1;\r
683 gMPSystem.FinishCount = 0;\r
684\r
685 SetApProcedure (&gMPSystem.ProcessorData[ProcessorNumber], Procedure, ProcedureArgument);\r
686\r
8b6d0c05 687 if (WaitEvent != NULL) {\r
688 // Non Blocking\r
689 gMPSystem.WaitEvent = WaitEvent;\r
690 Status = gBS->SetTimer (\r
691 gMPSystem.ProcessorData[ProcessorNumber].CheckThisAPEvent,\r
692 TimerPeriodic,\r
693 gPollInterval\r
694 );\r
695 return EFI_SUCCESS;\r
696 }\r
697\r
698 // Blocking\r
c4671a67 699 while (TRUE) {\r
10d1be3e 700 gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 701 if (gMPSystem.ProcessorData[ProcessorNumber].State == CPU_STATE_FINISHED) {\r
702 gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;\r
10d1be3e 703 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 704 break;\r
705 }\r
706\r
10d1be3e 707 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 708\r
709 if ((TimeoutInMicroseconds != 0) && (Timeout < 0)) {\r
c4671a67 710 return EFI_TIMEOUT;\r
711 }\r
712\r
713 gBS->Stall (gPollInterval);\r
714 Timeout -= gPollInterval;\r
715 }\r
716\r
717 return EFI_SUCCESS;\r
718\r
719}\r
720\r
721\r
722/**\r
723 This service switches the requested AP to be the BSP from that point onward. \r
724 This service changes the BSP for all purposes. This call can only be performed \r
725 by the current BSP.\r
726\r
727 This service switches the requested AP to be the BSP from that point onward. \r
728 This service changes the BSP for all purposes. The new BSP can take over the \r
729 execution of the old BSP and continue seamlessly from where the old one left \r
730 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT \r
731 is signaled.\r
732\r
733 If the BSP cannot be switched prior to the return from this service, then \r
734 EFI_UNSUPPORTED must be returned.\r
735\r
736 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
737 @param[in] ProcessorNumber The handle number of AP that is to become the new \r
738 BSP. The range is from 0 to the total number of \r
739 logical processors minus 1. The total number of \r
740 logical processors can be retrieved by\r
741 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
742 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an \r
743 enabled AP. Otherwise, it will be disabled.\r
744\r
745 @retval EFI_SUCCESS BSP successfully switched.\r
746 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to \r
747 this service returning.\r
748 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
749 @retval EFI_SUCCESS The calling processor is an AP.\r
750 @retval EFI_NOT_FOUND The processor with the handle specified by\r
751 ProcessorNumber does not exist.\r
752 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or \r
753 a disabled AP.\r
754 @retval EFI_NOT_READY The specified AP is busy.\r
755\r
756**/\r
757EFI_STATUS\r
758EFIAPI\r
759CpuMpServicesSwitchBSP (\r
760 IN EFI_MP_SERVICES_PROTOCOL *This,\r
761 IN UINTN ProcessorNumber,\r
762 IN BOOLEAN EnableOldBSP\r
763 )\r
764{\r
765 UINTN Index;\r
766 \r
767 if (!IsBSP ()) {\r
768 return EFI_DEVICE_ERROR;\r
769 }\r
770 \r
771 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
772 return EFI_NOT_FOUND;\r
773 }\r
774 \r
775 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
776 return EFI_INVALID_PARAMETER;\r
777 }\r
778\r
779 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
780 return EFI_INVALID_PARAMETER;\r
781 }\r
782 \r
783 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
784 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
785 break;\r
786 }\r
787 }\r
788 ASSERT (Index != gMPSystem.NumberOfProcessors);\r
789 \r
790 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
791 return EFI_NOT_READY;\r
792 }\r
793 \r
794 // Skip for now as we need switch a bunch of stack stuff around and it's complex\r
795 // May not be worth it?\r
796 return EFI_NOT_READY;\r
797}\r
798\r
799\r
800/**\r
801 This service lets the caller enable or disable an AP from this point onward. \r
802 This service may only be called from the BSP.\r
803\r
804 This service allows the caller enable or disable an AP from this point onward. \r
805 The caller can optionally specify the health status of the AP by Health. If \r
806 an AP is being disabled, then the state of the disabled AP is implementation \r
807 dependent. If an AP is enabled, then the implementation must guarantee that a \r
808 complete initialization sequence is performed on the AP, so the AP is in a state \r
809 that is compatible with an MP operating system. This service may not be supported \r
810 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
811\r
812 If the enable or disable AP operation cannot be completed prior to the return \r
813 from this service, then EFI_UNSUPPORTED must be returned.\r
814\r
815 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
816 @param[in] ProcessorNumber The handle number of AP that is to become the new \r
817 BSP. The range is from 0 to the total number of \r
818 logical processors minus 1. The total number of \r
819 logical processors can be retrieved by\r
820 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
821 @param[in] EnableAP Specifies the new state for the processor for \r
822 enabled, FALSE for disabled.\r
823 @param[in] HealthFlag If not NULL, a pointer to a value that specifies \r
824 the new health status of the AP. This flag \r
825 corresponds to StatusFlag defined in \r
826 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only \r
827 the PROCESSOR_HEALTH_STATUS_BIT is used. All other \r
828 bits are ignored. If it is NULL, this parameter \r
829 is ignored.\r
830\r
831 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
832 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed \r
833 prior to this service returning.\r
834 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
835 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
836 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
837 does not exist.\r
838 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
839\r
840**/\r
841EFI_STATUS\r
842EFIAPI\r
843CpuMpServicesEnableDisableAP (\r
844 IN EFI_MP_SERVICES_PROTOCOL *This,\r
845 IN UINTN ProcessorNumber,\r
846 IN BOOLEAN EnableAP,\r
847 IN UINT32 *HealthFlag OPTIONAL\r
848 )\r
849{\r
850 if (!IsBSP ()) {\r
851 return EFI_DEVICE_ERROR;\r
852 }\r
853 \r
854 if (ProcessorNumber >= gMPSystem.NumberOfProcessors) {\r
855 return EFI_NOT_FOUND;\r
856 }\r
857 \r
858 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {\r
859 return EFI_INVALID_PARAMETER;\r
860 } \r
861\r
862 if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {\r
863 return EFI_UNSUPPORTED;\r
864 }\r
865\r
10d1be3e 866 gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 867 \r
868 if (EnableAP) {\r
869 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0 ) {\r
870 gMPSystem.NumberOfEnabledProcessors++;\r
871 }\r
872 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_ENABLED_BIT;\r
873 } else {\r
874 if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == PROCESSOR_ENABLED_BIT ) {\r
875 gMPSystem.NumberOfEnabledProcessors--;\r
876 }\r
877 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
878 }\r
879 \r
880 if (HealthFlag != NULL) {\r
881 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;\r
882 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT);\r
883 }\r
884 \r
10d1be3e 885 gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 886 \r
887 return EFI_SUCCESS;\r
888}\r
889\r
890\r
891/**\r
892 This return the handle number for the calling processor. This service may be \r
893 called from the BSP and APs.\r
894\r
895 This service returns the processor handle number for the calling processor. \r
896 The returned value is in the range from 0 to the total number of logical \r
897 processors minus 1. The total number of logical processors can be retrieved \r
898 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be \r
899 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER \r
900 is returned. Otherwise, the current processors handle number is returned in \r
901 ProcessorNumber, and EFI_SUCCESS is returned.\r
902\r
903 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
904 @param[in] ProcessorNumber The handle number of AP that is to become the new \r
905 BSP. The range is from 0 to the total number of \r
906 logical processors minus 1. The total number of \r
907 logical processors can be retrieved by\r
908 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
909\r
910 @retval EFI_SUCCESS The current processor handle number was returned \r
911 in ProcessorNumber.\r
912 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
913\r
914**/\r
915EFI_STATUS\r
916EFIAPI\r
917CpuMpServicesWhoAmI (\r
918 IN EFI_MP_SERVICES_PROTOCOL *This,\r
919 OUT UINTN *ProcessorNumber\r
920 )\r
921{\r
922 UINTN Index;\r
923 UINT64 ProcessorId;\r
924 \r
925 if (ProcessorNumber == NULL) {\r
926 return EFI_INVALID_PARAMETER;\r
927 }\r
928 \r
10d1be3e 929 ProcessorId = gThread->Self ();\r
c4671a67 930 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
931 if (gMPSystem.ProcessorData[Index].Info.ProcessorId == ProcessorId) {\r
932 break;\r
933 }\r
934 }\r
935\r
936 *ProcessorNumber = Index;\r
937 return EFI_SUCCESS;\r
938}\r
939\r
940\r
941\r
942EFI_MP_SERVICES_PROTOCOL mMpSercicesTemplate = {\r
943 CpuMpServicesGetNumberOfProcessors,\r
944 CpuMpServicesGetProcessorInfo,\r
945 CpuMpServicesStartupAllAps,\r
946 CpuMpServicesStartupThisAP,\r
947 CpuMpServicesSwitchBSP,\r
948 CpuMpServicesEnableDisableAP,\r
949 CpuMpServicesWhoAmI\r
950};\r
951\r
952\r
953\r
954/*++\r
955 If timeout occurs in StartupAllAps(), a timer is set, which invokes this\r
956 procedure periodically to check whether all APs have finished.\r
957\r
958\r
959--*/\r
960VOID\r
961EFIAPI\r
962CpuCheckAllAPsStatus (\r
963 IN EFI_EVENT Event,\r
964 IN VOID *Context\r
965 )\r
966{\r
967 UINTN ProcessorNumber;\r
968 UINTN NextNumber;\r
969 PROCESSOR_DATA_BLOCK *ProcessorData;\r
970 PROCESSOR_DATA_BLOCK *NextData;\r
971 EFI_STATUS Status;\r
972 PROCESSOR_STATE ProcessorState;\r
8b6d0c05 973 UINTN Cpu;\r
974 BOOLEAN Found;\r
c4671a67 975\r
8b6d0c05 976 if (gMPSystem.TimeoutActive) {\r
977 gMPSystem.Timeout -= gPollInterval;\r
978 }\r
979 \r
224e1333 980 ProcessorData = (PROCESSOR_DATA_BLOCK *) Context;\r
981\r
c4671a67 982 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {\r
983 if ((ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
984 // Skip BSP\r
985 continue;\r
986 }\r
987\r
8b6d0c05 988 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
989 // Skip Disabled processors\r
990 continue;\r
991 }\r
992\r
c4671a67 993 // This is an Interrupt Service routine.\r
994 // This can grab a lock that is held in a non-interrupt\r
995 // context. Meaning deadlock. Which is a bad thing.\r
996 // So, try lock it. If we can get it, cool, do our thing.\r
997 // otherwise, just dump out & try again on the next iteration.\r
10d1be3e 998 Status = gThread->MutexTryLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 999 if (EFI_ERROR(Status)) {\r
1000 return;\r
1001 }\r
1002 ProcessorState = gMPSystem.ProcessorData[ProcessorNumber].State;\r
10d1be3e 1003 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
c4671a67 1004\r
1005 switch (ProcessorState) {\r
1006 case CPU_STATE_READY:\r
1007 SetApProcedure (ProcessorData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);\r
1008 break;\r
1009\r
1010 case CPU_STATE_FINISHED:\r
1011 if (gMPSystem.SingleThread) {\r
1012 Status = GetNextBlockedNumber (&NextNumber);\r
1013 if (!EFI_ERROR (Status)) {\r
1014 NextData = &gMPSystem.ProcessorData[NextNumber];\r
1015\r
10d1be3e 1016 gThread->MutexLock (&NextData->ProcedureLock);\r
c4671a67 1017 NextData->State = CPU_STATE_READY;\r
10d1be3e 1018 gThread->MutexUnlock (&NextData->ProcedureLock);\r
c4671a67 1019\r
1020 SetApProcedure (NextData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);\r
1021 }\r
1022 }\r
1023\r
1024 gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;\r
1025 gMPSystem.FinishCount++;\r
1026 break;\r
1027\r
1028 default:\r
1029 break;\r
1030 }\r
1031 }\r
8b6d0c05 1032 \r
1033 if (gMPSystem.TimeoutActive && gMPSystem.Timeout < 0) {\r
1034 //\r
1035 // Timeout\r
1036 //\r
1037 if (gMPSystem.FailedList != NULL) {\r
1038 for (ProcessorNumber = 0; ProcessorNumber < gMPSystem.NumberOfProcessors; ProcessorNumber++) {\r
1039 if ((ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
1040 // Skip BSP\r
1041 continue;\r
1042 }\r
c4671a67 1043\r
8b6d0c05 1044 if ((ProcessorData->Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0) {\r
1045 // Skip Disabled processors\r
1046 continue;\r
1047 }\r
1048 \r
1049 // Mark the \r
1050 Status = gThread->MutexTryLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
1051 if (EFI_ERROR(Status)) {\r
1052 return;\r
1053 }\r
1054 ProcessorState = gMPSystem.ProcessorData[ProcessorNumber].State;\r
1055 gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);\r
1056 \r
1057 if (ProcessorState != CPU_STATE_IDLE) {\r
1058 // If we are retrying make sure we don't double count\r
1059 for (Cpu = 0, Found = FALSE; Cpu < gMPSystem.NumberOfProcessors; Cpu++) {\r
1060 if (gMPSystem.FailedList[Cpu] == END_OF_CPU_LIST) {\r
1061 break;\r
1062 }\r
1063 if (gMPSystem.FailedList[ProcessorNumber] == Cpu) {\r
1064 Found = TRUE;\r
1065 break;\r
1066 }\r
1067 }\r
1068 if (!Found) {\r
1069 gMPSystem.FailedList[gMPSystem.FailedListIndex++] = Cpu;\r
1070 }\r
1071 }\r
1072 }\r
1073 }\r
1074 // Force terminal exit\r
1075 gMPSystem.FinishCount = gMPSystem.StartCount;\r
1076 }\r
1077\r
1078 if (gMPSystem.FinishCount != gMPSystem.StartCount) {\r
1079 return;\r
c4671a67 1080 }\r
8b6d0c05 1081 \r
1082 gBS->SetTimer (\r
1083 gMPSystem.CheckAllAPsEvent,\r
1084 TimerCancel,\r
1085 0\r
1086 );\r
1087\r
1088 if (gMPSystem.FailedListIndex == 0) {\r
1089 if (gMPSystem.FailedList != NULL) {\r
1090 FreePool (gMPSystem.FailedList);\r
1091 gMPSystem.FailedList = NULL;\r
1092 }\r
1093 }\r
1094\r
1095 Status = gBS->SignalEvent (gMPSystem.WaitEvent);\r
c4671a67 1096\r
1097 return ;\r
1098}\r
1099\r
1100VOID\r
1101EFIAPI\r
1102CpuCheckThisAPStatus (\r
1103 IN EFI_EVENT Event,\r
1104 IN VOID *Context\r
1105 )\r
1106{\r
1107 EFI_STATUS Status;\r
1108 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1109 PROCESSOR_STATE ProcessorState;\r
1110\r
1111 ProcessorData = (PROCESSOR_DATA_BLOCK *) Context;\r
1112\r
1113 //\r
8b6d0c05 1114 // This is an Interrupt Service routine.\r
1115 // that can grab a lock that is held in a non-interrupt\r
c4671a67 1116 // context. Meaning deadlock. Which is a badddd thing.\r
1117 // So, try lock it. If we can get it, cool, do our thing.\r
1118 // otherwise, just dump out & try again on the next iteration.\r
1119 //\r
10d1be3e 1120 Status = gThread->MutexTryLock (ProcessorData->StateLock);\r
c4671a67 1121 if (EFI_ERROR(Status)) {\r
1122 return;\r
1123 }\r
1124 ProcessorState = ProcessorData->State;\r
10d1be3e 1125 gThread->MutexUnlock (ProcessorData->StateLock);\r
c4671a67 1126\r
1127 if (ProcessorState == CPU_STATE_FINISHED) {\r
1128 Status = gBS->SetTimer (ProcessorData->CheckThisAPEvent, TimerCancel, 0);\r
1129 ASSERT_EFI_ERROR (Status);\r
1130 \r
1131 Status = gBS->SignalEvent (gMPSystem.WaitEvent);\r
1132 ASSERT_EFI_ERROR (Status);\r
1133 \r
10d1be3e 1134 gThread->MutexLock (ProcessorData->StateLock);\r
c4671a67 1135 ProcessorData->State = CPU_STATE_IDLE;\r
10d1be3e 1136 gThread->MutexUnlock (ProcessorData->StateLock);\r
c4671a67 1137 }\r
1138\r
1139 return ;\r
1140}\r
1141\r
1142\r
1143/*++\r
1144 This function is called by all processors (both BSP and AP) once and collects MP related data\r
1145\r
1146 MPSystemData - Pointer to the data structure containing MP related data\r
1147 BSP - TRUE if the CPU is BSP\r
1148\r
1149 EFI_SUCCESS - Data for the processor collected and filled in\r
1150\r
1151--*/\r
1152EFI_STATUS\r
1153FillInProcessorInformation (\r
1154 IN BOOLEAN BSP,\r
1155 IN UINTN ProcessorNumber\r
1156 )\r
1157{\r
1158 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1159\r
1160 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];\r
1161 \r
10d1be3e 1162 gMPSystem.ProcessorData[ProcessorNumber].Info.ProcessorId = gThread->Self ();\r
c4671a67 1163 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
1164 if (BSP) {\r
1165 gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1166 }\r
1167 \r
1168 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Package = ProcessorNumber;\r
1169 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Core = 0;\r
1170 gMPSystem.ProcessorData[ProcessorNumber].Info.Location.Thread = 0;\r
1171 gMPSystem.ProcessorData[ProcessorNumber].State = BSP ? CPU_STATE_BUSY : CPU_STATE_IDLE;\r
1172 \r
1173 gMPSystem.ProcessorData[ProcessorNumber].Procedure = NULL;\r
1174 gMPSystem.ProcessorData[ProcessorNumber].Parameter = NULL;\r
10d1be3e 1175 gMPSystem.ProcessorData[ProcessorNumber].StateLock = gThread->MutexInit ();\r
1176 gMPSystem.ProcessorData[ProcessorNumber].ProcedureLock = gThread->MutexInit ();\r
c4671a67 1177\r
1178 return EFI_SUCCESS;\r
1179}\r
1180\r
1181VOID *\r
1182EFIAPI\r
1183CpuDriverApIdolLoop (\r
1184 VOID *Context\r
1185 )\r
1186{\r
1187 EFI_AP_PROCEDURE Procedure;\r
1188 VOID *Parameter;\r
1189 UINTN ProcessorNumber;\r
1190 PROCESSOR_DATA_BLOCK *ProcessorData;\r
1191 \r
1192 ProcessorNumber = (UINTN)Context;\r
1193 ProcessorData = &gMPSystem.ProcessorData[ProcessorNumber];\r
1194 \r
10d1be3e 1195 ProcessorData->Info.ProcessorId = gThread->Self ();\r
c4671a67 1196 \r
1197 while (TRUE) {\r
1198 //\r
1199 // Make a local copy on the stack to be extra safe\r
1200 //\r
10d1be3e 1201 gThread->MutexLock (ProcessorData->ProcedureLock);\r
c4671a67 1202 Procedure = ProcessorData->Procedure;\r
1203 Parameter = ProcessorData->Parameter;\r
10d1be3e 1204 gThread->MutexUnlock (ProcessorData->ProcedureLock);\r
c4671a67 1205 \r
1206 if (Procedure != NULL) {\r
10d1be3e 1207 gThread->MutexLock (ProcessorData->StateLock);\r
c4671a67 1208 ProcessorData->State = CPU_STATE_BUSY;\r
10d1be3e 1209 gThread->MutexUnlock (ProcessorData->StateLock);\r
c4671a67 1210 \r
1211 Procedure (Parameter);\r
1212 \r
10d1be3e 1213 gThread->MutexLock (ProcessorData->ProcedureLock);\r
c4671a67 1214 ProcessorData->Procedure = NULL;\r
10d1be3e 1215 gThread->MutexUnlock (ProcessorData->ProcedureLock);\r
c4671a67 1216 \r
10d1be3e 1217 gThread->MutexLock (ProcessorData->StateLock);\r
c4671a67 1218 ProcessorData->State = CPU_STATE_FINISHED;\r
10d1be3e 1219 gThread->MutexUnlock (ProcessorData->StateLock); \r
c4671a67 1220 }\r
1221 \r
1222 // Poll 5 times a seconds, 200ms\r
1223 // Don't want to burn too many system resources doing nothing.\r
1ef41207 1224 gEmuThunk->Sleep (200 * 1000);\r
c4671a67 1225 }\r
1226 \r
1227 return 0;\r
1228}\r
1229\r
1230\r
1231EFI_STATUS\r
1232InitializeMpSystemData (\r
1233 IN UINTN NumberOfProcessors\r
1234 )\r
1235{\r
1236 EFI_STATUS Status;\r
1237 UINTN Index;\r
1238\r
1239 \r
1240 //\r
1241 // Clear the data structure area first.\r
1242 //\r
1243 ZeroMem (&gMPSystem, sizeof (MP_SYSTEM_DATA));\r
1244\r
1245 //\r
1246 // First BSP fills and inits all known values, including it's own records.\r
1247 //\r
1248 gMPSystem.NumberOfProcessors = NumberOfProcessors;\r
1249 gMPSystem.NumberOfEnabledProcessors = NumberOfProcessors;\r
1250 \r
1251 gMPSystem.ProcessorData = AllocateZeroPool (gMPSystem.NumberOfProcessors * sizeof (PROCESSOR_DATA_BLOCK));\r
1252 ASSERT (gMPSystem.ProcessorData != NULL);\r
1253\r
1254 FillInProcessorInformation (TRUE, 0);\r
1255 \r
1256 Status = gBS->CreateEvent (\r
1257 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1258 TPL_CALLBACK,\r
1259 CpuCheckAllAPsStatus,\r
1260 NULL,\r
1261 &gMPSystem.CheckAllAPsEvent\r
1262 );\r
1263 ASSERT_EFI_ERROR (Status);\r
1264 \r
1265\r
1266 for (Index = 0; Index < gMPSystem.NumberOfProcessors; Index++) {\r
1267 if ((gMPSystem.ProcessorData[Index].Info.StatusFlag & PROCESSOR_AS_BSP_BIT) == PROCESSOR_AS_BSP_BIT) {\r
1268 // Skip BSP\r
1269 continue;\r
1270 }\r
1271 \r
1272 FillInProcessorInformation (FALSE, Index);\r
1273 \r
10d1be3e 1274 Status = gThread->CreateThread (\r
c4671a67 1275 (VOID *)&gMPSystem.ProcessorData[Index].Info.ProcessorId, \r
1276 NULL,\r
1277 CpuDriverApIdolLoop,\r
1278 (VOID *)Index\r
1279 );\r
1280 \r
1281 \r
1282 Status = gBS->CreateEvent (\r
1283 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
1284 TPL_CALLBACK,\r
1285 CpuCheckThisAPStatus,\r
1286 (VOID *) &gMPSystem.ProcessorData[Index],\r
1287 &gMPSystem.ProcessorData[Index].CheckThisAPEvent\r
1288 );\r
1289 }\r
1290\r
1291 return EFI_SUCCESS;\r
1292}\r
1293\r
1294\r
1295\r
1296/**\r
1297 Invoke a notification event\r
1298\r
1299 @param Event Event whose notification function is being invoked.\r
1300 @param Context The pointer to the notification function's context,\r
1301 which is implementation-dependent.\r
1302\r
1303**/\r
1304VOID\r
1305EFIAPI\r
1306CpuReadToBootFunction (\r
1307 IN EFI_EVENT Event,\r
1308 IN VOID *Context\r
1309 )\r
1310{\r
1311 gReadToBoot = TRUE;\r
1312}\r
1313\r
1314\r
1315\r
1316EFI_STATUS\r
1317CpuMpServicesInit (\r
1318 VOID\r
1319 )\r
1320{\r
1321 EFI_STATUS Status;\r
1322 EFI_HANDLE Handle;\r
1323 EMU_IO_THUNK_PROTOCOL *IoThunk;\r
1324 UINTN MaxCpus;\r
c4671a67 1325\r
1326 MaxCpus = 1; // BSP\r
1327 \r
10d1be3e 1328 IoThunk = GetIoThunkInstance (&gEmuThreadThunkProtocolGuid, 0);\r
c4671a67 1329 if (IoThunk != NULL) {\r
1330 Status = IoThunk->Open (IoThunk);\r
1331 if (!EFI_ERROR (Status)) {\r
1332 if (IoThunk->ConfigString != NULL) {\r
1333 MaxCpus += StrDecimalToUintn (IoThunk->ConfigString);\r
10d1be3e 1334 gThread = IoThunk->Interface;\r
c4671a67 1335 }\r
1336 }\r
1337 }\r
1338\r
1339 if (MaxCpus == 1) {\r
1340 // We are not MP so nothing to do\r
1341 return EFI_SUCCESS;\r
1342 }\r
1343\r
1344 gPollInterval = PcdGet64 (PcdEmuMpServicesPollingInterval);\r
1345\r
1346 Status = InitializeMpSystemData (MaxCpus);\r
1347 if (EFI_ERROR (Status)) {\r
1348 return Status;\r
1349 }\r
1350\r
1351 Status = EfiCreateEventReadyToBootEx (TPL_CALLBACK, CpuReadToBootFunction, NULL, &gReadToBootEvent);\r
1352 ASSERT_EFI_ERROR (Status);\r
1353\r
1354 //\r
1355 // Now install the MP services protocol.\r
1356 //\r
1357 Handle = NULL;\r
1358 Status = gBS->InstallMultipleProtocolInterfaces (\r
1359 &Handle,\r
1360 &gEfiMpServiceProtocolGuid, &mMpSercicesTemplate,\r
1361 NULL\r
1362 );\r
1363 return Status;\r
1364}\r
1365\r
1366\r