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
;
23 EFI_HANDLE mMpServiceHandle
= NULL
;
25 VOID
*mCommonStack
= 0;
26 VOID
*mTopOfApCommonStack
= 0;
27 VOID
*mApStackStart
= 0;
29 volatile BOOLEAN mAPsAlreadyInitFinished
= FALSE
;
30 volatile BOOLEAN mStopCheckAllAPsStatus
= TRUE
;
32 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate
= {
33 GetNumberOfProcessors
,
45 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
50 IN CPU_DATA_BLOCK
*CpuData
53 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
56 CpuData
->LockSelf
= GetApicId ();
60 Release Mp Service Lock.
62 @param CpuData the pointer to CPU_DATA_BLOCK of specified processor
67 IN CPU_DATA_BLOCK
*CpuData
70 ReleaseSpinLock (&CpuData
->CpuDataLock
);
74 Check whether caller processor is BSP.
76 @retval TRUE the caller is BSP
77 @retval FALSE the caller is AP
86 CPU_DATA_BLOCK
*CpuData
;
90 WhoAmI (&mMpServicesTemplate
, &CpuIndex
);
91 CpuData
= &mMpSystemData
.CpuDatas
[CpuIndex
];
93 return CpuData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
? TRUE
: FALSE
;
97 Get the Application Processors state.
99 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
101 @retval CPU_STATE the AP status
106 IN CPU_DATA_BLOCK
*CpuData
111 GetMpSpinLock (CpuData
);
112 State
= CpuData
->State
;
113 ReleaseMpSpinLock (CpuData
);
119 Set the Application Processors state.
121 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP
122 @param State The AP status
127 IN CPU_DATA_BLOCK
*CpuData
,
131 GetMpSpinLock (CpuData
);
132 CpuData
->State
= State
;
133 ReleaseMpSpinLock (CpuData
);
137 Set the Application Processor prepare to run a function specified
140 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
141 @param Procedure A pointer to the function to be run on enabled APs of the system
142 @param ProcedureArgument Pointer to the optional parameter of the assigned function
147 IN CPU_DATA_BLOCK
*CpuData
,
148 IN EFI_AP_PROCEDURE Procedure
,
149 IN VOID
*ProcedureArgument
152 GetMpSpinLock (CpuData
);
153 CpuData
->Parameter
= ProcedureArgument
;
154 CpuData
->Procedure
= Procedure
;
155 ReleaseMpSpinLock (CpuData
);
159 Check the Application Processors Status whether contains the Flags.
161 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
162 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
164 @retval TRUE the AP status includes the StatusFlag
165 @retval FALSE the AP status excludes the StatusFlag
170 IN CPU_DATA_BLOCK
*CpuData
,
176 GetMpSpinLock (CpuData
);
177 Ret
= CpuData
->Info
.StatusFlag
& Flags
;
178 ReleaseMpSpinLock (CpuData
);
184 Bitwise-Or of the Application Processors Status with the Flags.
186 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
187 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
192 IN CPU_DATA_BLOCK
*CpuData
,
196 GetMpSpinLock (CpuData
);
197 CpuData
->Info
.StatusFlag
|= Flags
;
198 ReleaseMpSpinLock (CpuData
);
202 Bitwise-AndNot of the Application Processors Status with the Flags.
204 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
205 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
209 CpuStatusFlagAndNot (
210 IN CPU_DATA_BLOCK
*CpuData
,
214 GetMpSpinLock (CpuData
);
215 CpuData
->Info
.StatusFlag
&= ~Flags
;
216 ReleaseMpSpinLock (CpuData
);
220 Searches for the next blocking AP.
222 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().
224 @param NextNumber Pointer to the processor number of the next blocking AP.
226 @retval EFI_SUCCESS The next blocking AP has been found.
227 @retval EFI_NOT_FOUND No blocking AP exists.
231 GetNextBlockedNumber (
232 OUT UINTN
*NextNumber
237 CPU_DATA_BLOCK
*CpuData
;
239 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
240 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
241 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
248 CpuState
= GetApState (CpuData
);
249 if (CpuState
== CpuStateBlocked
) {
250 *NextNumber
= Number
;
255 return EFI_NOT_FOUND
;
259 Check if the APs state are finished, and update them to idle state
264 CheckAndUpdateAllAPsToIdleState (
268 UINTN ProcessorNumber
;
270 CPU_DATA_BLOCK
*CpuData
;
274 for (ProcessorNumber
= 0; ProcessorNumber
< mMpSystemData
.NumberOfProcessors
; ProcessorNumber
++) {
275 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
276 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
283 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
285 // Skip Disabled processors
290 CpuState
= GetApState (CpuData
);
291 if (CpuState
== CpuStateFinished
) {
292 mMpSystemData
.FinishCount
++;
293 if (mMpSystemData
.SingleThread
) {
294 Status
= GetNextBlockedNumber (&NextNumber
);
295 if (!EFI_ERROR (Status
)) {
296 SetApState (&mMpSystemData
.CpuDatas
[NextNumber
], CpuStateReady
);
297 SetApProcedure (&mMpSystemData
.CpuDatas
[NextNumber
],
298 mMpSystemData
.Procedure
,
299 mMpSystemData
.ProcedureArgument
);
303 SetApState (CpuData
, CpuStateIdle
);
309 If the timeout expires before all APs returns from Procedure,
310 we should forcibly terminate the executing AP and fill FailedList back
319 CPU_DATA_BLOCK
*CpuData
;
323 if (mMpSystemData
.FailedList
!= NULL
) {
324 *mMpSystemData
.FailedList
= AllocatePool ((mMpSystemData
.StartCount
- mMpSystemData
.FinishCount
+ 1) * sizeof(UINTN
));
325 ASSERT (*mMpSystemData
.FailedList
!= NULL
);
328 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
329 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
330 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
337 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
339 // Skip Disabled processors
344 CpuState
= GetApState (CpuData
);
345 if (CpuState
!= CpuStateIdle
) {
346 if (mMpSystemData
.FailedList
!= NULL
) {
347 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
++] = Number
;
349 ResetProcessorToIdleState (CpuData
);
353 if (mMpSystemData
.FailedList
!= NULL
) {
354 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
] = END_OF_CPU_LIST
;
359 This service retrieves the number of logical processor in the platform
360 and the number of those logical processors that are enabled on this boot.
361 This service may only be called from the BSP.
363 This function is used to retrieve the following information:
364 - The number of logical processors that are present in the system.
365 - The number of enabled logical processors in the system at the instant
368 Because MP Service Protocol provides services to enable and disable processors
369 dynamically, the number of enabled logical processors may vary during the
370 course of a boot session.
372 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
373 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
374 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
375 is returned in NumberOfProcessors, the number of currently enabled processor
376 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
378 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
380 @param[out] NumberOfProcessors Pointer to the total number of logical
381 processors in the system, including the BSP
383 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
384 processors that exist in system, including
387 @retval EFI_SUCCESS The number of logical processors and enabled
388 logical processors was retrieved.
389 @retval EFI_DEVICE_ERROR The calling processor is an AP.
390 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
391 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
396 GetNumberOfProcessors (
397 IN EFI_MP_SERVICES_PROTOCOL
*This
,
398 OUT UINTN
*NumberOfProcessors
,
399 OUT UINTN
*NumberOfEnabledProcessors
402 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
403 return EFI_INVALID_PARAMETER
;
407 return EFI_DEVICE_ERROR
;
410 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
411 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
416 Gets detailed MP-related information on the requested processor at the
417 instant this call is made. This service may only be called from the BSP.
419 This service retrieves detailed MP-related information about any processor
420 on the platform. Note the following:
421 - The processor information may change during the course of a boot session.
422 - The information presented here is entirely MP related.
424 Information regarding the number of caches and their sizes, frequency of operation,
425 slot numbers is all considered platform-related information and is not provided
428 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
430 @param[in] ProcessorNumber The handle number of processor.
431 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
432 the requested processor is deposited.
434 @retval EFI_SUCCESS Processor information was returned.
435 @retval EFI_DEVICE_ERROR The calling processor is an AP.
436 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
437 @retval EFI_NOT_FOUND The processor with the handle specified by
438 ProcessorNumber does not exist in the platform.
444 IN EFI_MP_SERVICES_PROTOCOL
*This
,
445 IN UINTN ProcessorNumber
,
446 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
449 if (ProcessorInfoBuffer
== NULL
) {
450 return EFI_INVALID_PARAMETER
;
454 return EFI_DEVICE_ERROR
;
457 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
458 return EFI_NOT_FOUND
;
461 CopyMem (ProcessorInfoBuffer
, &mMpSystemData
.CpuDatas
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
466 This service executes a caller provided function on all enabled APs. APs can
467 run either simultaneously or one at a time in sequence. This service supports
468 both blocking and non-blocking requests. The non-blocking requests use EFI
469 events so the BSP can detect when the APs have finished. This service may only
470 be called from the BSP.
472 This function is used to dispatch all the enabled APs to the function specified
473 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
474 immediately and Procedure is not started on any AP.
476 If SingleThread is TRUE, all the enabled APs execute the function specified by
477 Procedure one by one, in ascending order of processor handle number. Otherwise,
478 all the enabled APs execute the function specified by Procedure simultaneously.
480 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
481 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
482 mode, and the BSP returns from this service without waiting for APs. If a
483 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
484 is signaled, then EFI_UNSUPPORTED must be returned.
486 If the timeout specified by TimeoutInMicroseconds expires before all APs return
487 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
488 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
489 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
490 content points to the list of processor handle numbers in which Procedure was
493 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
494 to make sure that the nature of the code that is executed on the BSP and the
495 dispatched APs is well controlled. The MP Services Protocol does not guarantee
496 that the Procedure function is MP-safe. Hence, the tasks that can be run in
497 parallel are limited to certain independent tasks and well-controlled exclusive
498 code. EFI services and protocols may not be called by APs unless otherwise
501 In blocking execution mode, BSP waits until all APs finish or
502 TimeoutInMicroseconds expires.
504 In non-blocking execution mode, BSP is freed to return to the caller and then
505 proceed to the next task without having to wait for APs. The following
506 sequence needs to occur in a non-blocking execution mode:
508 -# The caller that intends to use this MP Services Protocol in non-blocking
509 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
510 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
511 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
512 the function specified by Procedure to be started on all the enabled APs,
513 and releases the BSP to continue with other tasks.
514 -# The caller can use the CheckEvent() and WaitForEvent() services to check
515 the state of the WaitEvent created in step 1.
516 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
517 Service signals WaitEvent by calling the EFI SignalEvent() function. If
518 FailedCpuList is not NULL, its content is available when WaitEvent is
519 signaled. If all APs returned from Procedure prior to the timeout, then
520 FailedCpuList is set to NULL. If not all APs return from Procedure before
521 the timeout, then FailedCpuList is filled in with the list of the failed
522 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
523 It is the caller's responsibility to free the buffer with FreePool() service.
524 -# This invocation of SignalEvent() function informs the caller that invoked
525 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
526 the specified task or a timeout occurred. The contents of FailedCpuList
527 can be examined to determine which APs did not complete the specified task
528 prior to the timeout.
530 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
532 @param[in] Procedure A pointer to the function to be run on
533 enabled APs of the system. See type
535 @param[in] SingleThread If TRUE, then all the enabled APs execute
536 the function specified by Procedure one by
537 one, in ascending order of processor handle
538 number. If FALSE, then all the enabled APs
539 execute the function specified by Procedure
541 @param[in] WaitEvent The event created by the caller with CreateEvent()
542 service. If it is NULL, then execute in
543 blocking mode. BSP waits until all APs finish
544 or TimeoutInMicroseconds expires. If it's
545 not NULL, then execute in non-blocking mode.
546 BSP requests the function specified by
547 Procedure to be started on all the enabled
548 APs, and go on executing immediately. If
549 all return from Procedure, or TimeoutInMicroseconds
550 expires, this event is signaled. The BSP
551 can use the CheckEvent() or WaitForEvent()
552 services to check the state of event. Type
553 EFI_EVENT is defined in CreateEvent() in
554 the Unified Extensible Firmware Interface
556 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
557 APs to return from Procedure, either for
558 blocking or non-blocking mode. Zero means
559 infinity. If the timeout expires before
560 all APs return from Procedure, then Procedure
561 on the failed APs is terminated. All enabled
562 APs are available for next function assigned
563 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
564 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
565 If the timeout expires in blocking mode,
566 BSP returns EFI_TIMEOUT. If the timeout
567 expires in non-blocking mode, WaitEvent
568 is signaled with SignalEvent().
569 @param[in] ProcedureArgument The parameter passed into Procedure for
571 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
572 if all APs finish successfully, then its
573 content is set to NULL. If not all APs
574 finish before timeout expires, then its
575 content is set to address of the buffer
576 holding handle numbers of the failed APs.
577 The buffer is allocated by MP Service Protocol,
578 and it's the caller's responsibility to
579 free the buffer with FreePool() service.
580 In blocking mode, it is ready for consumption
581 when the call returns. In non-blocking mode,
582 it is ready when WaitEvent is signaled. The
583 list of failed CPU is terminated by
586 @retval EFI_SUCCESS In blocking mode, all APs have finished before
588 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
590 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
591 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
593 @retval EFI_DEVICE_ERROR Caller processor is AP.
594 @retval EFI_NOT_STARTED No enabled APs exist in the system.
595 @retval EFI_NOT_READY Any enabled APs are busy.
596 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
597 all enabled APs have finished.
598 @retval EFI_INVALID_PARAMETER Procedure is NULL.
604 IN EFI_MP_SERVICES_PROTOCOL
*This
,
605 IN EFI_AP_PROCEDURE Procedure
,
606 IN BOOLEAN SingleThread
,
607 IN EFI_EVENT WaitEvent OPTIONAL
,
608 IN UINTN TimeoutInMicroseconds
,
609 IN VOID
*ProcedureArgument OPTIONAL
,
610 OUT UINTN
**FailedCpuList OPTIONAL
614 CPU_DATA_BLOCK
*CpuData
;
616 CPU_STATE APInitialState
;
620 if (FailedCpuList
!= NULL
) {
621 *FailedCpuList
= NULL
;
625 return EFI_DEVICE_ERROR
;
628 if (mMpSystemData
.NumberOfProcessors
== 1) {
629 return EFI_NOT_STARTED
;
632 if (Procedure
== NULL
) {
633 return EFI_INVALID_PARAMETER
;
637 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.
639 mStopCheckAllAPsStatus
= TRUE
;
641 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
642 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
643 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
650 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
652 // Skip Disabled processors
657 if (GetApState (CpuData
) != CpuStateIdle
) {
658 return EFI_NOT_READY
;
662 mMpSystemData
.Procedure
= Procedure
;
663 mMpSystemData
.ProcedureArgument
= ProcedureArgument
;
664 mMpSystemData
.WaitEvent
= WaitEvent
;
665 mMpSystemData
.Timeout
= TimeoutInMicroseconds
;
666 mMpSystemData
.TimeoutActive
= !!(TimeoutInMicroseconds
);
667 mMpSystemData
.FinishCount
= 0;
668 mMpSystemData
.StartCount
= 0;
669 mMpSystemData
.SingleThread
= SingleThread
;
670 mMpSystemData
.FailedList
= FailedCpuList
;
671 mMpSystemData
.FailedListIndex
= 0;
672 APInitialState
= CpuStateReady
;
674 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
675 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
676 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
683 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
685 // Skip Disabled processors
691 // Get APs prepared, and put failing APs into FailedCpuList
692 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
693 // state 1 by 1, until the previous 1 finished its task
694 // if not "SingleThread", all APs are put to ready state from the beginning
696 if (GetApState (CpuData
) == CpuStateIdle
) {
697 mMpSystemData
.StartCount
++;
699 SetApState (CpuData
, APInitialState
);
701 if (APInitialState
== CpuStateReady
) {
702 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
706 APInitialState
= CpuStateBlocked
;
711 mStopCheckAllAPsStatus
= FALSE
;
713 if (WaitEvent
!= NULL
) {
721 // Blocking temporarily stop CheckAllAPsStatus()
723 mStopCheckAllAPsStatus
= TRUE
;
726 CheckAndUpdateAllAPsToIdleState ();
727 if (mMpSystemData
.FinishCount
== mMpSystemData
.StartCount
) {
728 Status
= EFI_SUCCESS
;
735 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
737 Status
= EFI_TIMEOUT
;
741 gBS
->Stall (gPollInterval
);
742 mMpSystemData
.Timeout
-= gPollInterval
;
751 This service lets the caller get one enabled AP to execute a caller-provided
752 function. The caller can request the BSP to either wait for the completion
753 of the AP or just proceed with the next task by using the EFI event mechanism.
754 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
755 execution support. This service may only be called from the BSP.
757 This function is used to dispatch one enabled AP to the function specified by
758 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
759 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
760 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
761 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
762 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
763 then EFI_UNSUPPORTED must be returned.
765 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
766 from Procedure, then execution of Procedure by the AP is terminated. The AP is
767 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
768 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
770 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
772 @param[in] Procedure A pointer to the function to be run on
773 enabled APs of the system. See type
775 @param[in] ProcessorNumber The handle number of the AP. The range is
776 from 0 to the total number of logical
777 processors minus 1. The total number of
778 logical processors can be retrieved by
779 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
780 @param[in] WaitEvent The event created by the caller with CreateEvent()
781 service. If it is NULL, then execute in
782 blocking mode. BSP waits until all APs finish
783 or TimeoutInMicroseconds expires. If it's
784 not NULL, then execute in non-blocking mode.
785 BSP requests the function specified by
786 Procedure to be started on all the enabled
787 APs, and go on executing immediately. If
788 all return from Procedure or TimeoutInMicroseconds
789 expires, this event is signaled. The BSP
790 can use the CheckEvent() or WaitForEvent()
791 services to check the state of event. Type
792 EFI_EVENT is defined in CreateEvent() in
793 the Unified Extensible Firmware Interface
795 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
796 APs to return from Procedure, either for
797 blocking or non-blocking mode. Zero means
798 infinity. If the timeout expires before
799 all APs return from Procedure, then Procedure
800 on the failed APs is terminated. All enabled
801 APs are available for next function assigned
802 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
803 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
804 If the timeout expires in blocking mode,
805 BSP returns EFI_TIMEOUT. If the timeout
806 expires in non-blocking mode, WaitEvent
807 is signaled with SignalEvent().
808 @param[in] ProcedureArgument The parameter passed into Procedure for
810 @param[out] Finished If NULL, this parameter is ignored. In
811 blocking mode, this parameter is ignored.
812 In non-blocking mode, if AP returns from
813 Procedure before the timeout expires, its
814 content is set to TRUE. Otherwise, the
815 value is set to FALSE. The caller can
816 determine if the AP returned from Procedure
817 by evaluating this value.
819 @retval EFI_SUCCESS In blocking mode, specified AP finished before
821 @retval EFI_SUCCESS In non-blocking mode, the function has been
822 dispatched to specified AP.
823 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
824 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
826 @retval EFI_DEVICE_ERROR The calling processor is an AP.
827 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
828 the specified AP has finished.
829 @retval EFI_NOT_READY The specified AP is busy.
830 @retval EFI_NOT_FOUND The processor with the handle specified by
831 ProcessorNumber does not exist.
832 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
833 @retval EFI_INVALID_PARAMETER Procedure is NULL.
839 IN EFI_MP_SERVICES_PROTOCOL
*This
,
840 IN EFI_AP_PROCEDURE Procedure
,
841 IN UINTN ProcessorNumber
,
842 IN EFI_EVENT WaitEvent OPTIONAL
,
843 IN UINTN TimeoutInMicroseconds
,
844 IN VOID
*ProcedureArgument OPTIONAL
,
845 OUT BOOLEAN
*Finished OPTIONAL
848 CPU_DATA_BLOCK
*CpuData
;
852 if (Finished
!= NULL
) {
857 return EFI_DEVICE_ERROR
;
860 if (Procedure
== NULL
) {
861 return EFI_INVALID_PARAMETER
;
864 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
865 return EFI_NOT_FOUND
;
869 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.
871 mStopCheckAllAPsStatus
= TRUE
;
873 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
874 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
) ||
875 !TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
876 return EFI_INVALID_PARAMETER
;
879 if (GetApState (CpuData
) != CpuStateIdle
) {
880 return EFI_NOT_READY
;
883 SetApState (CpuData
, CpuStateReady
);
885 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
887 CpuData
->Timeout
= TimeoutInMicroseconds
;
888 CpuData
->WaitEvent
= WaitEvent
;
889 CpuData
->TimeoutActive
= !!(TimeoutInMicroseconds
);
890 CpuData
->Finished
= Finished
;
892 mStopCheckAllAPsStatus
= FALSE
;
894 if (WaitEvent
!= NULL
) {
905 if (GetApState (CpuData
) == CpuStateFinished
) {
906 SetApState (CpuData
, CpuStateIdle
);
910 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
911 ResetProcessorToIdleState (CpuData
);
915 gBS
->Stall (gPollInterval
);
916 CpuData
->Timeout
-= gPollInterval
;
923 This service switches the requested AP to be the BSP from that point onward.
924 This service changes the BSP for all purposes. This call can only be performed
927 This service switches the requested AP to be the BSP from that point onward.
928 This service changes the BSP for all purposes. The new BSP can take over the
929 execution of the old BSP and continue seamlessly from where the old one left
930 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
933 If the BSP cannot be switched prior to the return from this service, then
934 EFI_UNSUPPORTED must be returned.
936 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
937 @param[in] ProcessorNumber The handle number of AP that is to become the new
938 BSP. The range is from 0 to the total number of
939 logical processors minus 1. The total number of
940 logical processors can be retrieved by
941 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
942 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
943 enabled AP. Otherwise, it will be disabled.
945 @retval EFI_SUCCESS BSP successfully switched.
946 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
947 this service returning.
948 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
949 @retval EFI_SUCCESS The calling processor is an AP.
950 @retval EFI_NOT_FOUND The processor with the handle specified by
951 ProcessorNumber does not exist.
952 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
954 @retval EFI_NOT_READY The specified AP is busy.
960 IN EFI_MP_SERVICES_PROTOCOL
*This
,
961 IN UINTN ProcessorNumber
,
962 IN BOOLEAN EnableOldBSP
966 // Current always return unsupported.
968 return EFI_UNSUPPORTED
;
972 This service lets the caller enable or disable an AP from this point onward.
973 This service may only be called from the BSP.
975 This service allows the caller enable or disable an AP from this point onward.
976 The caller can optionally specify the health status of the AP by Health. If
977 an AP is being disabled, then the state of the disabled AP is implementation
978 dependent. If an AP is enabled, then the implementation must guarantee that a
979 complete initialization sequence is performed on the AP, so the AP is in a state
980 that is compatible with an MP operating system. This service may not be supported
981 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
983 If the enable or disable AP operation cannot be completed prior to the return
984 from this service, then EFI_UNSUPPORTED must be returned.
986 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
987 @param[in] ProcessorNumber The handle number of AP that is to become the new
988 BSP. The range is from 0 to the total number of
989 logical processors minus 1. The total number of
990 logical processors can be retrieved by
991 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
992 @param[in] EnableAP Specifies the new state for the processor for
993 enabled, FALSE for disabled.
994 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
995 the new health status of the AP. This flag
996 corresponds to StatusFlag defined in
997 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
998 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
999 bits are ignored. If it is NULL, this parameter
1002 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1003 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
1004 prior to this service returning.
1005 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
1006 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1007 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
1009 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
1015 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1016 IN UINTN ProcessorNumber
,
1017 IN BOOLEAN EnableAP
,
1018 IN UINT32
*HealthFlag OPTIONAL
1021 CPU_DATA_BLOCK
*CpuData
;
1022 BOOLEAN TempStopCheckState
;
1025 TempStopCheckState
= FALSE
;
1028 return EFI_DEVICE_ERROR
;
1031 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
1032 return EFI_NOT_FOUND
;
1036 // temporarily stop checkAllAPsStatus for initialize parameters.
1038 if (!mStopCheckAllAPsStatus
) {
1039 mStopCheckAllAPsStatus
= TRUE
;
1040 TempStopCheckState
= TRUE
;
1043 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1044 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1045 return EFI_INVALID_PARAMETER
;
1048 if (GetApState (CpuData
) != CpuStateIdle
) {
1049 return EFI_UNSUPPORTED
;
1053 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
1054 mMpSystemData
.NumberOfEnabledProcessors
++;
1056 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
1058 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1059 mMpSystemData
.NumberOfEnabledProcessors
--;
1061 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
1064 if (HealthFlag
!= NULL
) {
1065 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
1066 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
1069 if (TempStopCheckState
) {
1070 mStopCheckAllAPsStatus
= FALSE
;
1077 This return the handle number for the calling processor. This service may be
1078 called from the BSP and APs.
1080 This service returns the processor handle number for the calling processor.
1081 The returned value is in the range from 0 to the total number of logical
1082 processors minus 1. The total number of logical processors can be retrieved
1083 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1084 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1085 is returned. Otherwise, the current processors handle number is returned in
1086 ProcessorNumber, and EFI_SUCCESS is returned.
1088 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1089 @param[out] ProcessorNumber The handle number of AP that is to become the new
1090 BSP. The range is from 0 to the total number of
1091 logical processors minus 1. The total number of
1092 logical processors can be retrieved by
1093 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1095 @retval EFI_SUCCESS The current processor handle number was returned
1097 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1103 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1104 OUT UINTN
*ProcessorNumber
1110 if (ProcessorNumber
== NULL
) {
1111 return EFI_INVALID_PARAMETER
;
1114 ProcessorId
= GetApicId ();
1115 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
1116 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
1121 *ProcessorNumber
= Index
;
1126 Terminate AP's task and set it to idle state.
1128 This function terminates AP's task due to timeout by sending INIT-SIPI,
1129 and sends it to idle state.
1131 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1135 ResetProcessorToIdleState (
1136 IN CPU_DATA_BLOCK
*CpuData
1139 ResetApStackless ((UINT32
)CpuData
->Info
.ProcessorId
);
1143 Application Processors do loop routine
1144 after switch to its own stack.
1146 @param Context1 A pointer to the context to pass into the function.
1147 @param Context2 A pointer to the context to pass into the function.
1151 ProcessorToIdleState (
1152 IN VOID
*Context1
, OPTIONAL
1153 IN VOID
*Context2 OPTIONAL
1156 UINTN ProcessorNumber
;
1157 CPU_DATA_BLOCK
*CpuData
;
1158 EFI_AP_PROCEDURE Procedure
;
1159 VOID
*ProcedureArgument
;
1161 AsmApDoneWithCommonStack ();
1163 while (!mAPsAlreadyInitFinished
) {
1167 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1168 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1171 // Avoid forcibly reset AP caused the AP got lock not release.
1173 if (CpuData
->LockSelf
== (INTN
) GetApicId ()) {
1174 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1178 // Avoid forcibly reset AP caused the AP State is not updated.
1180 GetMpSpinLock (CpuData
);
1181 CpuData
->State
= CpuStateIdle
;
1182 CpuData
->Procedure
= NULL
;
1183 ReleaseMpSpinLock (CpuData
);
1186 GetMpSpinLock (CpuData
);
1187 ProcedureArgument
= CpuData
->Parameter
;
1188 Procedure
= CpuData
->Procedure
;
1189 ReleaseMpSpinLock (CpuData
);
1191 if (Procedure
!= NULL
) {
1192 Procedure (ProcedureArgument
);
1194 GetMpSpinLock (CpuData
);
1195 CpuData
->Procedure
= NULL
;
1196 CpuData
->State
= CpuStateFinished
;
1197 ReleaseMpSpinLock (CpuData
);
1208 Checks AP' status periodically.
1210 This function is triggerred by timer perodically to check the
1211 state of AP forStartupThisAP() executed in non-blocking mode.
1213 @param Event Event triggered.
1214 @param Context Parameter passed with the event.
1224 CPU_DATA_BLOCK
*CpuData
;
1227 CpuData
= (CPU_DATA_BLOCK
*) Context
;
1228 if (CpuData
->TimeoutActive
) {
1229 CpuData
->Timeout
-= gPollInterval
;
1232 CpuState
= GetApState (CpuData
);
1234 if (CpuState
== CpuStateFinished
) {
1235 if (CpuData
->Finished
) {
1236 *CpuData
->Finished
= TRUE
;
1238 SetApState (CpuData
, CpuStateIdle
);
1242 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
1243 if (CpuState
!= CpuStateIdle
&&
1244 CpuData
->Finished
) {
1245 *CpuData
->Finished
= FALSE
;
1247 ResetProcessorToIdleState (CpuData
);
1254 CpuData
->TimeoutActive
= FALSE
;
1255 gBS
->SignalEvent (CpuData
->WaitEvent
);
1256 CpuData
->WaitEvent
= NULL
;
1260 Checks APs' status periodically.
1262 This function is triggerred by timer perodically to check the
1263 state of APs for StartupAllAPs() executed in non-blocking mode.
1265 @param Event Event triggered.
1266 @param Context Parameter passed with the event.
1276 CPU_DATA_BLOCK
*CpuData
;
1280 if (mMpSystemData
.TimeoutActive
) {
1281 mMpSystemData
.Timeout
-= gPollInterval
;
1284 if (mStopCheckAllAPsStatus
) {
1289 // avoid next timer enter.
1291 Status
= gBS
->SetTimer (
1292 mMpSystemData
.CheckAllAPsEvent
,
1296 ASSERT_EFI_ERROR (Status
);
1298 if (mMpSystemData
.WaitEvent
!= NULL
) {
1299 CheckAndUpdateAllAPsToIdleState ();
1303 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
1304 ResetAllFailedAPs();
1308 mMpSystemData
.FinishCount
= mMpSystemData
.StartCount
;
1311 if (mMpSystemData
.FinishCount
!= mMpSystemData
.StartCount
) {
1315 mMpSystemData
.TimeoutActive
= FALSE
;
1316 gBS
->SignalEvent (mMpSystemData
.WaitEvent
);
1317 mMpSystemData
.WaitEvent
= NULL
;
1318 mStopCheckAllAPsStatus
= TRUE
;
1324 // check each AP status for StartupThisAP
1326 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
1327 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
1328 if (CpuData
->WaitEvent
) {
1329 CheckThisAPStatus (NULL
, (VOID
*)CpuData
);
1334 Status
= gBS
->SetTimer (
1335 mMpSystemData
.CheckAllAPsEvent
,
1337 EFI_TIMER_PERIOD_MICROSECONDS (100)
1339 ASSERT_EFI_ERROR (Status
);
1343 Application Processor C code entry point.
1353 UINTN ProcessorNumber
;
1355 if (!mAPsAlreadyInitFinished
) {
1356 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
1357 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
1358 mApStackStart
= TopOfApStack
;
1361 // Store the Stack address, when reset the AP, We can found the original address.
1363 mMpSystemData
.CpuDatas
[mMpSystemData
.NumberOfProcessors
].TopOfStack
= TopOfApStack
;
1364 mMpSystemData
.NumberOfProcessors
++;
1365 mMpSystemData
.NumberOfEnabledProcessors
++;
1367 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1369 // Get the original stack address.
1371 TopOfApStack
= mMpSystemData
.CpuDatas
[ProcessorNumber
].TopOfStack
;
1375 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
1382 This function is called by all processors (both BSP and AP) once and collects MP related data.
1384 @param Bsp TRUE if the CPU is BSP
1385 @param ProcessorNumber The specific processor number
1387 @retval EFI_SUCCESS Data for the processor collected and filled in
1391 FillInProcessorInformation (
1393 IN UINTN ProcessorNumber
1396 CPU_DATA_BLOCK
*CpuData
;
1399 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1400 ProcessorId
= GetApicId ();
1401 CpuData
->Info
.ProcessorId
= ProcessorId
;
1402 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1404 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1406 CpuData
->Info
.Location
.Package
= ProcessorId
;
1407 CpuData
->Info
.Location
.Core
= 0;
1408 CpuData
->Info
.Location
.Thread
= 0;
1409 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
1411 CpuData
->Procedure
= NULL
;
1412 CpuData
->Parameter
= NULL
;
1413 InitializeSpinLock (&CpuData
->CpuDataLock
);
1414 CpuData
->LockSelf
= -1;
1420 Prepare the System Data.
1422 @retval EFI_SUCCESS the System Data finished initilization.
1432 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
1434 mMpSystemData
.NumberOfProcessors
= 1;
1435 mMpSystemData
.NumberOfEnabledProcessors
= 1;
1437 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
1438 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
1440 Status
= gBS
->CreateEvent (
1441 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1445 &mMpSystemData
.CheckAllAPsEvent
1447 ASSERT_EFI_ERROR (Status
);
1450 // Set timer to check all APs status.
1452 Status
= gBS
->SetTimer (
1453 mMpSystemData
.CheckAllAPsEvent
,
1455 EFI_TIMER_PERIOD_MICROSECONDS (100)
1457 ASSERT_EFI_ERROR (Status
);
1462 FillInProcessorInformation (TRUE
, 0);
1468 Initialize Multi-processor support.
1472 InitializeMpSupport (
1478 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1479 if (gMaxLogicalProcessorNumber
< 1) {
1480 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1484 if (gMaxLogicalProcessorNumber
== 1) {
1488 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
1489 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
1491 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1492 ASSERT (mApStackStart
!= NULL
);
1495 // the first buffer of stack size used for common stack, when the amount of AP
1496 // more than 1, we should never free the common stack which maybe used for AP reset.
1498 mCommonStack
= mApStackStart
;
1499 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
1500 mApStackStart
= mTopOfApCommonStack
;
1502 InitMpSystemData ();
1504 PrepareAPStartupCode ();
1506 StartApsStackless ();
1508 DEBUG ((DEBUG_INFO
, "Detect CPU count: %d\n", mMpSystemData
.NumberOfProcessors
));
1509 if (mMpSystemData
.NumberOfProcessors
== 1) {
1510 FreeApStartupCode ();
1511 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1515 mMpSystemData
.CpuDatas
= ReallocatePool (
1516 sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
,
1517 sizeof (CPU_DATA_BLOCK
) * mMpSystemData
.NumberOfProcessors
,
1518 mMpSystemData
.CpuDatas
);
1520 mAPsAlreadyInitFinished
= TRUE
;
1522 Status
= gBS
->InstallMultipleProtocolInterfaces (
1524 &gEfiMpServiceProtocolGuid
, &mMpServicesTemplate
,
1527 ASSERT_EFI_ERROR (Status
);
1529 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
1530 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
1531 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *