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
,
41 Check whether caller processor is BSP.
43 @retval TRUE the caller is BSP
44 @retval FALSE the caller is AP
53 CPU_DATA_BLOCK
*CpuData
;
57 WhoAmI (&mMpServicesTemplate
, &CpuIndex
);
58 CpuData
= &mMpSystemData
.CpuDatas
[CpuIndex
];
60 return CpuData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
? TRUE
: FALSE
;
64 Get the Application Processors state.
66 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
68 @retval CPU_STATE the AP status
73 IN CPU_DATA_BLOCK
*CpuData
78 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
82 State
= CpuData
->State
;
83 ReleaseSpinLock (&CpuData
->CpuDataLock
);
89 Set the Application Processors state.
91 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP
92 @param State The AP status
97 IN CPU_DATA_BLOCK
*CpuData
,
101 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
105 CpuData
->State
= State
;
106 ReleaseSpinLock (&CpuData
->CpuDataLock
);
110 Set the Application Processor prepare to run a function specified
113 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
114 @param Procedure A pointer to the function to be run on enabled APs of the system
115 @param ProcedureArgument Pointer to the optional parameter of the assigned function
120 IN CPU_DATA_BLOCK
*CpuData
,
121 IN EFI_AP_PROCEDURE Procedure
,
122 IN VOID
*ProcedureArgument
125 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
129 CpuData
->Parameter
= ProcedureArgument
;
130 CpuData
->Procedure
= Procedure
;
131 ReleaseSpinLock (&CpuData
->CpuDataLock
);
135 Check the Application Processors Status whether contains the Flags.
137 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
138 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
140 @retval TRUE the AP status includes the StatusFlag
141 @retval FALSE the AP status excludes the StatusFlag
146 IN CPU_DATA_BLOCK
*CpuData
,
152 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
156 Ret
= CpuData
->Info
.StatusFlag
& Flags
;
157 ReleaseSpinLock (&CpuData
->CpuDataLock
);
163 Bitwise-Or of the Application Processors Status with the Flags.
165 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
166 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
171 IN CPU_DATA_BLOCK
*CpuData
,
175 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
179 CpuData
->Info
.StatusFlag
|= Flags
;
180 ReleaseSpinLock (&CpuData
->CpuDataLock
);
184 Bitwise-AndNot 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
191 CpuStatusFlagAndNot (
192 IN CPU_DATA_BLOCK
*CpuData
,
196 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
200 CpuData
->Info
.StatusFlag
&= ~Flags
;
201 ReleaseSpinLock (&CpuData
->CpuDataLock
);
205 Searches for the next blocking AP.
207 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().
209 @param NextNumber Pointer to the processor number of the next blocking AP.
211 @retval EFI_SUCCESS The next blocking AP has been found.
212 @retval EFI_NOT_FOUND No blocking AP exists.
216 GetNextBlockedNumber (
217 OUT UINTN
*NextNumber
222 CPU_DATA_BLOCK
*CpuData
;
224 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
225 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
226 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
233 CpuState
= GetApState (CpuData
);
234 if (CpuState
== CpuStateBlocked
) {
235 *NextNumber
= Number
;
240 return EFI_NOT_FOUND
;
244 Check if the APs state are finished, and update them to idle state
249 CheckAndUpdateAllAPsToIdleState (
253 UINTN ProcessorNumber
;
255 CPU_DATA_BLOCK
*CpuData
;
259 for (ProcessorNumber
= 0; ProcessorNumber
< mMpSystemData
.NumberOfProcessors
; ProcessorNumber
++) {
260 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
261 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
268 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
270 // Skip Disabled processors
275 CpuState
= GetApState (CpuData
);
276 if (CpuState
== CpuStateFinished
) {
277 mMpSystemData
.FinishCount
++;
278 if (mMpSystemData
.SingleThread
) {
279 Status
= GetNextBlockedNumber (&NextNumber
);
280 if (!EFI_ERROR (Status
)) {
281 SetApState (&mMpSystemData
.CpuDatas
[NextNumber
], CpuStateReady
);
282 SetApProcedure (&mMpSystemData
.CpuDatas
[NextNumber
],
283 mMpSystemData
.Procedure
,
284 mMpSystemData
.ProcedureArgument
);
288 SetApState (CpuData
, CpuStateIdle
);
294 If the timeout expires before all APs returns from Procedure,
295 we should forcibly terminate the executing AP and fill FailedList back
304 CPU_DATA_BLOCK
*CpuData
;
308 if (mMpSystemData
.FailedList
!= NULL
) {
309 *mMpSystemData
.FailedList
= AllocatePool ((mMpSystemData
.StartCount
- mMpSystemData
.FinishCount
+ 1) * sizeof(UINTN
));
310 ASSERT (*mMpSystemData
.FailedList
!= NULL
);
313 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
314 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
315 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
322 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
324 // Skip Disabled processors
329 CpuState
= GetApState (CpuData
);
330 if (CpuState
!= CpuStateIdle
) {
331 if (mMpSystemData
.FailedList
!= NULL
) {
332 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
++] = Number
;
334 ResetProcessorToIdleState (CpuData
);
338 if (mMpSystemData
.FailedList
!= NULL
) {
339 (*mMpSystemData
.FailedList
)[mMpSystemData
.FailedListIndex
] = END_OF_CPU_LIST
;
344 This service retrieves the number of logical processor in the platform
345 and the number of those logical processors that are enabled on this boot.
346 This service may only be called from the BSP.
348 This function is used to retrieve the following information:
349 - The number of logical processors that are present in the system.
350 - The number of enabled logical processors in the system at the instant
353 Because MP Service Protocol provides services to enable and disable processors
354 dynamically, the number of enabled logical processors may vary during the
355 course of a boot session.
357 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
358 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
359 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
360 is returned in NumberOfProcessors, the number of currently enabled processor
361 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
363 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
365 @param[out] NumberOfProcessors Pointer to the total number of logical
366 processors in the system, including the BSP
368 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
369 processors that exist in system, including
372 @retval EFI_SUCCESS The number of logical processors and enabled
373 logical processors was retrieved.
374 @retval EFI_DEVICE_ERROR The calling processor is an AP.
375 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
376 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
381 GetNumberOfProcessors (
382 IN EFI_MP_SERVICES_PROTOCOL
*This
,
383 OUT UINTN
*NumberOfProcessors
,
384 OUT UINTN
*NumberOfEnabledProcessors
387 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
388 return EFI_INVALID_PARAMETER
;
392 return EFI_DEVICE_ERROR
;
395 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
396 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
401 Gets detailed MP-related information on the requested processor at the
402 instant this call is made. This service may only be called from the BSP.
404 This service retrieves detailed MP-related information about any processor
405 on the platform. Note the following:
406 - The processor information may change during the course of a boot session.
407 - The information presented here is entirely MP related.
409 Information regarding the number of caches and their sizes, frequency of operation,
410 slot numbers is all considered platform-related information and is not provided
413 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
415 @param[in] ProcessorNumber The handle number of processor.
416 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
417 the requested processor is deposited.
419 @retval EFI_SUCCESS Processor information was returned.
420 @retval EFI_DEVICE_ERROR The calling processor is an AP.
421 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
422 @retval EFI_NOT_FOUND The processor with the handle specified by
423 ProcessorNumber does not exist in the platform.
429 IN EFI_MP_SERVICES_PROTOCOL
*This
,
430 IN UINTN ProcessorNumber
,
431 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
434 if (ProcessorInfoBuffer
== NULL
) {
435 return EFI_INVALID_PARAMETER
;
439 return EFI_DEVICE_ERROR
;
442 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
443 return EFI_NOT_FOUND
;
446 CopyMem (ProcessorInfoBuffer
, &mMpSystemData
.CpuDatas
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
451 This service executes a caller provided function on all enabled APs. APs can
452 run either simultaneously or one at a time in sequence. This service supports
453 both blocking and non-blocking requests. The non-blocking requests use EFI
454 events so the BSP can detect when the APs have finished. This service may only
455 be called from the BSP.
457 This function is used to dispatch all the enabled APs to the function specified
458 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
459 immediately and Procedure is not started on any AP.
461 If SingleThread is TRUE, all the enabled APs execute the function specified by
462 Procedure one by one, in ascending order of processor handle number. Otherwise,
463 all the enabled APs execute the function specified by Procedure simultaneously.
465 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
466 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
467 mode, and the BSP returns from this service without waiting for APs. If a
468 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
469 is signaled, then EFI_UNSUPPORTED must be returned.
471 If the timeout specified by TimeoutInMicroseconds expires before all APs return
472 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
473 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
474 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
475 content points to the list of processor handle numbers in which Procedure was
478 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
479 to make sure that the nature of the code that is executed on the BSP and the
480 dispatched APs is well controlled. The MP Services Protocol does not guarantee
481 that the Procedure function is MP-safe. Hence, the tasks that can be run in
482 parallel are limited to certain independent tasks and well-controlled exclusive
483 code. EFI services and protocols may not be called by APs unless otherwise
486 In blocking execution mode, BSP waits until all APs finish or
487 TimeoutInMicroseconds expires.
489 In non-blocking execution mode, BSP is freed to return to the caller and then
490 proceed to the next task without having to wait for APs. The following
491 sequence needs to occur in a non-blocking execution mode:
493 -# The caller that intends to use this MP Services Protocol in non-blocking
494 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
495 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
496 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
497 the function specified by Procedure to be started on all the enabled APs,
498 and releases the BSP to continue with other tasks.
499 -# The caller can use the CheckEvent() and WaitForEvent() services to check
500 the state of the WaitEvent created in step 1.
501 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
502 Service signals WaitEvent by calling the EFI SignalEvent() function. If
503 FailedCpuList is not NULL, its content is available when WaitEvent is
504 signaled. If all APs returned from Procedure prior to the timeout, then
505 FailedCpuList is set to NULL. If not all APs return from Procedure before
506 the timeout, then FailedCpuList is filled in with the list of the failed
507 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
508 It is the caller's responsibility to free the buffer with FreePool() service.
509 -# This invocation of SignalEvent() function informs the caller that invoked
510 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
511 the specified task or a timeout occurred. The contents of FailedCpuList
512 can be examined to determine which APs did not complete the specified task
513 prior to the timeout.
515 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
517 @param[in] Procedure A pointer to the function to be run on
518 enabled APs of the system. See type
520 @param[in] SingleThread If TRUE, then all the enabled APs execute
521 the function specified by Procedure one by
522 one, in ascending order of processor handle
523 number. If FALSE, then all the enabled APs
524 execute the function specified by Procedure
526 @param[in] WaitEvent The event created by the caller with CreateEvent()
527 service. If it is NULL, then execute in
528 blocking mode. BSP waits until all APs finish
529 or TimeoutInMicroseconds expires. If it's
530 not NULL, then execute in non-blocking mode.
531 BSP requests the function specified by
532 Procedure to be started on all the enabled
533 APs, and go on executing immediately. If
534 all return from Procedure, or TimeoutInMicroseconds
535 expires, this event is signaled. The BSP
536 can use the CheckEvent() or WaitForEvent()
537 services to check the state of event. Type
538 EFI_EVENT is defined in CreateEvent() in
539 the Unified Extensible Firmware Interface
541 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
542 APs to return from Procedure, either for
543 blocking or non-blocking mode. Zero means
544 infinity. If the timeout expires before
545 all APs return from Procedure, then Procedure
546 on the failed APs is terminated. All enabled
547 APs are available for next function assigned
548 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
549 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
550 If the timeout expires in blocking mode,
551 BSP returns EFI_TIMEOUT. If the timeout
552 expires in non-blocking mode, WaitEvent
553 is signaled with SignalEvent().
554 @param[in] ProcedureArgument The parameter passed into Procedure for
556 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
557 if all APs finish successfully, then its
558 content is set to NULL. If not all APs
559 finish before timeout expires, then its
560 content is set to address of the buffer
561 holding handle numbers of the failed APs.
562 The buffer is allocated by MP Service Protocol,
563 and it's the caller's responsibility to
564 free the buffer with FreePool() service.
565 In blocking mode, it is ready for consumption
566 when the call returns. In non-blocking mode,
567 it is ready when WaitEvent is signaled. The
568 list of failed CPU is terminated by
571 @retval EFI_SUCCESS In blocking mode, all APs have finished before
573 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
575 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
576 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
578 @retval EFI_DEVICE_ERROR Caller processor is AP.
579 @retval EFI_NOT_STARTED No enabled APs exist in the system.
580 @retval EFI_NOT_READY Any enabled APs are busy.
581 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
582 all enabled APs have finished.
583 @retval EFI_INVALID_PARAMETER Procedure is NULL.
589 IN EFI_MP_SERVICES_PROTOCOL
*This
,
590 IN EFI_AP_PROCEDURE Procedure
,
591 IN BOOLEAN SingleThread
,
592 IN EFI_EVENT WaitEvent OPTIONAL
,
593 IN UINTN TimeoutInMicroseconds
,
594 IN VOID
*ProcedureArgument OPTIONAL
,
595 OUT UINTN
**FailedCpuList OPTIONAL
599 CPU_DATA_BLOCK
*CpuData
;
601 CPU_STATE APInitialState
;
605 if (FailedCpuList
!= NULL
) {
606 *FailedCpuList
= NULL
;
610 return EFI_DEVICE_ERROR
;
613 if (mMpSystemData
.NumberOfProcessors
== 1) {
614 return EFI_NOT_STARTED
;
617 if (Procedure
== NULL
) {
618 return EFI_INVALID_PARAMETER
;
621 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
622 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
623 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
630 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
632 // Skip Disabled processors
637 if (GetApState (CpuData
) != CpuStateIdle
) {
638 return EFI_NOT_READY
;
643 // temporarily stop checkAllAPsStatus for initialize parameters.
645 mStopCheckAllAPsStatus
= TRUE
;
647 mMpSystemData
.Procedure
= Procedure
;
648 mMpSystemData
.ProcedureArgument
= ProcedureArgument
;
649 mMpSystemData
.WaitEvent
= WaitEvent
;
650 mMpSystemData
.Timeout
= TimeoutInMicroseconds
;
651 mMpSystemData
.TimeoutActive
= !!(TimeoutInMicroseconds
);
652 mMpSystemData
.FinishCount
= 0;
653 mMpSystemData
.StartCount
= 0;
654 mMpSystemData
.SingleThread
= SingleThread
;
655 mMpSystemData
.FailedList
= FailedCpuList
;
656 mMpSystemData
.FailedListIndex
= 0;
657 APInitialState
= CpuStateReady
;
659 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
660 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
661 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
668 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
670 // Skip Disabled processors
676 // Get APs prepared, and put failing APs into FailedCpuList
677 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
678 // state 1 by 1, until the previous 1 finished its task
679 // if not "SingleThread", all APs are put to ready state from the beginning
681 if (GetApState (CpuData
) == CpuStateIdle
) {
682 mMpSystemData
.StartCount
++;
684 SetApState (CpuData
, APInitialState
);
686 if (APInitialState
== CpuStateReady
) {
687 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
691 APInitialState
= CpuStateBlocked
;
696 mStopCheckAllAPsStatus
= FALSE
;
698 if (WaitEvent
!= NULL
) {
706 CheckAndUpdateAllAPsToIdleState ();
707 if (mMpSystemData
.FinishCount
== mMpSystemData
.StartCount
) {
708 Status
= EFI_SUCCESS
;
715 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
717 Status
= EFI_TIMEOUT
;
721 gBS
->Stall (gPollInterval
);
722 mMpSystemData
.Timeout
-= gPollInterval
;
731 This service lets the caller get one enabled AP to execute a caller-provided
732 function. The caller can request the BSP to either wait for the completion
733 of the AP or just proceed with the next task by using the EFI event mechanism.
734 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
735 execution support. This service may only be called from the BSP.
737 This function is used to dispatch one enabled AP to the function specified by
738 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
739 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
740 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
741 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
742 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
743 then EFI_UNSUPPORTED must be returned.
745 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
746 from Procedure, then execution of Procedure by the AP is terminated. The AP is
747 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
748 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
750 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
752 @param[in] Procedure A pointer to the function to be run on
753 enabled APs of the system. See type
755 @param[in] ProcessorNumber The handle number of the AP. The range is
756 from 0 to the total number of logical
757 processors minus 1. The total number of
758 logical processors can be retrieved by
759 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
760 @param[in] WaitEvent The event created by the caller with CreateEvent()
761 service. If it is NULL, then execute in
762 blocking mode. BSP waits until all APs finish
763 or TimeoutInMicroseconds expires. If it's
764 not NULL, then execute in non-blocking mode.
765 BSP requests the function specified by
766 Procedure to be started on all the enabled
767 APs, and go on executing immediately. If
768 all return from Procedure or TimeoutInMicroseconds
769 expires, this event is signaled. The BSP
770 can use the CheckEvent() or WaitForEvent()
771 services to check the state of event. Type
772 EFI_EVENT is defined in CreateEvent() in
773 the Unified Extensible Firmware Interface
775 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
776 APs to return from Procedure, either for
777 blocking or non-blocking mode. Zero means
778 infinity. If the timeout expires before
779 all APs return from Procedure, then Procedure
780 on the failed APs is terminated. All enabled
781 APs are available for next function assigned
782 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
783 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
784 If the timeout expires in blocking mode,
785 BSP returns EFI_TIMEOUT. If the timeout
786 expires in non-blocking mode, WaitEvent
787 is signaled with SignalEvent().
788 @param[in] ProcedureArgument The parameter passed into Procedure for
790 @param[out] Finished If NULL, this parameter is ignored. In
791 blocking mode, this parameter is ignored.
792 In non-blocking mode, if AP returns from
793 Procedure before the timeout expires, its
794 content is set to TRUE. Otherwise, the
795 value is set to FALSE. The caller can
796 determine if the AP returned from Procedure
797 by evaluating this value.
799 @retval EFI_SUCCESS In blocking mode, specified AP finished before
801 @retval EFI_SUCCESS In non-blocking mode, the function has been
802 dispatched to specified AP.
803 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
804 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
806 @retval EFI_DEVICE_ERROR The calling processor is an AP.
807 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
808 the specified AP has finished.
809 @retval EFI_NOT_READY The specified AP is busy.
810 @retval EFI_NOT_FOUND The processor with the handle specified by
811 ProcessorNumber does not exist.
812 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
813 @retval EFI_INVALID_PARAMETER Procedure is NULL.
819 IN EFI_MP_SERVICES_PROTOCOL
*This
,
820 IN EFI_AP_PROCEDURE Procedure
,
821 IN UINTN ProcessorNumber
,
822 IN EFI_EVENT WaitEvent OPTIONAL
,
823 IN UINTN TimeoutInMicroseconds
,
824 IN VOID
*ProcedureArgument OPTIONAL
,
825 OUT BOOLEAN
*Finished OPTIONAL
828 CPU_DATA_BLOCK
*CpuData
;
832 if (Finished
!= NULL
) {
837 return EFI_DEVICE_ERROR
;
840 if (Procedure
== NULL
) {
841 return EFI_INVALID_PARAMETER
;
844 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
845 return EFI_NOT_FOUND
;
848 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
849 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
) ||
850 !TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
851 return EFI_INVALID_PARAMETER
;
854 if (GetApState (CpuData
) != CpuStateIdle
) {
855 return EFI_NOT_READY
;
859 // temporarily stop checkAllAPsStatus for initialize parameters.
861 mStopCheckAllAPsStatus
= TRUE
;
863 SetApState (CpuData
, CpuStateReady
);
865 SetApProcedure (CpuData
, Procedure
, ProcedureArgument
);
867 CpuData
->Timeout
= TimeoutInMicroseconds
;
868 CpuData
->WaitEvent
= WaitEvent
;
869 CpuData
->TimeoutActive
= !!(TimeoutInMicroseconds
);
870 CpuData
->Finished
= Finished
;
872 mStopCheckAllAPsStatus
= FALSE
;
874 if (WaitEvent
!= NULL
) {
885 if (GetApState (CpuData
) == CpuStateFinished
) {
886 SetApState (CpuData
, CpuStateIdle
);
890 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
891 ResetProcessorToIdleState (CpuData
);
895 gBS
->Stall (gPollInterval
);
896 CpuData
->Timeout
-= gPollInterval
;
903 This service switches the requested AP to be the BSP from that point onward.
904 This service changes the BSP for all purposes. This call can only be performed
907 This service switches the requested AP to be the BSP from that point onward.
908 This service changes the BSP for all purposes. The new BSP can take over the
909 execution of the old BSP and continue seamlessly from where the old one left
910 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
913 If the BSP cannot be switched prior to the return from this service, then
914 EFI_UNSUPPORTED must be returned.
916 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
917 @param[in] ProcessorNumber The handle number of AP that is to become the new
918 BSP. The range is from 0 to the total number of
919 logical processors minus 1. The total number of
920 logical processors can be retrieved by
921 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
922 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
923 enabled AP. Otherwise, it will be disabled.
925 @retval EFI_SUCCESS BSP successfully switched.
926 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
927 this service returning.
928 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
929 @retval EFI_SUCCESS The calling processor is an AP.
930 @retval EFI_NOT_FOUND The processor with the handle specified by
931 ProcessorNumber does not exist.
932 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
934 @retval EFI_NOT_READY The specified AP is busy.
940 IN EFI_MP_SERVICES_PROTOCOL
*This
,
941 IN UINTN ProcessorNumber
,
942 IN BOOLEAN EnableOldBSP
946 // Current always return unsupported.
948 return EFI_UNSUPPORTED
;
952 This service lets the caller enable or disable an AP from this point onward.
953 This service may only be called from the BSP.
955 This service allows the caller enable or disable an AP from this point onward.
956 The caller can optionally specify the health status of the AP by Health. If
957 an AP is being disabled, then the state of the disabled AP is implementation
958 dependent. If an AP is enabled, then the implementation must guarantee that a
959 complete initialization sequence is performed on the AP, so the AP is in a state
960 that is compatible with an MP operating system. This service may not be supported
961 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
963 If the enable or disable AP operation cannot be completed prior to the return
964 from this service, then EFI_UNSUPPORTED must be returned.
966 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
967 @param[in] ProcessorNumber The handle number of AP that is to become the new
968 BSP. The range is from 0 to the total number of
969 logical processors minus 1. The total number of
970 logical processors can be retrieved by
971 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
972 @param[in] EnableAP Specifies the new state for the processor for
973 enabled, FALSE for disabled.
974 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
975 the new health status of the AP. This flag
976 corresponds to StatusFlag defined in
977 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
978 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
979 bits are ignored. If it is NULL, this parameter
982 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
983 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
984 prior to this service returning.
985 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
986 @retval EFI_DEVICE_ERROR The calling processor is an AP.
987 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
989 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
995 IN EFI_MP_SERVICES_PROTOCOL
*This
,
996 IN UINTN ProcessorNumber
,
998 IN UINT32
*HealthFlag OPTIONAL
1001 CPU_DATA_BLOCK
*CpuData
;
1004 return EFI_DEVICE_ERROR
;
1007 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
1008 return EFI_NOT_FOUND
;
1011 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1012 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1013 return EFI_INVALID_PARAMETER
;
1016 if (GetApState (CpuData
) != CpuStateIdle
) {
1017 return EFI_UNSUPPORTED
;
1021 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
1022 mMpSystemData
.NumberOfEnabledProcessors
++;
1024 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
1026 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1027 mMpSystemData
.NumberOfEnabledProcessors
--;
1029 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
1032 if (HealthFlag
!= NULL
) {
1033 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
1034 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
1041 This return the handle number for the calling processor. This service may be
1042 called from the BSP and APs.
1044 This service returns the processor handle number for the calling processor.
1045 The returned value is in the range from 0 to the total number of logical
1046 processors minus 1. The total number of logical processors can be retrieved
1047 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1048 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1049 is returned. Otherwise, the current processors handle number is returned in
1050 ProcessorNumber, and EFI_SUCCESS is returned.
1052 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1053 @param[out] ProcessorNumber The handle number of AP that is to become the new
1054 BSP. The range is from 0 to the total number of
1055 logical processors minus 1. The total number of
1056 logical processors can be retrieved by
1057 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1059 @retval EFI_SUCCESS The current processor handle number was returned
1061 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1067 IN EFI_MP_SERVICES_PROTOCOL
*This
,
1068 OUT UINTN
*ProcessorNumber
1074 if (ProcessorNumber
== NULL
) {
1075 return EFI_INVALID_PARAMETER
;
1078 ProcessorId
= GetApicId ();
1079 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
1080 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
1085 *ProcessorNumber
= Index
;
1090 Terminate AP's task and set it to idle state.
1092 This function terminates AP's task due to timeout by sending INIT-SIPI,
1093 and sends it to idle state.
1095 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1099 ResetProcessorToIdleState (
1100 IN CPU_DATA_BLOCK
*CpuData
1106 Application Processors do loop routine
1107 after switch to its own stack.
1109 @param Context1 A pointer to the context to pass into the function.
1110 @param Context2 A pointer to the context to pass into the function.
1114 ProcessorToIdleState (
1115 IN VOID
*Context1
, OPTIONAL
1116 IN VOID
*Context2 OPTIONAL
1119 UINTN ProcessorNumber
;
1120 CPU_DATA_BLOCK
*CpuData
;
1121 EFI_AP_PROCEDURE Procedure
;
1122 VOID
*ProcedureArgument
;
1124 WhoAmI (&mMpServicesTemplate
, &ProcessorNumber
);
1125 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1127 AsmApDoneWithCommonStack ();
1130 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
1134 ProcedureArgument
= CpuData
->Parameter
;
1135 Procedure
= CpuData
->Procedure
;
1136 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1138 if (Procedure
!= NULL
) {
1139 Procedure (ProcedureArgument
);
1141 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
1145 CpuData
->Procedure
= NULL
;
1146 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1148 SetApState (CpuData
, CpuStateFinished
);
1159 Checks AP' status periodically.
1161 This function is triggerred by timer perodically to check the
1162 state of AP forStartupThisAP() executed in non-blocking mode.
1164 @param Event Event triggered.
1165 @param Context Parameter passed with the event.
1175 CPU_DATA_BLOCK
*CpuData
;
1178 CpuData
= (CPU_DATA_BLOCK
*) Context
;
1179 if (CpuData
->TimeoutActive
) {
1180 CpuData
->Timeout
-= gPollInterval
;
1183 CpuState
= GetApState (CpuData
);
1185 if (CpuState
== CpuStateFinished
) {
1186 if (CpuData
->Finished
) {
1187 *CpuData
->Finished
= TRUE
;
1189 SetApState (CpuData
, CpuStateIdle
);
1193 if (CpuData
->TimeoutActive
&& CpuData
->Timeout
< 0) {
1194 if (CpuState
!= CpuStateIdle
&&
1195 CpuData
->Finished
) {
1196 *CpuData
->Finished
= FALSE
;
1198 ResetProcessorToIdleState (CpuData
);
1205 CpuData
->TimeoutActive
= FALSE
;
1206 gBS
->SignalEvent (CpuData
->WaitEvent
);
1207 CpuData
->WaitEvent
= NULL
;
1211 Checks APs' status periodically.
1213 This function is triggerred by timer perodically to check the
1214 state of APs for StartupAllAPs() executed in non-blocking mode.
1216 @param Event Event triggered.
1217 @param Context Parameter passed with the event.
1227 CPU_DATA_BLOCK
*CpuData
;
1230 if (mMpSystemData
.TimeoutActive
) {
1231 mMpSystemData
.Timeout
-= gPollInterval
;
1234 if (mStopCheckAllAPsStatus
) {
1238 if (mMpSystemData
.WaitEvent
!= NULL
) {
1239 CheckAndUpdateAllAPsToIdleState ();
1243 if (mMpSystemData
.TimeoutActive
&& mMpSystemData
.Timeout
< 0) {
1244 ResetAllFailedAPs();
1248 mMpSystemData
.FinishCount
= mMpSystemData
.StartCount
;
1251 if (mMpSystemData
.FinishCount
!= mMpSystemData
.StartCount
) {
1255 mMpSystemData
.TimeoutActive
= FALSE
;
1256 gBS
->SignalEvent (mMpSystemData
.WaitEvent
);
1257 mMpSystemData
.WaitEvent
= NULL
;
1258 mStopCheckAllAPsStatus
= TRUE
;
1262 // check each AP status for StartupThisAP
1264 for (Number
= 0; Number
< mMpSystemData
.NumberOfProcessors
; Number
++) {
1265 CpuData
= &mMpSystemData
.CpuDatas
[Number
];
1266 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
1273 if (!TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
1275 // Skip Disabled processors
1280 if (CpuData
->WaitEvent
) {
1281 CheckThisAPStatus (NULL
, (VOID
*)CpuData
);
1287 Application Processor C code entry point.
1298 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
1299 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
1300 mApStackStart
= TopOfApStack
;
1302 mMpSystemData
.NumberOfProcessors
++;
1305 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
1312 This function is called by all processors (both BSP and AP) once and collects MP related data.
1314 @param Bsp TRUE if the CPU is BSP
1315 @param ProcessorNumber The specific processor number
1317 @retval EFI_SUCCESS Data for the processor collected and filled in
1321 FillInProcessorInformation (
1323 IN UINTN ProcessorNumber
1326 CPU_DATA_BLOCK
*CpuData
;
1329 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
1330 ProcessorId
= GetApicId ();
1331 CpuData
->Info
.ProcessorId
= ProcessorId
;
1332 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
1334 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1336 CpuData
->Info
.Location
.Package
= ProcessorId
;
1337 CpuData
->Info
.Location
.Core
= 0;
1338 CpuData
->Info
.Location
.Thread
= 0;
1339 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
1341 CpuData
->Procedure
= NULL
;
1342 CpuData
->Parameter
= NULL
;
1343 InitializeSpinLock (&CpuData
->CpuDataLock
);
1349 Prepare the System Data.
1351 @retval EFI_SUCCESS the System Data finished initilization.
1361 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
1363 mMpSystemData
.NumberOfProcessors
= 1;
1364 mMpSystemData
.NumberOfEnabledProcessors
= 1;
1366 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
1367 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
1369 Status
= gBS
->CreateEvent (
1370 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1374 &mMpSystemData
.CheckAllAPsEvent
1376 ASSERT_EFI_ERROR (Status
);
1379 // Set timer to check all APs status.
1381 Status
= gBS
->SetTimer (
1382 mMpSystemData
.CheckAllAPsEvent
,
1384 EFI_TIMER_PERIOD_MICROSECONDS (100)
1386 ASSERT_EFI_ERROR (Status
);
1391 FillInProcessorInformation (TRUE
, 0);
1397 Initialize Multi-processor support.
1401 InitializeMpSupport (
1405 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1406 if (gMaxLogicalProcessorNumber
< 1) {
1407 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1411 if (gMaxLogicalProcessorNumber
== 1) {
1415 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
1416 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
1418 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1419 ASSERT (mApStackStart
!= NULL
);
1422 // the first buffer of stack size used for common stack, when the amount of AP
1423 // more than 1, we should never free the common stack which maybe used for AP reset.
1425 mCommonStack
= mApStackStart
;
1426 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
1427 mApStackStart
= mTopOfApCommonStack
;
1429 InitMpSystemData ();
1431 if (mMpSystemData
.NumberOfProcessors
== 1) {
1432 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
1436 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
1437 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
1438 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *