2 Construct MP Services Protocol on top of pthreads. This code makes APs show up
3 in the emulator. PcdEmuApCount is the number of APs the emulator should produce.
5 The MP Services Protocol provides a generalized way of performing following tasks:
6 - Retrieving information of multi-processor environment and MP-related status of
8 - Dispatching user-provided function to APs.
9 - Maintain MP-related processor status.
11 The MP Services Protocol must be produced on any system with more than one logical
14 The Protocol is available only during boot time.
16 MP Services Protocol is hardware-independent. Most of the logic of this protocol
17 is architecturally neutral. It abstracts the multi-processor environment and
18 status of processors, and provides interfaces to retrieve information, maintain,
21 MP Services Protocol may be consumed by ACPI module. The ACPI module may use this
22 protocol to retrieve data that are needed for an MP platform and report them to OS.
23 MP Services Protocol may also be used to program and configure processors, such
24 as MTRR synchronization for memory space attributes setting in DXE Services.
25 MP Services Protocol may be used by non-CPU DXE drivers to speed up platform boot
26 by taking advantage of the processing capabilities of the APs, for example, using
27 APs to help test system memory in parallel with other device initialization.
28 Diagnostics applications may also use this protocol for multi-processor.
30 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
31 Portitions Copyright (c) 2011, Apple Inc. All rights reserved.
32 This program and the accompanying materials are licensed and made available under
33 the terms and conditions of the BSD License that accompanies this distribution.
34 The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php.
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
43 #include "CpuDriver.h"
46 MP_SYSTEM_DATA gMPSystem
;
47 EMU_PTREAD_THUNK_PROTOCOL
*gPthread
= NULL
;
48 EFI_EVENT gReadToBootEvent
;
49 BOOLEAN gReadToBoot
= FALSE
;
59 UINTN ProcessorNumber
;
61 Status
= CpuMpServicesWhoAmI (&mMpSercicesTemplate
, &ProcessorNumber
);
62 if (EFI_ERROR (Status
)) {
66 return (gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0;
72 IN PROCESSOR_DATA_BLOCK
*Processor
,
73 IN EFI_AP_PROCEDURE Procedure
,
74 IN VOID
*ProcedureArgument
77 gPthread
->MutextLock (Processor
->ProcedureLock
);
78 Processor
->Parameter
= ProcedureArgument
;
79 Processor
->Procedure
= Procedure
;
80 gPthread
->MutexUnlock (Processor
->ProcedureLock
);
85 GetNextBlockedNumber (
90 PROCESSOR_STATE ProcessorState
;
91 PROCESSOR_DATA_BLOCK
*Data
;
93 for (Number
= 0; Number
< gMPSystem
.NumberOfProcessors
; Number
++) {
94 Data
= &gMPSystem
.ProcessorData
[Number
];
95 if ((Data
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0) {
100 gPthread
->MutextLock (Data
->StateLock
);
101 ProcessorState
= Data
->State
;
102 gPthread
->MutexUnlock (Data
->StateLock
);
104 if (ProcessorState
== CPU_STATE_BLOCKED
) {
105 *NextNumber
= Number
;
110 return EFI_NOT_FOUND
;
117 This service retrieves the number of logical processor in the platform
118 and the number of those logical processors that are enabled on this boot.
119 This service may only be called from the BSP.
121 This function is used to retrieve the following information:
122 - The number of logical processors that are present in the system.
123 - The number of enabled logical processors in the system at the instant
126 Because MP Service Protocol provides services to enable and disable processors
127 dynamically, the number of enabled logical processors may vary during the
128 course of a boot session.
130 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
131 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
132 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
133 is returned in NumberOfProcessors, the number of currently enabled processor
134 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
136 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
138 @param[out] NumberOfProcessors Pointer to the total number of logical
139 processors in the system, including the BSP
141 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
142 processors that exist in system, including
145 @retval EFI_SUCCESS The number of logical processors and enabled
146 logical processors was retrieved.
147 @retval EFI_DEVICE_ERROR The calling processor is an AP.
148 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
149 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
154 CpuMpServicesGetNumberOfProcessors (
155 IN EFI_MP_SERVICES_PROTOCOL
*This
,
156 OUT UINTN
*NumberOfProcessors
,
157 OUT UINTN
*NumberOfEnabledProcessors
160 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
161 return EFI_INVALID_PARAMETER
;
165 return EFI_DEVICE_ERROR
;
168 *NumberOfProcessors
= gMPSystem
.NumberOfProcessors
;
169 *NumberOfEnabledProcessors
= gMPSystem
.NumberOfEnabledProcessors
;
176 Gets detailed MP-related information on the requested processor at the
177 instant this call is made. This service may only be called from the BSP.
179 This service retrieves detailed MP-related information about any processor
180 on the platform. Note the following:
181 - The processor information may change during the course of a boot session.
182 - The information presented here is entirely MP related.
184 Information regarding the number of caches and their sizes, frequency of operation,
185 slot numbers is all considered platform-related information and is not provided
188 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
190 @param[in] ProcessorNumber The handle number of processor.
191 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
192 the requested processor is deposited.
194 @retval EFI_SUCCESS Processor information was returned.
195 @retval EFI_DEVICE_ERROR The calling processor is an AP.
196 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
197 @retval EFI_NOT_FOUND The processor with the handle specified by
198 ProcessorNumber does not exist in the platform.
203 CpuMpServicesGetProcessorInfo (
204 IN EFI_MP_SERVICES_PROTOCOL
*This
,
205 IN UINTN ProcessorNumber
,
206 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
209 if (ProcessorInfoBuffer
== NULL
) {
210 return EFI_INVALID_PARAMETER
;
214 return EFI_DEVICE_ERROR
;
217 if (ProcessorNumber
>= gMPSystem
.NumberOfProcessors
) {
218 return EFI_NOT_FOUND
;
221 CopyMem (ProcessorInfoBuffer
, &gMPSystem
.ProcessorData
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
227 This service executes a caller provided function on all enabled APs. APs can
228 run either simultaneously or one at a time in sequence. This service supports
229 both blocking and non-blocking requests. The non-blocking requests use EFI
230 events so the BSP can detect when the APs have finished. This service may only
231 be called from the BSP.
233 This function is used to dispatch all the enabled APs to the function specified
234 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
235 immediately and Procedure is not started on any AP.
237 If SingleThread is TRUE, all the enabled APs execute the function specified by
238 Procedure one by one, in ascending order of processor handle number. Otherwise,
239 all the enabled APs execute the function specified by Procedure simultaneously.
241 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
242 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
243 mode, and the BSP returns from this service without waiting for APs. If a
244 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
245 is signaled, then EFI_UNSUPPORTED must be returned.
247 If the timeout specified by TimeoutInMicroseconds expires before all APs return
248 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
249 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
250 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
251 content points to the list of processor handle numbers in which Procedure was
254 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
255 to make sure that the nature of the code that is executed on the BSP and the
256 dispatched APs is well controlled. The MP Services Protocol does not guarantee
257 that the Procedure function is MP-safe. Hence, the tasks that can be run in
258 parallel are limited to certain independent tasks and well-controlled exclusive
259 code. EFI services and protocols may not be called by APs unless otherwise
262 In blocking execution mode, BSP waits until all APs finish or
263 TimeoutInMicroseconds expires.
265 In non-blocking execution mode, BSP is freed to return to the caller and then
266 proceed to the next task without having to wait for APs. The following
267 sequence needs to occur in a non-blocking execution mode:
269 -# The caller that intends to use this MP Services Protocol in non-blocking
270 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
271 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
272 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
273 the function specified by Procedure to be started on all the enabled APs,
274 and releases the BSP to continue with other tasks.
275 -# The caller can use the CheckEvent() and WaitForEvent() services to check
276 the state of the WaitEvent created in step 1.
277 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
278 Service signals WaitEvent by calling the EFI SignalEvent() function. If
279 FailedCpuList is not NULL, its content is available when WaitEvent is
280 signaled. If all APs returned from Procedure prior to the timeout, then
281 FailedCpuList is set to NULL. If not all APs return from Procedure before
282 the timeout, then FailedCpuList is filled in with the list of the failed
283 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
284 It is the caller's responsibility to free the buffer with FreePool() service.
285 -# This invocation of SignalEvent() function informs the caller that invoked
286 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
287 the specified task or a timeout occurred. The contents of FailedCpuList
288 can be examined to determine which APs did not complete the specified task
289 prior to the timeout.
291 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
293 @param[in] Procedure A pointer to the function to be run on
294 enabled APs of the system. See type
296 @param[in] SingleThread If TRUE, then all the enabled APs execute
297 the function specified by Procedure one by
298 one, in ascending order of processor handle
299 number. If FALSE, then all the enabled APs
300 execute the function specified by Procedure
302 @param[in] WaitEvent The event created by the caller with CreateEvent()
303 service. If it is NULL, then execute in
304 blocking mode. BSP waits until all APs finish
305 or TimeoutInMicroseconds expires. If it's
306 not NULL, then execute in non-blocking mode.
307 BSP requests the function specified by
308 Procedure to be started on all the enabled
309 APs, and go on executing immediately. If
310 all return from Procedure, or TimeoutInMicroseconds
311 expires, this event is signaled. The BSP
312 can use the CheckEvent() or WaitForEvent()
313 services to check the state of event. Type
314 EFI_EVENT is defined in CreateEvent() in
315 the Unified Extensible Firmware Interface
317 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
318 APs to return from Procedure, either for
319 blocking or non-blocking mode. Zero means
320 infinity. If the timeout expires before
321 all APs return from Procedure, then Procedure
322 on the failed APs is terminated. All enabled
323 APs are available for next function assigned
324 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
325 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
326 If the timeout expires in blocking mode,
327 BSP returns EFI_TIMEOUT. If the timeout
328 expires in non-blocking mode, WaitEvent
329 is signaled with SignalEvent().
330 @param[in] ProcedureArgument The parameter passed into Procedure for
332 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
333 if all APs finish successfully, then its
334 content is set to NULL. If not all APs
335 finish before timeout expires, then its
336 content is set to address of the buffer
337 holding handle numbers of the failed APs.
338 The buffer is allocated by MP Service Protocol,
339 and it's the caller's responsibility to
340 free the buffer with FreePool() service.
341 In blocking mode, it is ready for consumption
342 when the call returns. In non-blocking mode,
343 it is ready when WaitEvent is signaled. The
344 list of failed CPU is terminated by
347 @retval EFI_SUCCESS In blocking mode, all APs have finished before
349 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
351 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
352 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
354 @retval EFI_DEVICE_ERROR Caller processor is AP.
355 @retval EFI_NOT_STARTED No enabled APs exist in the system.
356 @retval EFI_NOT_READY Any enabled APs are busy.
357 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
358 all enabled APs have finished.
359 @retval EFI_INVALID_PARAMETER Procedure is NULL.
364 CpuMpServicesStartupAllAps (
365 IN EFI_MP_SERVICES_PROTOCOL
*This
,
366 IN EFI_AP_PROCEDURE Procedure
,
367 IN BOOLEAN SingleThread
,
368 IN EFI_EVENT WaitEvent OPTIONAL
,
369 IN UINTN TimeoutInMicroseconds
,
370 IN VOID
*ProcedureArgument OPTIONAL
,
371 OUT UINTN
**FailedCpuList OPTIONAL
375 PROCESSOR_DATA_BLOCK
*ProcessorData
;
377 UINTN FailedListIndex
;
381 PROCESSOR_STATE APInitialState
;
382 PROCESSOR_STATE ProcessorState
;
387 return EFI_DEVICE_ERROR
;
390 if (gMPSystem
.NumberOfProcessors
== 1) {
391 return EFI_NOT_STARTED
;
394 if (Procedure
== NULL
) {
395 return EFI_INVALID_PARAMETER
;
398 if ((WaitEvent
!= NULL
) && gReadToBoot
) {
399 return EFI_UNSUPPORTED
;
403 if (FailedCpuList
!= NULL
) {
404 FailledList
= AllocatePool ((gMPSystem
.NumberOfProcessors
+ 1) * sizeof (UINTN
));
405 SetMemN (FailledList
, (gMPSystem
.NumberOfProcessors
+ 1) * sizeof (UINTN
), END_OF_CPU_LIST
);
407 *FailedCpuList
= FailledList
;
410 Timeout
= TimeoutInMicroseconds
;
413 ProcessorData
= NULL
;
415 gMPSystem
.FinishCount
= 0;
416 gMPSystem
.StartCount
= 0;
417 APInitialState
= CPU_STATE_READY
;
419 for (Number
= 0; Number
< gMPSystem
.NumberOfProcessors
; Number
++) {
420 ProcessorData
= &gMPSystem
.ProcessorData
[Number
];
422 if ((ProcessorData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) == PROCESSOR_AS_BSP_BIT
) {
428 // Get APs prepared, and put failing APs into FailedCpuList
429 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
430 // state 1 by 1, until the previous 1 finished its task
431 // if not "SingleThread", all APs are put to ready state from the beginning
433 if (ProcessorData
->State
== CPU_STATE_IDLE
) {
434 gMPSystem
.StartCount
++;
436 gPthread
->MutextLock (&ProcessorData
->StateLock
);
437 ProcessorData
->State
= APInitialState
;
438 gPthread
->MutexUnlock (&ProcessorData
->StateLock
);
441 APInitialState
= CPU_STATE_BLOCKED
;
444 } else if (FailedCpuList
!= NULL
) {
445 FailledList
[FailedListIndex
++] = Number
;
450 if (FailedCpuList
!= NULL
) {
451 if (FailedListIndex
== 0) {
452 FreePool (*FailedCpuList
);
453 *FailedCpuList
= NULL
;
458 for (Number
= 0; Number
< gMPSystem
.NumberOfProcessors
; Number
++) {
459 ProcessorData
= &gMPSystem
.ProcessorData
[Number
];
460 if ((ProcessorData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) == PROCESSOR_AS_BSP_BIT
) {
465 gPthread
->MutextLock (ProcessorData
->StateLock
);
466 ProcessorState
= ProcessorData
->State
;
467 gPthread
->MutexUnlock (ProcessorData
->StateLock
);
469 switch (ProcessorState
) {
470 case CPU_STATE_READY
:
471 SetApProcedure (ProcessorData
, Procedure
, ProcedureArgument
);
474 case CPU_STATE_FINISHED
:
475 gMPSystem
.FinishCount
++;
477 Status
= GetNextBlockedNumber (&NextNumber
);
478 if (!EFI_ERROR (Status
)) {
479 gMPSystem
.ProcessorData
[NextNumber
].State
= CPU_STATE_READY
;
483 ProcessorData
->State
= CPU_STATE_IDLE
;
491 if (gMPSystem
.FinishCount
== gMPSystem
.StartCount
) {
495 if ((TimeoutInMicroseconds
!= 0) && (Timeout
< 0)) {
497 // Save data into private data structure, and create timer to poll AP state before exiting
499 gMPSystem
.Procedure
= Procedure
;
500 gMPSystem
.ProcedureArgument
= ProcedureArgument
;
501 gMPSystem
.WaitEvent
= WaitEvent
;
503 Status
= gBS
->SetTimer (
504 gMPSystem
.CheckAllAPsEvent
,
511 gBS
->Stall (gPollInterval
);
512 Timeout
-= gPollInterval
;
520 This service lets the caller get one enabled AP to execute a caller-provided
521 function. The caller can request the BSP to either wait for the completion
522 of the AP or just proceed with the next task by using the EFI event mechanism.
523 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
524 execution support. This service may only be called from the BSP.
526 This function is used to dispatch one enabled AP to the function specified by
527 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
528 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
529 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
530 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
531 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
532 then EFI_UNSUPPORTED must be returned.
534 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
535 from Procedure, then execution of Procedure by the AP is terminated. The AP is
536 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
537 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
539 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
541 @param[in] Procedure A pointer to the function to be run on
542 enabled APs of the system. See type
544 @param[in] ProcessorNumber The handle number of the AP. The range is
545 from 0 to the total number of logical
546 processors minus 1. The total number of
547 logical processors can be retrieved by
548 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
549 @param[in] WaitEvent The event created by the caller with CreateEvent()
550 service. If it is NULL, then execute in
551 blocking mode. BSP waits until all APs finish
552 or TimeoutInMicroseconds expires. If it's
553 not NULL, then execute in non-blocking mode.
554 BSP requests the function specified by
555 Procedure to be started on all the enabled
556 APs, and go on executing immediately. If
557 all return from Procedure or TimeoutInMicroseconds
558 expires, this event is signaled. The BSP
559 can use the CheckEvent() or WaitForEvent()
560 services to check the state of event. Type
561 EFI_EVENT is defined in CreateEvent() in
562 the Unified Extensible Firmware Interface
564 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
565 APs to return from Procedure, either for
566 blocking or non-blocking mode. Zero means
567 infinity. If the timeout expires before
568 all APs return from Procedure, then Procedure
569 on the failed APs is terminated. All enabled
570 APs are available for next function assigned
571 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
572 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
573 If the timeout expires in blocking mode,
574 BSP returns EFI_TIMEOUT. If the timeout
575 expires in non-blocking mode, WaitEvent
576 is signaled with SignalEvent().
577 @param[in] ProcedureArgument The parameter passed into Procedure for
579 @param[out] Finished If NULL, this parameter is ignored. In
580 blocking mode, this parameter is ignored.
581 In non-blocking mode, if AP returns from
582 Procedure before the timeout expires, its
583 content is set to TRUE. Otherwise, the
584 value is set to FALSE. The caller can
585 determine if the AP returned from Procedure
586 by evaluating this value.
588 @retval EFI_SUCCESS In blocking mode, specified AP finished before
590 @retval EFI_SUCCESS In non-blocking mode, the function has been
591 dispatched to specified AP.
592 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
593 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
595 @retval EFI_DEVICE_ERROR The calling processor is an AP.
596 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
597 the specified AP has finished.
598 @retval EFI_NOT_READY The specified AP is busy.
599 @retval EFI_NOT_FOUND The processor with the handle specified by
600 ProcessorNumber does not exist.
601 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
602 @retval EFI_INVALID_PARAMETER Procedure is NULL.
607 CpuMpServicesStartupThisAP (
608 IN EFI_MP_SERVICES_PROTOCOL
*This
,
609 IN EFI_AP_PROCEDURE Procedure
,
610 IN UINTN ProcessorNumber
,
611 IN EFI_EVENT WaitEvent OPTIONAL
,
612 IN UINTN TimeoutInMicroseconds
,
613 IN VOID
*ProcedureArgument OPTIONAL
,
614 OUT BOOLEAN
*Finished OPTIONAL
621 return EFI_DEVICE_ERROR
;
624 if (Procedure
== NULL
) {
625 return EFI_INVALID_PARAMETER
;
628 if (ProcessorNumber
>= gMPSystem
.NumberOfProcessors
) {
629 return EFI_NOT_FOUND
;
632 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0) {
633 return EFI_INVALID_PARAMETER
;
636 if (gMPSystem
.ProcessorData
[ProcessorNumber
].State
!= CPU_STATE_IDLE
) {
637 return EFI_NOT_READY
;
640 if ((WaitEvent
!= NULL
) && gReadToBoot
) {
641 return EFI_UNSUPPORTED
;
644 Timeout
= TimeoutInMicroseconds
;
646 gMPSystem
.StartCount
= 1;
647 gMPSystem
.FinishCount
= 0;
649 SetApProcedure (&gMPSystem
.ProcessorData
[ProcessorNumber
], Procedure
, ProcedureArgument
);
652 gPthread
->MutextLock (&gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
653 if (gMPSystem
.ProcessorData
[ProcessorNumber
].State
== CPU_STATE_FINISHED
) {
654 gMPSystem
.ProcessorData
[ProcessorNumber
].State
= CPU_STATE_IDLE
;
655 gPthread
->MutexUnlock (&gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
659 gPthread
->MutexUnlock (&gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
661 if ((TimeoutInMicroseconds
!= 0) && (Timeout
< 0)) {
662 gMPSystem
.WaitEvent
= WaitEvent
;
663 Status
= gBS
->SetTimer (
664 gMPSystem
.ProcessorData
[ProcessorNumber
].CheckThisAPEvent
,
671 gBS
->Stall (gPollInterval
);
672 Timeout
-= gPollInterval
;
681 This service switches the requested AP to be the BSP from that point onward.
682 This service changes the BSP for all purposes. This call can only be performed
685 This service switches the requested AP to be the BSP from that point onward.
686 This service changes the BSP for all purposes. The new BSP can take over the
687 execution of the old BSP and continue seamlessly from where the old one left
688 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
691 If the BSP cannot be switched prior to the return from this service, then
692 EFI_UNSUPPORTED must be returned.
694 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
695 @param[in] ProcessorNumber The handle number of AP that is to become the new
696 BSP. The range is from 0 to the total number of
697 logical processors minus 1. The total number of
698 logical processors can be retrieved by
699 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
700 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
701 enabled AP. Otherwise, it will be disabled.
703 @retval EFI_SUCCESS BSP successfully switched.
704 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
705 this service returning.
706 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
707 @retval EFI_SUCCESS The calling processor is an AP.
708 @retval EFI_NOT_FOUND The processor with the handle specified by
709 ProcessorNumber does not exist.
710 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
712 @retval EFI_NOT_READY The specified AP is busy.
717 CpuMpServicesSwitchBSP (
718 IN EFI_MP_SERVICES_PROTOCOL
*This
,
719 IN UINTN ProcessorNumber
,
720 IN BOOLEAN EnableOldBSP
726 return EFI_DEVICE_ERROR
;
729 if (ProcessorNumber
>= gMPSystem
.NumberOfProcessors
) {
730 return EFI_NOT_FOUND
;
733 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_ENABLED_BIT
) == 0) {
734 return EFI_INVALID_PARAMETER
;
737 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0) {
738 return EFI_INVALID_PARAMETER
;
741 for (Index
= 0; Index
< gMPSystem
.NumberOfProcessors
; Index
++) {
742 if ((gMPSystem
.ProcessorData
[Index
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0) {
746 ASSERT (Index
!= gMPSystem
.NumberOfProcessors
);
748 if (gMPSystem
.ProcessorData
[ProcessorNumber
].State
!= CPU_STATE_IDLE
) {
749 return EFI_NOT_READY
;
752 // Skip for now as we need switch a bunch of stack stuff around and it's complex
753 // May not be worth it?
754 return EFI_NOT_READY
;
759 This service lets the caller enable or disable an AP from this point onward.
760 This service may only be called from the BSP.
762 This service allows the caller enable or disable an AP from this point onward.
763 The caller can optionally specify the health status of the AP by Health. If
764 an AP is being disabled, then the state of the disabled AP is implementation
765 dependent. If an AP is enabled, then the implementation must guarantee that a
766 complete initialization sequence is performed on the AP, so the AP is in a state
767 that is compatible with an MP operating system. This service may not be supported
768 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
770 If the enable or disable AP operation cannot be completed prior to the return
771 from this service, then EFI_UNSUPPORTED must be returned.
773 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
774 @param[in] ProcessorNumber The handle number of AP that is to become the new
775 BSP. The range is from 0 to the total number of
776 logical processors minus 1. The total number of
777 logical processors can be retrieved by
778 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
779 @param[in] EnableAP Specifies the new state for the processor for
780 enabled, FALSE for disabled.
781 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
782 the new health status of the AP. This flag
783 corresponds to StatusFlag defined in
784 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
785 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
786 bits are ignored. If it is NULL, this parameter
789 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
790 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
791 prior to this service returning.
792 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
793 @retval EFI_DEVICE_ERROR The calling processor is an AP.
794 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
796 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
801 CpuMpServicesEnableDisableAP (
802 IN EFI_MP_SERVICES_PROTOCOL
*This
,
803 IN UINTN ProcessorNumber
,
805 IN UINT32
*HealthFlag OPTIONAL
809 return EFI_DEVICE_ERROR
;
812 if (ProcessorNumber
>= gMPSystem
.NumberOfProcessors
) {
813 return EFI_NOT_FOUND
;
816 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) != 0) {
817 return EFI_INVALID_PARAMETER
;
820 if (gMPSystem
.ProcessorData
[ProcessorNumber
].State
!= CPU_STATE_IDLE
) {
821 return EFI_UNSUPPORTED
;
824 gPthread
->MutextLock (&gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
827 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_ENABLED_BIT
) == 0 ) {
828 gMPSystem
.NumberOfEnabledProcessors
++;
830 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
|= PROCESSOR_ENABLED_BIT
;
832 if ((gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_ENABLED_BIT
) == PROCESSOR_ENABLED_BIT
) {
833 gMPSystem
.NumberOfEnabledProcessors
--;
835 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
838 if (HealthFlag
!= NULL
) {
839 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
&= ~PROCESSOR_HEALTH_STATUS_BIT
;
840 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
|= (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
);
843 gPthread
->MutexUnlock (&gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
850 This return the handle number for the calling processor. This service may be
851 called from the BSP and APs.
853 This service returns the processor handle number for the calling processor.
854 The returned value is in the range from 0 to the total number of logical
855 processors minus 1. The total number of logical processors can be retrieved
856 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
857 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
858 is returned. Otherwise, the current processors handle number is returned in
859 ProcessorNumber, and EFI_SUCCESS is returned.
861 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
862 @param[in] ProcessorNumber The handle number of AP that is to become the new
863 BSP. The range is from 0 to the total number of
864 logical processors minus 1. The total number of
865 logical processors can be retrieved by
866 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
868 @retval EFI_SUCCESS The current processor handle number was returned
870 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
875 CpuMpServicesWhoAmI (
876 IN EFI_MP_SERVICES_PROTOCOL
*This
,
877 OUT UINTN
*ProcessorNumber
883 if (ProcessorNumber
== NULL
) {
884 return EFI_INVALID_PARAMETER
;
887 ProcessorId
= gPthread
->Self ();
888 for (Index
= 0; Index
< gMPSystem
.NumberOfProcessors
; Index
++) {
889 if (gMPSystem
.ProcessorData
[Index
].Info
.ProcessorId
== ProcessorId
) {
894 *ProcessorNumber
= Index
;
900 EFI_MP_SERVICES_PROTOCOL mMpSercicesTemplate
= {
901 CpuMpServicesGetNumberOfProcessors
,
902 CpuMpServicesGetProcessorInfo
,
903 CpuMpServicesStartupAllAps
,
904 CpuMpServicesStartupThisAP
,
905 CpuMpServicesSwitchBSP
,
906 CpuMpServicesEnableDisableAP
,
913 If timeout occurs in StartupAllAps(), a timer is set, which invokes this
914 procedure periodically to check whether all APs have finished.
920 CpuCheckAllAPsStatus (
925 UINTN ProcessorNumber
;
927 PROCESSOR_DATA_BLOCK
*ProcessorData
;
928 PROCESSOR_DATA_BLOCK
*NextData
;
930 PROCESSOR_STATE ProcessorState
;
932 for (ProcessorNumber
= 0; ProcessorNumber
< gMPSystem
.NumberOfProcessors
; ProcessorNumber
++) {
933 if ((ProcessorData
[ProcessorNumber
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) == PROCESSOR_AS_BSP_BIT
) {
938 // This is an Interrupt Service routine.
939 // This can grab a lock that is held in a non-interrupt
940 // context. Meaning deadlock. Which is a bad thing.
941 // So, try lock it. If we can get it, cool, do our thing.
942 // otherwise, just dump out & try again on the next iteration.
943 Status
= gPthread
->MutexTryLock (gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
944 if (EFI_ERROR(Status
)) {
947 ProcessorState
= gMPSystem
.ProcessorData
[ProcessorNumber
].State
;
948 gPthread
->MutexUnlock (gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
);
950 switch (ProcessorState
) {
951 case CPU_STATE_READY
:
952 SetApProcedure (ProcessorData
, gMPSystem
.Procedure
, gMPSystem
.ProcedureArgument
);
955 case CPU_STATE_FINISHED
:
956 if (gMPSystem
.SingleThread
) {
957 Status
= GetNextBlockedNumber (&NextNumber
);
958 if (!EFI_ERROR (Status
)) {
959 NextData
= &gMPSystem
.ProcessorData
[NextNumber
];
961 gPthread
->MutextLock (&NextData
->ProcedureLock
);
962 NextData
->State
= CPU_STATE_READY
;
963 gPthread
->MutexUnlock (&NextData
->ProcedureLock
);
965 SetApProcedure (NextData
, gMPSystem
.Procedure
, gMPSystem
.ProcedureArgument
);
969 gMPSystem
.ProcessorData
[ProcessorNumber
].State
= CPU_STATE_IDLE
;
970 gMPSystem
.FinishCount
++;
978 if (gMPSystem
.FinishCount
== gMPSystem
.StartCount
) {
980 gMPSystem
.CheckAllAPsEvent
,
984 Status
= gBS
->SignalEvent (gMPSystem
.WaitEvent
);
992 CpuCheckThisAPStatus (
998 PROCESSOR_DATA_BLOCK
*ProcessorData
;
999 PROCESSOR_STATE ProcessorState
;
1001 ProcessorData
= (PROCESSOR_DATA_BLOCK
*) Context
;
1004 // rdar://6260979 - This is an Interrupt Service routine.
1005 // this can grab a lock that is held in a non-interrupt
1006 // context. Meaning deadlock. Which is a badddd thing.
1007 // So, try lock it. If we can get it, cool, do our thing.
1008 // otherwise, just dump out & try again on the next iteration.
1010 Status
= gPthread
->MutexTryLock (ProcessorData
->StateLock
);
1011 if (EFI_ERROR(Status
)) {
1014 ProcessorState
= ProcessorData
->State
;
1015 gPthread
->MutexUnlock (ProcessorData
->StateLock
);
1017 if (ProcessorState
== CPU_STATE_FINISHED
) {
1018 Status
= gBS
->SetTimer (ProcessorData
->CheckThisAPEvent
, TimerCancel
, 0);
1019 ASSERT_EFI_ERROR (Status
);
1021 Status
= gBS
->SignalEvent (gMPSystem
.WaitEvent
);
1022 ASSERT_EFI_ERROR (Status
);
1024 gPthread
->MutextLock (ProcessorData
->StateLock
);
1025 ProcessorData
->State
= CPU_STATE_IDLE
;
1026 gPthread
->MutexUnlock (ProcessorData
->StateLock
);
1034 This function is called by all processors (both BSP and AP) once and collects MP related data
1036 MPSystemData - Pointer to the data structure containing MP related data
1037 BSP - TRUE if the CPU is BSP
1039 EFI_SUCCESS - Data for the processor collected and filled in
1043 FillInProcessorInformation (
1045 IN UINTN ProcessorNumber
1048 PROCESSOR_DATA_BLOCK
*ProcessorData
;
1050 ProcessorData
= &gMPSystem
.ProcessorData
[ProcessorNumber
];
1052 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.ProcessorId
= gPthread
->Self ();
1053 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1055 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1058 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.Location
.Package
= ProcessorNumber
;
1059 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.Location
.Core
= 0;
1060 gMPSystem
.ProcessorData
[ProcessorNumber
].Info
.Location
.Thread
= 0;
1061 gMPSystem
.ProcessorData
[ProcessorNumber
].State
= BSP
? CPU_STATE_BUSY
: CPU_STATE_IDLE
;
1063 gMPSystem
.ProcessorData
[ProcessorNumber
].Procedure
= NULL
;
1064 gMPSystem
.ProcessorData
[ProcessorNumber
].Parameter
= NULL
;
1065 gMPSystem
.ProcessorData
[ProcessorNumber
].StateLock
= gPthread
->MutexInit ();
1066 gMPSystem
.ProcessorData
[ProcessorNumber
].ProcedureLock
= gPthread
->MutexInit ();
1073 CpuDriverApIdolLoop (
1077 EFI_AP_PROCEDURE Procedure
;
1079 UINTN ProcessorNumber
;
1080 PROCESSOR_DATA_BLOCK
*ProcessorData
;
1082 ProcessorNumber
= (UINTN
)Context
;
1083 ProcessorData
= &gMPSystem
.ProcessorData
[ProcessorNumber
];
1085 ProcessorData
->Info
.ProcessorId
= gPthread
->Self ();
1089 // Make a local copy on the stack to be extra safe
1091 gPthread
->MutextLock (ProcessorData
->ProcedureLock
);
1092 Procedure
= ProcessorData
->Procedure
;
1093 Parameter
= ProcessorData
->Parameter
;
1094 gPthread
->MutexUnlock (ProcessorData
->ProcedureLock
);
1096 if (Procedure
!= NULL
) {
1097 gPthread
->MutextLock (ProcessorData
->StateLock
);
1098 ProcessorData
->State
= CPU_STATE_BUSY
;
1099 gPthread
->MutexUnlock (ProcessorData
->StateLock
);
1101 Procedure (Parameter
);
1103 gPthread
->MutextLock (ProcessorData
->ProcedureLock
);
1104 ProcessorData
->Procedure
= NULL
;
1105 gPthread
->MutexUnlock (ProcessorData
->ProcedureLock
);
1107 gPthread
->MutextLock (ProcessorData
->StateLock
);
1108 ProcessorData
->State
= CPU_STATE_FINISHED
;
1109 gPthread
->MutexUnlock (ProcessorData
->StateLock
);
1112 // Poll 5 times a seconds, 200ms
1113 // Don't want to burn too many system resources doing nothing.
1114 gEmuThunk
->Sleep (200);
1122 InitializeMpSystemData (
1123 IN UINTN NumberOfProcessors
1131 // Clear the data structure area first.
1133 ZeroMem (&gMPSystem
, sizeof (MP_SYSTEM_DATA
));
1136 // First BSP fills and inits all known values, including it's own records.
1138 gMPSystem
.NumberOfProcessors
= NumberOfProcessors
;
1139 gMPSystem
.NumberOfEnabledProcessors
= NumberOfProcessors
;
1141 gMPSystem
.ProcessorData
= AllocateZeroPool (gMPSystem
.NumberOfProcessors
* sizeof (PROCESSOR_DATA_BLOCK
));
1142 ASSERT (gMPSystem
.ProcessorData
!= NULL
);
1144 FillInProcessorInformation (TRUE
, 0);
1146 Status
= gBS
->CreateEvent (
1147 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1149 CpuCheckAllAPsStatus
,
1151 &gMPSystem
.CheckAllAPsEvent
1153 ASSERT_EFI_ERROR (Status
);
1156 for (Index
= 0; Index
< gMPSystem
.NumberOfProcessors
; Index
++) {
1157 if ((gMPSystem
.ProcessorData
[Index
].Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
) == PROCESSOR_AS_BSP_BIT
) {
1162 FillInProcessorInformation (FALSE
, Index
);
1164 Status
= gPthread
->CreateThread (
1165 (VOID
*)&gMPSystem
.ProcessorData
[Index
].Info
.ProcessorId
,
1167 CpuDriverApIdolLoop
,
1172 Status
= gBS
->CreateEvent (
1173 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1175 CpuCheckThisAPStatus
,
1176 (VOID
*) &gMPSystem
.ProcessorData
[Index
],
1177 &gMPSystem
.ProcessorData
[Index
].CheckThisAPEvent
1187 Invoke a notification event
1189 @param Event Event whose notification function is being invoked.
1190 @param Context The pointer to the notification function's context,
1191 which is implementation-dependent.
1196 CpuReadToBootFunction (
1213 EMU_IO_THUNK_PROTOCOL
*IoThunk
;
1219 IoThunk
= GetIoThunkInstance (&gEmuPthreadThunkProtocolGuid
, 0);
1220 if (IoThunk
!= NULL
) {
1221 Status
= IoThunk
->Open (IoThunk
);
1222 if (!EFI_ERROR (Status
)) {
1223 if (IoThunk
->ConfigString
!= NULL
) {
1224 MaxCpus
+= StrDecimalToUintn (IoThunk
->ConfigString
);
1225 gPthread
= IoThunk
->Interface
;
1231 // We are not MP so nothing to do
1235 gPollInterval
= PcdGet64 (PcdEmuMpServicesPollingInterval
);
1237 Status
= InitializeMpSystemData (MaxCpus
);
1238 if (EFI_ERROR (Status
)) {
1242 Status
= EfiCreateEventReadyToBootEx (TPL_CALLBACK
, CpuReadToBootFunction
, NULL
, &gReadToBootEvent
);
1243 ASSERT_EFI_ERROR (Status
);
1246 // Now install the MP services protocol.
1249 Status
= gBS
->InstallMultipleProtocolInterfaces (
1251 &gEfiMpServiceProtocolGuid
, &mMpSercicesTemplate
,