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 mStopCheckAllAPsStatus
= TRUE
;
30 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate
= {
31 GetNumberOfProcessors
,
43 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
48 IN CPU_DATA_BLOCK
*CpuData
51 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
57 Release Mp Service Lock.
59 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
64 IN CPU_DATA_BLOCK
*CpuData
67 ReleaseSpinLock (&CpuData
->CpuDataLock
);
71 Check whether caller processor is BSP.
73 @retval TRUE the caller is BSP
74 @retval FALSE the caller is AP
83 CPU_DATA_BLOCK
*CpuData
;
87 WhoAmI (&mMpServicesTemplate
, &CpuIndex
);
88 CpuData
= &mMpSystemData
.CpuDatas
[CpuIndex
];
90 return CpuData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
? TRUE
: FALSE
;
94 Get the Application Processors state.
96 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
98 @retval CPU_STATE the AP status
103 IN CPU_DATA_BLOCK
*CpuData
108 GetMpSpinLock (CpuData
);
109 State
= CpuData
->State
;
110 ReleaseMpSpinLock (CpuData
);
116 Set the Application Processors state.
118 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP
119 @param State The AP status
124 IN CPU_DATA_BLOCK
*CpuData
,
128 GetMpSpinLock (CpuData
);
129 CpuData
->State
= State
;
130 ReleaseMpSpinLock (CpuData
);
134 Set the Application Processor prepare to run a function specified
137 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
138 @param Procedure A pointer to the function to be run on enabled APs of the system
139 @param ProcedureArgument Pointer to the optional parameter of the assigned function
144 IN CPU_DATA_BLOCK
*CpuData
,
145 IN EFI_AP_PROCEDURE Procedure
,
146 IN VOID
*ProcedureArgument
149 GetMpSpinLock (CpuData
);
150 CpuData
->Parameter
= ProcedureArgument
;
151 CpuData
->Procedure
= Procedure
;
152 ReleaseMpSpinLock (CpuData
);
156 Check the Application Processors Status whether contains the Flags.
158 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
159 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
161 @retval TRUE the AP status includes the StatusFlag
162 @retval FALSE the AP status excludes the StatusFlag
167 IN CPU_DATA_BLOCK
*CpuData
,
173 GetMpSpinLock (CpuData
);
174 Ret
= CpuData
->Info
.StatusFlag
& Flags
;
175 ReleaseMpSpinLock (CpuData
);
181 Bitwise-Or of the Application Processors Status with the Flags.
183 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
184 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
189 IN CPU_DATA_BLOCK
*CpuData
,
193 GetMpSpinLock (CpuData
);
194 CpuData
->Info
.StatusFlag
|= Flags
;
195 ReleaseMpSpinLock (CpuData
);
199 Bitwise-AndNot of the Application Processors Status with the Flags.
201 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
202 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
206 CpuStatusFlagAndNot (
207 IN CPU_DATA_BLOCK
*CpuData
,
211 GetMpSpinLock (CpuData
);
212 CpuData
->Info
.StatusFlag
&= ~Flags
;
213 ReleaseMpSpinLock (CpuData
);
217 Searches for the next blocking AP.
219 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().
221 @param NextNumber Pointer to the processor number of the next blocking AP.
223 @retval EFI_SUCCESS The next blocking AP has been found.
224 @retval EFI_NOT_FOUND No blocking AP exists.
228 GetNextBlockedNumber (
229 OUT UINTN
*NextNumber
234 CPU_DATA_BLOCK
*CpuData
;
236 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
237 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
238 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
245 CpuState
= GetApState (CpuData
);
246 if (CpuState
== CpuStateBlocked
) {
247 *NextNumber
= Number
;
252 return EFI_NOT_FOUND
;
256 Check if the APs state are finished, and update them to idle state
261 CheckAndUpdateAllAPsToIdleState (
265 UINTN ProcessorNumber
;
267 CPU_DATA_BLOCK
*CpuData
;
271 for (ProcessorNumber
= 0; ProcessorNumber
< mMpSystemData
.NumberOfProcessors
; ProcessorNumber
++) {
272 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
273 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
280 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
282 // Skip Disabled processors
287 CpuState
= GetApState (CpuData
);
288 if (CpuState
== CpuStateFinished
) {
289 mMpSystemData
.FinishCount
++;
290 if (mMpSystemData
.SingleThread
) {
291 Status
= GetNextBlockedNumber (&NextNumber
);
292 if (!EFI_ERROR (Status
)) {
293 SetApState (&mMpSystemData
.CpuDatas
[NextNumber
], CpuStateReady
);
294 SetApProcedure (&mMpSystemData
.CpuDatas
[NextNumber
],
295 mMpSystemData
.Procedure
,
296 mMpSystemData
.ProcedureArgument
);
300 SetApState (CpuData
, CpuStateIdle
);
306 If the timeout expires before all APs returns from Procedure,
307 we should forcibly terminate the executing AP and fill FailedList back
316 CPU_DATA_BLOCK
*CpuData
;
320 if (mMpSystemData
.FailedList
!= NULL
) {
321 *mMpSystemData
.FailedList
= AllocatePool ((mMpSystemData
.StartCount
- mMpSystemData
.FinishCount
+ 1) * sizeof(UINTN
));
322 ASSERT (*mMpSystemData
.FailedList
!= NULL
);
325 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
326 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
327 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
334 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
336 // Skip Disabled processors
341 CpuState
= GetApState (CpuData
);
342 if (CpuState
!= CpuStateIdle
) {
343 if (mMpSystemData
.FailedList
!= NULL
) {
344 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
++] = Number
;
346 ResetProcessorToIdleState (CpuData
);
350 if (mMpSystemData
.FailedList
!= NULL
) {
351 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
] = END_OF_CPU_LIST
;
356 This service retrieves the number of logical processor in the platform
357 and the number of those logical processors that are enabled on this boot.
358 This service may only be called from the BSP.
360 This function is used to retrieve the following information:
361 - The number of logical processors that are present in the system.
362 - The number of enabled logical processors in the system at the instant
365 Because MP Service Protocol provides services to enable and disable processors
366 dynamically, the number of enabled logical processors may vary during the
367 course of a boot session.
369 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
370 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
371 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
372 is returned in NumberOfProcessors, the number of currently enabled processor
373 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
375 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
377 @param[out] NumberOfProcessors Pointer to the total number of logical
378 processors in the system, including the BSP
380 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
381 processors that exist in system, including
384 @retval EFI_SUCCESS The number of logical processors and enabled
385 logical processors was retrieved.
386 @retval EFI_DEVICE_ERROR The calling processor is an AP.
387 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
388 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
393 GetNumberOfProcessors (
394 IN EFI_MP_SERVICES_PROTOCOL
*This
,
395 OUT UINTN
*NumberOfProcessors
,
396 OUT UINTN
*NumberOfEnabledProcessors
399 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
400 return EFI_INVALID_PARAMETER
;
404 return EFI_DEVICE_ERROR
;
407 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
408 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
413 Gets detailed MP-related information on the requested processor at the
414 instant this call is made. This service may only be called from the BSP.
416 This service retrieves detailed MP-related information about any processor
417 on the platform. Note the following:
418 - The processor information may change during the course of a boot session.
419 - The information presented here is entirely MP related.
421 Information regarding the number of caches and their sizes, frequency of operation,
422 slot numbers is all considered platform-related information and is not provided
425 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
427 @param[in] ProcessorNumber The handle number of processor.
428 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
429 the requested processor is deposited.
431 @retval EFI_SUCCESS Processor information was returned.
432 @retval EFI_DEVICE_ERROR The calling processor is an AP.
433 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
434 @retval EFI_NOT_FOUND The processor with the handle specified by
435 ProcessorNumber does not exist in the platform.
441 IN EFI_MP_SERVICES_PROTOCOL
*This
,
442 IN UINTN ProcessorNumber
,
443 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
446 if (ProcessorInfoBuffer
== NULL
) {
447 return EFI_INVALID_PARAMETER
;
451 return EFI_DEVICE_ERROR
;
454 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
455 return EFI_NOT_FOUND
;
458 CopyMem (ProcessorInfoBuffer
, &mMpSystemData
.CpuDatas
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
463 This service executes a caller provided function on all enabled APs. APs can
464 run either simultaneously or one at a time in sequence. This service supports
465 both blocking and non-blocking requests. The non-blocking requests use EFI
466 events so the BSP can detect when the APs have finished. This service may only
467 be called from the BSP.
469 This function is used to dispatch all the enabled APs to the function specified
470 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
471 immediately and Procedure is not started on any AP.
473 If SingleThread is TRUE, all the enabled APs execute the function specified by
474 Procedure one by one, in ascending order of processor handle number. Otherwise,
475 all the enabled APs execute the function specified by Procedure simultaneously.
477 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
478 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
479 mode, and the BSP returns from this service without waiting for APs. If a
480 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
481 is signaled, then EFI_UNSUPPORTED must be returned.
483 If the timeout specified by TimeoutInMicroseconds expires before all APs return
484 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
485 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
486 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
487 content points to the list of processor handle numbers in which Procedure was
490 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
491 to make sure that the nature of the code that is executed on the BSP and the
492 dispatched APs is well controlled. The MP Services Protocol does not guarantee
493 that the Procedure function is MP-safe. Hence, the tasks that can be run in
494 parallel are limited to certain independent tasks and well-controlled exclusive
495 code. EFI services and protocols may not be called by APs unless otherwise
498 In blocking execution mode, BSP waits until all APs finish or
499 TimeoutInMicroseconds expires.
501 In non-blocking execution mode, BSP is freed to return to the caller and then
502 proceed to the next task without having to wait for APs. The following
503 sequence needs to occur in a non-blocking execution mode:
505 -# The caller that intends to use this MP Services Protocol in non-blocking
506 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
507 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
508 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
509 the function specified by Procedure to be started on all the enabled APs,
510 and releases the BSP to continue with other tasks.
511 -# The caller can use the CheckEvent() and WaitForEvent() services to check
512 the state of the WaitEvent created in step 1.
513 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
514 Service signals WaitEvent by calling the EFI SignalEvent() function. If
515 FailedCpuList is not NULL, its content is available when WaitEvent is
516 signaled. If all APs returned from Procedure prior to the timeout, then
517 FailedCpuList is set to NULL. If not all APs return from Procedure before
518 the timeout, then FailedCpuList is filled in with the list of the failed
519 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
520 It is the caller's responsibility to free the buffer with FreePool() service.
521 -# This invocation of SignalEvent() function informs the caller that invoked
522 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
523 the specified task or a timeout occurred. The contents of FailedCpuList
524 can be examined to determine which APs did not complete the specified task
525 prior to the timeout.
527 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
529 @param[in] Procedure A pointer to the function to be run on
530 enabled APs of the system. See type
532 @param[in] SingleThread If TRUE, then all the enabled APs execute
533 the function specified by Procedure one by
534 one, in ascending order of processor handle
535 number. If FALSE, then all the enabled APs
536 execute the function specified by Procedure
538 @param[in] WaitEvent The event created by the caller with CreateEvent()
539 service. If it is NULL, then execute in
540 blocking mode. BSP waits until all APs finish
541 or TimeoutInMicroseconds expires. If it's
542 not NULL, then execute in non-blocking mode.
543 BSP requests the function specified by
544 Procedure to be started on all the enabled
545 APs, and go on executing immediately. If
546 all return from Procedure, or TimeoutInMicroseconds
547 expires, this event is signaled. The BSP
548 can use the CheckEvent() or WaitForEvent()
549 services to check the state of event. Type
550 EFI_EVENT is defined in CreateEvent() in
551 the Unified Extensible Firmware Interface
553 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
554 APs to return from Procedure, either for
555 blocking or non-blocking mode. Zero means
556 infinity. If the timeout expires before
557 all APs return from Procedure, then Procedure
558 on the failed APs is terminated. All enabled
559 APs are available for next function assigned
560 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
561 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
562 If the timeout expires in blocking mode,
563 BSP returns EFI_TIMEOUT. If the timeout
564 expires in non-blocking mode, WaitEvent
565 is signaled with SignalEvent().
566 @param[in] ProcedureArgument The parameter passed into Procedure for
568 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
569 if all APs finish successfully, then its
570 content is set to NULL. If not all APs
571 finish before timeout expires, then its
572 content is set to address of the buffer
573 holding handle numbers of the failed APs.
574 The buffer is allocated by MP Service Protocol,
575 and it's the caller's responsibility to
576 free the buffer with FreePool() service.
577 In blocking mode, it is ready for consumption
578 when the call returns. In non-blocking mode,
579 it is ready when WaitEvent is signaled. The
580 list of failed CPU is terminated by
583 @retval EFI_SUCCESS In blocking mode, all APs have finished before
585 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
587 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
588 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
590 @retval EFI_DEVICE_ERROR Caller processor is AP.
591 @retval EFI_NOT_STARTED No enabled APs exist in the system.
592 @retval EFI_NOT_READY Any enabled APs are busy.
593 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
594 all enabled APs have finished.
595 @retval EFI_INVALID_PARAMETER Procedure is NULL.
601 IN EFI_MP_SERVICES_PROTOCOL
*This
,
602 IN EFI_AP_PROCEDURE Procedure
,
603 IN BOOLEAN SingleThread
,
604 IN EFI_EVENT WaitEvent OPTIONAL
,
605 IN UINTN TimeoutInMicroseconds
,
606 IN VOID
*ProcedureArgument OPTIONAL
,
607 OUT UINTN
**FailedCpuList OPTIONAL
611 CPU_DATA_BLOCK
*CpuData
;
613 CPU_STATE APInitialState
;
617 if (FailedCpuList
!= NULL
) {
618 *FailedCpuList
= NULL
;
622 return EFI_DEVICE_ERROR
;
625 if (mMpSystemData
.NumberOfProcessors
== 1) {
626 return EFI_NOT_STARTED
;
629 if (Procedure
== NULL
) {
630 return EFI_INVALID_PARAMETER
;
633 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
634 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
635 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
642 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
644 // Skip Disabled processors
649 if (GetApState (CpuData
) != CpuStateIdle
) {
650 return EFI_NOT_READY
;
655 // temporarily stop checkAllAPsStatus for initialize parameters.
657 mStopCheckAllAPsStatus
= TRUE
;
659 mMpSystemData
.Procedure
= Procedure
;
660 mMpSystemData
.ProcedureArgument
= ProcedureArgument
;
661 mMpSystemData
.WaitEvent
= WaitEvent
;
662 mMpSystemData
.Timeout
= TimeoutInMicroseconds
;
663 mMpSystemData
.TimeoutActive
= !!(TimeoutInMicroseconds
);
664 mMpSystemData
.FinishCount
= 0;
665 mMpSystemData
.StartCount
= 0;
666 mMpSystemData
.SingleThread
= SingleThread
;
667 mMpSystemData
.FailedList
= FailedCpuList
;
668 mMpSystemData
.FailedListIndex
= 0;
669 APInitialState
= CpuStateReady
;
671 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
672 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
673 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
680 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
682 // Skip Disabled processors
688 // Get APs prepared, and put failing APs into FailedCpuList
689 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
690 // state 1 by 1, until the previous 1 finished its task
691 // if not "SingleThread", all APs are put to ready state from the beginning
693 if (GetApState (CpuData
) == CpuStateIdle
) {
694 mMpSystemData
.StartCount
++;
696 SetApState (CpuData
, APInitialState
);
698 if (APInitialState
== CpuStateReady
) {
699 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
703 APInitialState
= CpuStateBlocked
;
708 mStopCheckAllAPsStatus
= FALSE
;
710 if (WaitEvent
!= NULL
) {
718 CheckAndUpdateAllAPsToIdleState ();
719 if (mMpSystemData
.FinishCount
== mMpSystemData
.StartCount
) {
720 Status
= EFI_SUCCESS
;
727 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
729 Status
= EFI_TIMEOUT
;
733 gBS
->Stall (gPollInterval
);
734 mMpSystemData
.Timeout
-= gPollInterval
;
743 This service lets the caller get one enabled AP to execute a caller-provided
744 function. The caller can request the BSP to either wait for the completion
745 of the AP or just proceed with the next task by using the EFI event mechanism.
746 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
747 execution support. This service may only be called from the BSP.
749 This function is used to dispatch one enabled AP to the function specified by
750 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
751 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
752 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
753 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
754 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
755 then EFI_UNSUPPORTED must be returned.
757 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
758 from Procedure, then execution of Procedure by the AP is terminated. The AP is
759 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
760 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
762 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
764 @param[in] Procedure A pointer to the function to be run on
765 enabled APs of the system. See type
767 @param[in] ProcessorNumber The handle number of the AP. The range is
768 from 0 to the total number of logical
769 processors minus 1. The total number of
770 logical processors can be retrieved by
771 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
772 @param[in] WaitEvent The event created by the caller with CreateEvent()
773 service. If it is NULL, then execute in
774 blocking mode. BSP waits until all APs finish
775 or TimeoutInMicroseconds expires. If it's
776 not NULL, then execute in non-blocking mode.
777 BSP requests the function specified by
778 Procedure to be started on all the enabled
779 APs, and go on executing immediately. If
780 all return from Procedure or TimeoutInMicroseconds
781 expires, this event is signaled. The BSP
782 can use the CheckEvent() or WaitForEvent()
783 services to check the state of event. Type
784 EFI_EVENT is defined in CreateEvent() in
785 the Unified Extensible Firmware Interface
787 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
788 APs to return from Procedure, either for
789 blocking or non-blocking mode. Zero means
790 infinity. If the timeout expires before
791 all APs return from Procedure, then Procedure
792 on the failed APs is terminated. All enabled
793 APs are available for next function assigned
794 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
795 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
796 If the timeout expires in blocking mode,
797 BSP returns EFI_TIMEOUT. If the timeout
798 expires in non-blocking mode, WaitEvent
799 is signaled with SignalEvent().
800 @param[in] ProcedureArgument The parameter passed into Procedure for
802 @param[out] Finished If NULL, this parameter is ignored. In
803 blocking mode, this parameter is ignored.
804 In non-blocking mode, if AP returns from
805 Procedure before the timeout expires, its
806 content is set to TRUE. Otherwise, the
807 value is set to FALSE. The caller can
808 determine if the AP returned from Procedure
809 by evaluating this value.
811 @retval EFI_SUCCESS In blocking mode, specified AP finished before
813 @retval EFI_SUCCESS In non-blocking mode, the function has been
814 dispatched to specified AP.
815 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
816 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
818 @retval EFI_DEVICE_ERROR The calling processor is an AP.
819 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
820 the specified AP has finished.
821 @retval EFI_NOT_READY The specified AP is busy.
822 @retval EFI_NOT_FOUND The processor with the handle specified by
823 ProcessorNumber does not exist.
824 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
825 @retval EFI_INVALID_PARAMETER Procedure is NULL.
831 IN EFI_MP_SERVICES_PROTOCOL
*This
,
832 IN EFI_AP_PROCEDURE Procedure
,
833 IN UINTN ProcessorNumber
,
834 IN EFI_EVENT WaitEvent OPTIONAL
,
835 IN UINTN TimeoutInMicroseconds
,
836 IN VOID
*ProcedureArgument OPTIONAL
,
837 OUT BOOLEAN
*Finished OPTIONAL
840 CPU_DATA_BLOCK
*CpuData
;
844 if (Finished
!= NULL
) {
849 return EFI_DEVICE_ERROR
;
852 if (Procedure
== NULL
) {
853 return EFI_INVALID_PARAMETER
;
856 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
857 return EFI_NOT_FOUND
;
860 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
861 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
) ||
862 !TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
863 return EFI_INVALID_PARAMETER
;
866 if (GetApState (CpuData
) != CpuStateIdle
) {
867 return EFI_NOT_READY
;
871 // temporarily stop checkAllAPsStatus for initialize parameters.
873 mStopCheckAllAPsStatus
= TRUE
;
875 SetApState (CpuData
, CpuStateReady
);
877 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
879 CpuData
->Timeout
= TimeoutInMicroseconds
;
880 CpuData
->WaitEvent
= WaitEvent
;
881 CpuData
->TimeoutActive
= !!(TimeoutInMicroseconds
);
882 CpuData
->Finished
= Finished
;
884 mStopCheckAllAPsStatus
= FALSE
;
886 if (WaitEvent
!= NULL
) {
897 if (GetApState (CpuData
) == CpuStateFinished
) {
898 SetApState (CpuData
, CpuStateIdle
);
902 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
903 ResetProcessorToIdleState (CpuData
);
907 gBS
->Stall (gPollInterval
);
908 CpuData
->Timeout
-= gPollInterval
;
915 This service switches the requested AP to be the BSP from that point onward.
916 This service changes the BSP for all purposes. This call can only be performed
919 This service switches the requested AP to be the BSP from that point onward.
920 This service changes the BSP for all purposes. The new BSP can take over the
921 execution of the old BSP and continue seamlessly from where the old one left
922 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
925 If the BSP cannot be switched prior to the return from this service, then
926 EFI_UNSUPPORTED must be returned.
928 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
929 @param[in] ProcessorNumber The handle number of AP that is to become the new
930 BSP. The range is from 0 to the total number of
931 logical processors minus 1. The total number of
932 logical processors can be retrieved by
933 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
934 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
935 enabled AP. Otherwise, it will be disabled.
937 @retval EFI_SUCCESS BSP successfully switched.
938 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
939 this service returning.
940 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
941 @retval EFI_SUCCESS The calling processor is an AP.
942 @retval EFI_NOT_FOUND The processor with the handle specified by
943 ProcessorNumber does not exist.
944 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
946 @retval EFI_NOT_READY The specified AP is busy.
952 IN EFI_MP_SERVICES_PROTOCOL
*This
,
953 IN UINTN ProcessorNumber
,
954 IN BOOLEAN EnableOldBSP
958 // Current always return unsupported.
960 return EFI_UNSUPPORTED
;
964 This service lets the caller enable or disable an AP from this point onward.
965 This service may only be called from the BSP.
967 This service allows the caller enable or disable an AP from this point onward.
968 The caller can optionally specify the health status of the AP by Health. If
969 an AP is being disabled, then the state of the disabled AP is implementation
970 dependent. If an AP is enabled, then the implementation must guarantee that a
971 complete initialization sequence is performed on the AP, so the AP is in a state
972 that is compatible with an MP operating system. This service may not be supported
973 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
975 If the enable or disable AP operation cannot be completed prior to the return
976 from this service, then EFI_UNSUPPORTED must be returned.
978 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
979 @param[in] ProcessorNumber The handle number of AP that is to become the new
980 BSP. The range is from 0 to the total number of
981 logical processors minus 1. The total number of
982 logical processors can be retrieved by
983 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
984 @param[in] EnableAP Specifies the new state for the processor for
985 enabled, FALSE for disabled.
986 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
987 the new health status of the AP. This flag
988 corresponds to StatusFlag defined in
989 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
990 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
991 bits are ignored. If it is NULL, this parameter
994 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
995 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
996 prior to this service returning.
997 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
998 @retval EFI_DEVICE_ERROR The calling processor is an AP.
999 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
1001 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
1007 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1008 IN UINTN ProcessorNumber
,
1009 IN BOOLEAN EnableAP
,
1010 IN UINT32
*HealthFlag OPTIONAL
1013 CPU_DATA_BLOCK
*CpuData
;
1016 return EFI_DEVICE_ERROR
;
1019 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
1020 return EFI_NOT_FOUND
;
1023 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1024 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1025 return EFI_INVALID_PARAMETER
;
1028 if (GetApState (CpuData
) != CpuStateIdle
) {
1029 return EFI_UNSUPPORTED
;
1033 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
1034 mMpSystemData
.NumberOfEnabledProcessors
++;
1036 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
1038 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1039 mMpSystemData
.NumberOfEnabledProcessors
--;
1041 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
1044 if (HealthFlag
!= NULL
) {
1045 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
1046 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
1053 This return the handle number for the calling processor. This service may be
1054 called from the BSP and APs.
1056 This service returns the processor handle number for the calling processor.
1057 The returned value is in the range from 0 to the total number of logical
1058 processors minus 1. The total number of logical processors can be retrieved
1059 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1060 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1061 is returned. Otherwise, the current processors handle number is returned in
1062 ProcessorNumber, and EFI_SUCCESS is returned.
1064 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1065 @param[out] ProcessorNumber The handle number of AP that is to become the new
1066 BSP. The range is from 0 to the total number of
1067 logical processors minus 1. The total number of
1068 logical processors can be retrieved by
1069 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1071 @retval EFI_SUCCESS The current processor handle number was returned
1073 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1079 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1080 OUT UINTN
*ProcessorNumber
1086 if (ProcessorNumber
== NULL
) {
1087 return EFI_INVALID_PARAMETER
;
1090 ProcessorId
= GetApicId ();
1091 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
1092 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
1097 *ProcessorNumber
= Index
;
1102 Terminate AP's task and set it to idle state.
1104 This function terminates AP's task due to timeout by sending INIT-SIPI,
1105 and sends it to idle state.
1107 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1111 ResetProcessorToIdleState (
1112 IN CPU_DATA_BLOCK
*CpuData
1118 Application Processors do loop routine
1119 after switch to its own stack.
1121 @param Context1 A pointer to the context to pass into the function.
1122 @param Context2 A pointer to the context to pass into the function.
1126 ProcessorToIdleState (
1127 IN VOID
*Context1
, OPTIONAL
1128 IN VOID
*Context2 OPTIONAL
1131 UINTN ProcessorNumber
;
1132 CPU_DATA_BLOCK
*CpuData
;
1133 EFI_AP_PROCEDURE Procedure
;
1134 VOID
*ProcedureArgument
;
1136 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1137 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1139 AsmApDoneWithCommonStack ();
1142 GetMpSpinLock (CpuData
);
1143 ProcedureArgument
= CpuData
->Parameter
;
1144 Procedure
= CpuData
->Procedure
;
1145 ReleaseMpSpinLock (CpuData
);
1147 if (Procedure
!= NULL
) {
1148 Procedure (ProcedureArgument
);
1150 GetMpSpinLock (CpuData
);
1151 CpuData
->Procedure
= NULL
;
1152 CpuData
->State
= CpuStateFinished
;
1153 ReleaseMpSpinLock (CpuData
);
1164 Checks AP' status periodically.
1166 This function is triggerred by timer perodically to check the
1167 state of AP forStartupThisAP() executed in non-blocking mode.
1169 @param Event Event triggered.
1170 @param Context Parameter passed with the event.
1180 CPU_DATA_BLOCK
*CpuData
;
1183 CpuData
= (CPU_DATA_BLOCK
*) Context
;
1184 if (CpuData
->TimeoutActive
) {
1185 CpuData
->Timeout
-= gPollInterval
;
1188 CpuState
= GetApState (CpuData
);
1190 if (CpuState
== CpuStateFinished
) {
1191 if (CpuData
->Finished
) {
1192 *CpuData
->Finished
= TRUE
;
1194 SetApState (CpuData
, CpuStateIdle
);
1198 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
1199 if (CpuState
!= CpuStateIdle
&&
1200 CpuData
->Finished
) {
1201 *CpuData
->Finished
= FALSE
;
1203 ResetProcessorToIdleState (CpuData
);
1210 CpuData
->TimeoutActive
= FALSE
;
1211 gBS
->SignalEvent (CpuData
->WaitEvent
);
1212 CpuData
->WaitEvent
= NULL
;
1216 Checks APs' status periodically.
1218 This function is triggerred by timer perodically to check the
1219 state of APs for StartupAllAPs() executed in non-blocking mode.
1221 @param Event Event triggered.
1222 @param Context Parameter passed with the event.
1232 CPU_DATA_BLOCK
*CpuData
;
1236 if (mMpSystemData
.TimeoutActive
) {
1237 mMpSystemData
.Timeout
-= gPollInterval
;
1240 if (mStopCheckAllAPsStatus
) {
1245 // avoid next timer enter.
1247 Status
= gBS
->SetTimer (
1248 mMpSystemData
.CheckAllAPsEvent
,
1252 ASSERT_EFI_ERROR (Status
);
1254 if (mMpSystemData
.WaitEvent
!= NULL
) {
1255 CheckAndUpdateAllAPsToIdleState ();
1259 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
1260 ResetAllFailedAPs();
1264 mMpSystemData
.FinishCount
= mMpSystemData
.StartCount
;
1267 if (mMpSystemData
.FinishCount
!= mMpSystemData
.StartCount
) {
1271 mMpSystemData
.TimeoutActive
= FALSE
;
1272 gBS
->SignalEvent (mMpSystemData
.WaitEvent
);
1273 mMpSystemData
.WaitEvent
= NULL
;
1274 mStopCheckAllAPsStatus
= TRUE
;
1280 // check each AP status for StartupThisAP
1282 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
1283 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
1284 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1291 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1293 // Skip Disabled processors
1298 if (CpuData
->WaitEvent
) {
1299 CheckThisAPStatus (NULL
, (VOID
*)CpuData
);
1304 Status
= gBS
->SetTimer (
1305 mMpSystemData
.CheckAllAPsEvent
,
1307 EFI_TIMER_PERIOD_MICROSECONDS (100)
1309 ASSERT_EFI_ERROR (Status
);
1313 Application Processor C code entry point.
1324 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
1325 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
1326 mApStackStart
= TopOfApStack
;
1328 mMpSystemData
.NumberOfProcessors
++;
1331 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
1338 This function is called by all processors (both BSP and AP) once and collects MP related data.
1340 @param Bsp TRUE if the CPU is BSP
1341 @param ProcessorNumber The specific processor number
1343 @retval EFI_SUCCESS Data for the processor collected and filled in
1347 FillInProcessorInformation (
1349 IN UINTN ProcessorNumber
1352 CPU_DATA_BLOCK
*CpuData
;
1355 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1356 ProcessorId
= GetApicId ();
1357 CpuData
->Info
.ProcessorId
= ProcessorId
;
1358 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1360 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1362 CpuData
->Info
.Location
.Package
= ProcessorId
;
1363 CpuData
->Info
.Location
.Core
= 0;
1364 CpuData
->Info
.Location
.Thread
= 0;
1365 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
1367 CpuData
->Procedure
= NULL
;
1368 CpuData
->Parameter
= NULL
;
1369 InitializeSpinLock (&CpuData
->CpuDataLock
);
1375 Prepare the System Data.
1377 @retval EFI_SUCCESS the System Data finished initilization.
1387 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
1389 mMpSystemData
.NumberOfProcessors
= 1;
1390 mMpSystemData
.NumberOfEnabledProcessors
= 1;
1392 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
1393 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
1395 Status
= gBS
->CreateEvent (
1396 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1400 &mMpSystemData
.CheckAllAPsEvent
1402 ASSERT_EFI_ERROR (Status
);
1405 // Set timer to check all APs status.
1407 Status
= gBS
->SetTimer (
1408 mMpSystemData
.CheckAllAPsEvent
,
1410 EFI_TIMER_PERIOD_MICROSECONDS (100)
1412 ASSERT_EFI_ERROR (Status
);
1417 FillInProcessorInformation (TRUE
, 0);
1423 Initialize Multi-processor support.
1427 InitializeMpSupport (
1431 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1432 if (gMaxLogicalProcessorNumber
< 1) {
1433 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1437 if (gMaxLogicalProcessorNumber
== 1) {
1441 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
1442 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
1444 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1445 ASSERT (mApStackStart
!= NULL
);
1448 // the first buffer of stack size used for common stack, when the amount of AP
1449 // more than 1, we should never free the common stack which maybe used for AP reset.
1451 mCommonStack
= mApStackStart
;
1452 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
1453 mApStackStart
= mTopOfApCommonStack
;
1455 InitMpSystemData ();
1457 if (mMpSystemData
.NumberOfProcessors
== 1) {
1458 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1462 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
1463 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
1464 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *