2 Produces PI MP Services Protocol on top of Framework MP Services Protocol.
4 Intel's Framework MP Services Protocol is replaced by EFI_MP_SERVICES_PROTOCOL in PI 1.1.
5 This module produces PI MP Services Protocol on top of Framework MP Services Protocol.
7 Copyright (c) 2009 Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "MpServicesOnFrameworkMpServicesThunk.h"
21 EFI_HANDLE mHandle
= NULL
;
22 MP_SYSTEM_DATA mMPSystemData
;
23 EFI_PHYSICAL_ADDRESS mStartupVector
;
24 MP_CPU_EXCHANGE_INFO
*mExchangeInfo
;
25 VOID
*mStackStartAddress
;
26 BOOLEAN mStopCheckAPsStatus
= FALSE
;
27 UINTN mNumberOfProcessors
;
28 EFI_GENERIC_MEMORY_TEST_PROTOCOL
*mGenMemoryTest
;
30 FRAMEWORK_EFI_MP_SERVICES_PROTOCOL
*mFrameworkMpService
;
31 EFI_MP_SERVICES_PROTOCOL mMpService
= {
32 GetNumberOfProcessors
,
43 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
45 This service retrieves the number of logical processor in the platform
46 and the number of those logical processors that are enabled on this boot.
47 This service may only be called from the BSP.
49 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
50 @param NumberOfProcessors Pointer to the total number of logical processors in the system,
51 including the BSP and disabled APs.
52 @param NumberOfEnabledProcessors Pointer to the number of enabled logical processors that exist
53 in system, including the BSP.
55 @retval EFI_SUCCESS Number of logical processors and enabled logical processors retrieved..
56 @retval EFI_DEVICE_ERROR Caller processor is AP.
57 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL
58 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL
63 GetNumberOfProcessors (
64 IN EFI_MP_SERVICES_PROTOCOL
*This
,
65 OUT UINTN
*NumberOfProcessors
,
66 OUT UINTN
*NumberOfEnabledProcessors
73 // Check whether caller processor is BSP
75 WhoAmI (This
, &CallerNumber
);
76 if (CallerNumber
!= GetBspNumber ()) {
77 return EFI_DEVICE_ERROR
;
81 // Check parameter NumberOfProcessors
83 if (NumberOfProcessors
== NULL
) {
84 return EFI_INVALID_PARAMETER
;
88 // Check parameter NumberOfEnabledProcessors
90 if (NumberOfEnabledProcessors
== NULL
) {
91 return EFI_INVALID_PARAMETER
;
94 Status
= mFrameworkMpService
->GetGeneralMPInfo (
98 NumberOfEnabledProcessors
,
102 ASSERT_EFI_ERROR (Status
);
108 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
110 Gets detailed MP-related information on the requested processor at the
111 instant this call is made. This service may only be called from the BSP.
113 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
114 @param ProcessorNumber The handle number of processor.
115 @param ProcessorInfoBuffer A pointer to the buffer where information for the requested processor is deposited.
117 @retval EFI_SUCCESS Processor information successfully returned.
118 @retval EFI_DEVICE_ERROR Caller processor is AP.
119 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL
120 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
126 IN EFI_MP_SERVICES_PROTOCOL
*This
,
127 IN UINTN ProcessorNumber
,
128 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
134 EFI_MP_PROC_CONTEXT ProcessorContextBuffer
;
137 // Check whether caller processor is BSP
139 WhoAmI (This
, &CallerNumber
);
140 if (CallerNumber
!= GetBspNumber ()) {
141 return EFI_DEVICE_ERROR
;
145 // Check parameter ProcessorInfoBuffer
147 if (ProcessorInfoBuffer
== NULL
) {
148 return EFI_INVALID_PARAMETER
;
152 // Check whether processor with the handle specified by ProcessorNumber exists
154 if (ProcessorNumber
>= mNumberOfProcessors
) {
155 return EFI_NOT_FOUND
;
158 BufferSize
= sizeof (EFI_MP_PROC_CONTEXT
);
159 Status
= mFrameworkMpService
->GetProcessorContext (
163 &ProcessorContextBuffer
165 ASSERT_EFI_ERROR (Status
);
167 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) ProcessorContextBuffer
.ApicID
;
170 // Get Status Flag of specified processor
172 ProcessorInfoBuffer
->StatusFlag
= 0;
174 if (ProcessorContextBuffer
.Enabled
) {
175 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
178 if (ProcessorContextBuffer
.Designation
== EfiCpuBSP
) {
179 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
182 if (ProcessorContextBuffer
.Health
.Flags
.Uint32
== 0) {
183 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
186 ProcessorInfoBuffer
->Location
.Package
= (UINT32
) ProcessorContextBuffer
.PackageNumber
;
187 ProcessorInfoBuffer
->Location
.Core
= (UINT32
) ProcessorContextBuffer
.NumberOfCores
;
188 ProcessorInfoBuffer
->Location
.Thread
= (UINT32
) ProcessorContextBuffer
.NumberOfThreads
;
194 Implementation of StartupAllAPs() service of MP Services Protocol.
196 This service lets the caller get all enabled APs to execute a caller-provided function.
197 This service may only be called from the BSP.
199 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
200 @param Procedure A pointer to the function to be run on enabled APs of the system.
201 @param SingleThread Indicates whether to execute the function simultaneously or one by one..
202 @param WaitEvent The event created by the caller.
203 If it is NULL, then execute in blocking mode.
204 If it is not NULL, then execute in non-blocking mode.
205 @param TimeoutInMicroSeconds The time limit in microseconds for this AP to finish the function.
207 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
208 @param FailedCpuList The list of processor numbers that fail to finish the function before
209 TimeoutInMicrosecsond expires.
211 @retval EFI_SUCCESS In blocking mode, all APs have finished before the timeout expired.
212 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to all enabled APs.
213 @retval EFI_DEVICE_ERROR Caller processor is AP.
214 @retval EFI_NOT_STARTED No enabled AP exists in the system.
215 @retval EFI_NOT_READY Any enabled AP is busy.
216 @retval EFI_TIMEOUT In blocking mode, The timeout expired before all enabled APs have finished.
217 @retval EFI_INVALID_PARAMETER Procedure is NULL.
223 IN EFI_MP_SERVICES_PROTOCOL
*This
,
224 IN EFI_AP_PROCEDURE Procedure
,
225 IN BOOLEAN SingleThread
,
226 IN EFI_EVENT WaitEvent OPTIONAL
,
227 IN UINTN TimeoutInMicroSeconds
,
228 IN VOID
*ProcedureArgument OPTIONAL
,
229 OUT UINTN
**FailedCpuList OPTIONAL
233 UINTN ProcessorNumber
;
234 CPU_DATA_BLOCK
*CpuData
;
238 if (FailedCpuList
!= NULL
) {
239 *FailedCpuList
= NULL
;
243 // Check whether caller processor is BSP
245 BspNumber
= GetBspNumber ();
246 WhoAmI (This
, &ProcessorNumber
);
247 if (ProcessorNumber
!= BspNumber
) {
248 return EFI_DEVICE_ERROR
;
252 // Check parameter Procedure
254 if (Procedure
== NULL
) {
255 return EFI_INVALID_PARAMETER
;
259 // Temporarily suppress CheckAPsStatus()
261 mStopCheckAPsStatus
= TRUE
;
264 // Check whether all enabled APs are idle.
265 // If any enabled AP is not idle, return EFI_NOT_READY.
267 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
269 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
271 mMPSystemData
.CpuList
[ProcessorNumber
] = FALSE
;
272 if (ProcessorNumber
!= BspNumber
) {
273 if (CpuData
->State
!= CpuStateDisabled
) {
274 if (CpuData
->State
!= CpuStateIdle
) {
275 mStopCheckAPsStatus
= FALSE
;
276 return EFI_NOT_READY
;
279 // Mark this processor as responsible for current calling.
281 mMPSystemData
.CpuList
[ProcessorNumber
] = TRUE
;
287 mMPSystemData
.FinishCount
= 0;
288 mMPSystemData
.StartCount
= 0;
291 // Go through all enabled APs to wakeup them for Procedure.
292 // If in Single Thread mode, then only one AP is woken up, and others are waiting.
294 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
296 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
298 // Check whether this processor is responsible for current calling.
300 if (mMPSystemData
.CpuList
[ProcessorNumber
]) {
302 mMPSystemData
.StartCount
++;
304 AcquireSpinLock (&CpuData
->CpuDataLock
);
305 CpuData
->State
= CpuStateReady
;
306 ReleaseSpinLock (&CpuData
->CpuDataLock
);
323 // If no enabled AP exists, return EFI_NOT_STARTED.
325 if (mMPSystemData
.StartCount
== 0) {
326 mStopCheckAPsStatus
= FALSE
;
327 return EFI_NOT_STARTED
;
331 // If WaitEvent is not NULL, execute in non-blocking mode.
332 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
333 // CheckAPsStatus() will check completion and timeout periodically.
335 mMPSystemData
.Procedure
= Procedure
;
336 mMPSystemData
.ProcArguments
= ProcedureArgument
;
337 mMPSystemData
.SingleThread
= SingleThread
;
338 mMPSystemData
.FailedCpuList
= FailedCpuList
;
339 mMPSystemData
.ExpectedTime
= CalculateTimeout (TimeoutInMicroSeconds
, &mMPSystemData
.CurrentTime
);
340 mMPSystemData
.WaitEvent
= WaitEvent
;
343 // Allow CheckAPsStatus()
345 mStopCheckAPsStatus
= FALSE
;
347 if (WaitEvent
!= NULL
) {
352 // If WaitEvent is NULL, execute in blocking mode.
353 // BSP checks APs'state until all APs finish or TimeoutInMicrosecsond expires.
356 Status
= CheckAllAPs ();
357 } while (Status
== EFI_NOT_READY
);
363 Implementation of StartupThisAP() service of MP Services Protocol.
365 This service lets the caller get one enabled AP to execute a caller-provided function.
366 This service may only be called from the BSP.
368 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
369 @param Procedure A pointer to the function to be run on the designated AP.
370 @param ProcessorNumber The handle number of AP..
371 @param WaitEvent The event created by the caller.
372 If it is NULL, then execute in blocking mode.
373 If it is not NULL, then execute in non-blocking mode.
374 @param TimeoutInMicroseconds The time limit in microseconds for this AP to finish the function.
376 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
377 @param Finished Indicates whether AP has finished assigned function.
378 In blocking mode, it is ignored.
380 @retval EFI_SUCCESS In blocking mode, specified AP has finished before the timeout expires.
381 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to specified AP.
382 @retval EFI_DEVICE_ERROR Caller processor is AP.
383 @retval EFI_TIMEOUT In blocking mode, the timeout expires before specified AP has finished.
384 @retval EFI_NOT_READY Specified AP is busy.
385 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
386 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
387 @retval EFI_INVALID_PARAMETER Procedure is NULL.
393 IN EFI_MP_SERVICES_PROTOCOL
*This
,
394 IN EFI_AP_PROCEDURE Procedure
,
395 IN UINTN ProcessorNumber
,
396 IN EFI_EVENT WaitEvent OPTIONAL
,
397 IN UINTN TimeoutInMicroseconds
,
398 IN VOID
*ProcedureArgument OPTIONAL
,
399 OUT BOOLEAN
*Finished OPTIONAL
402 CPU_DATA_BLOCK
*CpuData
;
407 if (Finished
!= NULL
) {
412 // Check whether caller processor is BSP
414 BspNumber
= GetBspNumber ();
415 WhoAmI (This
, &CallerNumber
);
416 if (CallerNumber
!= BspNumber
) {
417 return EFI_DEVICE_ERROR
;
421 // Check whether processor with the handle specified by ProcessorNumber exists
423 if (ProcessorNumber
>= mNumberOfProcessors
) {
424 return EFI_NOT_FOUND
;
428 // Check whether specified processor is BSP
430 if (ProcessorNumber
== BspNumber
) {
431 return EFI_INVALID_PARAMETER
;
435 // Check parameter Procedure
437 if (Procedure
== NULL
) {
438 return EFI_INVALID_PARAMETER
;
441 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
444 // Temporarily suppress CheckAPsStatus()
446 mStopCheckAPsStatus
= TRUE
;
449 // Check whether specified AP is disabled
451 if (CpuData
->State
== CpuStateDisabled
) {
452 mStopCheckAPsStatus
= FALSE
;
453 return EFI_INVALID_PARAMETER
;
457 // Check whether specified AP is busy
459 if (CpuData
->State
!= CpuStateIdle
) {
460 mStopCheckAPsStatus
= FALSE
;
461 return EFI_NOT_READY
;
465 // Wakeup specified AP for Procedure.
467 AcquireSpinLock (&CpuData
->CpuDataLock
);
468 CpuData
->State
= CpuStateReady
;
469 ReleaseSpinLock (&CpuData
->CpuDataLock
);
478 // If WaitEvent is not NULL, execute in non-blocking mode.
479 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
480 // CheckAPsStatus() will check completion and timeout periodically.
482 CpuData
->WaitEvent
= WaitEvent
;
483 CpuData
->Finished
= Finished
;
484 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
487 // Allow CheckAPsStatus()
489 mStopCheckAPsStatus
= FALSE
;
491 if (WaitEvent
!= NULL
) {
496 // If WaitEvent is NULL, execute in blocking mode.
497 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
500 Status
= CheckThisAP (ProcessorNumber
);
501 } while (Status
== EFI_NOT_READY
);
507 Implementation of SwitchBSP() service of MP Services Protocol.
509 This service switches the requested AP to be the BSP from that point onward.
510 This service may only be called from the current BSP.
512 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
513 @param ProcessorNumber The handle number of processor.
514 @param EnableOldBSP Whether to enable or disable the original BSP.
516 @retval EFI_SUCCESS BSP successfully switched.
517 @retval EFI_DEVICE_ERROR Caller processor is AP.
518 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
519 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
520 @retval EFI_NOT_READY Specified AP is busy.
526 IN EFI_MP_SERVICES_PROTOCOL
*This
,
527 IN UINTN ProcessorNumber
,
528 IN BOOLEAN EnableOldBSP
532 CPU_DATA_BLOCK
*CpuData
;
537 // Check whether caller processor is BSP
539 BspNumber
= GetBspNumber ();
540 WhoAmI (This
, &CallerNumber
);
541 if (CallerNumber
!= BspNumber
) {
542 return EFI_DEVICE_ERROR
;
546 // Check whether processor with the handle specified by ProcessorNumber exists
548 if (ProcessorNumber
>= mNumberOfProcessors
) {
549 return EFI_NOT_FOUND
;
553 // Check whether specified processor is BSP
555 if (ProcessorNumber
== BspNumber
) {
556 return EFI_INVALID_PARAMETER
;
559 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
562 // Check whether specified AP is disabled
564 if (CpuData
->State
== CpuStateDisabled
) {
565 return EFI_INVALID_PARAMETER
;
569 // Check whether specified AP is busy
571 if (CpuData
->State
!= CpuStateIdle
) {
572 return EFI_NOT_READY
;
575 Status
= mFrameworkMpService
->SwitchBSP (
580 ASSERT_EFI_ERROR (Status
);
582 ChangeCpuState (BspNumber
, EnableOldBSP
);
588 Implementation of EnableDisableAP() service of MP Services Protocol.
590 This service lets the caller enable or disable an AP.
591 This service may only be called from the BSP.
593 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
594 @param ProcessorNumber The handle number of processor.
595 @param EnableAP Indicates whether the newstate of the AP is enabled or disabled.
596 @param HealthFlag Indicates new health state of the AP..
598 @retval EFI_SUCCESS AP successfully enabled or disabled.
599 @retval EFI_DEVICE_ERROR Caller processor is AP.
600 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
601 @retval EFI_INVALID_PARAMETERS ProcessorNumber specifies the BSP.
607 IN EFI_MP_SERVICES_PROTOCOL
*This
,
608 IN UINTN ProcessorNumber
,
610 IN UINT32
*HealthFlag OPTIONAL
615 EFI_MP_HEALTH HealthState
;
616 EFI_MP_HEALTH
*HealthStatePointer
;
620 // Check whether caller processor is BSP
622 BspNumber
= GetBspNumber ();
623 WhoAmI (This
, &CallerNumber
);
624 if (CallerNumber
!= BspNumber
) {
625 return EFI_DEVICE_ERROR
;
629 // Check whether processor with the handle specified by ProcessorNumber exists
631 if (ProcessorNumber
>= mNumberOfProcessors
) {
632 return EFI_NOT_FOUND
;
636 // Check whether specified processor is BSP
638 if (ProcessorNumber
== BspNumber
) {
639 return EFI_INVALID_PARAMETER
;
642 if (HealthFlag
== NULL
) {
643 HealthStatePointer
= NULL
;
645 if ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) == 0) {
646 HealthState
.Flags
.Uint32
= 1;
648 HealthState
.Flags
.Uint32
= 0;
650 HealthState
.TestStatus
= 0;
652 HealthStatePointer
= &HealthState
;
655 Status
= mFrameworkMpService
->EnableDisableAP (
661 ASSERT_EFI_ERROR (Status
);
663 ChangeCpuState (ProcessorNumber
, EnableAP
);
669 Implementation of WhoAmI() service of MP Services Protocol.
671 This service lets the caller processor get its handle number.
672 This service may be called from the BSP and APs.
674 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
675 @param ProcessorNumber Pointer to the handle number of AP.
677 @retval EFI_SUCCESS Processor number successfully returned.
678 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL
684 IN EFI_MP_SERVICES_PROTOCOL
*This
,
685 OUT UINTN
*ProcessorNumber
690 if (ProcessorNumber
== NULL
) {
691 return EFI_INVALID_PARAMETER
;
694 Status
= mFrameworkMpService
->WhoAmI (
698 ASSERT_EFI_ERROR (Status
);
704 Checks APs' status periodically.
706 This function is triggerred by timer perodically to check the
707 state of APs for StartupAllAPs() and StartupThisAP() executed
708 in non-blocking mode.
710 @param Event Event triggered.
711 @param Context Parameter passed with the event.
721 UINTN ProcessorNumber
;
722 CPU_DATA_BLOCK
*CpuData
;
726 // If CheckAPsStatus() is stopped, then return immediately.
728 if (mStopCheckAPsStatus
) {
733 // First, check whether pending StartupAllAPs() exists.
735 if (mMPSystemData
.WaitEvent
!= NULL
) {
737 Status
= CheckAllAPs ();
739 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it..
741 if (Status
!= EFI_NOT_READY
) {
742 Status
= gBS
->SignalEvent (mMPSystemData
.WaitEvent
);
743 mMPSystemData
.WaitEvent
= NULL
;
748 // Second, check whether pending StartupThisAPs() callings exist.
750 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
752 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
754 if (CpuData
->WaitEvent
== NULL
) {
758 Status
= CheckThisAP (ProcessorNumber
);
760 if (Status
!= EFI_NOT_READY
) {
761 gBS
->SignalEvent (CpuData
->WaitEvent
);
762 CpuData
->WaitEvent
= NULL
;
769 Checks status of all APs.
771 This function checks whether all APs have finished task assigned by StartupAllAPs(),
772 and whether timeout expires.
774 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
775 @retval EFI_TIMEOUT The timeout expires.
776 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
784 UINTN ProcessorNumber
;
785 UINTN NextProcessorNumber
;
789 CPU_DATA_BLOCK
*CpuData
;
791 NextProcessorNumber
= 0;
794 // Go through all APs that are responsible for the StartupAllAPs().
796 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
797 if (!mMPSystemData
.CpuList
[ProcessorNumber
]) {
801 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
804 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
805 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
806 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
808 AcquireSpinLock (&CpuData
->CpuDataLock
);
809 CpuState
= CpuData
->State
;
810 ReleaseSpinLock (&CpuData
->CpuDataLock
);
812 if (CpuState
== CpuStateFinished
) {
813 mMPSystemData
.FinishCount
++;
814 mMPSystemData
.CpuList
[ProcessorNumber
] = FALSE
;
816 AcquireSpinLock (&CpuData
->CpuDataLock
);
817 CpuData
->State
= CpuStateIdle
;
818 ReleaseSpinLock (&CpuData
->CpuDataLock
);
821 // If in Single Thread mode, then search for the next waiting AP for execution.
823 if (mMPSystemData
.SingleThread
) {
824 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
826 if (!EFI_ERROR (Status
)) {
829 mMPSystemData
.Procedure
,
830 mMPSystemData
.ProcArguments
838 // If all APs finish, return EFI_SUCCESS.
840 if (mMPSystemData
.FinishCount
== mMPSystemData
.StartCount
) {
845 // If timeout expires, report timeout.
847 if (CheckTimeout (&mMPSystemData
.CurrentTime
, &mMPSystemData
.TotalTime
, mMPSystemData
.ExpectedTime
)) {
849 // If FailedCpuList is not NULL, record all failed APs in it.
851 if (mMPSystemData
.FailedCpuList
!= NULL
) {
852 *mMPSystemData
.FailedCpuList
= AllocatePool ((mMPSystemData
.StartCount
- mMPSystemData
.FinishCount
+ 1) * sizeof(UINTN
));
853 ASSERT (*mMPSystemData
.FailedCpuList
!= NULL
);
857 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
859 // Check whether this processor is responsible for StartupAllAPs().
861 if (mMPSystemData
.CpuList
[ProcessorNumber
]) {
863 // Reset failed APs to idle state
865 ResetProcessorToIdleState (ProcessorNumber
);
866 mMPSystemData
.CpuList
[ProcessorNumber
] = FALSE
;
867 if (mMPSystemData
.FailedCpuList
!= NULL
) {
868 (*mMPSystemData
.FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
872 if (mMPSystemData
.FailedCpuList
!= NULL
) {
873 (*mMPSystemData
.FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
877 return EFI_NOT_READY
;
881 Checks status of specified AP.
883 This function checks whether specified AP has finished task assigned by StartupThisAP(),
884 and whether timeout expires.
886 @param ProcessorNumber The handle number of processor.
888 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
889 @retval EFI_TIMEOUT The timeout expires.
890 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
895 UINTN ProcessorNumber
898 CPU_DATA_BLOCK
*CpuData
;
901 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
904 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
905 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
906 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
908 AcquireSpinLock (&CpuData
->CpuDataLock
);
909 CpuState
= CpuData
->State
;
910 ReleaseSpinLock (&CpuData
->CpuDataLock
);
913 // If the APs finishes for StartupThisAP(), return EFI_SUCCESS.
915 if (CpuState
== CpuStateFinished
) {
917 AcquireSpinLock (&CpuData
->CpuDataLock
);
918 CpuData
->State
= CpuStateIdle
;
919 ReleaseSpinLock (&CpuData
->CpuDataLock
);
921 if (CpuData
->Finished
!= NULL
) {
922 *(CpuData
->Finished
) = TRUE
;
927 // If timeout expires for StartupThisAP(), report timeout.
929 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
931 if (CpuData
->Finished
!= NULL
) {
932 *(CpuData
->Finished
) = FALSE
;
935 // Reset failed AP to idle state
937 ResetProcessorToIdleState (ProcessorNumber
);
942 return EFI_NOT_READY
;
946 Calculate timeout value and return the current performance counter value.
948 Calculate the number of performance counter ticks required for a timeout.
949 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
952 @param TimeoutInMicroseconds Timeout value in microseconds.
953 @param CurrentTime Returns the current value of the performance counter.
955 @return Expected timestamp counter for timeout.
956 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
962 IN UINTN TimeoutInMicroseconds
,
963 OUT UINT64
*CurrentTime
967 // Read the current value of the performance counter
969 *CurrentTime
= GetPerformanceCounter ();
972 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
975 if (TimeoutInMicroseconds
== 0) {
980 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
981 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
982 // it by 1,000,000, to get the number of ticks for the timeout value.
986 GetPerformanceCounterProperties (NULL
, NULL
),
987 TimeoutInMicroseconds
994 Checks whether timeout expires.
996 Check whether the number of ellapsed performance counter ticks required for a timeout condition
997 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.
999 @param PreviousTime On input, the value of the performance counter when it was last read.
1000 On output, the current value of the performance counter
1001 @param TotalTime The total amount of ellapsed time in performance counter ticks.
1002 @param Timeout The number of performance counter ticks required to reach a timeout condition.
1004 @retval TRUE A timeout condition has been reached.
1005 @retval FALSE A timeout condition has not been reached.
1010 IN OUT UINT64
*PreviousTime
,
1011 IN UINT64
*TotalTime
,
1024 GetPerformanceCounterProperties (&Start
, &End
);
1025 Cycle
= End
- Start
;
1030 CurrentTime
= GetPerformanceCounter();
1031 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1038 *TotalTime
+= Delta
;
1039 *PreviousTime
= CurrentTime
;
1040 if (*TotalTime
> Timeout
) {
1047 Searches for the next waiting AP.
1049 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1051 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.
1053 @retval EFI_SUCCESS The next waiting AP has been found.
1054 @retval EFI_NOT_FOUND No waiting AP exists.
1058 GetNextWaitingProcessorNumber (
1059 OUT UINTN
*NextProcessorNumber
1062 UINTN ProcessorNumber
;
1064 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
1066 if (mMPSystemData
.CpuList
[ProcessorNumber
]) {
1067 *NextProcessorNumber
= ProcessorNumber
;
1072 return EFI_NOT_FOUND
;
1076 Wrapper function for all procedures assigned to AP.
1078 Wrapper function for all procedures assigned to AP via MP service protocol.
1079 It controls states of AP and invokes assigned precedure.
1087 EFI_AP_PROCEDURE Procedure
;
1089 UINTN ProcessorNumber
;
1090 CPU_DATA_BLOCK
*CpuData
;
1092 WhoAmI (&mMpService
, &ProcessorNumber
);
1093 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
1095 AcquireSpinLock (&CpuData
->CpuDataLock
);
1096 CpuData
->State
= CpuStateBusy
;
1097 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1100 // Now let us check it out.
1102 AcquireSpinLock (&CpuData
->CpuDataLock
);
1103 Procedure
= CpuData
->Procedure
;
1104 Parameter
= CpuData
->Parameter
;
1105 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1107 if (Procedure
!= NULL
) {
1109 Procedure (Parameter
);
1112 // if BSP is switched to AP, it continue execute from here, but it carries register state
1113 // of the old AP, so need to reload CpuData (might be stored in a register after compiler
1114 // optimization) to make sure it points to the right data
1116 WhoAmI (&mMpService
, &ProcessorNumber
);
1117 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
1119 AcquireSpinLock (&CpuData
->CpuDataLock
);
1120 CpuData
->Procedure
= NULL
;
1121 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1124 AcquireSpinLock (&CpuData
->CpuDataLock
);
1125 CpuData
->State
= CpuStateFinished
;
1126 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1130 Sends INIT-SIPI-SIPI to AP.
1132 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.
1134 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.
1135 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.
1136 @param ApFunction The procedure for AP to work on.
1141 IN BOOLEAN Broadcast
,
1150 UINT32 VectorNumber
;
1151 UINT32 DeliveryMode
;
1153 mExchangeInfo
->ApFunction
= ApFunction
;
1154 mExchangeInfo
->StackStart
= mStackStartAddress
;
1158 ICRLow
= BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT
| TRIGGER_MODE_LEVEL_BIT
| ASSERT_BIT
;
1160 ICRHigh
= ApicID
<< 24;
1161 ICRLow
= SPECIFY_CPU_MODE_BIT
| TRIGGER_MODE_LEVEL_BIT
| ASSERT_BIT
;
1165 DeliveryMode
= DELIVERY_MODE_INIT
;
1166 ICRLow
|= VectorNumber
| (DeliveryMode
<< 8);
1168 ApicBase
= 0xfee00000;
1171 // Write Interrupt Command Registers to send INIT IPI.
1173 MmioWrite32 (ApicBase
+ APIC_REGISTER_ICR_HIGH_OFFSET
, ICRHigh
);
1174 MmioWrite32 (ApicBase
+ APIC_REGISTER_ICR_LOW_OFFSET
, ICRLow
);
1176 MicroSecondDelay (10);
1178 VectorNumber
= (UINT32
) RShiftU64 (mStartupVector
, 12);
1179 DeliveryMode
= DELIVERY_MODE_SIPI
;
1181 ICRLow
= BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT
| TRIGGER_MODE_LEVEL_BIT
| ASSERT_BIT
;
1183 ICRLow
= SPECIFY_CPU_MODE_BIT
| TRIGGER_MODE_LEVEL_BIT
| ASSERT_BIT
;
1186 ICRLow
|= VectorNumber
| (DeliveryMode
<< 8);
1189 // Write Interrupt Command Register to send first SIPI IPI.
1191 MmioWrite32 (ApicBase
+ APIC_REGISTER_ICR_LOW_OFFSET
, ICRLow
);
1193 MicroSecondDelay (200);
1196 // Write Interrupt Command Register to send second SIPI IPI.
1198 MmioWrite32 (ApicBase
+ APIC_REGISTER_ICR_LOW_OFFSET
, ICRLow
);
1202 Function to wake up a specified AP and assign procedure to it.
1204 @param ProcessorNumber Handle number of the specified processor.
1205 @param Procedure Procedure to assign.
1206 @param ProcArguments Argument for Procedure.
1211 IN UINTN ProcessorNumber
,
1212 IN EFI_AP_PROCEDURE Procedure
,
1213 IN VOID
*ProcArguments
1217 CPU_DATA_BLOCK
*CpuData
;
1218 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer
;
1220 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
1222 AcquireSpinLock (&CpuData
->CpuDataLock
);
1223 CpuData
->Parameter
= ProcArguments
;
1224 CpuData
->Procedure
= Procedure
;
1225 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1227 Status
= GetProcessorInfo (
1230 &ProcessorInfoBuffer
1232 ASSERT_EFI_ERROR (Status
);
1236 (UINT32
) ProcessorInfoBuffer
.ProcessorId
,
1237 (VOID
*) (UINTN
) ApProcWrapper
1242 Terminate AP's task and set it to idle state.
1244 This function terminates AP's task due to timeout by sending INIT-SIPI,
1245 and sends it to idle state.
1247 @param ProcessorNumber Handle number of the specified processor.
1251 ResetProcessorToIdleState (
1252 UINTN ProcessorNumber
1256 CPU_DATA_BLOCK
*CpuData
;
1257 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer
;
1259 Status
= GetProcessorInfo (
1262 &ProcessorInfoBuffer
1264 ASSERT_EFI_ERROR (Status
);
1268 (UINT32
) ProcessorInfoBuffer
.ProcessorId
,
1272 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
1274 AcquireSpinLock (&CpuData
->CpuDataLock
);
1275 CpuData
->State
= CpuStateIdle
;
1276 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1280 Worker function of EnableDisableAP ()
1282 Worker function of EnableDisableAP (). Changes state of specified processor.
1284 @param ProcessorNumber Processor number of specified AP.
1285 @param NewState Desired state of the specified AP.
1287 @retval EFI_SUCCESS AP's state successfully changed.
1292 IN UINTN ProcessorNumber
,
1296 CPU_DATA_BLOCK
*CpuData
;
1298 CpuData
= &mMPSystemData
.CpuData
[ProcessorNumber
];
1301 AcquireSpinLock (&CpuData
->CpuDataLock
);
1302 CpuData
->State
= CpuStateDisabled
;
1303 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1305 AcquireSpinLock (&CpuData
->CpuDataLock
);
1306 CpuData
->State
= CpuStateIdle
;
1307 ReleaseSpinLock (&CpuData
->CpuDataLock
);
1314 Test memory region of EfiGcdMemoryTypeReserved.
1316 @param Length The length of memory region to test.
1318 @retval EFI_SUCCESS The memory region passes test.
1319 @retval EFI_NOT_FOUND The memory region is not reserved memory.
1320 @retval EFI_DEVICE_ERROR The memory fails on test.
1324 TestReservedMemory (
1329 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
1330 EFI_PHYSICAL_ADDRESS Address
;
1331 UINTN LengthCovered
;
1332 UINTN RemainingLength
;
1335 // Walk through the memory descriptors covering the memory range.
1337 Address
= mStartupVector
;
1338 RemainingLength
= Length
;
1339 while (Address
< mStartupVector
+ Length
) {
1340 Status
= gDS
->GetMemorySpaceDescriptor(
1344 if (EFI_ERROR (Status
)) {
1345 return EFI_NOT_FOUND
;
1348 if (Descriptor
.GcdMemoryType
!= EfiGcdMemoryTypeReserved
) {
1349 return EFI_NOT_FOUND
;
1352 // Calculated the length of the intersected range.
1354 LengthCovered
= (UINTN
) (Descriptor
.BaseAddress
+ Descriptor
.Length
- Address
);
1355 if (LengthCovered
> RemainingLength
) {
1356 LengthCovered
= RemainingLength
;
1359 Status
= mGenMemoryTest
->CompatibleRangeTest (
1364 if (EFI_ERROR (Status
)) {
1365 return EFI_DEVICE_ERROR
;
1368 Address
+= LengthCovered
;
1369 RemainingLength
-= LengthCovered
;
1376 Allocates startup vector for APs.
1378 This function allocates Startup vector for APs.
1380 @param Size The size of startup vector.
1384 AllocateStartupVector (
1390 Status
= gBS
->LocateProtocol (
1391 &gEfiGenericMemTestProtocolGuid
,
1393 (VOID
**) &mGenMemoryTest
1395 if (EFI_ERROR (Status
)) {
1396 mGenMemoryTest
= NULL
;
1399 for (mStartupVector
= 0x7F000; mStartupVector
>= 0x2000; mStartupVector
-= EFI_PAGE_SIZE
) {
1400 if (mGenMemoryTest
!= NULL
) {
1402 // Test memory if it is EfiGcdMemoryTypeReserved.
1404 Status
= TestReservedMemory (EFI_SIZE_TO_PAGES (Size
) * EFI_PAGE_SIZE
);
1405 if (Status
== EFI_DEVICE_ERROR
) {
1410 Status
= gBS
->AllocatePages (
1412 EfiBootServicesCode
,
1413 EFI_SIZE_TO_PAGES (Size
),
1417 if (!EFI_ERROR (Status
)) {
1422 ASSERT_EFI_ERROR (Status
);
1426 Prepares Startup Vector for APs.
1428 This function prepares Startup Vector for APs.
1432 PrepareAPStartupVector (
1436 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1437 IA32_DESCRIPTOR GdtrForBSP
;
1440 // Get the address map of startup code for AP,
1441 // including code size, and offset of long jump instructions to redirect.
1443 AsmGetAddressMap (&AddressMap
);
1446 // Allocate a 4K-aligned region under 1M for startup vector for AP.
1447 // The region contains AP startup code and exchange data between BSP and AP.
1449 AllocateStartupVector (AddressMap
.Size
+ sizeof (MP_CPU_EXCHANGE_INFO
));
1452 // Copy AP startup code to startup vector, and then redirect the long jump
1453 // instructions for mode switching.
1455 CopyMem ((VOID
*) (UINTN
) mStartupVector
, AddressMap
.RendezvousFunnelAddress
, AddressMap
.Size
);
1456 *(UINT32
*) (UINTN
) (mStartupVector
+ AddressMap
.FlatJumpOffset
+ 3) = (UINT32
) (mStartupVector
+ AddressMap
.PModeEntryOffset
);
1458 // For IA32 mode, LongJumpOffset is filled with zero. If non-zero, then we are in X64 mode, so further redirect for long mode switch.
1460 if (AddressMap
.LongJumpOffset
!= 0) {
1461 *(UINT32
*) (UINTN
) (mStartupVector
+ AddressMap
.LongJumpOffset
+ 2) = (UINT32
) (mStartupVector
+ AddressMap
.LModeEntryOffset
);
1465 // Get the start address of exchange data between BSP and AP.
1467 mExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (mStartupVector
+ AddressMap
.Size
);
1469 ZeroMem ((VOID
*) mExchangeInfo
, sizeof (MP_CPU_EXCHANGE_INFO
));
1471 mStackStartAddress
= AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER
* AP_STACK_SIZE
));
1472 mExchangeInfo
->StackSize
= AP_STACK_SIZE
;
1474 AsmReadGdtr (&GdtrForBSP
);
1475 mExchangeInfo
->GdtrProfile
.Base
= GdtrForBSP
.Base
;
1476 mExchangeInfo
->GdtrProfile
.Limit
= GdtrForBSP
.Limit
;
1478 mExchangeInfo
->BufferStart
= (UINT32
) mStartupVector
;
1479 mExchangeInfo
->Cr3
= (UINT32
) (AsmReadCr3 ());
1483 Prepares memory region for processor configuration.
1485 This function prepares memory region for processor configuration.
1489 PrepareMemoryForConfiguration (
1496 // Initialize Spin Locks for system
1498 InitializeSpinLock (&mMPSystemData
.APSerializeLock
);
1499 for (Index
= 0; Index
< MAX_CPU_NUMBER
; Index
++) {
1500 InitializeSpinLock (&mMPSystemData
.CpuData
[Index
].CpuDataLock
);
1503 PrepareAPStartupVector ();
1507 Gets the processor number of BSP.
1509 @return The processor number of BSP.
1517 UINTN ProcessorNumber
;
1518 EFI_MP_PROC_CONTEXT ProcessorContextBuffer
;
1522 BufferSize
= sizeof (EFI_MP_PROC_CONTEXT
);
1524 for (ProcessorNumber
= 0; ProcessorNumber
< mNumberOfProcessors
; ProcessorNumber
++) {
1525 Status
= mFrameworkMpService
->GetProcessorContext (
1526 mFrameworkMpService
,
1529 &ProcessorContextBuffer
1531 ASSERT_EFI_ERROR (Status
);
1533 if (ProcessorContextBuffer
.Designation
== EfiCpuBSP
) {
1537 ASSERT (ProcessorNumber
< mNumberOfProcessors
);
1539 return ProcessorNumber
;
1543 Entrypoint of MP Services Protocol thunk driver.
1545 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1546 @param[in] SystemTable A pointer to the EFI System Table.
1548 @retval EFI_SUCCESS The entry point is executed successfully.
1553 InitializeMpServicesProtocol (
1554 IN EFI_HANDLE ImageHandle
,
1555 IN EFI_SYSTEM_TABLE
*SystemTable
1560 PrepareMemoryForConfiguration ();
1563 // Locates Framework version MP Services Protocol
1565 Status
= gBS
->LocateProtocol (
1566 &gFrameworkEfiMpServiceProtocolGuid
,
1568 (VOID
**) &mFrameworkMpService
1570 ASSERT_EFI_ERROR (Status
);
1572 Status
= mFrameworkMpService
->GetGeneralMPInfo (
1573 mFrameworkMpService
,
1574 &mNumberOfProcessors
,
1580 ASSERT_EFI_ERROR (Status
);
1583 // Create timer event to check AP state for non-blocking execution.
1585 Status
= gBS
->CreateEvent (
1586 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1590 &mMPSystemData
.CheckAPsEvent
1592 ASSERT_EFI_ERROR (Status
);
1595 // Now install the MP services protocol.
1597 Status
= gBS
->InstallProtocolInterface (
1599 &gEfiMpServiceProtocolGuid
,
1600 EFI_NATIVE_INTERFACE
,
1603 ASSERT_EFI_ERROR (Status
);
1606 // Launch the timer event to check AP state.
1608 Status
= gBS
->SetTimer (
1609 mMPSystemData
.CheckAPsEvent
,
1613 ASSERT_EFI_ERROR (Status
);