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