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