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