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