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
;
21 MP_SYSTEM_DATA mMpSystemData
;
23 VOID
*mCommonStack
= 0;
24 VOID
*mTopOfApCommonStack
= 0;
25 VOID
*mApStackStart
= 0;
27 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate
= {
28 GetNumberOfProcessors
,
30 NULL
, // StartupAllAPs,
31 NULL
, // StartupThisAP,
38 Check whether caller processor is BSP.
40 @retval TRUE the caller is BSP
41 @retval FALSE the caller is AP
50 CPU_DATA_BLOCK
*CpuData
;
54 WhoAmI (&mMpServicesTemplate
, &CpuIndex
);
55 CpuData
= &mMpSystemData
.CpuDatas
[CpuIndex
];
57 return CpuData
->Info
.StatusFlag
& PROCESSOR_AS_BSP_BIT
? TRUE
: FALSE
;
61 Get the Application Processors state.
63 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
65 @retval CPU_STATE the AP status
70 IN CPU_DATA_BLOCK
*CpuData
75 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
79 State
= CpuData
->State
;
80 ReleaseSpinLock (&CpuData
->CpuDataLock
);
86 Check the Application Processors Status whether contains the Flags.
88 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
89 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
91 @retval TRUE the AP status includes the StatusFlag
92 @retval FALSE the AP status excludes the StatusFlag
97 IN CPU_DATA_BLOCK
*CpuData
,
103 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
107 Ret
= CpuData
->Info
.StatusFlag
& Flags
;
108 ReleaseSpinLock (&CpuData
->CpuDataLock
);
114 Bitwise-Or of the Application Processors Status with the Flags.
116 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
117 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
122 IN CPU_DATA_BLOCK
*CpuData
,
126 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
130 CpuData
->Info
.StatusFlag
|= Flags
;
131 ReleaseSpinLock (&CpuData
->CpuDataLock
);
135 Bitwise-AndNot of the Application Processors Status with the Flags.
137 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
138 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
142 CpuStatusFlagAndNot (
143 IN CPU_DATA_BLOCK
*CpuData
,
147 while (!AcquireSpinLockOrFail (&CpuData
->CpuDataLock
)) {
151 CpuData
->Info
.StatusFlag
&= ~Flags
;
152 ReleaseSpinLock (&CpuData
->CpuDataLock
);
156 This service retrieves the number of logical processor in the platform
157 and the number of those logical processors that are enabled on this boot.
158 This service may only be called from the BSP.
160 This function is used to retrieve the following information:
161 - The number of logical processors that are present in the system.
162 - The number of enabled logical processors in the system at the instant
165 Because MP Service Protocol provides services to enable and disable processors
166 dynamically, the number of enabled logical processors may vary during the
167 course of a boot session.
169 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
170 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
171 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
172 is returned in NumberOfProcessors, the number of currently enabled processor
173 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
175 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
177 @param[out] NumberOfProcessors Pointer to the total number of logical
178 processors in the system, including the BSP
180 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
181 processors that exist in system, including
184 @retval EFI_SUCCESS The number of logical processors and enabled
185 logical processors was retrieved.
186 @retval EFI_DEVICE_ERROR The calling processor is an AP.
187 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
188 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
193 GetNumberOfProcessors (
194 IN EFI_MP_SERVICES_PROTOCOL
*This
,
195 OUT UINTN
*NumberOfProcessors
,
196 OUT UINTN
*NumberOfEnabledProcessors
199 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
200 return EFI_INVALID_PARAMETER
;
204 return EFI_DEVICE_ERROR
;
207 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
208 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
213 Gets detailed MP-related information on the requested processor at the
214 instant this call is made. This service may only be called from the BSP.
216 This service retrieves detailed MP-related information about any processor
217 on the platform. Note the following:
218 - The processor information may change during the course of a boot session.
219 - The information presented here is entirely MP related.
221 Information regarding the number of caches and their sizes, frequency of operation,
222 slot numbers is all considered platform-related information and is not provided
225 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
227 @param[in] ProcessorNumber The handle number of processor.
228 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
229 the requested processor is deposited.
231 @retval EFI_SUCCESS Processor information was returned.
232 @retval EFI_DEVICE_ERROR The calling processor is an AP.
233 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
234 @retval EFI_NOT_FOUND The processor with the handle specified by
235 ProcessorNumber does not exist in the platform.
241 IN EFI_MP_SERVICES_PROTOCOL
*This
,
242 IN UINTN ProcessorNumber
,
243 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
246 if (ProcessorInfoBuffer
== NULL
) {
247 return EFI_INVALID_PARAMETER
;
251 return EFI_DEVICE_ERROR
;
254 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
255 return EFI_NOT_FOUND
;
258 CopyMem (ProcessorInfoBuffer
, &mMpSystemData
.CpuDatas
[ProcessorNumber
], sizeof (EFI_PROCESSOR_INFORMATION
));
263 This service lets the caller enable or disable an AP from this point onward.
264 This service may only be called from the BSP.
266 This service allows the caller enable or disable an AP from this point onward.
267 The caller can optionally specify the health status of the AP by Health. If
268 an AP is being disabled, then the state of the disabled AP is implementation
269 dependent. If an AP is enabled, then the implementation must guarantee that a
270 complete initialization sequence is performed on the AP, so the AP is in a state
271 that is compatible with an MP operating system. This service may not be supported
272 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
274 If the enable or disable AP operation cannot be completed prior to the return
275 from this service, then EFI_UNSUPPORTED must be returned.
277 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
278 @param[in] ProcessorNumber The handle number of AP that is to become the new
279 BSP. The range is from 0 to the total number of
280 logical processors minus 1. The total number of
281 logical processors can be retrieved by
282 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
283 @param[in] EnableAP Specifies the new state for the processor for
284 enabled, FALSE for disabled.
285 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
286 the new health status of the AP. This flag
287 corresponds to StatusFlag defined in
288 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
289 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
290 bits are ignored. If it is NULL, this parameter
293 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
294 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
295 prior to this service returning.
296 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
297 @retval EFI_DEVICE_ERROR The calling processor is an AP.
298 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
300 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
306 IN EFI_MP_SERVICES_PROTOCOL
*This
,
307 IN UINTN ProcessorNumber
,
309 IN UINT32
*HealthFlag OPTIONAL
312 CPU_DATA_BLOCK
*CpuData
;
315 return EFI_DEVICE_ERROR
;
318 if (ProcessorNumber
>= mMpSystemData
.NumberOfProcessors
) {
319 return EFI_NOT_FOUND
;
322 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
323 if (TestCpuStatusFlag (CpuData
, PROCESSOR_AS_BSP_BIT
)) {
324 return EFI_INVALID_PARAMETER
;
327 if (GetApState (CpuData
) != CpuStateIdle
) {
328 return EFI_UNSUPPORTED
;
332 if (!(TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
))) {
333 mMpSystemData
.NumberOfEnabledProcessors
++;
335 CpuStatusFlagOr (CpuData
, PROCESSOR_ENABLED_BIT
);
337 if (TestCpuStatusFlag (CpuData
, PROCESSOR_ENABLED_BIT
)) {
338 mMpSystemData
.NumberOfEnabledProcessors
--;
340 CpuStatusFlagAndNot (CpuData
, PROCESSOR_ENABLED_BIT
);
343 if (HealthFlag
!= NULL
) {
344 CpuStatusFlagAndNot (CpuData
, (UINT32
)~PROCESSOR_HEALTH_STATUS_BIT
);
345 CpuStatusFlagOr (CpuData
, (*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
));
352 This return the handle number for the calling processor. This service may be
353 called from the BSP and APs.
355 This service returns the processor handle number for the calling processor.
356 The returned value is in the range from 0 to the total number of logical
357 processors minus 1. The total number of logical processors can be retrieved
358 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
359 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
360 is returned. Otherwise, the current processors handle number is returned in
361 ProcessorNumber, and EFI_SUCCESS is returned.
363 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
364 @param[out] ProcessorNumber The handle number of AP that is to become the new
365 BSP. The range is from 0 to the total number of
366 logical processors minus 1. The total number of
367 logical processors can be retrieved by
368 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
370 @retval EFI_SUCCESS The current processor handle number was returned
372 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
378 IN EFI_MP_SERVICES_PROTOCOL
*This
,
379 OUT UINTN
*ProcessorNumber
385 if (ProcessorNumber
== NULL
) {
386 return EFI_INVALID_PARAMETER
;
389 ProcessorId
= GetApicId ();
390 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
391 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
396 *ProcessorNumber
= Index
;
401 Application Processors do loop routine
402 after switch to its own stack.
404 @param Context1 A pointer to the context to pass into the function.
405 @param Context2 A pointer to the context to pass into the function.
409 ProcessorToIdleState (
410 IN VOID
*Context1
, OPTIONAL
411 IN VOID
*Context2 OPTIONAL
414 DEBUG ((DEBUG_INFO
, "Ap apicid is %d\n", GetApicId ()));
416 AsmApDoneWithCommonStack ();
423 Application Processor C code entry point.
434 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
435 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
436 mApStackStart
= TopOfApStack
;
438 mMpSystemData
.NumberOfProcessors
++;
441 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
448 This function is called by all processors (both BSP and AP) once and collects MP related data.
450 @param Bsp TRUE if the CPU is BSP
451 @param ProcessorNumber The specific processor number
453 @retval EFI_SUCCESS Data for the processor collected and filled in
457 FillInProcessorInformation (
459 IN UINTN ProcessorNumber
462 CPU_DATA_BLOCK
*CpuData
;
465 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
466 ProcessorId
= GetApicId ();
467 CpuData
->Info
.ProcessorId
= ProcessorId
;
468 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
470 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
472 CpuData
->Info
.Location
.Package
= ProcessorId
;
473 CpuData
->Info
.Location
.Core
= 0;
474 CpuData
->Info
.Location
.Thread
= 0;
475 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
477 CpuData
->Procedure
= NULL
;
478 CpuData
->Parameter
= NULL
;
479 InitializeSpinLock (&CpuData
->CpuDataLock
);
485 Prepare the System Data.
487 @retval EFI_SUCCESS the System Data finished initilization.
495 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
497 mMpSystemData
.NumberOfProcessors
= 1;
498 mMpSystemData
.NumberOfEnabledProcessors
= 1;
500 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
501 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
506 FillInProcessorInformation (TRUE
, 0);
512 Initialize Multi-processor support.
516 InitializeMpSupport (
520 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
521 if (gMaxLogicalProcessorNumber
< 1) {
522 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
526 if (gMaxLogicalProcessorNumber
== 1) {
530 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
531 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
533 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
534 ASSERT (mApStackStart
!= NULL
);
537 // the first buffer of stack size used for common stack, when the amount of AP
538 // more than 1, we should never free the common stack which maybe used for AP reset.
540 mCommonStack
= mApStackStart
;
541 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
542 mApStackStart
= mTopOfApCommonStack
;
546 if (mMpSystemData
.NumberOfProcessors
== 1) {
547 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
551 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
552 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
553 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *