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
,
29 NULL
, // GetProcessorInfo,
30 NULL
, // StartupAllAPs,
31 NULL
, // StartupThisAP,
33 NULL
, // EnableDisableAP,
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 This service retrieves the number of logical processor in the platform
62 and the number of those logical processors that are enabled on this boot.
63 This service may only be called from the BSP.
65 This function is used to retrieve the following information:
66 - The number of logical processors that are present in the system.
67 - The number of enabled logical processors in the system at the instant
70 Because MP Service Protocol provides services to enable and disable processors
71 dynamically, the number of enabled logical processors may vary during the
72 course of a boot session.
74 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
75 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
76 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
77 is returned in NumberOfProcessors, the number of currently enabled processor
78 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
80 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
82 @param[out] NumberOfProcessors Pointer to the total number of logical
83 processors in the system, including the BSP
85 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
86 processors that exist in system, including
89 @retval EFI_SUCCESS The number of logical processors and enabled
90 logical processors was retrieved.
91 @retval EFI_DEVICE_ERROR The calling processor is an AP.
92 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
93 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
98 GetNumberOfProcessors (
99 IN EFI_MP_SERVICES_PROTOCOL
*This
,
100 OUT UINTN
*NumberOfProcessors
,
101 OUT UINTN
*NumberOfEnabledProcessors
104 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
105 return EFI_INVALID_PARAMETER
;
109 return EFI_DEVICE_ERROR
;
112 *NumberOfProcessors
= mMpSystemData
.NumberOfProcessors
;
113 *NumberOfEnabledProcessors
= mMpSystemData
.NumberOfEnabledProcessors
;
118 This return the handle number for the calling processor. This service may be
119 called from the BSP and APs.
121 This service returns the processor handle number for the calling processor.
122 The returned value is in the range from 0 to the total number of logical
123 processors minus 1. The total number of logical processors can be retrieved
124 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
125 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
126 is returned. Otherwise, the current processors handle number is returned in
127 ProcessorNumber, and EFI_SUCCESS is returned.
129 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
130 @param[out] ProcessorNumber The handle number of AP that is to become the new
131 BSP. The range is from 0 to the total number of
132 logical processors minus 1. The total number of
133 logical processors can be retrieved by
134 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
136 @retval EFI_SUCCESS The current processor handle number was returned
138 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
144 IN EFI_MP_SERVICES_PROTOCOL
*This
,
145 OUT UINTN
*ProcessorNumber
151 if (ProcessorNumber
== NULL
) {
152 return EFI_INVALID_PARAMETER
;
155 ProcessorId
= GetApicId ();
156 for (Index
= 0; Index
< mMpSystemData
.NumberOfProcessors
; Index
++) {
157 if (mMpSystemData
.CpuDatas
[Index
].Info
.ProcessorId
== ProcessorId
) {
162 *ProcessorNumber
= Index
;
167 Application Processors do loop routine
168 after switch to its own stack.
170 @param Context1 A pointer to the context to pass into the function.
171 @param Context2 A pointer to the context to pass into the function.
175 ProcessorToIdleState (
176 IN VOID
*Context1
, OPTIONAL
177 IN VOID
*Context2 OPTIONAL
180 DEBUG ((DEBUG_INFO
, "Ap apicid is %d\n", GetApicId ()));
182 AsmApDoneWithCommonStack ();
189 Application Processor C code entry point.
200 FillInProcessorInformation (FALSE
, mMpSystemData
.NumberOfProcessors
);
201 TopOfApStack
= (UINT8
*)mApStackStart
+ gApStackSize
;
202 mApStackStart
= TopOfApStack
;
204 mMpSystemData
.NumberOfProcessors
++;
207 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)ProcessorToIdleState
,
214 This function is called by all processors (both BSP and AP) once and collects MP related data.
216 @param Bsp TRUE if the CPU is BSP
217 @param ProcessorNumber The specific processor number
219 @retval EFI_SUCCESS Data for the processor collected and filled in
223 FillInProcessorInformation (
225 IN UINTN ProcessorNumber
228 CPU_DATA_BLOCK
*CpuData
;
231 CpuData
= &mMpSystemData
.CpuDatas
[ProcessorNumber
];
232 ProcessorId
= GetApicId ();
233 CpuData
->Info
.ProcessorId
= ProcessorId
;
234 CpuData
->Info
.StatusFlag
= PROCESSOR_ENABLED_BIT
| PROCESSOR_HEALTH_STATUS_BIT
;
236 CpuData
->Info
.StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
238 CpuData
->Info
.Location
.Package
= ProcessorId
;
239 CpuData
->Info
.Location
.Core
= 0;
240 CpuData
->Info
.Location
.Thread
= 0;
241 CpuData
->State
= Bsp
? CpuStateBuzy
: CpuStateIdle
;
243 CpuData
->Procedure
= NULL
;
244 CpuData
->Parameter
= NULL
;
245 InitializeSpinLock (&CpuData
->CpuDataLock
);
251 Prepare the System Data.
253 @retval EFI_SUCCESS the System Data finished initilization.
261 ZeroMem (&mMpSystemData
, sizeof (MP_SYSTEM_DATA
));
263 mMpSystemData
.NumberOfProcessors
= 1;
264 mMpSystemData
.NumberOfEnabledProcessors
= 1;
266 mMpSystemData
.CpuDatas
= AllocateZeroPool (sizeof (CPU_DATA_BLOCK
) * gMaxLogicalProcessorNumber
);
267 ASSERT(mMpSystemData
.CpuDatas
!= NULL
);
272 FillInProcessorInformation (TRUE
, 0);
278 Initialize Multi-processor support.
282 InitializeMpSupport (
286 gMaxLogicalProcessorNumber
= (UINTN
) PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
287 if (gMaxLogicalProcessorNumber
< 1) {
288 DEBUG ((DEBUG_ERROR
, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
292 if (gMaxLogicalProcessorNumber
== 1) {
296 gApStackSize
= (UINTN
) PcdGet32 (PcdCpuApStackSize
);
297 ASSERT ((gApStackSize
& (SIZE_4KB
- 1)) == 0);
299 mApStackStart
= AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
300 ASSERT (mApStackStart
!= NULL
);
303 // the first buffer of stack size used for common stack, when the amount of AP
304 // more than 1, we should never free the common stack which maybe used for AP reset.
306 mCommonStack
= mApStackStart
;
307 mTopOfApCommonStack
= (UINT8
*) mApStackStart
+ gApStackSize
;
308 mApStackStart
= mTopOfApCommonStack
;
312 if (mMpSystemData
.NumberOfProcessors
== 1) {
313 FreePages (mCommonStack
, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber
* gApStackSize
));
317 if (mMpSystemData
.NumberOfProcessors
< gMaxLogicalProcessorNumber
) {
318 FreePages (mApStackStart
, EFI_SIZE_TO_PAGES (
319 (gMaxLogicalProcessorNumber
- mMpSystemData
.NumberOfProcessors
) *