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