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