]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Protocol:EnableDisableAP()
[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 GetProcessorInfo,\r
30 NULL, // StartupAllAPs,\r
31 NULL, // StartupThisAP,\r
32 NULL, // SwitchBSP,\r
33 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 Get the Application Processors state.\r
62\r
63 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
64\r
65 @retval CPU_STATE the AP status\r
66\r
67**/\r
68CPU_STATE\r
69GetApState (\r
70 IN CPU_DATA_BLOCK *CpuData\r
71 )\r
72{\r
73 CPU_STATE State;\r
74\r
75 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
76 CpuPause ();\r
77 }\r
78\r
79 State = CpuData->State;\r
80 ReleaseSpinLock (&CpuData->CpuDataLock);\r
81\r
82 return State;\r
83}\r
84\r
85/**\r
86 Check the Application Processors Status whether contains the Flags.\r
87\r
88 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
89 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
90\r
91 @retval TRUE the AP status includes the StatusFlag\r
92 @retval FALSE the AP status excludes the StatusFlag\r
93\r
94**/\r
95BOOLEAN\r
96TestCpuStatusFlag (\r
97 IN CPU_DATA_BLOCK *CpuData,\r
98 IN UINT32 Flags\r
99 )\r
100{\r
101 UINT32 Ret;\r
102\r
103 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
104 CpuPause ();\r
105 }\r
106\r
107 Ret = CpuData->Info.StatusFlag & Flags;\r
108 ReleaseSpinLock (&CpuData->CpuDataLock);\r
109\r
110 return !!(Ret);\r
111}\r
112\r
113/**\r
114 Bitwise-Or of the Application Processors Status with the Flags.\r
115\r
116 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
117 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
118\r
119**/\r
120VOID\r
121CpuStatusFlagOr (\r
122 IN CPU_DATA_BLOCK *CpuData,\r
123 IN UINT32 Flags\r
124 )\r
125{\r
126 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
127 CpuPause ();\r
128 }\r
129\r
130 CpuData->Info.StatusFlag |= Flags;\r
131 ReleaseSpinLock (&CpuData->CpuDataLock);\r
132}\r
133\r
134/**\r
135 Bitwise-AndNot of the Application Processors Status with the Flags.\r
136\r
137 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
138 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
139\r
140**/\r
141VOID\r
142CpuStatusFlagAndNot (\r
143 IN CPU_DATA_BLOCK *CpuData,\r
144 IN UINT32 Flags\r
145 )\r
146{\r
147 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
148 CpuPause ();\r
149 }\r
150\r
151 CpuData->Info.StatusFlag &= ~Flags;\r
152 ReleaseSpinLock (&CpuData->CpuDataLock);\r
153}\r
154\r
155/**\r
156 This service retrieves the number of logical processor in the platform\r
157 and the number of those logical processors that are enabled on this boot.\r
158 This service may only be called from the BSP.\r
159\r
160 This function is used to retrieve the following information:\r
161 - The number of logical processors that are present in the system.\r
162 - The number of enabled logical processors in the system at the instant\r
163 this call is made.\r
164\r
165 Because MP Service Protocol provides services to enable and disable processors\r
166 dynamically, the number of enabled logical processors may vary during the\r
167 course of a boot session.\r
168\r
169 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
170 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
171 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
172 is returned in NumberOfProcessors, the number of currently enabled processor\r
173 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
174\r
175 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
176 instance.\r
177 @param[out] NumberOfProcessors Pointer to the total number of logical\r
178 processors in the system, including the BSP\r
179 and disabled APs.\r
180 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
181 processors that exist in system, including\r
182 the BSP.\r
183\r
184 @retval EFI_SUCCESS The number of logical processors and enabled\r
185 logical processors was retrieved.\r
186 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
187 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
188 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
189\r
190**/\r
191EFI_STATUS\r
192EFIAPI\r
193GetNumberOfProcessors (\r
194 IN EFI_MP_SERVICES_PROTOCOL *This,\r
195 OUT UINTN *NumberOfProcessors,\r
196 OUT UINTN *NumberOfEnabledProcessors\r
197 )\r
198{\r
199 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
200 return EFI_INVALID_PARAMETER;\r
201 }\r
202\r
203 if (!IsBSP ()) {\r
204 return EFI_DEVICE_ERROR;\r
205 }\r
206\r
207 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;\r
208 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\r
209 return EFI_SUCCESS;\r
210}\r
211\r
212/**\r
213 Gets detailed MP-related information on the requested processor at the\r
214 instant this call is made. This service may only be called from the BSP.\r
215\r
216 This service retrieves detailed MP-related information about any processor\r
217 on the platform. Note the following:\r
218 - The processor information may change during the course of a boot session.\r
219 - The information presented here is entirely MP related.\r
220\r
221 Information regarding the number of caches and their sizes, frequency of operation,\r
222 slot numbers is all considered platform-related information and is not provided\r
223 by this service.\r
224\r
225 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
226 instance.\r
227 @param[in] ProcessorNumber The handle number of processor.\r
228 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
229 the requested processor is deposited.\r
230\r
231 @retval EFI_SUCCESS Processor information was returned.\r
232 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
233 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
234 @retval EFI_NOT_FOUND The processor with the handle specified by\r
235 ProcessorNumber does not exist in the platform.\r
236\r
237**/\r
238EFI_STATUS\r
239EFIAPI\r
240GetProcessorInfo (\r
241 IN EFI_MP_SERVICES_PROTOCOL *This,\r
242 IN UINTN ProcessorNumber,\r
243 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
244 )\r
245{\r
246 if (ProcessorInfoBuffer == NULL) {\r
247 return EFI_INVALID_PARAMETER;\r
248 }\r
249\r
250 if (!IsBSP ()) {\r
251 return EFI_DEVICE_ERROR;\r
252 }\r
253\r
254 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
255 return EFI_NOT_FOUND;\r
256 }\r
257\r
258 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
259 return EFI_SUCCESS;\r
260}\r
261\r
262/**\r
263 This service lets the caller enable or disable an AP from this point onward.\r
264 This service may only be called from the BSP.\r
265\r
266 This service allows the caller enable or disable an AP from this point onward.\r
267 The caller can optionally specify the health status of the AP by Health. If\r
268 an AP is being disabled, then the state of the disabled AP is implementation\r
269 dependent. If an AP is enabled, then the implementation must guarantee that a\r
270 complete initialization sequence is performed on the AP, so the AP is in a state\r
271 that is compatible with an MP operating system. This service may not be supported\r
272 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
273\r
274 If the enable or disable AP operation cannot be completed prior to the return\r
275 from this service, then EFI_UNSUPPORTED must be returned.\r
276\r
277 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
278 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
279 BSP. The range is from 0 to the total number of\r
280 logical processors minus 1. The total number of\r
281 logical processors can be retrieved by\r
282 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
283 @param[in] EnableAP Specifies the new state for the processor for\r
284 enabled, FALSE for disabled.\r
285 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
286 the new health status of the AP. This flag\r
287 corresponds to StatusFlag defined in\r
288 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
289 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
290 bits are ignored. If it is NULL, this parameter\r
291 is ignored.\r
292\r
293 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
294 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
295 prior to this service returning.\r
296 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
297 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
298 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
299 does not exist.\r
300 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
301\r
302**/\r
303EFI_STATUS\r
304EFIAPI\r
305EnableDisableAP (\r
306 IN EFI_MP_SERVICES_PROTOCOL *This,\r
307 IN UINTN ProcessorNumber,\r
308 IN BOOLEAN EnableAP,\r
309 IN UINT32 *HealthFlag OPTIONAL\r
310 )\r
311{\r
312 CPU_DATA_BLOCK *CpuData;\r
313\r
314 if (!IsBSP ()) {\r
315 return EFI_DEVICE_ERROR;\r
316 }\r
317\r
318 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
319 return EFI_NOT_FOUND;\r
320 }\r
321\r
322 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
323 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
324 return EFI_INVALID_PARAMETER;\r
325 }\r
326\r
327 if (GetApState (CpuData) != CpuStateIdle) {\r
328 return EFI_UNSUPPORTED;\r
329 }\r
330\r
331 if (EnableAP) {\r
332 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
333 mMpSystemData.NumberOfEnabledProcessors++;\r
334 }\r
335 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
336 } else {\r
337 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
338 mMpSystemData.NumberOfEnabledProcessors--;\r
339 }\r
340 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
341 }\r
342\r
343 if (HealthFlag != NULL) {\r
344 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
345 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
346 }\r
347\r
348 return EFI_SUCCESS;\r
349}\r
350\r
351/**\r
352 This return the handle number for the calling processor. This service may be\r
353 called from the BSP and APs.\r
354\r
355 This service returns the processor handle number for the calling processor.\r
356 The returned value is in the range from 0 to the total number of logical\r
357 processors minus 1. The total number of logical processors can be retrieved\r
358 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
359 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
360 is returned. Otherwise, the current processors handle number is returned in\r
361 ProcessorNumber, and EFI_SUCCESS is returned.\r
362\r
363 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
364 @param[out] ProcessorNumber The handle number of AP that is to become the new\r
365 BSP. The range is from 0 to the total number of\r
366 logical processors minus 1. The total number of\r
367 logical processors can be retrieved by\r
368 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
369\r
370 @retval EFI_SUCCESS The current processor handle number was returned\r
371 in ProcessorNumber.\r
372 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
373\r
374**/\r
375EFI_STATUS\r
376EFIAPI\r
377WhoAmI (\r
378 IN EFI_MP_SERVICES_PROTOCOL *This,\r
379 OUT UINTN *ProcessorNumber\r
380 )\r
381{\r
382 UINTN Index;\r
383 UINT32 ProcessorId;\r
384\r
385 if (ProcessorNumber == NULL) {\r
386 return EFI_INVALID_PARAMETER;\r
387 }\r
388\r
389 ProcessorId = GetApicId ();\r
390 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
391 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
392 break;\r
393 }\r
394 }\r
395\r
396 *ProcessorNumber = Index;\r
397 return EFI_SUCCESS;\r
398}\r
399\r
400/**\r
401 Application Processors do loop routine\r
402 after switch to its own stack.\r
403\r
404 @param Context1 A pointer to the context to pass into the function.\r
405 @param Context2 A pointer to the context to pass into the function.\r
406\r
407**/\r
408VOID\r
409ProcessorToIdleState (\r
410 IN VOID *Context1, OPTIONAL\r
411 IN VOID *Context2 OPTIONAL\r
412 )\r
413{\r
414 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
415\r
416 AsmApDoneWithCommonStack ();\r
417\r
418 CpuSleep ();\r
419 CpuDeadLoop ();\r
420}\r
421\r
422/**\r
423 Application Processor C code entry point.\r
424\r
425**/\r
426VOID\r
427EFIAPI\r
428ApEntryPointInC (\r
429 VOID\r
430 )\r
431{\r
432 VOID* TopOfApStack;\r
433\r
434 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
435 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
436 mApStackStart = TopOfApStack;\r
437\r
438 mMpSystemData.NumberOfProcessors++;\r
439\r
440 SwitchStack (\r
441 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
442 NULL,\r
443 NULL,\r
444 TopOfApStack);\r
445}\r
446\r
447/**\r
448 This function is called by all processors (both BSP and AP) once and collects MP related data.\r
449\r
450 @param Bsp TRUE if the CPU is BSP\r
451 @param ProcessorNumber The specific processor number\r
452\r
453 @retval EFI_SUCCESS Data for the processor collected and filled in\r
454\r
455**/\r
456EFI_STATUS\r
457FillInProcessorInformation (\r
458 IN BOOLEAN Bsp,\r
459 IN UINTN ProcessorNumber\r
460 )\r
461{\r
462 CPU_DATA_BLOCK *CpuData;\r
463 UINT32 ProcessorId;\r
464\r
465 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
466 ProcessorId = GetApicId ();\r
467 CpuData->Info.ProcessorId = ProcessorId;\r
468 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
469 if (Bsp) {\r
470 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
471 }\r
472 CpuData->Info.Location.Package = ProcessorId;\r
473 CpuData->Info.Location.Core = 0;\r
474 CpuData->Info.Location.Thread = 0;\r
475 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
476\r
477 CpuData->Procedure = NULL;\r
478 CpuData->Parameter = NULL;\r
479 InitializeSpinLock (&CpuData->CpuDataLock);\r
480\r
481 return EFI_SUCCESS;\r
482}\r
483\r
484/**\r
485 Prepare the System Data.\r
486\r
487 @retval EFI_SUCCESS the System Data finished initilization.\r
488\r
489**/\r
490EFI_STATUS\r
491InitMpSystemData (\r
492 VOID\r
493 )\r
494{\r
495 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
496\r
497 mMpSystemData.NumberOfProcessors = 1;\r
498 mMpSystemData.NumberOfEnabledProcessors = 1;\r
499\r
500 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
501 ASSERT(mMpSystemData.CpuDatas != NULL);\r
502\r
503 //\r
504 // BSP\r
505 //\r
506 FillInProcessorInformation (TRUE, 0);\r
507\r
508 return EFI_SUCCESS;\r
509}\r
510\r
511/**\r
512 Initialize Multi-processor support.\r
513\r
514**/\r
515VOID\r
516InitializeMpSupport (\r
517 VOID\r
518 )\r
519{\r
520 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
521 if (gMaxLogicalProcessorNumber < 1) {\r
522 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
523 return;\r
524 }\r
525\r
526 if (gMaxLogicalProcessorNumber == 1) {\r
527 return;\r
528 }\r
529\r
530 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
531 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
532\r
533 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
534 ASSERT (mApStackStart != NULL);\r
535\r
536 //\r
537 // the first buffer of stack size used for common stack, when the amount of AP\r
538 // more than 1, we should never free the common stack which maybe used for AP reset.\r
539 //\r
540 mCommonStack = mApStackStart;\r
541 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
542 mApStackStart = mTopOfApCommonStack;\r
543\r
544 InitMpSystemData ();\r
545\r
546 if (mMpSystemData.NumberOfProcessors == 1) {\r
547 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
548 return;\r
549 }\r
550\r
551 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
552 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
553 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
554 gApStackSize));\r
555 }\r
556}\r