]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Services:StartupAllAPs()
[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 UINTN gPollInterval = 100; // 100 microseconds
21
22 MP_SYSTEM_DATA mMpSystemData;
23
24 VOID *mCommonStack = 0;
25 VOID *mTopOfApCommonStack = 0;
26 VOID *mApStackStart = 0;
27
28 EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {
29 GetNumberOfProcessors,
30 GetProcessorInfo,
31 StartupAllAPs,
32 StartupThisAP,
33 NULL, // SwitchBSP,
34 EnableDisableAP,
35 WhoAmI
36 };
37
38 /**
39 Check whether caller processor is BSP.
40
41 @retval TRUE the caller is BSP
42 @retval FALSE the caller is AP
43
44 **/
45 BOOLEAN
46 IsBSP (
47 VOID
48 )
49 {
50 UINTN CpuIndex;
51 CPU_DATA_BLOCK *CpuData;
52
53 CpuData = NULL;
54
55 WhoAmI (&mMpServicesTemplate, &CpuIndex);
56 CpuData = &mMpSystemData.CpuDatas[CpuIndex];
57
58 return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;
59 }
60
61 /**
62 Get the Application Processors state.
63
64 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
65
66 @retval CPU_STATE the AP status
67
68 **/
69 CPU_STATE
70 GetApState (
71 IN CPU_DATA_BLOCK *CpuData
72 )
73 {
74 CPU_STATE State;
75
76 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
77 CpuPause ();
78 }
79
80 State = CpuData->State;
81 ReleaseSpinLock (&CpuData->CpuDataLock);
82
83 return State;
84 }
85
86 /**
87 Set the Application Processors state.
88
89 @param CpuData The pointer to CPU_DATA_BLOCK of specified AP
90 @param State The AP status
91
92 **/
93 VOID
94 SetApState (
95 IN CPU_DATA_BLOCK *CpuData,
96 IN CPU_STATE State
97 )
98 {
99 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
100 CpuPause ();
101 }
102
103 CpuData->State = State;
104 ReleaseSpinLock (&CpuData->CpuDataLock);
105 }
106
107 /**
108 Set the Application Processor prepare to run a function specified
109 by Params.
110
111 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
112 @param Procedure A pointer to the function to be run on enabled APs of the system
113 @param ProcedureArgument Pointer to the optional parameter of the assigned function
114
115 **/
116 VOID
117 SetApProcedure (
118 IN CPU_DATA_BLOCK *CpuData,
119 IN EFI_AP_PROCEDURE Procedure,
120 IN VOID *ProcedureArgument
121 )
122 {
123 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
124 CpuPause ();
125 }
126
127 CpuData->Parameter = ProcedureArgument;
128 CpuData->Procedure = Procedure;
129 ReleaseSpinLock (&CpuData->CpuDataLock);
130 }
131
132 /**
133 Check the Application Processors Status whether contains the Flags.
134
135 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
136 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
137
138 @retval TRUE the AP status includes the StatusFlag
139 @retval FALSE the AP status excludes the StatusFlag
140
141 **/
142 BOOLEAN
143 TestCpuStatusFlag (
144 IN CPU_DATA_BLOCK *CpuData,
145 IN UINT32 Flags
146 )
147 {
148 UINT32 Ret;
149
150 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
151 CpuPause ();
152 }
153
154 Ret = CpuData->Info.StatusFlag & Flags;
155 ReleaseSpinLock (&CpuData->CpuDataLock);
156
157 return !!(Ret);
158 }
159
160 /**
161 Bitwise-Or of the Application Processors Status with the Flags.
162
163 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
164 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
165
166 **/
167 VOID
168 CpuStatusFlagOr (
169 IN CPU_DATA_BLOCK *CpuData,
170 IN UINT32 Flags
171 )
172 {
173 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
174 CpuPause ();
175 }
176
177 CpuData->Info.StatusFlag |= Flags;
178 ReleaseSpinLock (&CpuData->CpuDataLock);
179 }
180
181 /**
182 Bitwise-AndNot of the Application Processors Status with the Flags.
183
184 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
185 @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION
186
187 **/
188 VOID
189 CpuStatusFlagAndNot (
190 IN CPU_DATA_BLOCK *CpuData,
191 IN UINT32 Flags
192 )
193 {
194 while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {
195 CpuPause ();
196 }
197
198 CpuData->Info.StatusFlag &= ~Flags;
199 ReleaseSpinLock (&CpuData->CpuDataLock);
200 }
201
202 /**
203 Searches for the next blocking AP.
204
205 Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().
206
207 @param NextNumber Pointer to the processor number of the next blocking AP.
208
209 @retval EFI_SUCCESS The next blocking AP has been found.
210 @retval EFI_NOT_FOUND No blocking AP exists.
211
212 **/
213 EFI_STATUS
214 GetNextBlockedNumber (
215 OUT UINTN *NextNumber
216 )
217 {
218 UINTN Number;
219 CPU_STATE CpuState;
220 CPU_DATA_BLOCK *CpuData;
221
222 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
223 CpuData = &mMpSystemData.CpuDatas[Number];
224 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
225 //
226 // Skip BSP
227 //
228 continue;
229 }
230
231 CpuState = GetApState (CpuData);
232 if (CpuState == CpuStateBlocked) {
233 *NextNumber = Number;
234 return EFI_SUCCESS;
235 }
236 }
237
238 return EFI_NOT_FOUND;
239 }
240
241 /**
242 Check if the APs state are finished, and update them to idle state
243 by StartupAllAPs().
244
245 **/
246 VOID
247 CheckAndUpdateAllAPsToIdleState (
248 VOID
249 )
250 {
251 UINTN ProcessorNumber;
252 UINTN NextNumber;
253 CPU_DATA_BLOCK *CpuData;
254 EFI_STATUS Status;
255 CPU_STATE CpuState;
256
257 for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) {
258 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
259 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
260 //
261 // Skip BSP
262 //
263 continue;
264 }
265
266 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
267 //
268 // Skip Disabled processors
269 //
270 continue;
271 }
272
273 CpuState = GetApState (CpuData);
274 if (CpuState == CpuStateFinished) {
275 mMpSystemData.FinishCount++;
276 if (mMpSystemData.SingleThread) {
277 Status = GetNextBlockedNumber (&NextNumber);
278 if (!EFI_ERROR (Status)) {
279 SetApState (&mMpSystemData.CpuDatas[NextNumber], CpuStateReady);
280 SetApProcedure (&mMpSystemData.CpuDatas[NextNumber],
281 mMpSystemData.Procedure,
282 mMpSystemData.ProcedureArgument);
283 }
284 }
285
286 SetApState (CpuData, CpuStateIdle);
287 }
288 }
289 }
290
291 /**
292 If the timeout expires before all APs returns from Procedure,
293 we should forcibly terminate the executing AP and fill FailedList back
294 by StartupAllAPs().
295
296 **/
297 VOID
298 ResetAllFailedAPs (
299 VOID
300 )
301 {
302 CPU_DATA_BLOCK *CpuData;
303 UINTN Number;
304 CPU_STATE CpuState;
305
306 if (mMpSystemData.FailedList != NULL) {
307 *mMpSystemData.FailedList = AllocatePool ((mMpSystemData.StartCount - mMpSystemData.FinishCount + 1) * sizeof(UINTN));
308 ASSERT (*mMpSystemData.FailedList != NULL);
309 }
310
311 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
312 CpuData = &mMpSystemData.CpuDatas[Number];
313 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
314 //
315 // Skip BSP
316 //
317 continue;
318 }
319
320 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
321 //
322 // Skip Disabled processors
323 //
324 continue;
325 }
326
327 CpuState = GetApState (CpuData);
328 if (CpuState != CpuStateIdle) {
329 if (mMpSystemData.FailedList != NULL) {
330 (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number;
331 }
332 ResetProcessorToIdleState (CpuData);
333 }
334 }
335
336 if (mMpSystemData.FailedList != NULL) {
337 (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex] = END_OF_CPU_LIST;
338 }
339 }
340
341 /**
342 This service retrieves the number of logical processor in the platform
343 and the number of those logical processors that are enabled on this boot.
344 This service may only be called from the BSP.
345
346 This function is used to retrieve the following information:
347 - The number of logical processors that are present in the system.
348 - The number of enabled logical processors in the system at the instant
349 this call is made.
350
351 Because MP Service Protocol provides services to enable and disable processors
352 dynamically, the number of enabled logical processors may vary during the
353 course of a boot session.
354
355 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
356 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
357 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
358 is returned in NumberOfProcessors, the number of currently enabled processor
359 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
360
361 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
362 instance.
363 @param[out] NumberOfProcessors Pointer to the total number of logical
364 processors in the system, including the BSP
365 and disabled APs.
366 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
367 processors that exist in system, including
368 the BSP.
369
370 @retval EFI_SUCCESS The number of logical processors and enabled
371 logical processors was retrieved.
372 @retval EFI_DEVICE_ERROR The calling processor is an AP.
373 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
374 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
375
376 **/
377 EFI_STATUS
378 EFIAPI
379 GetNumberOfProcessors (
380 IN EFI_MP_SERVICES_PROTOCOL *This,
381 OUT UINTN *NumberOfProcessors,
382 OUT UINTN *NumberOfEnabledProcessors
383 )
384 {
385 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
386 return EFI_INVALID_PARAMETER;
387 }
388
389 if (!IsBSP ()) {
390 return EFI_DEVICE_ERROR;
391 }
392
393 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;
394 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;
395 return EFI_SUCCESS;
396 }
397
398 /**
399 Gets detailed MP-related information on the requested processor at the
400 instant this call is made. This service may only be called from the BSP.
401
402 This service retrieves detailed MP-related information about any processor
403 on the platform. Note the following:
404 - The processor information may change during the course of a boot session.
405 - The information presented here is entirely MP related.
406
407 Information regarding the number of caches and their sizes, frequency of operation,
408 slot numbers is all considered platform-related information and is not provided
409 by this service.
410
411 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
412 instance.
413 @param[in] ProcessorNumber The handle number of processor.
414 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
415 the requested processor is deposited.
416
417 @retval EFI_SUCCESS Processor information was returned.
418 @retval EFI_DEVICE_ERROR The calling processor is an AP.
419 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
420 @retval EFI_NOT_FOUND The processor with the handle specified by
421 ProcessorNumber does not exist in the platform.
422
423 **/
424 EFI_STATUS
425 EFIAPI
426 GetProcessorInfo (
427 IN EFI_MP_SERVICES_PROTOCOL *This,
428 IN UINTN ProcessorNumber,
429 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
430 )
431 {
432 if (ProcessorInfoBuffer == NULL) {
433 return EFI_INVALID_PARAMETER;
434 }
435
436 if (!IsBSP ()) {
437 return EFI_DEVICE_ERROR;
438 }
439
440 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
441 return EFI_NOT_FOUND;
442 }
443
444 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
445 return EFI_SUCCESS;
446 }
447
448 /**
449 This service executes a caller provided function on all enabled APs. APs can
450 run either simultaneously or one at a time in sequence. This service supports
451 both blocking and non-blocking requests. The non-blocking requests use EFI
452 events so the BSP can detect when the APs have finished. This service may only
453 be called from the BSP.
454
455 This function is used to dispatch all the enabled APs to the function specified
456 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
457 immediately and Procedure is not started on any AP.
458
459 If SingleThread is TRUE, all the enabled APs execute the function specified by
460 Procedure one by one, in ascending order of processor handle number. Otherwise,
461 all the enabled APs execute the function specified by Procedure simultaneously.
462
463 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all
464 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking
465 mode, and the BSP returns from this service without waiting for APs. If a
466 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
467 is signaled, then EFI_UNSUPPORTED must be returned.
468
469 If the timeout specified by TimeoutInMicroseconds expires before all APs return
470 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
471 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
472 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its
473 content points to the list of processor handle numbers in which Procedure was
474 terminated.
475
476 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
477 to make sure that the nature of the code that is executed on the BSP and the
478 dispatched APs is well controlled. The MP Services Protocol does not guarantee
479 that the Procedure function is MP-safe. Hence, the tasks that can be run in
480 parallel are limited to certain independent tasks and well-controlled exclusive
481 code. EFI services and protocols may not be called by APs unless otherwise
482 specified.
483
484 In blocking execution mode, BSP waits until all APs finish or
485 TimeoutInMicroseconds expires.
486
487 In non-blocking execution mode, BSP is freed to return to the caller and then
488 proceed to the next task without having to wait for APs. The following
489 sequence needs to occur in a non-blocking execution mode:
490
491 -# The caller that intends to use this MP Services Protocol in non-blocking
492 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller
493 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent
494 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests
495 the function specified by Procedure to be started on all the enabled APs,
496 and releases the BSP to continue with other tasks.
497 -# The caller can use the CheckEvent() and WaitForEvent() services to check
498 the state of the WaitEvent created in step 1.
499 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP
500 Service signals WaitEvent by calling the EFI SignalEvent() function. If
501 FailedCpuList is not NULL, its content is available when WaitEvent is
502 signaled. If all APs returned from Procedure prior to the timeout, then
503 FailedCpuList is set to NULL. If not all APs return from Procedure before
504 the timeout, then FailedCpuList is filled in with the list of the failed
505 APs. The buffer is allocated by MP Service Protocol using AllocatePool().
506 It is the caller's responsibility to free the buffer with FreePool() service.
507 -# This invocation of SignalEvent() function informs the caller that invoked
508 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed
509 the specified task or a timeout occurred. The contents of FailedCpuList
510 can be examined to determine which APs did not complete the specified task
511 prior to the timeout.
512
513 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
514 instance.
515 @param[in] Procedure A pointer to the function to be run on
516 enabled APs of the system. See type
517 EFI_AP_PROCEDURE.
518 @param[in] SingleThread If TRUE, then all the enabled APs execute
519 the function specified by Procedure one by
520 one, in ascending order of processor handle
521 number. If FALSE, then all the enabled APs
522 execute the function specified by Procedure
523 simultaneously.
524 @param[in] WaitEvent The event created by the caller with CreateEvent()
525 service. If it is NULL, then execute in
526 blocking mode. BSP waits until all APs finish
527 or TimeoutInMicroseconds expires. If it's
528 not NULL, then execute in non-blocking mode.
529 BSP requests the function specified by
530 Procedure to be started on all the enabled
531 APs, and go on executing immediately. If
532 all return from Procedure, or TimeoutInMicroseconds
533 expires, this event is signaled. The BSP
534 can use the CheckEvent() or WaitForEvent()
535 services to check the state of event. Type
536 EFI_EVENT is defined in CreateEvent() in
537 the Unified Extensible Firmware Interface
538 Specification.
539 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
540 APs to return from Procedure, either for
541 blocking or non-blocking mode. Zero means
542 infinity. If the timeout expires before
543 all APs return from Procedure, then Procedure
544 on the failed APs is terminated. All enabled
545 APs are available for next function assigned
546 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
547 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
548 If the timeout expires in blocking mode,
549 BSP returns EFI_TIMEOUT. If the timeout
550 expires in non-blocking mode, WaitEvent
551 is signaled with SignalEvent().
552 @param[in] ProcedureArgument The parameter passed into Procedure for
553 all APs.
554 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
555 if all APs finish successfully, then its
556 content is set to NULL. If not all APs
557 finish before timeout expires, then its
558 content is set to address of the buffer
559 holding handle numbers of the failed APs.
560 The buffer is allocated by MP Service Protocol,
561 and it's the caller's responsibility to
562 free the buffer with FreePool() service.
563 In blocking mode, it is ready for consumption
564 when the call returns. In non-blocking mode,
565 it is ready when WaitEvent is signaled. The
566 list of failed CPU is terminated by
567 END_OF_CPU_LIST.
568
569 @retval EFI_SUCCESS In blocking mode, all APs have finished before
570 the timeout expired.
571 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
572 to all enabled APs.
573 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
574 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
575 signaled.
576 @retval EFI_DEVICE_ERROR Caller processor is AP.
577 @retval EFI_NOT_STARTED No enabled APs exist in the system.
578 @retval EFI_NOT_READY Any enabled APs are busy.
579 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
580 all enabled APs have finished.
581 @retval EFI_INVALID_PARAMETER Procedure is NULL.
582
583 **/
584 EFI_STATUS
585 EFIAPI
586 StartupAllAPs (
587 IN EFI_MP_SERVICES_PROTOCOL *This,
588 IN EFI_AP_PROCEDURE Procedure,
589 IN BOOLEAN SingleThread,
590 IN EFI_EVENT WaitEvent OPTIONAL,
591 IN UINTN TimeoutInMicroseconds,
592 IN VOID *ProcedureArgument OPTIONAL,
593 OUT UINTN **FailedCpuList OPTIONAL
594 )
595 {
596 EFI_STATUS Status;
597 CPU_DATA_BLOCK *CpuData;
598 UINTN Number;
599 CPU_STATE APInitialState;
600
601 CpuData = NULL;
602
603 if (FailedCpuList != NULL) {
604 *FailedCpuList = NULL;
605 }
606
607 if (!IsBSP ()) {
608 return EFI_DEVICE_ERROR;
609 }
610
611 if (mMpSystemData.NumberOfProcessors == 1) {
612 return EFI_NOT_STARTED;
613 }
614
615 if (Procedure == NULL) {
616 return EFI_INVALID_PARAMETER;
617 }
618
619 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
620 CpuData = &mMpSystemData.CpuDatas[Number];
621 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
622 //
623 // Skip BSP
624 //
625 continue;
626 }
627
628 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
629 //
630 // Skip Disabled processors
631 //
632 continue;
633 }
634
635 if (GetApState (CpuData) != CpuStateIdle) {
636 return EFI_NOT_READY;
637 }
638 }
639
640 mMpSystemData.Procedure = Procedure;
641 mMpSystemData.ProcedureArgument = ProcedureArgument;
642 mMpSystemData.WaitEvent = WaitEvent;
643 mMpSystemData.Timeout = TimeoutInMicroseconds;
644 mMpSystemData.TimeoutActive = !!(TimeoutInMicroseconds);
645 mMpSystemData.FinishCount = 0;
646 mMpSystemData.StartCount = 0;
647 mMpSystemData.SingleThread = SingleThread;
648 mMpSystemData.FailedList = FailedCpuList;
649 mMpSystemData.FailedListIndex = 0;
650 APInitialState = CpuStateReady;
651
652 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
653 CpuData = &mMpSystemData.CpuDatas[Number];
654 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
655 //
656 // Skip BSP
657 //
658 continue;
659 }
660
661 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
662 //
663 // Skip Disabled processors
664 //
665 continue;
666 }
667
668 //
669 // Get APs prepared, and put failing APs into FailedCpuList
670 // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready
671 // state 1 by 1, until the previous 1 finished its task
672 // if not "SingleThread", all APs are put to ready state from the beginning
673 //
674 if (GetApState (CpuData) == CpuStateIdle) {
675 mMpSystemData.StartCount++;
676
677 SetApState (CpuData, APInitialState);
678
679 if (APInitialState == CpuStateReady) {
680 SetApProcedure (CpuData, Procedure, ProcedureArgument);
681 }
682
683 if (SingleThread) {
684 APInitialState = CpuStateBlocked;
685 }
686 }
687 }
688
689 if (WaitEvent != NULL) {
690 Status = gBS->SetTimer (
691 mMpSystemData.CheckAllAPsEvent,
692 TimerPeriodic,
693 EFI_TIMER_PERIOD_MICROSECONDS (100)
694 );
695 return Status;
696 }
697
698 while (TRUE) {
699 CheckAndUpdateAllAPsToIdleState ();
700 if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {
701 Status = EFI_SUCCESS;
702 goto Done;
703 }
704
705 //
706 // task timeout
707 //
708 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {
709 ResetAllFailedAPs();
710 Status = EFI_TIMEOUT;
711 goto Done;
712 }
713
714 gBS->Stall (gPollInterval);
715 mMpSystemData.Timeout -= gPollInterval;
716 }
717
718 Done:
719
720 return Status;
721 }
722
723 /**
724 This service lets the caller get one enabled AP to execute a caller-provided
725 function. The caller can request the BSP to either wait for the completion
726 of the AP or just proceed with the next task by using the EFI event mechanism.
727 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
728 execution support. This service may only be called from the BSP.
729
730 This function is used to dispatch one enabled AP to the function specified by
731 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
732 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
733 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
734 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
735 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
736 then EFI_UNSUPPORTED must be returned.
737
738 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
739 from Procedure, then execution of Procedure by the AP is terminated. The AP is
740 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
741 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
742
743 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
744 instance.
745 @param[in] Procedure A pointer to the function to be run on
746 enabled APs of the system. See type
747 EFI_AP_PROCEDURE.
748 @param[in] ProcessorNumber The handle number of the AP. The range is
749 from 0 to the total number of logical
750 processors minus 1. The total number of
751 logical processors can be retrieved by
752 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
753 @param[in] WaitEvent The event created by the caller with CreateEvent()
754 service. If it is NULL, then execute in
755 blocking mode. BSP waits until all APs finish
756 or TimeoutInMicroseconds expires. If it's
757 not NULL, then execute in non-blocking mode.
758 BSP requests the function specified by
759 Procedure to be started on all the enabled
760 APs, and go on executing immediately. If
761 all return from Procedure or TimeoutInMicroseconds
762 expires, this event is signaled. The BSP
763 can use the CheckEvent() or WaitForEvent()
764 services to check the state of event. Type
765 EFI_EVENT is defined in CreateEvent() in
766 the Unified Extensible Firmware Interface
767 Specification.
768 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
769 APs to return from Procedure, either for
770 blocking or non-blocking mode. Zero means
771 infinity. If the timeout expires before
772 all APs return from Procedure, then Procedure
773 on the failed APs is terminated. All enabled
774 APs are available for next function assigned
775 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
776 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
777 If the timeout expires in blocking mode,
778 BSP returns EFI_TIMEOUT. If the timeout
779 expires in non-blocking mode, WaitEvent
780 is signaled with SignalEvent().
781 @param[in] ProcedureArgument The parameter passed into Procedure for
782 all APs.
783 @param[out] Finished If NULL, this parameter is ignored. In
784 blocking mode, this parameter is ignored.
785 In non-blocking mode, if AP returns from
786 Procedure before the timeout expires, its
787 content is set to TRUE. Otherwise, the
788 value is set to FALSE. The caller can
789 determine if the AP returned from Procedure
790 by evaluating this value.
791
792 @retval EFI_SUCCESS In blocking mode, specified AP finished before
793 the timeout expires.
794 @retval EFI_SUCCESS In non-blocking mode, the function has been
795 dispatched to specified AP.
796 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
797 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
798 signaled.
799 @retval EFI_DEVICE_ERROR The calling processor is an AP.
800 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
801 the specified AP has finished.
802 @retval EFI_NOT_READY The specified AP is busy.
803 @retval EFI_NOT_FOUND The processor with the handle specified by
804 ProcessorNumber does not exist.
805 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
806 @retval EFI_INVALID_PARAMETER Procedure is NULL.
807
808 **/
809 EFI_STATUS
810 EFIAPI
811 StartupThisAP (
812 IN EFI_MP_SERVICES_PROTOCOL *This,
813 IN EFI_AP_PROCEDURE Procedure,
814 IN UINTN ProcessorNumber,
815 IN EFI_EVENT WaitEvent OPTIONAL,
816 IN UINTN TimeoutInMicroseconds,
817 IN VOID *ProcedureArgument OPTIONAL,
818 OUT BOOLEAN *Finished OPTIONAL
819 )
820 {
821 CPU_DATA_BLOCK *CpuData;
822 EFI_STATUS Status;
823
824 CpuData = NULL;
825
826 if (Finished != NULL) {
827 *Finished = FALSE;
828 }
829
830 if (!IsBSP ()) {
831 return EFI_DEVICE_ERROR;
832 }
833
834 if (Procedure == NULL) {
835 return EFI_INVALID_PARAMETER;
836 }
837
838 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
839 return EFI_NOT_FOUND;
840 }
841
842 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
843 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||
844 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
845 return EFI_INVALID_PARAMETER;
846 }
847
848 if (GetApState (CpuData) != CpuStateIdle) {
849 return EFI_NOT_READY;
850 }
851
852 SetApState (CpuData, CpuStateReady);
853
854 SetApProcedure (CpuData, Procedure, ProcedureArgument);
855
856 CpuData->Timeout = TimeoutInMicroseconds;
857 CpuData->WaitEvent = WaitEvent;
858 CpuData->TimeoutActive = !!(TimeoutInMicroseconds);
859 CpuData->Finished = Finished;
860
861 if (WaitEvent != NULL) {
862 //
863 // Non Blocking
864 //
865 Status = gBS->SetTimer (
866 CpuData->CheckThisAPEvent,
867 TimerPeriodic,
868 EFI_TIMER_PERIOD_MICROSECONDS (100)
869 );
870 return Status;
871 }
872
873 //
874 // Blocking
875 //
876 while (TRUE) {
877 if (GetApState (CpuData) == CpuStateFinished) {
878 SetApState (CpuData, CpuStateIdle);
879 break;
880 }
881
882 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
883 ResetProcessorToIdleState (CpuData);
884 return EFI_TIMEOUT;
885 }
886
887 gBS->Stall (gPollInterval);
888 CpuData->Timeout -= gPollInterval;
889 }
890
891 return EFI_SUCCESS;
892 }
893
894 /**
895 This service lets the caller enable or disable an AP from this point onward.
896 This service may only be called from the BSP.
897
898 This service allows the caller enable or disable an AP from this point onward.
899 The caller can optionally specify the health status of the AP by Health. If
900 an AP is being disabled, then the state of the disabled AP is implementation
901 dependent. If an AP is enabled, then the implementation must guarantee that a
902 complete initialization sequence is performed on the AP, so the AP is in a state
903 that is compatible with an MP operating system. This service may not be supported
904 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
905
906 If the enable or disable AP operation cannot be completed prior to the return
907 from this service, then EFI_UNSUPPORTED must be returned.
908
909 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
910 @param[in] ProcessorNumber The handle number of AP that is to become the new
911 BSP. The range is from 0 to the total number of
912 logical processors minus 1. The total number of
913 logical processors can be retrieved by
914 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
915 @param[in] EnableAP Specifies the new state for the processor for
916 enabled, FALSE for disabled.
917 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
918 the new health status of the AP. This flag
919 corresponds to StatusFlag defined in
920 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
921 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
922 bits are ignored. If it is NULL, this parameter
923 is ignored.
924
925 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
926 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
927 prior to this service returning.
928 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
929 @retval EFI_DEVICE_ERROR The calling processor is an AP.
930 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
931 does not exist.
932 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
933
934 **/
935 EFI_STATUS
936 EFIAPI
937 EnableDisableAP (
938 IN EFI_MP_SERVICES_PROTOCOL *This,
939 IN UINTN ProcessorNumber,
940 IN BOOLEAN EnableAP,
941 IN UINT32 *HealthFlag OPTIONAL
942 )
943 {
944 CPU_DATA_BLOCK *CpuData;
945
946 if (!IsBSP ()) {
947 return EFI_DEVICE_ERROR;
948 }
949
950 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
951 return EFI_NOT_FOUND;
952 }
953
954 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
955 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
956 return EFI_INVALID_PARAMETER;
957 }
958
959 if (GetApState (CpuData) != CpuStateIdle) {
960 return EFI_UNSUPPORTED;
961 }
962
963 if (EnableAP) {
964 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {
965 mMpSystemData.NumberOfEnabledProcessors++;
966 }
967 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);
968 } else {
969 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
970 mMpSystemData.NumberOfEnabledProcessors--;
971 }
972 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);
973 }
974
975 if (HealthFlag != NULL) {
976 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);
977 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));
978 }
979
980 return EFI_SUCCESS;
981 }
982
983 /**
984 This return the handle number for the calling processor. This service may be
985 called from the BSP and APs.
986
987 This service returns the processor handle number for the calling processor.
988 The returned value is in the range from 0 to the total number of logical
989 processors minus 1. The total number of logical processors can be retrieved
990 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
991 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
992 is returned. Otherwise, the current processors handle number is returned in
993 ProcessorNumber, and EFI_SUCCESS is returned.
994
995 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
996 @param[out] ProcessorNumber The handle number of AP that is to become the new
997 BSP. The range is from 0 to the total number of
998 logical processors minus 1. The total number of
999 logical processors can be retrieved by
1000 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1001
1002 @retval EFI_SUCCESS The current processor handle number was returned
1003 in ProcessorNumber.
1004 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1005
1006 **/
1007 EFI_STATUS
1008 EFIAPI
1009 WhoAmI (
1010 IN EFI_MP_SERVICES_PROTOCOL *This,
1011 OUT UINTN *ProcessorNumber
1012 )
1013 {
1014 UINTN Index;
1015 UINT32 ProcessorId;
1016
1017 if (ProcessorNumber == NULL) {
1018 return EFI_INVALID_PARAMETER;
1019 }
1020
1021 ProcessorId = GetApicId ();
1022 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
1023 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
1024 break;
1025 }
1026 }
1027
1028 *ProcessorNumber = Index;
1029 return EFI_SUCCESS;
1030 }
1031
1032 /**
1033 Terminate AP's task and set it to idle state.
1034
1035 This function terminates AP's task due to timeout by sending INIT-SIPI,
1036 and sends it to idle state.
1037
1038 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1039
1040 **/
1041 VOID
1042 ResetProcessorToIdleState (
1043 IN CPU_DATA_BLOCK *CpuData
1044 )
1045 {
1046 }
1047
1048 /**
1049 Application Processors do loop routine
1050 after switch to its own stack.
1051
1052 @param Context1 A pointer to the context to pass into the function.
1053 @param Context2 A pointer to the context to pass into the function.
1054
1055 **/
1056 VOID
1057 ProcessorToIdleState (
1058 IN VOID *Context1, OPTIONAL
1059 IN VOID *Context2 OPTIONAL
1060 )
1061 {
1062 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));
1063
1064 AsmApDoneWithCommonStack ();
1065
1066 CpuSleep ();
1067 CpuDeadLoop ();
1068 }
1069
1070 /**
1071 Checks AP' status periodically.
1072
1073 This function is triggerred by timer perodically to check the
1074 state of AP forStartupThisAP() executed in non-blocking mode.
1075
1076 @param Event Event triggered.
1077 @param Context Parameter passed with the event.
1078
1079 **/
1080 VOID
1081 EFIAPI
1082 CheckThisAPStatus (
1083 IN EFI_EVENT Event,
1084 IN VOID *Context
1085 )
1086 {
1087 CPU_DATA_BLOCK *CpuData;
1088 CPU_STATE CpuState;
1089
1090 CpuData = (CPU_DATA_BLOCK *) Context;
1091 if (CpuData->TimeoutActive) {
1092 CpuData->Timeout -= gPollInterval;
1093 }
1094
1095 CpuState = GetApState (CpuData);
1096
1097 if (CpuState == CpuStateFinished) {
1098 if (CpuData->Finished) {
1099 *CpuData->Finished = TRUE;
1100 }
1101 SetApState (CpuData, CpuStateIdle);
1102 goto out;
1103 }
1104
1105 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
1106 if (CpuState != CpuStateIdle &&
1107 CpuData->Finished) {
1108 *CpuData->Finished = FALSE;
1109 }
1110 ResetProcessorToIdleState (CpuData);
1111 goto out;
1112 }
1113
1114 return;
1115
1116 out:
1117 gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);
1118 if (CpuData->WaitEvent) {
1119 gBS->SignalEvent (CpuData->WaitEvent);
1120 CpuData->WaitEvent = NULL;
1121 }
1122 }
1123
1124 /**
1125 Checks APs' status periodically.
1126
1127 This function is triggerred by timer perodically to check the
1128 state of APs for StartupAllAPs() executed in non-blocking mode.
1129
1130 @param Event Event triggered.
1131 @param Context Parameter passed with the event.
1132
1133 **/
1134 VOID
1135 EFIAPI
1136 CheckAllAPsStatus (
1137 IN EFI_EVENT Event,
1138 IN VOID *Context
1139 )
1140 {
1141 if (mMpSystemData.TimeoutActive) {
1142 mMpSystemData.Timeout -= gPollInterval;
1143 }
1144
1145 CheckAndUpdateAllAPsToIdleState ();
1146
1147 //
1148 // task timeout
1149 //
1150 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {
1151 ResetAllFailedAPs();
1152 //
1153 // force exit
1154 //
1155 mMpSystemData.FinishCount = mMpSystemData.StartCount;
1156 }
1157
1158 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {
1159 return;
1160 }
1161
1162 gBS->SetTimer (
1163 mMpSystemData.CheckAllAPsEvent,
1164 TimerCancel,
1165 0
1166 );
1167
1168 if (mMpSystemData.WaitEvent) {
1169 gBS->SignalEvent (mMpSystemData.WaitEvent);
1170 mMpSystemData.WaitEvent = NULL;
1171 }
1172 }
1173
1174 /**
1175 Application Processor C code entry point.
1176
1177 **/
1178 VOID
1179 EFIAPI
1180 ApEntryPointInC (
1181 VOID
1182 )
1183 {
1184 VOID* TopOfApStack;
1185
1186 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
1187 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
1188 mApStackStart = TopOfApStack;
1189
1190 mMpSystemData.NumberOfProcessors++;
1191
1192 SwitchStack (
1193 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
1194 NULL,
1195 NULL,
1196 TopOfApStack);
1197 }
1198
1199 /**
1200 This function is called by all processors (both BSP and AP) once and collects MP related data.
1201
1202 @param Bsp TRUE if the CPU is BSP
1203 @param ProcessorNumber The specific processor number
1204
1205 @retval EFI_SUCCESS Data for the processor collected and filled in
1206
1207 **/
1208 EFI_STATUS
1209 FillInProcessorInformation (
1210 IN BOOLEAN Bsp,
1211 IN UINTN ProcessorNumber
1212 )
1213 {
1214 CPU_DATA_BLOCK *CpuData;
1215 UINT32 ProcessorId;
1216
1217 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1218 ProcessorId = GetApicId ();
1219 CpuData->Info.ProcessorId = ProcessorId;
1220 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
1221 if (Bsp) {
1222 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
1223 }
1224 CpuData->Info.Location.Package = ProcessorId;
1225 CpuData->Info.Location.Core = 0;
1226 CpuData->Info.Location.Thread = 0;
1227 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
1228
1229 CpuData->Procedure = NULL;
1230 CpuData->Parameter = NULL;
1231 InitializeSpinLock (&CpuData->CpuDataLock);
1232
1233 return EFI_SUCCESS;
1234 }
1235
1236 /**
1237 Prepare the System Data.
1238
1239 @retval EFI_SUCCESS the System Data finished initilization.
1240
1241 **/
1242 EFI_STATUS
1243 InitMpSystemData (
1244 VOID
1245 )
1246 {
1247 UINTN ProcessorNumber;
1248 CPU_DATA_BLOCK *CpuData;
1249 EFI_STATUS Status;
1250
1251 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
1252
1253 mMpSystemData.NumberOfProcessors = 1;
1254 mMpSystemData.NumberOfEnabledProcessors = 1;
1255
1256 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
1257 ASSERT(mMpSystemData.CpuDatas != NULL);
1258
1259 Status = gBS->CreateEvent (
1260 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1261 TPL_CALLBACK,
1262 CheckAllAPsStatus,
1263 NULL,
1264 &mMpSystemData.CheckAllAPsEvent
1265 );
1266 ASSERT_EFI_ERROR (Status);
1267
1268 for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {
1269 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1270 Status = gBS->CreateEvent (
1271 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1272 TPL_CALLBACK,
1273 CheckThisAPStatus,
1274 (VOID *) CpuData,
1275 &CpuData->CheckThisAPEvent
1276 );
1277 ASSERT_EFI_ERROR (Status);
1278 }
1279
1280 //
1281 // BSP
1282 //
1283 FillInProcessorInformation (TRUE, 0);
1284
1285 return EFI_SUCCESS;
1286 }
1287
1288 /**
1289 Initialize Multi-processor support.
1290
1291 **/
1292 VOID
1293 InitializeMpSupport (
1294 VOID
1295 )
1296 {
1297 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
1298 if (gMaxLogicalProcessorNumber < 1) {
1299 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1300 return;
1301 }
1302
1303 if (gMaxLogicalProcessorNumber == 1) {
1304 return;
1305 }
1306
1307 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
1308 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
1309
1310 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1311 ASSERT (mApStackStart != NULL);
1312
1313 //
1314 // the first buffer of stack size used for common stack, when the amount of AP
1315 // more than 1, we should never free the common stack which maybe used for AP reset.
1316 //
1317 mCommonStack = mApStackStart;
1318 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
1319 mApStackStart = mTopOfApCommonStack;
1320
1321 InitMpSystemData ();
1322
1323 if (mMpSystemData.NumberOfProcessors == 1) {
1324 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1325 return;
1326 }
1327
1328 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
1329 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
1330 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
1331 gApStackSize));
1332 }
1333 }