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