]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Protocol:GetNumberOfProcessors()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
1 /** @file
2 CPU DXE Module.
3
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
9
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.
12
13 **/
14
15 #include "CpuDxe.h"
16 #include "CpuMp.h"
17
18 UINTN gMaxLogicalProcessorNumber;
19 UINTN gApStackSize;
20
21 MP_SYSTEM_DATA mMpSystemData;
22
23 VOID *mCommonStack = 0;
24 VOID *mTopOfApCommonStack = 0;
25 VOID *mApStackStart = 0;
26
27 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {
28 GetNumberOfProcessors,
29 NULL, // GetProcessorInfo,
30 NULL, // StartupAllAPs,
31 NULL, // StartupThisAP,
32 NULL, // SwitchBSP,
33 NULL, // EnableDisableAP,
34 WhoAmI
35 };
36
37 /**
38 Check whether caller processor is BSP.
39
40 @retval TRUE the caller is BSP
41 @retval FALSE the caller is AP
42
43 **/
44 BOOLEAN
45 IsBSP (
46 VOID
47 )
48 {
49 UINTN CpuIndex;
50 CPU_DATA_BLOCK *CpuData;
51
52 CpuData = NULL;
53
54 WhoAmI (&mMpServicesTemplate, &CpuIndex);
55 CpuData = &mMpSystemData.CpuDatas[CpuIndex];
56
57 return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;
58 }
59
60 /**
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.
64
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
68 this call is made.
69
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.
73
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.
79
80 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
81 instance.
82 @param[out] NumberOfProcessors Pointer to the total number of logical
83 processors in the system, including the BSP
84 and disabled APs.
85 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
86 processors that exist in system, including
87 the BSP.
88
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.
94
95 **/
96 EFI_STATUS
97 EFIAPI
98 GetNumberOfProcessors (
99 IN EFI_MP_SERVICES_PROTOCOL *This,
100 OUT UINTN *NumberOfProcessors,
101 OUT UINTN *NumberOfEnabledProcessors
102 )
103 {
104 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
105 return EFI_INVALID_PARAMETER;
106 }
107
108 if (!IsBSP ()) {
109 return EFI_DEVICE_ERROR;
110 }
111
112 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;
113 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;
114 return EFI_SUCCESS;
115 }
116
117 /**
118 This return the handle number for the calling processor. This service may be
119 called from the BSP and APs.
120
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.
128
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().
135
136 @retval EFI_SUCCESS The current processor handle number was returned
137 in ProcessorNumber.
138 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
139
140 **/
141 EFI_STATUS
142 EFIAPI
143 WhoAmI (
144 IN EFI_MP_SERVICES_PROTOCOL *This,
145 OUT UINTN *ProcessorNumber
146 )
147 {
148 UINTN Index;
149 UINT32 ProcessorId;
150
151 if (ProcessorNumber == NULL) {
152 return EFI_INVALID_PARAMETER;
153 }
154
155 ProcessorId = GetApicId ();
156 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
157 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
158 break;
159 }
160 }
161
162 *ProcessorNumber = Index;
163 return EFI_SUCCESS;
164 }
165
166 /**
167 Application Processors do loop routine
168 after switch to its own stack.
169
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.
172
173 **/
174 VOID
175 ProcessorToIdleState (
176 IN VOID *Context1, OPTIONAL
177 IN VOID *Context2 OPTIONAL
178 )
179 {
180 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));
181
182 AsmApDoneWithCommonStack ();
183
184 CpuSleep ();
185 CpuDeadLoop ();
186 }
187
188 /**
189 Application Processor C code entry point.
190
191 **/
192 VOID
193 EFIAPI
194 ApEntryPointInC (
195 VOID
196 )
197 {
198 VOID* TopOfApStack;
199
200 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
201 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
202 mApStackStart = TopOfApStack;
203
204 mMpSystemData.NumberOfProcessors++;
205
206 SwitchStack (
207 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
208 NULL,
209 NULL,
210 TopOfApStack);
211 }
212
213 /**
214 This function is called by all processors (both BSP and AP) once and collects MP related data.
215
216 @param Bsp TRUE if the CPU is BSP
217 @param ProcessorNumber The specific processor number
218
219 @retval EFI_SUCCESS Data for the processor collected and filled in
220
221 **/
222 EFI_STATUS
223 FillInProcessorInformation (
224 IN BOOLEAN Bsp,
225 IN UINTN ProcessorNumber
226 )
227 {
228 CPU_DATA_BLOCK *CpuData;
229 UINT32 ProcessorId;
230
231 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
232 ProcessorId = GetApicId ();
233 CpuData->Info.ProcessorId = ProcessorId;
234 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
235 if (Bsp) {
236 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
237 }
238 CpuData->Info.Location.Package = ProcessorId;
239 CpuData->Info.Location.Core = 0;
240 CpuData->Info.Location.Thread = 0;
241 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
242
243 CpuData->Procedure = NULL;
244 CpuData->Parameter = NULL;
245 InitializeSpinLock (&CpuData->CpuDataLock);
246
247 return EFI_SUCCESS;
248 }
249
250 /**
251 Prepare the System Data.
252
253 @retval EFI_SUCCESS the System Data finished initilization.
254
255 **/
256 EFI_STATUS
257 InitMpSystemData (
258 VOID
259 )
260 {
261 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
262
263 mMpSystemData.NumberOfProcessors = 1;
264 mMpSystemData.NumberOfEnabledProcessors = 1;
265
266 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
267 ASSERT(mMpSystemData.CpuDatas != NULL);
268
269 //
270 // BSP
271 //
272 FillInProcessorInformation (TRUE, 0);
273
274 return EFI_SUCCESS;
275 }
276
277 /**
278 Initialize Multi-processor support.
279
280 **/
281 VOID
282 InitializeMpSupport (
283 VOID
284 )
285 {
286 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
287 if (gMaxLogicalProcessorNumber < 1) {
288 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
289 return;
290 }
291
292 if (gMaxLogicalProcessorNumber == 1) {
293 return;
294 }
295
296 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
297 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
298
299 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
300 ASSERT (mApStackStart != NULL);
301
302 //
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.
305 //
306 mCommonStack = mApStackStart;
307 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
308 mApStackStart = mTopOfApCommonStack;
309
310 InitMpSystemData ();
311
312 if (mMpSystemData.NumberOfProcessors == 1) {
313 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
314 return;
315 }
316
317 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
318 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
319 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
320 gApStackSize));
321 }
322 }