]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Protocol:GetNumberOfProcessors()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
... / ...
CommitLineData
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
18UINTN gMaxLogicalProcessorNumber;\r
19UINTN gApStackSize;\r
20\r
21MP_SYSTEM_DATA mMpSystemData;\r
22\r
23VOID *mCommonStack = 0;\r
24VOID *mTopOfApCommonStack = 0;\r
25VOID *mApStackStart = 0;\r
26\r
27EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
28 GetNumberOfProcessors,\r
29 NULL, // GetProcessorInfo,\r
30 NULL, // StartupAllAPs,\r
31 NULL, // StartupThisAP,\r
32 NULL, // SwitchBSP,\r
33 NULL, // EnableDisableAP,\r
34 WhoAmI\r
35};\r
36\r
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
117/**\r
118 This return the handle number for the calling processor. This service may be\r
119 called from the BSP and APs.\r
120\r
121 This service returns the processor handle number for the calling processor.\r
122 The returned value is in the range from 0 to the total number of logical\r
123 processors minus 1. The total number of logical processors can be retrieved\r
124 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
125 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
126 is returned. Otherwise, the current processors handle number is returned in\r
127 ProcessorNumber, and EFI_SUCCESS is returned.\r
128\r
129 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
130 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
131 BSP. The range is from 0 to the total number of\r
132 logical processors minus 1. The total number of\r
133 logical processors can be retrieved by\r
134 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
135\r
136 @retval EFI_SUCCESS The current processor handle number was returned\r
137 in ProcessorNumber.\r
138 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
139\r
140**/\r
141EFI_STATUS\r
142EFIAPI\r
143WhoAmI (\r
144 IN EFI_MP_SERVICES_PROTOCOL *This,\r
145 OUT UINTN *ProcessorNumber\r
146 )\r
147{\r
148 UINTN Index;\r
149 UINT32 ProcessorId;\r
150\r
151 if (ProcessorNumber == NULL) {\r
152 return EFI_INVALID_PARAMETER;\r
153 }\r
154\r
155 ProcessorId = GetApicId ();\r
156 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
157 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
158 break;\r
159 }\r
160 }\r
161\r
162 *ProcessorNumber = Index;\r
163 return EFI_SUCCESS;\r
164}\r
165\r
166/**\r
167 Application Processors do loop routine\r
168 after switch to its own stack.\r
169\r
170 @param Context1 A pointer to the context to pass into the function.\r
171 @param Context2 A pointer to the context to pass into the function.\r
172\r
173**/\r
174VOID\r
175ProcessorToIdleState (\r
176 IN VOID *Context1, OPTIONAL\r
177 IN VOID *Context2 OPTIONAL\r
178 )\r
179{\r
180 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
181\r
182 AsmApDoneWithCommonStack ();\r
183\r
184 CpuSleep ();\r
185 CpuDeadLoop ();\r
186}\r
187\r
188/**\r
189 Application Processor C code entry point.\r
190\r
191**/\r
192VOID\r
193EFIAPI\r
194ApEntryPointInC (\r
195 VOID\r
196 )\r
197{\r
198 VOID* TopOfApStack;\r
199\r
200 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
201 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
202 mApStackStart = TopOfApStack;\r
203\r
204 mMpSystemData.NumberOfProcessors++;\r
205\r
206 SwitchStack (\r
207 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
208 NULL,\r
209 NULL,\r
210 TopOfApStack);\r
211}\r
212\r
213/**\r
214 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
215\r
216 @param Bsp TRUE if the CPU is BSP\r
217 @param ProcessorNumber The specific processor number\r
218\r
219 @retval EFI_SUCCESS Data for the processor collected and filled in\r
220\r
221**/\r
222EFI_STATUS\r
223FillInProcessorInformation (\r
224 IN BOOLEAN Bsp,\r
225 IN UINTN ProcessorNumber\r
226 )\r
227{\r
228 CPU_DATA_BLOCK *CpuData;\r
229 UINT32 ProcessorId;\r
230\r
231 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
232 ProcessorId = GetApicId ();\r
233 CpuData->Info.ProcessorId = ProcessorId;\r
234 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
235 if (Bsp) {\r
236 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
237 }\r
238 CpuData->Info.Location.Package = ProcessorId;\r
239 CpuData->Info.Location.Core = 0;\r
240 CpuData->Info.Location.Thread = 0;\r
241 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
242\r
243 CpuData->Procedure = NULL;\r
244 CpuData->Parameter = NULL;\r
245 InitializeSpinLock (&CpuData->CpuDataLock);\r
246\r
247 return EFI_SUCCESS;\r
248}\r
249\r
250/**\r
251 Prepare the System Data.\r
252\r
253 @retval EFI_SUCCESS the System Data finished initilization.\r
254\r
255**/\r
256EFI_STATUS\r
257InitMpSystemData (\r
258 VOID\r
259 )\r
260{\r
261 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
262\r
263 mMpSystemData.NumberOfProcessors = 1;\r
264 mMpSystemData.NumberOfEnabledProcessors = 1;\r
265\r
266 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
267 ASSERT(mMpSystemData.CpuDatas != NULL);\r
268\r
269 //\r
270 // BSP\r
271 //\r
272 FillInProcessorInformation (TRUE, 0);\r
273\r
274 return EFI_SUCCESS;\r
275}\r
276\r
277/**\r
278 Initialize Multi-processor support.\r
279\r
280**/\r
281VOID\r
282InitializeMpSupport (\r
283 VOID\r
284 )\r
285{\r
286 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
287 if (gMaxLogicalProcessorNumber < 1) {\r
288 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
289 return;\r
290 }\r
291\r
292 if (gMaxLogicalProcessorNumber == 1) {\r
293 return;\r
294 }\r
295\r
296 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
297 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
298\r
299 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
300 ASSERT (mApStackStart != NULL);\r
301\r
302 //\r
303 // the first buffer of stack size used for common stack, when the amount of AP\r
304 // more than 1, we should never free the common stack which maybe used for AP reset.\r
305 //\r
306 mCommonStack = mApStackStart;\r
307 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
308 mApStackStart = mTopOfApCommonStack;\r
309\r
310 InitMpSystemData ();\r
311\r
312 if (mMpSystemData.NumberOfProcessors == 1) {\r
313 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
314 return;\r
315 }\r
316\r
317 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
318 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
319 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
320 gApStackSize));\r
321 }\r
322}\r