UefiCpuPkg/CpuDxe: implement Mp Services:GetProcessorInfo()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
CommitLineData
6022e28c
JJ
1/** @file\r
2 CPU DXE Module.\r
3\r
4 Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "CpuDxe.h"\r
16#include "CpuMp.h"\r
17\r
6a26a597
CF
18UINTN gMaxLogicalProcessorNumber;\r
19UINTN gApStackSize;\r
20\r
03673ae1
CF
21MP_SYSTEM_DATA mMpSystemData;\r
22\r
fab82c18
JJ
23VOID *mCommonStack = 0;\r
24VOID *mTopOfApCommonStack = 0;\r
6a26a597 25VOID *mApStackStart = 0;\r
fab82c18 26\r
003973d9 27EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
d894d8b7 28 GetNumberOfProcessors,\r
e7938b5a 29 GetProcessorInfo,\r
003973d9
CF
30 NULL, // StartupAllAPs,\r
31 NULL, // StartupThisAP,\r
32 NULL, // SwitchBSP,\r
33 NULL, // EnableDisableAP,\r
cfa2fac1 34 WhoAmI\r
003973d9
CF
35};\r
36\r
d894d8b7
CF
37/**\r
38 Check whether caller processor is BSP.\r
39\r
40 @retval TRUE the caller is BSP\r
41 @retval FALSE the caller is AP\r
42\r
43**/\r
44BOOLEAN\r
45IsBSP (\r
46 VOID\r
47 )\r
48{\r
49 UINTN CpuIndex;\r
50 CPU_DATA_BLOCK *CpuData;\r
51\r
52 CpuData = NULL;\r
53\r
54 WhoAmI (&mMpServicesTemplate, &CpuIndex);\r
55 CpuData = &mMpSystemData.CpuDatas[CpuIndex];\r
56\r
57 return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;\r
58}\r
59\r
60/**\r
61 This service retrieves the number of logical processor in the platform\r
62 and the number of those logical processors that are enabled on this boot.\r
63 This service may only be called from the BSP.\r
64\r
65 This function is used to retrieve the following information:\r
66 - The number of logical processors that are present in the system.\r
67 - The number of enabled logical processors in the system at the instant\r
68 this call is made.\r
69\r
70 Because MP Service Protocol provides services to enable and disable processors\r
71 dynamically, the number of enabled logical processors may vary during the\r
72 course of a boot session.\r
73\r
74 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
75 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
76 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
77 is returned in NumberOfProcessors, the number of currently enabled processor\r
78 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
79\r
80 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
81 instance.\r
82 @param[out] NumberOfProcessors Pointer to the total number of logical\r
83 processors in the system, including the BSP\r
84 and disabled APs.\r
85 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
86 processors that exist in system, including\r
87 the BSP.\r
88\r
89 @retval EFI_SUCCESS The number of logical processors and enabled\r
90 logical processors was retrieved.\r
91 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
92 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
93 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
94\r
95**/\r
96EFI_STATUS\r
97EFIAPI\r
98GetNumberOfProcessors (\r
99 IN EFI_MP_SERVICES_PROTOCOL *This,\r
100 OUT UINTN *NumberOfProcessors,\r
101 OUT UINTN *NumberOfEnabledProcessors\r
102 )\r
103{\r
104 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
105 return EFI_INVALID_PARAMETER;\r
106 }\r
107\r
108 if (!IsBSP ()) {\r
109 return EFI_DEVICE_ERROR;\r
110 }\r
111\r
112 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;\r
113 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\r
114 return EFI_SUCCESS;\r
115}\r
116\r
e7938b5a
CF
117/**\r
118 Gets detailed MP-related information on the requested processor at the\r
119 instant this call is made. This service may only be called from the BSP.\r
120\r
121 This service retrieves detailed MP-related information about any processor\r
122 on the platform. Note the following:\r
123 - The processor information may change during the course of a boot session.\r
124 - The information presented here is entirely MP related.\r
125\r
126 Information regarding the number of caches and their sizes, frequency of operation,\r
127 slot numbers is all considered platform-related information and is not provided\r
128 by this service.\r
129\r
130 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
131 instance.\r
132 @param[in] ProcessorNumber The handle number of processor.\r
133 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
134 the requested processor is deposited.\r
135\r
136 @retval EFI_SUCCESS Processor information was returned.\r
137 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
138 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
139 @retval EFI_NOT_FOUND The processor with the handle specified by\r
140 ProcessorNumber does not exist in the platform.\r
141\r
142**/\r
143EFI_STATUS\r
144EFIAPI\r
145GetProcessorInfo (\r
146 IN EFI_MP_SERVICES_PROTOCOL *This,\r
147 IN UINTN ProcessorNumber,\r
148 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
149 )\r
150{\r
151 if (ProcessorInfoBuffer == NULL) {\r
152 return EFI_INVALID_PARAMETER;\r
153 }\r
154\r
155 if (!IsBSP ()) {\r
156 return EFI_DEVICE_ERROR;\r
157 }\r
158\r
159 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
160 return EFI_NOT_FOUND;\r
161 }\r
162\r
163 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
164 return EFI_SUCCESS;\r
165}\r
166\r
cfa2fac1
CF
167/**\r
168 This return the handle number for the calling processor. This service may be\r
169 called from the BSP and APs.\r
170\r
171 This service returns the processor handle number for the calling processor.\r
172 The returned value is in the range from 0 to the total number of logical\r
173 processors minus 1. The total number of logical processors can be retrieved\r
174 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
175 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
176 is returned. Otherwise, the current processors handle number is returned in\r
177 ProcessorNumber, and EFI_SUCCESS is returned.\r
178\r
179 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
180 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
181 BSP. The range is from 0 to the total number of\r
182 logical processors minus 1. The total number of\r
183 logical processors can be retrieved by\r
184 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
185\r
186 @retval EFI_SUCCESS The current processor handle number was returned\r
187 in ProcessorNumber.\r
188 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
189\r
190**/\r
191EFI_STATUS\r
192EFIAPI\r
193WhoAmI (\r
194 IN EFI_MP_SERVICES_PROTOCOL *This,\r
195 OUT UINTN *ProcessorNumber\r
196 )\r
197{\r
198 UINTN Index;\r
199 UINT32 ProcessorId;\r
200\r
201 if (ProcessorNumber == NULL) {\r
202 return EFI_INVALID_PARAMETER;\r
203 }\r
204\r
205 ProcessorId = GetApicId ();\r
206 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
207 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
208 break;\r
209 }\r
210 }\r
211\r
212 *ProcessorNumber = Index;\r
213 return EFI_SUCCESS;\r
214}\r
215\r
e343f8f7
CF
216/**\r
217 Application Processors do loop routine\r
218 after switch to its own stack.\r
219\r
220 @param Context1 A pointer to the context to pass into the function.\r
221 @param Context2 A pointer to the context to pass into the function.\r
222\r
223**/\r
224VOID\r
225ProcessorToIdleState (\r
226 IN VOID *Context1, OPTIONAL\r
227 IN VOID *Context2 OPTIONAL\r
228 )\r
229{\r
230 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
231\r
232 AsmApDoneWithCommonStack ();\r
233\r
234 CpuSleep ();\r
235 CpuDeadLoop ();\r
236}\r
237\r
1535c888
JJ
238/**\r
239 Application Processor C code entry point.\r
240\r
241**/\r
242VOID\r
243EFIAPI\r
244ApEntryPointInC (\r
245 VOID\r
246 )\r
247{\r
03673ae1
CF
248 VOID* TopOfApStack;\r
249\r
250 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
251 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
252 mApStackStart = TopOfApStack;\r
253\r
254 mMpSystemData.NumberOfProcessors++;\r
e343f8f7
CF
255\r
256 SwitchStack (\r
257 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
258 NULL,\r
259 NULL,\r
03673ae1
CF
260 TopOfApStack);\r
261}\r
262\r
263/**\r
264 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
265\r
266 @param Bsp TRUE if the CPU is BSP\r
267 @param ProcessorNumber The specific processor number\r
268\r
269 @retval EFI_SUCCESS Data for the processor collected and filled in\r
270\r
271**/\r
272EFI_STATUS\r
273FillInProcessorInformation (\r
274 IN BOOLEAN Bsp,\r
275 IN UINTN ProcessorNumber\r
276 )\r
277{\r
278 CPU_DATA_BLOCK *CpuData;\r
279 UINT32 ProcessorId;\r
280\r
281 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
282 ProcessorId = GetApicId ();\r
283 CpuData->Info.ProcessorId = ProcessorId;\r
284 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
285 if (Bsp) {\r
286 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
287 }\r
288 CpuData->Info.Location.Package = ProcessorId;\r
289 CpuData->Info.Location.Core = 0;\r
290 CpuData->Info.Location.Thread = 0;\r
291 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
292\r
293 CpuData->Procedure = NULL;\r
294 CpuData->Parameter = NULL;\r
295 InitializeSpinLock (&CpuData->CpuDataLock);\r
296\r
297 return EFI_SUCCESS;\r
1535c888
JJ
298}\r
299\r
03673ae1
CF
300/**\r
301 Prepare the System Data.\r
302\r
303 @retval EFI_SUCCESS the System Data finished initilization.\r
304\r
305**/\r
306EFI_STATUS\r
307InitMpSystemData (\r
308 VOID\r
309 )\r
310{\r
311 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
312\r
313 mMpSystemData.NumberOfProcessors = 1;\r
314 mMpSystemData.NumberOfEnabledProcessors = 1;\r
315\r
316 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
317 ASSERT(mMpSystemData.CpuDatas != NULL);\r
318\r
319 //\r
320 // BSP\r
321 //\r
322 FillInProcessorInformation (TRUE, 0);\r
323\r
324 return EFI_SUCCESS;\r
325}\r
1535c888 326\r
6022e28c
JJ
327/**\r
328 Initialize Multi-processor support.\r
329\r
330**/\r
331VOID\r
332InitializeMpSupport (\r
333 VOID\r
334 )\r
335{\r
6a26a597
CF
336 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
337 if (gMaxLogicalProcessorNumber < 1) {\r
338 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
339 return;\r
340 }\r
341\r
342 if (gMaxLogicalProcessorNumber == 1) {\r
343 return;\r
344 }\r
345\r
346 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
347 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
348\r
349 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
350 ASSERT (mApStackStart != NULL);\r
6022e28c 351\r
6a26a597
CF
352 //\r
353 // the first buffer of stack size used for common stack, when the amount of AP\r
354 // more than 1, we should never free the common stack which maybe used for AP reset.\r
355 //\r
356 mCommonStack = mApStackStart;\r
357 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
358 mApStackStart = mTopOfApCommonStack;\r
359\r
03673ae1 360 InitMpSystemData ();\r
6a26a597 361\r
03673ae1 362 if (mMpSystemData.NumberOfProcessors == 1) {\r
6a26a597
CF
363 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
364 return;\r
365 }\r
366\r
03673ae1
CF
367 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
368 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
369 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
370 gApStackSize));\r
6a26a597
CF
371 }\r
372}\r