]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/MpService: avoid reset AP still hold a lock
[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 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
636 CpuData = &mMpSystemData.CpuDatas[Number];
637 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
638 //
639 // Skip BSP
640 //
641 continue;
642 }
643
644 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
645 //
646 // Skip Disabled processors
647 //
648 continue;
649 }
650
651 if (GetApState (CpuData) != CpuStateIdle) {
652 return EFI_NOT_READY;
653 }
654 }
655
656 //
657 // temporarily stop checkAllAPsStatus for initialize parameters.
658 //
659 mStopCheckAllAPsStatus = TRUE;
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 while (TRUE) {
720 CheckAndUpdateAllAPsToIdleState ();
721 if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {
722 Status = EFI_SUCCESS;
723 goto Done;
724 }
725
726 //
727 // task timeout
728 //
729 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {
730 ResetAllFailedAPs();
731 Status = EFI_TIMEOUT;
732 goto Done;
733 }
734
735 gBS->Stall (gPollInterval);
736 mMpSystemData.Timeout -= gPollInterval;
737 }
738
739 Done:
740
741 return Status;
742 }
743
744 /**
745 This service lets the caller get one enabled AP to execute a caller-provided
746 function. The caller can request the BSP to either wait for the completion
747 of the AP or just proceed with the next task by using the EFI event mechanism.
748 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
749 execution support. This service may only be called from the BSP.
750
751 This function is used to dispatch one enabled AP to the function specified by
752 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
753 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
754 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
755 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
756 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
757 then EFI_UNSUPPORTED must be returned.
758
759 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
760 from Procedure, then execution of Procedure by the AP is terminated. The AP is
761 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
762 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
763
764 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
765 instance.
766 @param[in] Procedure A pointer to the function to be run on
767 enabled APs of the system. See type
768 EFI_AP_PROCEDURE.
769 @param[in] ProcessorNumber The handle number of the AP. The range is
770 from 0 to the total number of logical
771 processors minus 1. The total number of
772 logical processors can be retrieved by
773 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
774 @param[in] WaitEvent The event created by the caller with CreateEvent()
775 service. If it is NULL, then execute in
776 blocking mode. BSP waits until all APs finish
777 or TimeoutInMicroseconds expires. If it's
778 not NULL, then execute in non-blocking mode.
779 BSP requests the function specified by
780 Procedure to be started on all the enabled
781 APs, and go on executing immediately. If
782 all return from Procedure or TimeoutInMicroseconds
783 expires, this event is signaled. The BSP
784 can use the CheckEvent() or WaitForEvent()
785 services to check the state of event. Type
786 EFI_EVENT is defined in CreateEvent() in
787 the Unified Extensible Firmware Interface
788 Specification.
789 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
790 APs to return from Procedure, either for
791 blocking or non-blocking mode. Zero means
792 infinity. If the timeout expires before
793 all APs return from Procedure, then Procedure
794 on the failed APs is terminated. All enabled
795 APs are available for next function assigned
796 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
797 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
798 If the timeout expires in blocking mode,
799 BSP returns EFI_TIMEOUT. If the timeout
800 expires in non-blocking mode, WaitEvent
801 is signaled with SignalEvent().
802 @param[in] ProcedureArgument The parameter passed into Procedure for
803 all APs.
804 @param[out] Finished If NULL, this parameter is ignored. In
805 blocking mode, this parameter is ignored.
806 In non-blocking mode, if AP returns from
807 Procedure before the timeout expires, its
808 content is set to TRUE. Otherwise, the
809 value is set to FALSE. The caller can
810 determine if the AP returned from Procedure
811 by evaluating this value.
812
813 @retval EFI_SUCCESS In blocking mode, specified AP finished before
814 the timeout expires.
815 @retval EFI_SUCCESS In non-blocking mode, the function has been
816 dispatched to specified AP.
817 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
818 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
819 signaled.
820 @retval EFI_DEVICE_ERROR The calling processor is an AP.
821 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
822 the specified AP has finished.
823 @retval EFI_NOT_READY The specified AP is busy.
824 @retval EFI_NOT_FOUND The processor with the handle specified by
825 ProcessorNumber does not exist.
826 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
827 @retval EFI_INVALID_PARAMETER Procedure is NULL.
828
829 **/
830 EFI_STATUS
831 EFIAPI
832 StartupThisAP (
833 IN EFI_MP_SERVICES_PROTOCOL *This,
834 IN EFI_AP_PROCEDURE Procedure,
835 IN UINTN ProcessorNumber,
836 IN EFI_EVENT WaitEvent OPTIONAL,
837 IN UINTN TimeoutInMicroseconds,
838 IN VOID *ProcedureArgument OPTIONAL,
839 OUT BOOLEAN *Finished OPTIONAL
840 )
841 {
842 CPU_DATA_BLOCK *CpuData;
843
844 CpuData = NULL;
845
846 if (Finished != NULL) {
847 *Finished = FALSE;
848 }
849
850 if (!IsBSP ()) {
851 return EFI_DEVICE_ERROR;
852 }
853
854 if (Procedure == NULL) {
855 return EFI_INVALID_PARAMETER;
856 }
857
858 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
859 return EFI_NOT_FOUND;
860 }
861
862 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
863 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||
864 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
865 return EFI_INVALID_PARAMETER;
866 }
867
868 if (GetApState (CpuData) != CpuStateIdle) {
869 return EFI_NOT_READY;
870 }
871
872 //
873 // temporarily stop checkAllAPsStatus for initialize parameters.
874 //
875 mStopCheckAllAPsStatus = TRUE;
876
877 SetApState (CpuData, CpuStateReady);
878
879 SetApProcedure (CpuData, Procedure, ProcedureArgument);
880
881 CpuData->Timeout = TimeoutInMicroseconds;
882 CpuData->WaitEvent = WaitEvent;
883 CpuData->TimeoutActive = !!(TimeoutInMicroseconds);
884 CpuData->Finished = Finished;
885
886 mStopCheckAllAPsStatus = FALSE;
887
888 if (WaitEvent != NULL) {
889 //
890 // Non Blocking
891 //
892 return EFI_SUCCESS;
893 }
894
895 //
896 // Blocking
897 //
898 while (TRUE) {
899 if (GetApState (CpuData) == CpuStateFinished) {
900 SetApState (CpuData, CpuStateIdle);
901 break;
902 }
903
904 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
905 ResetProcessorToIdleState (CpuData);
906 return EFI_TIMEOUT;
907 }
908
909 gBS->Stall (gPollInterval);
910 CpuData->Timeout -= gPollInterval;
911 }
912
913 return EFI_SUCCESS;
914 }
915
916 /**
917 This service switches the requested AP to be the BSP from that point onward.
918 This service changes the BSP for all purposes. This call can only be performed
919 by the current BSP.
920
921 This service switches the requested AP to be the BSP from that point onward.
922 This service changes the BSP for all purposes. The new BSP can take over the
923 execution of the old BSP and continue seamlessly from where the old one left
924 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT
925 is signaled.
926
927 If the BSP cannot be switched prior to the return from this service, then
928 EFI_UNSUPPORTED must be returned.
929
930 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
931 @param[in] ProcessorNumber The handle number of AP that is to become the new
932 BSP. The range is from 0 to the total number of
933 logical processors minus 1. The total number of
934 logical processors can be retrieved by
935 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
936 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
937 enabled AP. Otherwise, it will be disabled.
938
939 @retval EFI_SUCCESS BSP successfully switched.
940 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
941 this service returning.
942 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
943 @retval EFI_SUCCESS The calling processor is an AP.
944 @retval EFI_NOT_FOUND The processor with the handle specified by
945 ProcessorNumber does not exist.
946 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
947 a disabled AP.
948 @retval EFI_NOT_READY The specified AP is busy.
949
950 **/
951 EFI_STATUS
952 EFIAPI
953 SwitchBSP (
954 IN EFI_MP_SERVICES_PROTOCOL *This,
955 IN UINTN ProcessorNumber,
956 IN BOOLEAN EnableOldBSP
957 )
958 {
959 //
960 // Current always return unsupported.
961 //
962 return EFI_UNSUPPORTED;
963 }
964
965 /**
966 This service lets the caller enable or disable an AP from this point onward.
967 This service may only be called from the BSP.
968
969 This service allows the caller enable or disable an AP from this point onward.
970 The caller can optionally specify the health status of the AP by Health. If
971 an AP is being disabled, then the state of the disabled AP is implementation
972 dependent. If an AP is enabled, then the implementation must guarantee that a
973 complete initialization sequence is performed on the AP, so the AP is in a state
974 that is compatible with an MP operating system. This service may not be supported
975 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
976
977 If the enable or disable AP operation cannot be completed prior to the return
978 from this service, then EFI_UNSUPPORTED must be returned.
979
980 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
981 @param[in] ProcessorNumber The handle number of AP that is to become the new
982 BSP. The range is from 0 to the total number of
983 logical processors minus 1. The total number of
984 logical processors can be retrieved by
985 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
986 @param[in] EnableAP Specifies the new state for the processor for
987 enabled, FALSE for disabled.
988 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
989 the new health status of the AP. This flag
990 corresponds to StatusFlag defined in
991 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
992 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
993 bits are ignored. If it is NULL, this parameter
994 is ignored.
995
996 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
997 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
998 prior to this service returning.
999 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
1000 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1001 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
1002 does not exist.
1003 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
1004
1005 **/
1006 EFI_STATUS
1007 EFIAPI
1008 EnableDisableAP (
1009 IN EFI_MP_SERVICES_PROTOCOL *This,
1010 IN UINTN ProcessorNumber,
1011 IN BOOLEAN EnableAP,
1012 IN UINT32 *HealthFlag OPTIONAL
1013 )
1014 {
1015 CPU_DATA_BLOCK *CpuData;
1016
1017 if (!IsBSP ()) {
1018 return EFI_DEVICE_ERROR;
1019 }
1020
1021 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
1022 return EFI_NOT_FOUND;
1023 }
1024
1025 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1026 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
1027 return EFI_INVALID_PARAMETER;
1028 }
1029
1030 if (GetApState (CpuData) != CpuStateIdle) {
1031 return EFI_UNSUPPORTED;
1032 }
1033
1034 if (EnableAP) {
1035 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {
1036 mMpSystemData.NumberOfEnabledProcessors++;
1037 }
1038 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);
1039 } else {
1040 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
1041 mMpSystemData.NumberOfEnabledProcessors--;
1042 }
1043 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);
1044 }
1045
1046 if (HealthFlag != NULL) {
1047 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);
1048 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));
1049 }
1050
1051 return EFI_SUCCESS;
1052 }
1053
1054 /**
1055 This return the handle number for the calling processor. This service may be
1056 called from the BSP and APs.
1057
1058 This service returns the processor handle number for the calling processor.
1059 The returned value is in the range from 0 to the total number of logical
1060 processors minus 1. The total number of logical processors can be retrieved
1061 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
1062 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
1063 is returned. Otherwise, the current processors handle number is returned in
1064 ProcessorNumber, and EFI_SUCCESS is returned.
1065
1066 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
1067 @param[out] ProcessorNumber The handle number of AP that is to become the new
1068 BSP. The range is from 0 to the total number of
1069 logical processors minus 1. The total number of
1070 logical processors can be retrieved by
1071 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
1072
1073 @retval EFI_SUCCESS The current processor handle number was returned
1074 in ProcessorNumber.
1075 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1076
1077 **/
1078 EFI_STATUS
1079 EFIAPI
1080 WhoAmI (
1081 IN EFI_MP_SERVICES_PROTOCOL *This,
1082 OUT UINTN *ProcessorNumber
1083 )
1084 {
1085 UINTN Index;
1086 UINT32 ProcessorId;
1087
1088 if (ProcessorNumber == NULL) {
1089 return EFI_INVALID_PARAMETER;
1090 }
1091
1092 ProcessorId = GetApicId ();
1093 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
1094 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
1095 break;
1096 }
1097 }
1098
1099 *ProcessorNumber = Index;
1100 return EFI_SUCCESS;
1101 }
1102
1103 /**
1104 Terminate AP's task and set it to idle state.
1105
1106 This function terminates AP's task due to timeout by sending INIT-SIPI,
1107 and sends it to idle state.
1108
1109 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
1110
1111 **/
1112 VOID
1113 ResetProcessorToIdleState (
1114 IN CPU_DATA_BLOCK *CpuData
1115 )
1116 {
1117 ResetApStackless ((UINT32)CpuData->Info.ProcessorId);
1118 }
1119
1120 /**
1121 Application Processors do loop routine
1122 after switch to its own stack.
1123
1124 @param Context1 A pointer to the context to pass into the function.
1125 @param Context2 A pointer to the context to pass into the function.
1126
1127 **/
1128 VOID
1129 ProcessorToIdleState (
1130 IN VOID *Context1, OPTIONAL
1131 IN VOID *Context2 OPTIONAL
1132 )
1133 {
1134 UINTN ProcessorNumber;
1135 CPU_DATA_BLOCK *CpuData;
1136 EFI_AP_PROCEDURE Procedure;
1137 VOID *ProcedureArgument;
1138
1139 AsmApDoneWithCommonStack ();
1140
1141 while (!mAPsAlreadyInitFinished) {
1142 CpuPause ();
1143 }
1144
1145 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);
1146 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1147
1148 //
1149 // Avoid forcibly reset AP caused the AP got lock not release.
1150 //
1151 if (CpuData->LockSelf == (INTN) GetApicId ()) {
1152 ReleaseSpinLock (&CpuData->CpuDataLock);
1153 }
1154
1155 //
1156 // Avoid forcibly reset AP caused the AP State is not updated.
1157 //
1158 GetMpSpinLock (CpuData);
1159 CpuData->State = CpuStateIdle;
1160 CpuData->Procedure = NULL;
1161 ReleaseMpSpinLock (CpuData);
1162
1163 while (TRUE) {
1164 GetMpSpinLock (CpuData);
1165 ProcedureArgument = CpuData->Parameter;
1166 Procedure = CpuData->Procedure;
1167 ReleaseMpSpinLock (CpuData);
1168
1169 if (Procedure != NULL) {
1170 Procedure (ProcedureArgument);
1171
1172 GetMpSpinLock (CpuData);
1173 CpuData->Procedure = NULL;
1174 CpuData->State = CpuStateFinished;
1175 ReleaseMpSpinLock (CpuData);
1176 }
1177
1178 CpuPause ();
1179 }
1180
1181 CpuSleep ();
1182 CpuDeadLoop ();
1183 }
1184
1185 /**
1186 Checks AP' status periodically.
1187
1188 This function is triggerred by timer perodically to check the
1189 state of AP forStartupThisAP() executed in non-blocking mode.
1190
1191 @param Event Event triggered.
1192 @param Context Parameter passed with the event.
1193
1194 **/
1195 VOID
1196 EFIAPI
1197 CheckThisAPStatus (
1198 IN EFI_EVENT Event,
1199 IN VOID *Context
1200 )
1201 {
1202 CPU_DATA_BLOCK *CpuData;
1203 CPU_STATE CpuState;
1204
1205 CpuData = (CPU_DATA_BLOCK *) Context;
1206 if (CpuData->TimeoutActive) {
1207 CpuData->Timeout -= gPollInterval;
1208 }
1209
1210 CpuState = GetApState (CpuData);
1211
1212 if (CpuState == CpuStateFinished) {
1213 if (CpuData->Finished) {
1214 *CpuData->Finished = TRUE;
1215 }
1216 SetApState (CpuData, CpuStateIdle);
1217 goto out;
1218 }
1219
1220 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
1221 if (CpuState != CpuStateIdle &&
1222 CpuData->Finished) {
1223 *CpuData->Finished = FALSE;
1224 }
1225 ResetProcessorToIdleState (CpuData);
1226 goto out;
1227 }
1228
1229 return;
1230
1231 out:
1232 CpuData->TimeoutActive = FALSE;
1233 gBS->SignalEvent (CpuData->WaitEvent);
1234 CpuData->WaitEvent = NULL;
1235 }
1236
1237 /**
1238 Checks APs' status periodically.
1239
1240 This function is triggerred by timer perodically to check the
1241 state of APs for StartupAllAPs() executed in non-blocking mode.
1242
1243 @param Event Event triggered.
1244 @param Context Parameter passed with the event.
1245
1246 **/
1247 VOID
1248 EFIAPI
1249 CheckAllAPsStatus (
1250 IN EFI_EVENT Event,
1251 IN VOID *Context
1252 )
1253 {
1254 CPU_DATA_BLOCK *CpuData;
1255 UINTN Number;
1256 EFI_STATUS Status;
1257
1258 if (mMpSystemData.TimeoutActive) {
1259 mMpSystemData.Timeout -= gPollInterval;
1260 }
1261
1262 if (mStopCheckAllAPsStatus) {
1263 return;
1264 }
1265
1266 //
1267 // avoid next timer enter.
1268 //
1269 Status = gBS->SetTimer (
1270 mMpSystemData.CheckAllAPsEvent,
1271 TimerCancel,
1272 0
1273 );
1274 ASSERT_EFI_ERROR (Status);
1275
1276 if (mMpSystemData.WaitEvent != NULL) {
1277 CheckAndUpdateAllAPsToIdleState ();
1278 //
1279 // task timeout
1280 //
1281 if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {
1282 ResetAllFailedAPs();
1283 //
1284 // force exit
1285 //
1286 mMpSystemData.FinishCount = mMpSystemData.StartCount;
1287 }
1288
1289 if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {
1290 goto EXIT;
1291 }
1292
1293 mMpSystemData.TimeoutActive = FALSE;
1294 gBS->SignalEvent (mMpSystemData.WaitEvent);
1295 mMpSystemData.WaitEvent = NULL;
1296 mStopCheckAllAPsStatus = TRUE;
1297
1298 goto EXIT;
1299 }
1300
1301 //
1302 // check each AP status for StartupThisAP
1303 //
1304 for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {
1305 CpuData = &mMpSystemData.CpuDatas[Number];
1306 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
1307 //
1308 // Skip BSP
1309 //
1310 continue;
1311 }
1312
1313 if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
1314 //
1315 // Skip Disabled processors
1316 //
1317 continue;
1318 }
1319
1320 if (CpuData->WaitEvent) {
1321 CheckThisAPStatus (NULL, (VOID *)CpuData);
1322 }
1323 }
1324
1325 EXIT:
1326 Status = gBS->SetTimer (
1327 mMpSystemData.CheckAllAPsEvent,
1328 TimerPeriodic,
1329 EFI_TIMER_PERIOD_MICROSECONDS (100)
1330 );
1331 ASSERT_EFI_ERROR (Status);
1332 }
1333
1334 /**
1335 Application Processor C code entry point.
1336
1337 **/
1338 VOID
1339 EFIAPI
1340 ApEntryPointInC (
1341 VOID
1342 )
1343 {
1344 VOID* TopOfApStack;
1345 UINTN ProcessorNumber;
1346
1347 if (!mAPsAlreadyInitFinished) {
1348 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
1349 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
1350 mApStackStart = TopOfApStack;
1351
1352 //
1353 // Store the Stack address, when reset the AP, We can found the original address.
1354 //
1355 mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack;
1356 mMpSystemData.NumberOfProcessors++;
1357 mMpSystemData.NumberOfEnabledProcessors++;
1358 } else {
1359 WhoAmI (&mMpServicesTemplate, &ProcessorNumber);
1360 //
1361 // Get the original stack address.
1362 //
1363 TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack;
1364 }
1365
1366 SwitchStack (
1367 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
1368 NULL,
1369 NULL,
1370 TopOfApStack);
1371 }
1372
1373 /**
1374 This function is called by all processors (both BSP and AP) once and collects MP related data.
1375
1376 @param Bsp TRUE if the CPU is BSP
1377 @param ProcessorNumber The specific processor number
1378
1379 @retval EFI_SUCCESS Data for the processor collected and filled in
1380
1381 **/
1382 EFI_STATUS
1383 FillInProcessorInformation (
1384 IN BOOLEAN Bsp,
1385 IN UINTN ProcessorNumber
1386 )
1387 {
1388 CPU_DATA_BLOCK *CpuData;
1389 UINT32 ProcessorId;
1390
1391 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
1392 ProcessorId = GetApicId ();
1393 CpuData->Info.ProcessorId = ProcessorId;
1394 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
1395 if (Bsp) {
1396 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
1397 }
1398 CpuData->Info.Location.Package = ProcessorId;
1399 CpuData->Info.Location.Core = 0;
1400 CpuData->Info.Location.Thread = 0;
1401 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
1402
1403 CpuData->Procedure = NULL;
1404 CpuData->Parameter = NULL;
1405 InitializeSpinLock (&CpuData->CpuDataLock);
1406 CpuData->LockSelf = -1;
1407
1408 return EFI_SUCCESS;
1409 }
1410
1411 /**
1412 Prepare the System Data.
1413
1414 @retval EFI_SUCCESS the System Data finished initilization.
1415
1416 **/
1417 EFI_STATUS
1418 InitMpSystemData (
1419 VOID
1420 )
1421 {
1422 EFI_STATUS Status;
1423
1424 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
1425
1426 mMpSystemData.NumberOfProcessors = 1;
1427 mMpSystemData.NumberOfEnabledProcessors = 1;
1428
1429 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
1430 ASSERT(mMpSystemData.CpuDatas != NULL);
1431
1432 Status = gBS->CreateEvent (
1433 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1434 TPL_CALLBACK,
1435 CheckAllAPsStatus,
1436 NULL,
1437 &mMpSystemData.CheckAllAPsEvent
1438 );
1439 ASSERT_EFI_ERROR (Status);
1440
1441 //
1442 // Set timer to check all APs status.
1443 //
1444 Status = gBS->SetTimer (
1445 mMpSystemData.CheckAllAPsEvent,
1446 TimerPeriodic,
1447 EFI_TIMER_PERIOD_MICROSECONDS (100)
1448 );
1449 ASSERT_EFI_ERROR (Status);
1450
1451 //
1452 // BSP
1453 //
1454 FillInProcessorInformation (TRUE, 0);
1455
1456 return EFI_SUCCESS;
1457 }
1458
1459 /**
1460 Initialize Multi-processor support.
1461
1462 **/
1463 VOID
1464 InitializeMpSupport (
1465 VOID
1466 )
1467 {
1468 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
1469 if (gMaxLogicalProcessorNumber < 1) {
1470 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
1471 return;
1472 }
1473
1474 if (gMaxLogicalProcessorNumber == 1) {
1475 return;
1476 }
1477
1478 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
1479 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
1480
1481 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1482 ASSERT (mApStackStart != NULL);
1483
1484 //
1485 // the first buffer of stack size used for common stack, when the amount of AP
1486 // more than 1, we should never free the common stack which maybe used for AP reset.
1487 //
1488 mCommonStack = mApStackStart;
1489 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
1490 mApStackStart = mTopOfApCommonStack;
1491
1492 InitMpSystemData ();
1493
1494 PrepareAPStartupCode ();
1495
1496 if (mMpSystemData.NumberOfProcessors == 1) {
1497 FreeApStartupCode ();
1498 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
1499 return;
1500 }
1501
1502 mMpSystemData.CpuDatas = ReallocatePool (
1503 sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber,
1504 sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,
1505 mMpSystemData.CpuDatas);
1506
1507 mAPsAlreadyInitFinished = TRUE;
1508
1509 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
1510 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
1511 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
1512 gApStackSize));
1513 }
1514 }