]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Services:GetProcessorInfo()
[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 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 Gets detailed MP-related information on the requested processor at the
119 instant this call is made. This service may only be called from the BSP.
120
121 This service retrieves detailed MP-related information about any processor
122 on the platform. Note the following:
123 - The processor information may change during the course of a boot session.
124 - The information presented here is entirely MP related.
125
126 Information regarding the number of caches and their sizes, frequency of operation,
127 slot numbers is all considered platform-related information and is not provided
128 by this service.
129
130 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
131 instance.
132 @param[in] ProcessorNumber The handle number of processor.
133 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
134 the requested processor is deposited.
135
136 @retval EFI_SUCCESS Processor information was returned.
137 @retval EFI_DEVICE_ERROR The calling processor is an AP.
138 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
139 @retval EFI_NOT_FOUND The processor with the handle specified by
140 ProcessorNumber does not exist in the platform.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 GetProcessorInfo (
146 IN EFI_MP_SERVICES_PROTOCOL *This,
147 IN UINTN ProcessorNumber,
148 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
149 )
150 {
151 if (ProcessorInfoBuffer == NULL) {
152 return EFI_INVALID_PARAMETER;
153 }
154
155 if (!IsBSP ()) {
156 return EFI_DEVICE_ERROR;
157 }
158
159 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
160 return EFI_NOT_FOUND;
161 }
162
163 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
164 return EFI_SUCCESS;
165 }
166
167 /**
168 This return the handle number for the calling processor. This service may be
169 called from the BSP and APs.
170
171 This service returns the processor handle number for the calling processor.
172 The returned value is in the range from 0 to the total number of logical
173 processors minus 1. The total number of logical processors can be retrieved
174 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
175 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
176 is returned. Otherwise, the current processors handle number is returned in
177 ProcessorNumber, and EFI_SUCCESS is returned.
178
179 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
180 @param[out] ProcessorNumber The handle number of AP that is to become the new
181 BSP. The range is from 0 to the total number of
182 logical processors minus 1. The total number of
183 logical processors can be retrieved by
184 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
185
186 @retval EFI_SUCCESS The current processor handle number was returned
187 in ProcessorNumber.
188 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
189
190 **/
191 EFI_STATUS
192 EFIAPI
193 WhoAmI (
194 IN EFI_MP_SERVICES_PROTOCOL *This,
195 OUT UINTN *ProcessorNumber
196 )
197 {
198 UINTN Index;
199 UINT32 ProcessorId;
200
201 if (ProcessorNumber == NULL) {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 ProcessorId = GetApicId ();
206 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
207 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
208 break;
209 }
210 }
211
212 *ProcessorNumber = Index;
213 return EFI_SUCCESS;
214 }
215
216 /**
217 Application Processors do loop routine
218 after switch to its own stack.
219
220 @param Context1 A pointer to the context to pass into the function.
221 @param Context2 A pointer to the context to pass into the function.
222
223 **/
224 VOID
225 ProcessorToIdleState (
226 IN VOID *Context1, OPTIONAL
227 IN VOID *Context2 OPTIONAL
228 )
229 {
230 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));
231
232 AsmApDoneWithCommonStack ();
233
234 CpuSleep ();
235 CpuDeadLoop ();
236 }
237
238 /**
239 Application Processor C code entry point.
240
241 **/
242 VOID
243 EFIAPI
244 ApEntryPointInC (
245 VOID
246 )
247 {
248 VOID* TopOfApStack;
249
250 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
251 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
252 mApStackStart = TopOfApStack;
253
254 mMpSystemData.NumberOfProcessors++;
255
256 SwitchStack (
257 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
258 NULL,
259 NULL,
260 TopOfApStack);
261 }
262
263 /**
264 This function is called by all processors (both BSP and AP) once and collects MP related data.
265
266 @param Bsp TRUE if the CPU is BSP
267 @param ProcessorNumber The specific processor number
268
269 @retval EFI_SUCCESS Data for the processor collected and filled in
270
271 **/
272 EFI_STATUS
273 FillInProcessorInformation (
274 IN BOOLEAN Bsp,
275 IN UINTN ProcessorNumber
276 )
277 {
278 CPU_DATA_BLOCK *CpuData;
279 UINT32 ProcessorId;
280
281 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
282 ProcessorId = GetApicId ();
283 CpuData->Info.ProcessorId = ProcessorId;
284 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
285 if (Bsp) {
286 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
287 }
288 CpuData->Info.Location.Package = ProcessorId;
289 CpuData->Info.Location.Core = 0;
290 CpuData->Info.Location.Thread = 0;
291 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
292
293 CpuData->Procedure = NULL;
294 CpuData->Parameter = NULL;
295 InitializeSpinLock (&CpuData->CpuDataLock);
296
297 return EFI_SUCCESS;
298 }
299
300 /**
301 Prepare the System Data.
302
303 @retval EFI_SUCCESS the System Data finished initilization.
304
305 **/
306 EFI_STATUS
307 InitMpSystemData (
308 VOID
309 )
310 {
311 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
312
313 mMpSystemData.NumberOfProcessors = 1;
314 mMpSystemData.NumberOfEnabledProcessors = 1;
315
316 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
317 ASSERT(mMpSystemData.CpuDatas != NULL);
318
319 //
320 // BSP
321 //
322 FillInProcessorInformation (TRUE, 0);
323
324 return EFI_SUCCESS;
325 }
326
327 /**
328 Initialize Multi-processor support.
329
330 **/
331 VOID
332 InitializeMpSupport (
333 VOID
334 )
335 {
336 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
337 if (gMaxLogicalProcessorNumber < 1) {
338 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
339 return;
340 }
341
342 if (gMaxLogicalProcessorNumber == 1) {
343 return;
344 }
345
346 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
347 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
348
349 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
350 ASSERT (mApStackStart != NULL);
351
352 //
353 // the first buffer of stack size used for common stack, when the amount of AP
354 // more than 1, we should never free the common stack which maybe used for AP reset.
355 //
356 mCommonStack = mApStackStart;
357 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
358 mApStackStart = mTopOfApCommonStack;
359
360 InitMpSystemData ();
361
362 if (mMpSystemData.NumberOfProcessors == 1) {
363 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
364 return;
365 }
366
367 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
368 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
369 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
370 gApStackSize));
371 }
372 }