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