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
;
636 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.
638 mStopCheckAllAPsStatus
= TRUE
;
640 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
641 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
642 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
649 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
651 // Skip Disabled processors
656 if (GetApState (CpuData
) != CpuStateIdle
) {
657 return EFI_NOT_READY
;
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 // Blocking temporarily stop CheckAllAPsStatus()
722 mStopCheckAllAPsStatus
= TRUE
;
725 CheckAndUpdateAllAPsToIdleState ();
726 if (mMpSystemData
.FinishCount
== mMpSystemData
.StartCount
) {
727 Status
= EFI_SUCCESS
;
734 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
736 Status
= EFI_TIMEOUT
;
740 gBS
->Stall (gPollInterval
);
741 mMpSystemData
.Timeout
-= gPollInterval
;
750 This service lets the caller get one enabled AP to execute a caller-provided
751 function. The caller can request the BSP to either wait for the completion
752 of the AP or just proceed with the next task by using the EFI event mechanism.
753 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
754 execution support. This service may only be called from the BSP.
756 This function is used to dispatch one enabled AP to the function specified by
757 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
758 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
759 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
760 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
761 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
762 then EFI_UNSUPPORTED must be returned.
764 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
765 from Procedure, then execution of Procedure by the AP is terminated. The AP is
766 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
767 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
769 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
771 @param[in] Procedure A pointer to the function to be run on
772 enabled APs of the system. See type
774 @param[in] ProcessorNumber The handle number of the AP. The range is
775 from 0 to the total number of logical
776 processors minus 1. The total number of
777 logical processors can be retrieved by
778 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
779 @param[in] WaitEvent The event created by the caller with CreateEvent()
780 service. If it is NULL, then execute in
781 blocking mode. BSP waits until all APs finish
782 or TimeoutInMicroseconds expires. If it's
783 not NULL, then execute in non-blocking mode.
784 BSP requests the function specified by
785 Procedure to be started on all the enabled
786 APs, and go on executing immediately. If
787 all return from Procedure or TimeoutInMicroseconds
788 expires, this event is signaled. The BSP
789 can use the CheckEvent() or WaitForEvent()
790 services to check the state of event. Type
791 EFI_EVENT is defined in CreateEvent() in
792 the Unified Extensible Firmware Interface
794 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
795 APs to return from Procedure, either for
796 blocking or non-blocking mode. Zero means
797 infinity. If the timeout expires before
798 all APs return from Procedure, then Procedure
799 on the failed APs is terminated. All enabled
800 APs are available for next function assigned
801 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
802 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
803 If the timeout expires in blocking mode,
804 BSP returns EFI_TIMEOUT. If the timeout
805 expires in non-blocking mode, WaitEvent
806 is signaled with SignalEvent().
807 @param[in] ProcedureArgument The parameter passed into Procedure for
809 @param[out] Finished If NULL, this parameter is ignored. In
810 blocking mode, this parameter is ignored.
811 In non-blocking mode, if AP returns from
812 Procedure before the timeout expires, its
813 content is set to TRUE. Otherwise, the
814 value is set to FALSE. The caller can
815 determine if the AP returned from Procedure
816 by evaluating this value.
818 @retval EFI_SUCCESS In blocking mode, specified AP finished before
820 @retval EFI_SUCCESS In non-blocking mode, the function has been
821 dispatched to specified AP.
822 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
823 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
825 @retval EFI_DEVICE_ERROR The calling processor is an AP.
826 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
827 the specified AP has finished.
828 @retval EFI_NOT_READY The specified AP is busy.
829 @retval EFI_NOT_FOUND The processor with the handle specified by
830 ProcessorNumber does not exist.
831 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
832 @retval EFI_INVALID_PARAMETER Procedure is NULL.
838 IN EFI_MP_SERVICES_PROTOCOL
*This
,
839 IN EFI_AP_PROCEDURE Procedure
,
840 IN UINTN ProcessorNumber
,
841 IN EFI_EVENT WaitEvent OPTIONAL
,
842 IN UINTN TimeoutInMicroseconds
,
843 IN VOID
*ProcedureArgument OPTIONAL
,
844 OUT BOOLEAN
*Finished OPTIONAL
847 CPU_DATA_BLOCK
*CpuData
;
851 if (Finished
!= NULL
) {
856 return EFI_DEVICE_ERROR
;
859 if (Procedure
== NULL
) {
860 return EFI_INVALID_PARAMETER
;
863 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
864 return EFI_NOT_FOUND
;
868 // temporarily stop checkAllAPsStatus for avoid resource dead-lock.
870 mStopCheckAllAPsStatus
= TRUE
;
872 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
873 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
) ||
874 !TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
875 return EFI_INVALID_PARAMETER
;
878 if (GetApState (CpuData
) != CpuStateIdle
) {
879 return EFI_NOT_READY
;
882 SetApState (CpuData
, CpuStateReady
);
884 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
886 CpuData
->Timeout
= TimeoutInMicroseconds
;
887 CpuData
->WaitEvent
= WaitEvent
;
888 CpuData
->TimeoutActive
= !!(TimeoutInMicroseconds
);
889 CpuData
->Finished
= Finished
;
891 mStopCheckAllAPsStatus
= FALSE
;
893 if (WaitEvent
!= NULL
) {
904 if (GetApState (CpuData
) == CpuStateFinished
) {
905 SetApState (CpuData
, CpuStateIdle
);
909 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
910 ResetProcessorToIdleState (CpuData
);
914 gBS
->Stall (gPollInterval
);
915 CpuData
->Timeout
-= gPollInterval
;
922 This service switches the requested AP to be the BSP from that point onward.
923 This service changes the BSP for all purposes. This call can only be performed
926 This service switches the requested AP to be the BSP from that point onward.
927 This service changes the BSP for all purposes. The new BSP can take over the
928 execution of the old BSP and continue seamlessly from where the old one left
929 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
932 If the BSP cannot be switched prior to the return from this service, then
933 EFI_UNSUPPORTED must be returned.
935 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
936 @param[in] ProcessorNumber The handle number of AP that is to become the new
937 BSP. The range is from 0 to the total number of
938 logical processors minus 1. The total number of
939 logical processors can be retrieved by
940 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
941 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
942 enabled AP. Otherwise, it will be disabled.
944 @retval EFI_SUCCESS BSP successfully switched.
945 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
946 this service returning.
947 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
948 @retval EFI_SUCCESS The calling processor is an AP.
949 @retval EFI_NOT_FOUND The processor with the handle specified by
950 ProcessorNumber does not exist.
951 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
953 @retval EFI_NOT_READY The specified AP is busy.
959 IN EFI_MP_SERVICES_PROTOCOL
*This
,
960 IN UINTN ProcessorNumber
,
961 IN BOOLEAN EnableOldBSP
965 // Current always return unsupported.
967 return EFI_UNSUPPORTED
;
971 This service lets the caller enable or disable an AP from this point onward.
972 This service may only be called from the BSP.
974 This service allows the caller enable or disable an AP from this point onward.
975 The caller can optionally specify the health status of the AP by Health. If
976 an AP is being disabled, then the state of the disabled AP is implementation
977 dependent. If an AP is enabled, then the implementation must guarantee that a
978 complete initialization sequence is performed on the AP, so the AP is in a state
979 that is compatible with an MP operating system. This service may not be supported
980 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
982 If the enable or disable AP operation cannot be completed prior to the return
983 from this service, then EFI_UNSUPPORTED must be returned.
985 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
986 @param[in] ProcessorNumber The handle number of AP that is to become the new
987 BSP. The range is from 0 to the total number of
988 logical processors minus 1. The total number of
989 logical processors can be retrieved by
990 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
991 @param[in] EnableAP Specifies the new state for the processor for
992 enabled, FALSE for disabled.
993 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
994 the new health status of the AP. This flag
995 corresponds to StatusFlag defined in
996 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
997 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
998 bits are ignored. If it is NULL, this parameter
1001 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1002 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
1003 prior to this service returning.
1004 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
1005 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1006 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
1008 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
1014 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1015 IN UINTN ProcessorNumber
,
1016 IN BOOLEAN EnableAP
,
1017 IN UINT32
*HealthFlag OPTIONAL
1020 CPU_DATA_BLOCK
*CpuData
;
1021 BOOLEAN TempStopCheckState
;
1024 TempStopCheckState
= FALSE
;
1027 return EFI_DEVICE_ERROR
;
1030 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
1031 return EFI_NOT_FOUND
;
1035 // temporarily stop checkAllAPsStatus for initialize parameters.
1037 if (!mStopCheckAllAPsStatus
) {
1038 mStopCheckAllAPsStatus
= TRUE
;
1039 TempStopCheckState
= TRUE
;
1042 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1043 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1044 return EFI_INVALID_PARAMETER
;
1047 if (GetApState (CpuData
) != CpuStateIdle
) {
1048 return EFI_UNSUPPORTED
;
1052 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
1053 mMpSystemData
.NumberOfEnabledProcessors
++;
1055 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
1057 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1058 mMpSystemData
.NumberOfEnabledProcessors
--;
1060 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
1063 if (HealthFlag
!= NULL
) {
1064 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
1065 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
1068 if (TempStopCheckState
) {
1069 mStopCheckAllAPsStatus
= FALSE
;
1076 This return the handle number for the calling processor. This service may be
1077 called from the BSP and APs.
1079 This service returns the processor handle number for the calling processor.
1080 The returned value is in the range from 0 to the total number of logical
1081 processors minus 1. The total number of logical processors can be retrieved
1082 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1083 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1084 is returned. Otherwise, the current processors handle number is returned in
1085 ProcessorNumber, and EFI_SUCCESS is returned.
1087 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1088 @param[out] ProcessorNumber The handle number of AP that is to become the new
1089 BSP. The range is from 0 to the total number of
1090 logical processors minus 1. The total number of
1091 logical processors can be retrieved by
1092 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1094 @retval EFI_SUCCESS The current processor handle number was returned
1096 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1102 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1103 OUT UINTN
*ProcessorNumber
1109 if (ProcessorNumber
== NULL
) {
1110 return EFI_INVALID_PARAMETER
;
1113 ProcessorId
= GetApicId ();
1114 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
1115 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
1120 *ProcessorNumber
= Index
;
1125 Terminate AP's task and set it to idle state.
1127 This function terminates AP's task due to timeout by sending INIT-SIPI,
1128 and sends it to idle state.
1130 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1134 ResetProcessorToIdleState (
1135 IN CPU_DATA_BLOCK
*CpuData
1138 ResetApStackless ((UINT32
)CpuData
->Info
.ProcessorId
);
1142 Application Processors do loop routine
1143 after switch to its own stack.
1145 @param Context1 A pointer to the context to pass into the function.
1146 @param Context2 A pointer to the context to pass into the function.
1150 ProcessorToIdleState (
1151 IN VOID
*Context1
, OPTIONAL
1152 IN VOID
*Context2 OPTIONAL
1155 UINTN ProcessorNumber
;
1156 CPU_DATA_BLOCK
*CpuData
;
1157 EFI_AP_PROCEDURE Procedure
;
1158 VOID
*ProcedureArgument
;
1160 AsmApDoneWithCommonStack ();
1162 while (!mAPsAlreadyInitFinished
) {
1166 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1167 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1170 // Avoid forcibly reset AP caused the AP got lock not release.
1172 if (CpuData
->LockSelf
== (INTN
) GetApicId ()) {
1173 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1177 // Avoid forcibly reset AP caused the AP State is not updated.
1179 GetMpSpinLock (CpuData
);
1180 CpuData
->State
= CpuStateIdle
;
1181 CpuData
->Procedure
= NULL
;
1182 ReleaseMpSpinLock (CpuData
);
1185 GetMpSpinLock (CpuData
);
1186 ProcedureArgument
= CpuData
->Parameter
;
1187 Procedure
= CpuData
->Procedure
;
1188 ReleaseMpSpinLock (CpuData
);
1190 if (Procedure
!= NULL
) {
1191 Procedure (ProcedureArgument
);
1193 GetMpSpinLock (CpuData
);
1194 CpuData
->Procedure
= NULL
;
1195 CpuData
->State
= CpuStateFinished
;
1196 ReleaseMpSpinLock (CpuData
);
1207 Checks AP' status periodically.
1209 This function is triggerred by timer perodically to check the
1210 state of AP forStartupThisAP() executed in non-blocking mode.
1212 @param Event Event triggered.
1213 @param Context Parameter passed with the event.
1223 CPU_DATA_BLOCK
*CpuData
;
1226 CpuData
= (CPU_DATA_BLOCK
*) Context
;
1227 if (CpuData
->TimeoutActive
) {
1228 CpuData
->Timeout
-= gPollInterval
;
1231 CpuState
= GetApState (CpuData
);
1233 if (CpuState
== CpuStateFinished
) {
1234 if (CpuData
->Finished
) {
1235 *CpuData
->Finished
= TRUE
;
1237 SetApState (CpuData
, CpuStateIdle
);
1241 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
1242 if (CpuState
!= CpuStateIdle
&&
1243 CpuData
->Finished
) {
1244 *CpuData
->Finished
= FALSE
;
1246 ResetProcessorToIdleState (CpuData
);
1253 CpuData
->TimeoutActive
= FALSE
;
1254 gBS
->SignalEvent (CpuData
->WaitEvent
);
1255 CpuData
->WaitEvent
= NULL
;
1259 Checks APs' status periodically.
1261 This function is triggerred by timer perodically to check the
1262 state of APs for StartupAllAPs() executed in non-blocking mode.
1264 @param Event Event triggered.
1265 @param Context Parameter passed with the event.
1275 CPU_DATA_BLOCK
*CpuData
;
1279 if (mMpSystemData
.TimeoutActive
) {
1280 mMpSystemData
.Timeout
-= gPollInterval
;
1283 if (mStopCheckAllAPsStatus
) {
1288 // avoid next timer enter.
1290 Status
= gBS
->SetTimer (
1291 mMpSystemData
.CheckAllAPsEvent
,
1295 ASSERT_EFI_ERROR (Status
);
1297 if (mMpSystemData
.WaitEvent
!= NULL
) {
1298 CheckAndUpdateAllAPsToIdleState ();
1302 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
1303 ResetAllFailedAPs();
1307 mMpSystemData
.FinishCount
= mMpSystemData
.StartCount
;
1310 if (mMpSystemData
.FinishCount
!= mMpSystemData
.StartCount
) {
1314 mMpSystemData
.TimeoutActive
= FALSE
;
1315 gBS
->SignalEvent (mMpSystemData
.WaitEvent
);
1316 mMpSystemData
.WaitEvent
= NULL
;
1317 mStopCheckAllAPsStatus
= TRUE
;
1323 // check each AP status for StartupThisAP
1325 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
1326 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
1327 if (CpuData
->WaitEvent
) {
1328 CheckThisAPStatus (NULL
, (VOID
*)CpuData
);
1333 Status
= gBS
->SetTimer (
1334 mMpSystemData
.CheckAllAPsEvent
,
1336 EFI_TIMER_PERIOD_MICROSECONDS (100)
1338 ASSERT_EFI_ERROR (Status
);
1342 Application Processor C code entry point.
1352 UINTN ProcessorNumber
;
1354 if (!mAPsAlreadyInitFinished
) {
1355 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
1356 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
1357 mApStackStart
= TopOfApStack
;
1360 // Store the Stack address, when reset the AP, We can found the original address.
1362 mMpSystemData
.CpuDatas
[mMpSystemData
.NumberOfProcessors
].TopOfStack
= TopOfApStack
;
1363 mMpSystemData
.NumberOfProcessors
++;
1364 mMpSystemData
.NumberOfEnabledProcessors
++;
1366 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1368 // Get the original stack address.
1370 TopOfApStack
= mMpSystemData
.CpuDatas
[ProcessorNumber
].TopOfStack
;
1374 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
1381 This function is called by all processors (both BSP and AP) once and collects MP related data.
1383 @param Bsp TRUE if the CPU is BSP
1384 @param ProcessorNumber The specific processor number
1386 @retval EFI_SUCCESS Data for the processor collected and filled in
1390 FillInProcessorInformation (
1392 IN UINTN ProcessorNumber
1395 CPU_DATA_BLOCK
*CpuData
;
1398 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1399 ProcessorId
= GetApicId ();
1400 CpuData
->Info
.ProcessorId
= ProcessorId
;
1401 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1403 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1405 CpuData
->Info
.Location
.Package
= ProcessorId
;
1406 CpuData
->Info
.Location
.Core
= 0;
1407 CpuData
->Info
.Location
.Thread
= 0;
1408 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
1410 CpuData
->Procedure
= NULL
;
1411 CpuData
->Parameter
= NULL
;
1412 InitializeSpinLock (&CpuData
->CpuDataLock
);
1413 CpuData
->LockSelf
= -1;
1419 Prepare the System Data.
1421 @retval EFI_SUCCESS the System Data finished initilization.
1431 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
1433 mMpSystemData
.NumberOfProcessors
= 1;
1434 mMpSystemData
.NumberOfEnabledProcessors
= 1;
1436 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
1437 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
1439 Status
= gBS
->CreateEvent (
1440 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1444 &mMpSystemData
.CheckAllAPsEvent
1446 ASSERT_EFI_ERROR (Status
);
1449 // Set timer to check all APs status.
1451 Status
= gBS
->SetTimer (
1452 mMpSystemData
.CheckAllAPsEvent
,
1454 EFI_TIMER_PERIOD_MICROSECONDS (100)
1456 ASSERT_EFI_ERROR (Status
);
1461 FillInProcessorInformation (TRUE
, 0);
1467 Initialize Multi-processor support.
1471 InitializeMpSupport (
1475 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1476 if (gMaxLogicalProcessorNumber
< 1) {
1477 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1481 if (gMaxLogicalProcessorNumber
== 1) {
1485 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
1486 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
1488 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1489 ASSERT (mApStackStart
!= NULL
);
1492 // the first buffer of stack size used for common stack, when the amount of AP
1493 // more than 1, we should never free the common stack which maybe used for AP reset.
1495 mCommonStack
= mApStackStart
;
1496 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
1497 mApStackStart
= mTopOfApCommonStack
;
1499 InitMpSystemData ();
1501 PrepareAPStartupCode ();
1503 if (mMpSystemData
.NumberOfProcessors
== 1) {
1504 FreeApStartupCode ();
1505 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1509 mMpSystemData
.CpuDatas
= ReallocatePool (
1510 sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
,
1511 sizeof (CPU_DATA_BLOCK
) * mMpSystemData
.NumberOfProcessors
,
1512 mMpSystemData
.CpuDatas
);
1514 mAPsAlreadyInitFinished
= TRUE
;
1516 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
1517 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
1518 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *