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