]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Services:SwitchBSP()
[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 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 switches the requested AP to be the BSP from that point onward.
896 This service changes the BSP for all purposes. This call can only be performed
897 by the current BSP.
898
899 This service switches the requested AP to be the BSP from that point onward.
900 This service changes the BSP for all purposes. The new BSP can take over the
901 execution of the old BSP and continue seamlessly from where the old one left
902 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
903 is signaled.
904
905 If the BSP cannot be switched prior to the return from this service, then
906 EFI_UNSUPPORTED must be returned.
907
908 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
909 @param[in] ProcessorNumber The handle number of AP that is to become the new
910 BSP. The range is from 0 to the total number of
911 logical processors minus 1. The total number of
912 logical processors can be retrieved by
913 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
914 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
915 enabled AP. Otherwise, it will be disabled.
916
917 @retval EFI_SUCCESS BSP successfully switched.
918 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
919 this service returning.
920 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
921 @retval EFI_SUCCESS The calling processor is an AP.
922 @retval EFI_NOT_FOUND The processor with the handle specified by
923 ProcessorNumber does not exist.
924 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
925 a disabled AP.
926 @retval EFI_NOT_READY The specified AP is busy.
927
928 **/
929 EFI_STATUS
930 EFIAPI
931 SwitchBSP (
932 IN EFI_MP_SERVICES_PROTOCOL *This,
933 IN UINTN ProcessorNumber,
934 IN BOOLEAN EnableOldBSP
935 )
936 {
937 //
938 // Current always return unsupported.
939 //
940 return EFI_UNSUPPORTED;
941 }
942
943 /**
944 This service lets the caller enable or disable an AP from this point onward.
945 This service may only be called from the BSP.
946
947 This service allows the caller enable or disable an AP from this point onward.
948 The caller can optionally specify the health status of the AP by Health. If
949 an AP is being disabled, then the state of the disabled AP is implementation
950 dependent. If an AP is enabled, then the implementation must guarantee that a
951 complete initialization sequence is performed on the AP, so the AP is in a state
952 that is compatible with an MP operating system. This service may not be supported
953 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
954
955 If the enable or disable AP operation cannot be completed prior to the return
956 from this service, then EFI_UNSUPPORTED must be returned.
957
958 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
959 @param[in] ProcessorNumber The handle number of AP that is to become the new
960 BSP. The range is from 0 to the total number of
961 logical processors minus 1. The total number of
962 logical processors can be retrieved by
963 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
964 @param[in] EnableAP Specifies the new state for the processor for
965 enabled, FALSE for disabled.
966 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
967 the new health status of the AP. This flag
968 corresponds to StatusFlag defined in
969 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
970 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
971 bits are ignored. If it is NULL, this parameter
972 is ignored.
973
974 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
975 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
976 prior to this service returning.
977 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
978 @retval EFI_DEVICE_ERROR The calling processor is an AP.
979 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
980 does not exist.
981 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
982
983 **/
984 EFI_STATUS
985 EFIAPI
986 EnableDisableAP (
987 IN EFI_MP_SERVICES_PROTOCOL *This,
988 IN UINTN ProcessorNumber,
989 IN BOOLEAN EnableAP,
990 IN UINT32 *HealthFlag OPTIONAL
991 )
992 {
993 CPU_DATA_BLOCK *CpuData;
994
995 if (!IsBSP ()) {
996 return EFI_DEVICE_ERROR;
997 }
998
999 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
1000 return EFI_NOT_FOUND;
1001 }
1002
1003 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1004 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
1005 return EFI_INVALID_PARAMETER;
1006 }
1007
1008 if (GetApState (CpuData) != CpuStateIdle) {
1009 return EFI_UNSUPPORTED;
1010 }
1011
1012 if (EnableAP) {
1013 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {
1014 mMpSystemData.NumberOfEnabledProcessors++;
1015 }
1016 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);
1017 } else {
1018 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
1019 mMpSystemData.NumberOfEnabledProcessors--;
1020 }
1021 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);
1022 }
1023
1024 if (HealthFlag != NULL) {
1025 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);
1026 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));
1027 }
1028
1029 return EFI_SUCCESS;
1030 }
1031
1032 /**
1033 This return the handle number for the calling processor. This service may be
1034 called from the BSP and APs.
1035
1036 This service returns the processor handle number for the calling processor.
1037 The returned value is in the range from 0 to the total number of logical
1038 processors minus 1. The total number of logical processors can be retrieved
1039 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1040 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1041 is returned. Otherwise, the current processors handle number is returned in
1042 ProcessorNumber, and EFI_SUCCESS is returned.
1043
1044 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1045 @param[out] ProcessorNumber The handle number of AP that is to become the new
1046 BSP. The range is from 0 to the total number of
1047 logical processors minus 1. The total number of
1048 logical processors can be retrieved by
1049 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1050
1051 @retval EFI_SUCCESS The current processor handle number was returned
1052 in ProcessorNumber.
1053 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1054
1055 **/
1056 EFI_STATUS
1057 EFIAPI
1058 WhoAmI (
1059 IN EFI_MP_SERVICES_PROTOCOL *This,
1060 OUT UINTN *ProcessorNumber
1061 )
1062 {
1063 UINTN Index;
1064 UINT32 ProcessorId;
1065
1066 if (ProcessorNumber == NULL) {
1067 return EFI_INVALID_PARAMETER;
1068 }
1069
1070 ProcessorId = GetApicId ();
1071 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
1072 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
1073 break;
1074 }
1075 }
1076
1077 *ProcessorNumber = Index;
1078 return EFI_SUCCESS;
1079 }
1080
1081 /**
1082 Terminate AP's task and set it to idle state.
1083
1084 This function terminates AP's task due to timeout by sending INIT-SIPI,
1085 and sends it to idle state.
1086
1087 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1088
1089 **/
1090 VOID
1091 ResetProcessorToIdleState (
1092 IN CPU_DATA_BLOCK *CpuData
1093 )
1094 {
1095 }
1096
1097 /**
1098 Application Processors do loop routine
1099 after switch to its own stack.
1100
1101 @param Context1 A pointer to the context to pass into the function.
1102 @param Context2 A pointer to the context to pass into the function.
1103
1104 **/
1105 VOID
1106 ProcessorToIdleState (
1107 IN VOID *Context1, OPTIONAL
1108 IN VOID *Context2 OPTIONAL
1109 )
1110 {
1111 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));
1112
1113 AsmApDoneWithCommonStack ();
1114
1115 CpuSleep ();
1116 CpuDeadLoop ();
1117 }
1118
1119 /**
1120 Checks AP' status periodically.
1121
1122 This function is triggerred by timer perodically to check the
1123 state of AP forStartupThisAP() executed in non-blocking mode.
1124
1125 @param Event Event triggered.
1126 @param Context Parameter passed with the event.
1127
1128 **/
1129 VOID
1130 EFIAPI
1131 CheckThisAPStatus (
1132 IN EFI_EVENT Event,
1133 IN VOID *Context
1134 )
1135 {
1136 CPU_DATA_BLOCK *CpuData;
1137 CPU_STATE CpuState;
1138
1139 CpuData = (CPU_DATA_BLOCK *) Context;
1140 if (CpuData->TimeoutActive) {
1141 CpuData->Timeout -= gPollInterval;
1142 }
1143
1144 CpuState = GetApState (CpuData);
1145
1146 if (CpuState == CpuStateFinished) {
1147 if (CpuData->Finished) {
1148 *CpuData->Finished = TRUE;
1149 }
1150 SetApState (CpuData, CpuStateIdle);
1151 goto out;
1152 }
1153
1154 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
1155 if (CpuState != CpuStateIdle &&
1156 CpuData->Finished) {
1157 *CpuData->Finished = FALSE;
1158 }
1159 ResetProcessorToIdleState (CpuData);
1160 goto out;
1161 }
1162
1163 return;
1164
1165 out:
1166 gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);
1167 if (CpuData->WaitEvent) {
1168 gBS->SignalEvent (CpuData->WaitEvent);
1169 CpuData->WaitEvent = NULL;
1170 }
1171 }
1172
1173 /**
1174 Checks APs' status periodically.
1175
1176 This function is triggerred by timer perodically to check the
1177 state of APs for StartupAllAPs() executed in non-blocking mode.
1178
1179 @param Event Event triggered.
1180 @param Context Parameter passed with the event.
1181
1182 **/
1183 VOID
1184 EFIAPI
1185 CheckAllAPsStatus (
1186 IN EFI_EVENT Event,
1187 IN VOID *Context
1188 )
1189 {
1190 if (mMpSystemData.TimeoutActive) {
1191 mMpSystemData.Timeout -= gPollInterval;
1192 }
1193
1194 CheckAndUpdateAllAPsToIdleState ();
1195
1196 //
1197 // task timeout
1198 //
1199 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {
1200 ResetAllFailedAPs();
1201 //
1202 // force exit
1203 //
1204 mMpSystemData.FinishCount = mMpSystemData.StartCount;
1205 }
1206
1207 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {
1208 return;
1209 }
1210
1211 gBS->SetTimer (
1212 mMpSystemData.CheckAllAPsEvent,
1213 TimerCancel,
1214 0
1215 );
1216
1217 if (mMpSystemData.WaitEvent) {
1218 gBS->SignalEvent (mMpSystemData.WaitEvent);
1219 mMpSystemData.WaitEvent = NULL;
1220 }
1221 }
1222
1223 /**
1224 Application Processor C code entry point.
1225
1226 **/
1227 VOID
1228 EFIAPI
1229 ApEntryPointInC (
1230 VOID
1231 )
1232 {
1233 VOID* TopOfApStack;
1234
1235 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
1236 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
1237 mApStackStart = TopOfApStack;
1238
1239 mMpSystemData.NumberOfProcessors++;
1240
1241 SwitchStack (
1242 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
1243 NULL,
1244 NULL,
1245 TopOfApStack);
1246 }
1247
1248 /**
1249 This function is called by all processors (both BSP and AP) once and collects MP related data.
1250
1251 @param Bsp TRUE if the CPU is BSP
1252 @param ProcessorNumber The specific processor number
1253
1254 @retval EFI_SUCCESS Data for the processor collected and filled in
1255
1256 **/
1257 EFI_STATUS
1258 FillInProcessorInformation (
1259 IN BOOLEAN Bsp,
1260 IN UINTN ProcessorNumber
1261 )
1262 {
1263 CPU_DATA_BLOCK *CpuData;
1264 UINT32 ProcessorId;
1265
1266 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1267 ProcessorId = GetApicId ();
1268 CpuData->Info.ProcessorId = ProcessorId;
1269 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
1270 if (Bsp) {
1271 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
1272 }
1273 CpuData->Info.Location.Package = ProcessorId;
1274 CpuData->Info.Location.Core = 0;
1275 CpuData->Info.Location.Thread = 0;
1276 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
1277
1278 CpuData->Procedure = NULL;
1279 CpuData->Parameter = NULL;
1280 InitializeSpinLock (&CpuData->CpuDataLock);
1281
1282 return EFI_SUCCESS;
1283 }
1284
1285 /**
1286 Prepare the System Data.
1287
1288 @retval EFI_SUCCESS the System Data finished initilization.
1289
1290 **/
1291 EFI_STATUS
1292 InitMpSystemData (
1293 VOID
1294 )
1295 {
1296 UINTN ProcessorNumber;
1297 CPU_DATA_BLOCK *CpuData;
1298 EFI_STATUS Status;
1299
1300 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
1301
1302 mMpSystemData.NumberOfProcessors = 1;
1303 mMpSystemData.NumberOfEnabledProcessors = 1;
1304
1305 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
1306 ASSERT(mMpSystemData.CpuDatas != NULL);
1307
1308 Status = gBS->CreateEvent (
1309 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1310 TPL_CALLBACK,
1311 CheckAllAPsStatus,
1312 NULL,
1313 &mMpSystemData.CheckAllAPsEvent
1314 );
1315 ASSERT_EFI_ERROR (Status);
1316
1317 for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {
1318 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1319 Status = gBS->CreateEvent (
1320 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1321 TPL_CALLBACK,
1322 CheckThisAPStatus,
1323 (VOID *) CpuData,
1324 &CpuData->CheckThisAPEvent
1325 );
1326 ASSERT_EFI_ERROR (Status);
1327 }
1328
1329 //
1330 // BSP
1331 //
1332 FillInProcessorInformation (TRUE, 0);
1333
1334 return EFI_SUCCESS;
1335 }
1336
1337 /**
1338 Initialize Multi-processor support.
1339
1340 **/
1341 VOID
1342 InitializeMpSupport (
1343 VOID
1344 )
1345 {
1346 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
1347 if (gMaxLogicalProcessorNumber < 1) {
1348 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1349 return;
1350 }
1351
1352 if (gMaxLogicalProcessorNumber == 1) {
1353 return;
1354 }
1355
1356 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
1357 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
1358
1359 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1360 ASSERT (mApStackStart != NULL);
1361
1362 //
1363 // the first buffer of stack size used for common stack, when the amount of AP
1364 // more than 1, we should never free the common stack which maybe used for AP reset.
1365 //
1366 mCommonStack = mApStackStart;
1367 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
1368 mApStackStart = mTopOfApCommonStack;
1369
1370 InitMpSystemData ();
1371
1372 if (mMpSystemData.NumberOfProcessors == 1) {
1373 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1374 return;
1375 }
1376
1377 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
1378 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
1379 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
1380 gApStackSize));
1381 }
1382 }