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