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