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