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