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