4 Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 UINTN gMaxLogicalProcessorNumber
;
20 UINTN gPollInterval
= 100; // 100 microseconds
22 MP_SYSTEM_DATA mMpSystemData
;
24 VOID
*mCommonStack
= 0;
25 VOID
*mTopOfApCommonStack
= 0;
26 VOID
*mApStackStart
= 0;
28 volatile BOOLEAN mAPsAlreadyInitFinished
= FALSE
;
29 volatile BOOLEAN mStopCheckAllAPsStatus
= TRUE
;
31 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate
= {
32 GetNumberOfProcessors
,
44 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
49 IN CPU_DATA_BLOCK
*CpuData
52 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
55 CpuData
->LockSelf
= GetApicId ();
59 Release Mp Service Lock.
61 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
66 IN CPU_DATA_BLOCK
*CpuData
69 ReleaseSpinLock (&CpuData
->CpuDataLock
);
73 Check whether caller processor is BSP.
75 @retval TRUE the caller is BSP
76 @retval FALSE the caller is AP
85 CPU_DATA_BLOCK
*CpuData
;
89 WhoAmI (&mMpServicesTemplate
, &CpuIndex
);
90 CpuData
= &mMpSystemData
.CpuDatas
[CpuIndex
];
92 return CpuData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
? TRUE
: FALSE
;
96 Get the Application Processors state.
98 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
100 @retval CPU_STATE the AP status
105 IN CPU_DATA_BLOCK
*CpuData
110 GetMpSpinLock (CpuData
);
111 State
= CpuData
->State
;
112 ReleaseMpSpinLock (CpuData
);
118 Set the Application Processors state.
120 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP
121 @param State The AP status
126 IN CPU_DATA_BLOCK
*CpuData
,
130 GetMpSpinLock (CpuData
);
131 CpuData
->State
= State
;
132 ReleaseMpSpinLock (CpuData
);
136 Set the Application Processor prepare to run a function specified
139 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
140 @param Procedure A pointer to the function to be run on enabled APs of the system
141 @param ProcedureArgument Pointer to the optional parameter of the assigned function
146 IN CPU_DATA_BLOCK
*CpuData
,
147 IN EFI_AP_PROCEDURE Procedure
,
148 IN VOID
*ProcedureArgument
151 GetMpSpinLock (CpuData
);
152 CpuData
->Parameter
= ProcedureArgument
;
153 CpuData
->Procedure
= Procedure
;
154 ReleaseMpSpinLock (CpuData
);
158 Check the Application Processors Status whether contains the Flags.
160 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
161 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
163 @retval TRUE the AP status includes the StatusFlag
164 @retval FALSE the AP status excludes the StatusFlag
169 IN CPU_DATA_BLOCK
*CpuData
,
175 GetMpSpinLock (CpuData
);
176 Ret
= CpuData
->Info
.StatusFlag
& Flags
;
177 ReleaseMpSpinLock (CpuData
);
183 Bitwise-Or of the Application Processors Status with the Flags.
185 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
186 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
191 IN CPU_DATA_BLOCK
*CpuData
,
195 GetMpSpinLock (CpuData
);
196 CpuData
->Info
.StatusFlag
|= Flags
;
197 ReleaseMpSpinLock (CpuData
);
201 Bitwise-AndNot of the Application Processors Status with the Flags.
203 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
204 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
208 CpuStatusFlagAndNot (
209 IN CPU_DATA_BLOCK
*CpuData
,
213 GetMpSpinLock (CpuData
);
214 CpuData
->Info
.StatusFlag
&= ~Flags
;
215 ReleaseMpSpinLock (CpuData
);
219 Searches for the next blocking AP.
221 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().
223 @param NextNumber Pointer to the processor number of the next blocking AP.
225 @retval EFI_SUCCESS The next blocking AP has been found.
226 @retval EFI_NOT_FOUND No blocking AP exists.
230 GetNextBlockedNumber (
231 OUT UINTN
*NextNumber
236 CPU_DATA_BLOCK
*CpuData
;
238 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
239 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
240 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
247 CpuState
= GetApState (CpuData
);
248 if (CpuState
== CpuStateBlocked
) {
249 *NextNumber
= Number
;
254 return EFI_NOT_FOUND
;
258 Check if the APs state are finished, and update them to idle state
263 CheckAndUpdateAllAPsToIdleState (
267 UINTN ProcessorNumber
;
269 CPU_DATA_BLOCK
*CpuData
;
273 for (ProcessorNumber
= 0; ProcessorNumber
< mMpSystemData
.NumberOfProcessors
; ProcessorNumber
++) {
274 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
275 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
282 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
284 // Skip Disabled processors
289 CpuState
= GetApState (CpuData
);
290 if (CpuState
== CpuStateFinished
) {
291 mMpSystemData
.FinishCount
++;
292 if (mMpSystemData
.SingleThread
) {
293 Status
= GetNextBlockedNumber (&NextNumber
);
294 if (!EFI_ERROR (Status
)) {
295 SetApState (&mMpSystemData
.CpuDatas
[NextNumber
], CpuStateReady
);
296 SetApProcedure (&mMpSystemData
.CpuDatas
[NextNumber
],
297 mMpSystemData
.Procedure
,
298 mMpSystemData
.ProcedureArgument
);
302 SetApState (CpuData
, CpuStateIdle
);
308 If the timeout expires before all APs returns from Procedure,
309 we should forcibly terminate the executing AP and fill FailedList back
318 CPU_DATA_BLOCK
*CpuData
;
322 if (mMpSystemData
.FailedList
!= NULL
) {
323 *mMpSystemData
.FailedList
= AllocatePool ((mMpSystemData
.StartCount
- mMpSystemData
.FinishCount
+ 1) * sizeof(UINTN
));
324 ASSERT (*mMpSystemData
.FailedList
!= NULL
);
327 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
328 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
329 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
336 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
338 // Skip Disabled processors
343 CpuState
= GetApState (CpuData
);
344 if (CpuState
!= CpuStateIdle
) {
345 if (mMpSystemData
.FailedList
!= NULL
) {
346 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
++] = Number
;
348 ResetProcessorToIdleState (CpuData
);
352 if (mMpSystemData
.FailedList
!= NULL
) {
353 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
] = END_OF_CPU_LIST
;
358 This service retrieves the number of logical processor in the platform
359 and the number of those logical processors that are enabled on this boot.
360 This service may only be called from the BSP.
362 This function is used to retrieve the following information:
363 - The number of logical processors that are present in the system.
364 - The number of enabled logical processors in the system at the instant
367 Because MP Service Protocol provides services to enable and disable processors
368 dynamically, the number of enabled logical processors may vary during the
369 course of a boot session.
371 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
372 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
373 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
374 is returned in NumberOfProcessors, the number of currently enabled processor
375 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
377 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
379 @param[out] NumberOfProcessors Pointer to the total number of logical
380 processors in the system, including the BSP
382 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
383 processors that exist in system, including
386 @retval EFI_SUCCESS The number of logical processors and enabled
387 logical processors was retrieved.
388 @retval EFI_DEVICE_ERROR The calling processor is an AP.
389 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
390 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
395 GetNumberOfProcessors (
396 IN EFI_MP_SERVICES_PROTOCOL
*This
,
397 OUT UINTN
*NumberOfProcessors
,
398 OUT UINTN
*NumberOfEnabledProcessors
401 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
402 return EFI_INVALID_PARAMETER
;
406 return EFI_DEVICE_ERROR
;
409 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
410 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
415 Gets detailed MP-related information on the requested processor at the
416 instant this call is made. This service may only be called from the BSP.
418 This service retrieves detailed MP-related information about any processor
419 on the platform. Note the following:
420 - The processor information may change during the course of a boot session.
421 - The information presented here is entirely MP related.
423 Information regarding the number of caches and their sizes, frequency of operation,
424 slot numbers is all considered platform-related information and is not provided
427 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
429 @param[in] ProcessorNumber The handle number of processor.
430 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
431 the requested processor is deposited.
433 @retval EFI_SUCCESS Processor information was returned.
434 @retval EFI_DEVICE_ERROR The calling processor is an AP.
435 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
436 @retval EFI_NOT_FOUND The processor with the handle specified by
437 ProcessorNumber does not exist in the platform.
443 IN EFI_MP_SERVICES_PROTOCOL
*This
,
444 IN UINTN ProcessorNumber
,
445 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
448 if (ProcessorInfoBuffer
== NULL
) {
449 return EFI_INVALID_PARAMETER
;
453 return EFI_DEVICE_ERROR
;
456 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
457 return EFI_NOT_FOUND
;
460 CopyMem (ProcessorInfoBuffer
, &mMpSystemData
.CpuDatas
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
465 This service executes a caller provided function on all enabled APs. APs can
466 run either simultaneously or one at a time in sequence. This service supports
467 both blocking and non-blocking requests. The non-blocking requests use EFI
468 events so the BSP can detect when the APs have finished. This service may only
469 be called from the BSP.
471 This function is used to dispatch all the enabled APs to the function specified
472 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
473 immediately and Procedure is not started on any AP.
475 If SingleThread is TRUE, all the enabled APs execute the function specified by
476 Procedure one by one, in ascending order of processor handle number. Otherwise,
477 all the enabled APs execute the function specified by Procedure simultaneously.
479 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
480 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
481 mode, and the BSP returns from this service without waiting for APs. If a
482 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
483 is signaled, then EFI_UNSUPPORTED must be returned.
485 If the timeout specified by TimeoutInMicroseconds expires before all APs return
486 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
487 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
488 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
489 content points to the list of processor handle numbers in which Procedure was
492 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
493 to make sure that the nature of the code that is executed on the BSP and the
494 dispatched APs is well controlled. The MP Services Protocol does not guarantee
495 that the Procedure function is MP-safe. Hence, the tasks that can be run in
496 parallel are limited to certain independent tasks and well-controlled exclusive
497 code. EFI services and protocols may not be called by APs unless otherwise
500 In blocking execution mode, BSP waits until all APs finish or
501 TimeoutInMicroseconds expires.
503 In non-blocking execution mode, BSP is freed to return to the caller and then
504 proceed to the next task without having to wait for APs. The following
505 sequence needs to occur in a non-blocking execution mode:
507 -# The caller that intends to use this MP Services Protocol in non-blocking
508 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
509 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
510 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
511 the function specified by Procedure to be started on all the enabled APs,
512 and releases the BSP to continue with other tasks.
513 -# The caller can use the CheckEvent() and WaitForEvent() services to check
514 the state of the WaitEvent created in step 1.
515 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
516 Service signals WaitEvent by calling the EFI SignalEvent() function. If
517 FailedCpuList is not NULL, its content is available when WaitEvent is
518 signaled. If all APs returned from Procedure prior to the timeout, then
519 FailedCpuList is set to NULL. If not all APs return from Procedure before
520 the timeout, then FailedCpuList is filled in with the list of the failed
521 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
522 It is the caller's responsibility to free the buffer with FreePool() service.
523 -# This invocation of SignalEvent() function informs the caller that invoked
524 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
525 the specified task or a timeout occurred. The contents of FailedCpuList
526 can be examined to determine which APs did not complete the specified task
527 prior to the timeout.
529 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
531 @param[in] Procedure A pointer to the function to be run on
532 enabled APs of the system. See type
534 @param[in] SingleThread If TRUE, then all the enabled APs execute
535 the function specified by Procedure one by
536 one, in ascending order of processor handle
537 number. If FALSE, then all the enabled APs
538 execute the function specified by Procedure
540 @param[in] WaitEvent The event created by the caller with CreateEvent()
541 service. If it is NULL, then execute in
542 blocking mode. BSP waits until all APs finish
543 or TimeoutInMicroseconds expires. If it's
544 not NULL, then execute in non-blocking mode.
545 BSP requests the function specified by
546 Procedure to be started on all the enabled
547 APs, and go on executing immediately. If
548 all return from Procedure, or TimeoutInMicroseconds
549 expires, this event is signaled. The BSP
550 can use the CheckEvent() or WaitForEvent()
551 services to check the state of event. Type
552 EFI_EVENT is defined in CreateEvent() in
553 the Unified Extensible Firmware Interface
555 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
556 APs to return from Procedure, either for
557 blocking or non-blocking mode. Zero means
558 infinity. If the timeout expires before
559 all APs return from Procedure, then Procedure
560 on the failed APs is terminated. All enabled
561 APs are available for next function assigned
562 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
563 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
564 If the timeout expires in blocking mode,
565 BSP returns EFI_TIMEOUT. If the timeout
566 expires in non-blocking mode, WaitEvent
567 is signaled with SignalEvent().
568 @param[in] ProcedureArgument The parameter passed into Procedure for
570 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
571 if all APs finish successfully, then its
572 content is set to NULL. If not all APs
573 finish before timeout expires, then its
574 content is set to address of the buffer
575 holding handle numbers of the failed APs.
576 The buffer is allocated by MP Service Protocol,
577 and it's the caller's responsibility to
578 free the buffer with FreePool() service.
579 In blocking mode, it is ready for consumption
580 when the call returns. In non-blocking mode,
581 it is ready when WaitEvent is signaled. The
582 list of failed CPU is terminated by
585 @retval EFI_SUCCESS In blocking mode, all APs have finished before
587 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
589 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
590 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
592 @retval EFI_DEVICE_ERROR Caller processor is AP.
593 @retval EFI_NOT_STARTED No enabled APs exist in the system.
594 @retval EFI_NOT_READY Any enabled APs are busy.
595 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
596 all enabled APs have finished.
597 @retval EFI_INVALID_PARAMETER Procedure is NULL.
603 IN EFI_MP_SERVICES_PROTOCOL
*This
,
604 IN EFI_AP_PROCEDURE Procedure
,
605 IN BOOLEAN SingleThread
,
606 IN EFI_EVENT WaitEvent OPTIONAL
,
607 IN UINTN TimeoutInMicroseconds
,
608 IN VOID
*ProcedureArgument OPTIONAL
,
609 OUT UINTN
**FailedCpuList OPTIONAL
613 CPU_DATA_BLOCK
*CpuData
;
615 CPU_STATE APInitialState
;
619 if (FailedCpuList
!= NULL
) {
620 *FailedCpuList
= NULL
;
624 return EFI_DEVICE_ERROR
;
627 if (mMpSystemData
.NumberOfProcessors
== 1) {
628 return EFI_NOT_STARTED
;
631 if (Procedure
== NULL
) {
632 return EFI_INVALID_PARAMETER
;
635 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
636 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
637 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
644 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
646 // Skip Disabled processors
651 if (GetApState (CpuData
) != CpuStateIdle
) {
652 return EFI_NOT_READY
;
657 // temporarily stop checkAllAPsStatus for initialize parameters.
659 mStopCheckAllAPsStatus
= TRUE
;
661 mMpSystemData
.Procedure
= Procedure
;
662 mMpSystemData
.ProcedureArgument
= ProcedureArgument
;
663 mMpSystemData
.WaitEvent
= WaitEvent
;
664 mMpSystemData
.Timeout
= TimeoutInMicroseconds
;
665 mMpSystemData
.TimeoutActive
= !!(TimeoutInMicroseconds
);
666 mMpSystemData
.FinishCount
= 0;
667 mMpSystemData
.StartCount
= 0;
668 mMpSystemData
.SingleThread
= SingleThread
;
669 mMpSystemData
.FailedList
= FailedCpuList
;
670 mMpSystemData
.FailedListIndex
= 0;
671 APInitialState
= CpuStateReady
;
673 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
674 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
675 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
682 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
684 // Skip Disabled processors
690 // Get APs prepared, and put failing APs into FailedCpuList
691 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
692 // state 1 by 1, until the previous 1 finished its task
693 // if not "SingleThread", all APs are put to ready state from the beginning
695 if (GetApState (CpuData
) == CpuStateIdle
) {
696 mMpSystemData
.StartCount
++;
698 SetApState (CpuData
, APInitialState
);
700 if (APInitialState
== CpuStateReady
) {
701 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
705 APInitialState
= CpuStateBlocked
;
710 mStopCheckAllAPsStatus
= FALSE
;
712 if (WaitEvent
!= NULL
) {
720 CheckAndUpdateAllAPsToIdleState ();
721 if (mMpSystemData
.FinishCount
== mMpSystemData
.StartCount
) {
722 Status
= EFI_SUCCESS
;
729 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
731 Status
= EFI_TIMEOUT
;
735 gBS
->Stall (gPollInterval
);
736 mMpSystemData
.Timeout
-= gPollInterval
;
745 This service lets the caller get one enabled AP to execute a caller-provided
746 function. The caller can request the BSP to either wait for the completion
747 of the AP or just proceed with the next task by using the EFI event mechanism.
748 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
749 execution support. This service may only be called from the BSP.
751 This function is used to dispatch one enabled AP to the function specified by
752 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
753 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
754 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
755 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
756 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
757 then EFI_UNSUPPORTED must be returned.
759 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
760 from Procedure, then execution of Procedure by the AP is terminated. The AP is
761 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
762 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
764 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
766 @param[in] Procedure A pointer to the function to be run on
767 enabled APs of the system. See type
769 @param[in] ProcessorNumber The handle number of the AP. The range is
770 from 0 to the total number of logical
771 processors minus 1. The total number of
772 logical processors can be retrieved by
773 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
774 @param[in] WaitEvent The event created by the caller with CreateEvent()
775 service. If it is NULL, then execute in
776 blocking mode. BSP waits until all APs finish
777 or TimeoutInMicroseconds expires. If it's
778 not NULL, then execute in non-blocking mode.
779 BSP requests the function specified by
780 Procedure to be started on all the enabled
781 APs, and go on executing immediately. If
782 all return from Procedure or TimeoutInMicroseconds
783 expires, this event is signaled. The BSP
784 can use the CheckEvent() or WaitForEvent()
785 services to check the state of event. Type
786 EFI_EVENT is defined in CreateEvent() in
787 the Unified Extensible Firmware Interface
789 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
790 APs to return from Procedure, either for
791 blocking or non-blocking mode. Zero means
792 infinity. If the timeout expires before
793 all APs return from Procedure, then Procedure
794 on the failed APs is terminated. All enabled
795 APs are available for next function assigned
796 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
797 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
798 If the timeout expires in blocking mode,
799 BSP returns EFI_TIMEOUT. If the timeout
800 expires in non-blocking mode, WaitEvent
801 is signaled with SignalEvent().
802 @param[in] ProcedureArgument The parameter passed into Procedure for
804 @param[out] Finished If NULL, this parameter is ignored. In
805 blocking mode, this parameter is ignored.
806 In non-blocking mode, if AP returns from
807 Procedure before the timeout expires, its
808 content is set to TRUE. Otherwise, the
809 value is set to FALSE. The caller can
810 determine if the AP returned from Procedure
811 by evaluating this value.
813 @retval EFI_SUCCESS In blocking mode, specified AP finished before
815 @retval EFI_SUCCESS In non-blocking mode, the function has been
816 dispatched to specified AP.
817 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
818 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
820 @retval EFI_DEVICE_ERROR The calling processor is an AP.
821 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
822 the specified AP has finished.
823 @retval EFI_NOT_READY The specified AP is busy.
824 @retval EFI_NOT_FOUND The processor with the handle specified by
825 ProcessorNumber does not exist.
826 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
827 @retval EFI_INVALID_PARAMETER Procedure is NULL.
833 IN EFI_MP_SERVICES_PROTOCOL
*This
,
834 IN EFI_AP_PROCEDURE Procedure
,
835 IN UINTN ProcessorNumber
,
836 IN EFI_EVENT WaitEvent OPTIONAL
,
837 IN UINTN TimeoutInMicroseconds
,
838 IN VOID
*ProcedureArgument OPTIONAL
,
839 OUT BOOLEAN
*Finished OPTIONAL
842 CPU_DATA_BLOCK
*CpuData
;
846 if (Finished
!= NULL
) {
851 return EFI_DEVICE_ERROR
;
854 if (Procedure
== NULL
) {
855 return EFI_INVALID_PARAMETER
;
858 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
859 return EFI_NOT_FOUND
;
862 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
863 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
) ||
864 !TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
865 return EFI_INVALID_PARAMETER
;
868 if (GetApState (CpuData
) != CpuStateIdle
) {
869 return EFI_NOT_READY
;
873 // temporarily stop checkAllAPsStatus for initialize parameters.
875 mStopCheckAllAPsStatus
= TRUE
;
877 SetApState (CpuData
, CpuStateReady
);
879 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
881 CpuData
->Timeout
= TimeoutInMicroseconds
;
882 CpuData
->WaitEvent
= WaitEvent
;
883 CpuData
->TimeoutActive
= !!(TimeoutInMicroseconds
);
884 CpuData
->Finished
= Finished
;
886 mStopCheckAllAPsStatus
= FALSE
;
888 if (WaitEvent
!= NULL
) {
899 if (GetApState (CpuData
) == CpuStateFinished
) {
900 SetApState (CpuData
, CpuStateIdle
);
904 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
905 ResetProcessorToIdleState (CpuData
);
909 gBS
->Stall (gPollInterval
);
910 CpuData
->Timeout
-= gPollInterval
;
917 This service switches the requested AP to be the BSP from that point onward.
918 This service changes the BSP for all purposes. This call can only be performed
921 This service switches the requested AP to be the BSP from that point onward.
922 This service changes the BSP for all purposes. The new BSP can take over the
923 execution of the old BSP and continue seamlessly from where the old one left
924 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
927 If the BSP cannot be switched prior to the return from this service, then
928 EFI_UNSUPPORTED must be returned.
930 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
931 @param[in] ProcessorNumber The handle number of AP that is to become the new
932 BSP. The range is from 0 to the total number of
933 logical processors minus 1. The total number of
934 logical processors can be retrieved by
935 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
936 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
937 enabled AP. Otherwise, it will be disabled.
939 @retval EFI_SUCCESS BSP successfully switched.
940 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
941 this service returning.
942 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
943 @retval EFI_SUCCESS The calling processor is an AP.
944 @retval EFI_NOT_FOUND The processor with the handle specified by
945 ProcessorNumber does not exist.
946 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
948 @retval EFI_NOT_READY The specified AP is busy.
954 IN EFI_MP_SERVICES_PROTOCOL
*This
,
955 IN UINTN ProcessorNumber
,
956 IN BOOLEAN EnableOldBSP
960 // Current always return unsupported.
962 return EFI_UNSUPPORTED
;
966 This service lets the caller enable or disable an AP from this point onward.
967 This service may only be called from the BSP.
969 This service allows the caller enable or disable an AP from this point onward.
970 The caller can optionally specify the health status of the AP by Health. If
971 an AP is being disabled, then the state of the disabled AP is implementation
972 dependent. If an AP is enabled, then the implementation must guarantee that a
973 complete initialization sequence is performed on the AP, so the AP is in a state
974 that is compatible with an MP operating system. This service may not be supported
975 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
977 If the enable or disable AP operation cannot be completed prior to the return
978 from this service, then EFI_UNSUPPORTED must be returned.
980 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
981 @param[in] ProcessorNumber The handle number of AP that is to become the new
982 BSP. The range is from 0 to the total number of
983 logical processors minus 1. The total number of
984 logical processors can be retrieved by
985 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
986 @param[in] EnableAP Specifies the new state for the processor for
987 enabled, FALSE for disabled.
988 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
989 the new health status of the AP. This flag
990 corresponds to StatusFlag defined in
991 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
992 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
993 bits are ignored. If it is NULL, this parameter
996 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
997 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
998 prior to this service returning.
999 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
1000 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1001 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
1003 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
1009 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1010 IN UINTN ProcessorNumber
,
1011 IN BOOLEAN EnableAP
,
1012 IN UINT32
*HealthFlag OPTIONAL
1015 CPU_DATA_BLOCK
*CpuData
;
1018 return EFI_DEVICE_ERROR
;
1021 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
1022 return EFI_NOT_FOUND
;
1025 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1026 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1027 return EFI_INVALID_PARAMETER
;
1030 if (GetApState (CpuData
) != CpuStateIdle
) {
1031 return EFI_UNSUPPORTED
;
1035 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
1036 mMpSystemData
.NumberOfEnabledProcessors
++;
1038 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
1040 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1041 mMpSystemData
.NumberOfEnabledProcessors
--;
1043 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
1046 if (HealthFlag
!= NULL
) {
1047 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
1048 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
1055 This return the handle number for the calling processor. This service may be
1056 called from the BSP and APs.
1058 This service returns the processor handle number for the calling processor.
1059 The returned value is in the range from 0 to the total number of logical
1060 processors minus 1. The total number of logical processors can be retrieved
1061 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1062 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1063 is returned. Otherwise, the current processors handle number is returned in
1064 ProcessorNumber, and EFI_SUCCESS is returned.
1066 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1067 @param[out] ProcessorNumber The handle number of AP that is to become the new
1068 BSP. The range is from 0 to the total number of
1069 logical processors minus 1. The total number of
1070 logical processors can be retrieved by
1071 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1073 @retval EFI_SUCCESS The current processor handle number was returned
1075 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1081 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1082 OUT UINTN
*ProcessorNumber
1088 if (ProcessorNumber
== NULL
) {
1089 return EFI_INVALID_PARAMETER
;
1092 ProcessorId
= GetApicId ();
1093 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
1094 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
1099 *ProcessorNumber
= Index
;
1104 Terminate AP's task and set it to idle state.
1106 This function terminates AP's task due to timeout by sending INIT-SIPI,
1107 and sends it to idle state.
1109 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1113 ResetProcessorToIdleState (
1114 IN CPU_DATA_BLOCK
*CpuData
1117 ResetApStackless ((UINT32
)CpuData
->Info
.ProcessorId
);
1121 Application Processors do loop routine
1122 after switch to its own stack.
1124 @param Context1 A pointer to the context to pass into the function.
1125 @param Context2 A pointer to the context to pass into the function.
1129 ProcessorToIdleState (
1130 IN VOID
*Context1
, OPTIONAL
1131 IN VOID
*Context2 OPTIONAL
1134 UINTN ProcessorNumber
;
1135 CPU_DATA_BLOCK
*CpuData
;
1136 EFI_AP_PROCEDURE Procedure
;
1137 VOID
*ProcedureArgument
;
1139 AsmApDoneWithCommonStack ();
1141 while (!mAPsAlreadyInitFinished
) {
1145 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1146 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1149 // Avoid forcibly reset AP caused the AP got lock not release.
1151 if (CpuData
->LockSelf
== (INTN
) GetApicId ()) {
1152 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1156 // Avoid forcibly reset AP caused the AP State is not updated.
1158 GetMpSpinLock (CpuData
);
1159 CpuData
->State
= CpuStateIdle
;
1160 CpuData
->Procedure
= NULL
;
1161 ReleaseMpSpinLock (CpuData
);
1164 GetMpSpinLock (CpuData
);
1165 ProcedureArgument
= CpuData
->Parameter
;
1166 Procedure
= CpuData
->Procedure
;
1167 ReleaseMpSpinLock (CpuData
);
1169 if (Procedure
!= NULL
) {
1170 Procedure (ProcedureArgument
);
1172 GetMpSpinLock (CpuData
);
1173 CpuData
->Procedure
= NULL
;
1174 CpuData
->State
= CpuStateFinished
;
1175 ReleaseMpSpinLock (CpuData
);
1186 Checks AP' status periodically.
1188 This function is triggerred by timer perodically to check the
1189 state of AP forStartupThisAP() executed in non-blocking mode.
1191 @param Event Event triggered.
1192 @param Context Parameter passed with the event.
1202 CPU_DATA_BLOCK
*CpuData
;
1205 CpuData
= (CPU_DATA_BLOCK
*) Context
;
1206 if (CpuData
->TimeoutActive
) {
1207 CpuData
->Timeout
-= gPollInterval
;
1210 CpuState
= GetApState (CpuData
);
1212 if (CpuState
== CpuStateFinished
) {
1213 if (CpuData
->Finished
) {
1214 *CpuData
->Finished
= TRUE
;
1216 SetApState (CpuData
, CpuStateIdle
);
1220 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
1221 if (CpuState
!= CpuStateIdle
&&
1222 CpuData
->Finished
) {
1223 *CpuData
->Finished
= FALSE
;
1225 ResetProcessorToIdleState (CpuData
);
1232 CpuData
->TimeoutActive
= FALSE
;
1233 gBS
->SignalEvent (CpuData
->WaitEvent
);
1234 CpuData
->WaitEvent
= NULL
;
1238 Checks APs' status periodically.
1240 This function is triggerred by timer perodically to check the
1241 state of APs for StartupAllAPs() executed in non-blocking mode.
1243 @param Event Event triggered.
1244 @param Context Parameter passed with the event.
1254 CPU_DATA_BLOCK
*CpuData
;
1258 if (mMpSystemData
.TimeoutActive
) {
1259 mMpSystemData
.Timeout
-= gPollInterval
;
1262 if (mStopCheckAllAPsStatus
) {
1267 // avoid next timer enter.
1269 Status
= gBS
->SetTimer (
1270 mMpSystemData
.CheckAllAPsEvent
,
1274 ASSERT_EFI_ERROR (Status
);
1276 if (mMpSystemData
.WaitEvent
!= NULL
) {
1277 CheckAndUpdateAllAPsToIdleState ();
1281 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
1282 ResetAllFailedAPs();
1286 mMpSystemData
.FinishCount
= mMpSystemData
.StartCount
;
1289 if (mMpSystemData
.FinishCount
!= mMpSystemData
.StartCount
) {
1293 mMpSystemData
.TimeoutActive
= FALSE
;
1294 gBS
->SignalEvent (mMpSystemData
.WaitEvent
);
1295 mMpSystemData
.WaitEvent
= NULL
;
1296 mStopCheckAllAPsStatus
= TRUE
;
1302 // check each AP status for StartupThisAP
1304 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
1305 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
1306 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1313 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1315 // Skip Disabled processors
1320 if (CpuData
->WaitEvent
) {
1321 CheckThisAPStatus (NULL
, (VOID
*)CpuData
);
1326 Status
= gBS
->SetTimer (
1327 mMpSystemData
.CheckAllAPsEvent
,
1329 EFI_TIMER_PERIOD_MICROSECONDS (100)
1331 ASSERT_EFI_ERROR (Status
);
1335 Application Processor C code entry point.
1345 UINTN ProcessorNumber
;
1347 if (!mAPsAlreadyInitFinished
) {
1348 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
1349 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
1350 mApStackStart
= TopOfApStack
;
1353 // Store the Stack address, when reset the AP, We can found the original address.
1355 mMpSystemData
.CpuDatas
[mMpSystemData
.NumberOfProcessors
].TopOfStack
= TopOfApStack
;
1356 mMpSystemData
.NumberOfProcessors
++;
1357 mMpSystemData
.NumberOfEnabledProcessors
++;
1359 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1361 // Get the original stack address.
1363 TopOfApStack
= mMpSystemData
.CpuDatas
[ProcessorNumber
].TopOfStack
;
1367 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
1374 This function is called by all processors (both BSP and AP) once and collects MP related data.
1376 @param Bsp TRUE if the CPU is BSP
1377 @param ProcessorNumber The specific processor number
1379 @retval EFI_SUCCESS Data for the processor collected and filled in
1383 FillInProcessorInformation (
1385 IN UINTN ProcessorNumber
1388 CPU_DATA_BLOCK
*CpuData
;
1391 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1392 ProcessorId
= GetApicId ();
1393 CpuData
->Info
.ProcessorId
= ProcessorId
;
1394 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1396 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1398 CpuData
->Info
.Location
.Package
= ProcessorId
;
1399 CpuData
->Info
.Location
.Core
= 0;
1400 CpuData
->Info
.Location
.Thread
= 0;
1401 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
1403 CpuData
->Procedure
= NULL
;
1404 CpuData
->Parameter
= NULL
;
1405 InitializeSpinLock (&CpuData
->CpuDataLock
);
1406 CpuData
->LockSelf
= -1;
1412 Prepare the System Data.
1414 @retval EFI_SUCCESS the System Data finished initilization.
1424 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
1426 mMpSystemData
.NumberOfProcessors
= 1;
1427 mMpSystemData
.NumberOfEnabledProcessors
= 1;
1429 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
1430 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
1432 Status
= gBS
->CreateEvent (
1433 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1437 &mMpSystemData
.CheckAllAPsEvent
1439 ASSERT_EFI_ERROR (Status
);
1442 // Set timer to check all APs status.
1444 Status
= gBS
->SetTimer (
1445 mMpSystemData
.CheckAllAPsEvent
,
1447 EFI_TIMER_PERIOD_MICROSECONDS (100)
1449 ASSERT_EFI_ERROR (Status
);
1454 FillInProcessorInformation (TRUE
, 0);
1460 Initialize Multi-processor support.
1464 InitializeMpSupport (
1468 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1469 if (gMaxLogicalProcessorNumber
< 1) {
1470 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1474 if (gMaxLogicalProcessorNumber
== 1) {
1478 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
1479 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
1481 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1482 ASSERT (mApStackStart
!= NULL
);
1485 // the first buffer of stack size used for common stack, when the amount of AP
1486 // more than 1, we should never free the common stack which maybe used for AP reset.
1488 mCommonStack
= mApStackStart
;
1489 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
1490 mApStackStart
= mTopOfApCommonStack
;
1492 InitMpSystemData ();
1494 PrepareAPStartupCode ();
1496 if (mMpSystemData
.NumberOfProcessors
== 1) {
1497 FreeApStartupCode ();
1498 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1502 mMpSystemData
.CpuDatas
= ReallocatePool (
1503 sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
,
1504 sizeof (CPU_DATA_BLOCK
) * mMpSystemData
.NumberOfProcessors
,
1505 mMpSystemData
.CpuDatas
);
1507 mAPsAlreadyInitFinished
= TRUE
;
1509 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
1510 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
1511 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *