]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuMp.c
552bac3c6dc2c6268a419e4e7a4fcece5e2cba6f
[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 NULL, // StartupAllAPs,
32 StartupThisAP,
33 NULL, // 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 This service retrieves the number of logical processor in the platform
243 and the number of those logical processors that are enabled on this boot.
244 This service may only be called from the BSP.
245
246 This function is used to retrieve the following information:
247 - The number of logical processors that are present in the system.
248 - The number of enabled logical processors in the system at the instant
249 this call is made.
250
251 Because MP Service Protocol provides services to enable and disable processors
252 dynamically, the number of enabled logical processors may vary during the
253 course of a boot session.
254
255 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
256 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
257 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
258 is returned in NumberOfProcessors, the number of currently enabled processor
259 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
260
261 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
262 instance.
263 @param[out] NumberOfProcessors Pointer to the total number of logical
264 processors in the system, including the BSP
265 and disabled APs.
266 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
267 processors that exist in system, including
268 the BSP.
269
270 @retval EFI_SUCCESS The number of logical processors and enabled
271 logical processors was retrieved.
272 @retval EFI_DEVICE_ERROR The calling processor is an AP.
273 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
274 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.
275
276 **/
277 EFI_STATUS
278 EFIAPI
279 GetNumberOfProcessors (
280 IN EFI_MP_SERVICES_PROTOCOL *This,
281 OUT UINTN *NumberOfProcessors,
282 OUT UINTN *NumberOfEnabledProcessors
283 )
284 {
285 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {
286 return EFI_INVALID_PARAMETER;
287 }
288
289 if (!IsBSP ()) {
290 return EFI_DEVICE_ERROR;
291 }
292
293 *NumberOfProcessors = mMpSystemData.NumberOfProcessors;
294 *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;
295 return EFI_SUCCESS;
296 }
297
298 /**
299 Gets detailed MP-related information on the requested processor at the
300 instant this call is made. This service may only be called from the BSP.
301
302 This service retrieves detailed MP-related information about any processor
303 on the platform. Note the following:
304 - The processor information may change during the course of a boot session.
305 - The information presented here is entirely MP related.
306
307 Information regarding the number of caches and their sizes, frequency of operation,
308 slot numbers is all considered platform-related information and is not provided
309 by this service.
310
311 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
312 instance.
313 @param[in] ProcessorNumber The handle number of processor.
314 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
315 the requested processor is deposited.
316
317 @retval EFI_SUCCESS Processor information was returned.
318 @retval EFI_DEVICE_ERROR The calling processor is an AP.
319 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
320 @retval EFI_NOT_FOUND The processor with the handle specified by
321 ProcessorNumber does not exist in the platform.
322
323 **/
324 EFI_STATUS
325 EFIAPI
326 GetProcessorInfo (
327 IN EFI_MP_SERVICES_PROTOCOL *This,
328 IN UINTN ProcessorNumber,
329 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
330 )
331 {
332 if (ProcessorInfoBuffer == NULL) {
333 return EFI_INVALID_PARAMETER;
334 }
335
336 if (!IsBSP ()) {
337 return EFI_DEVICE_ERROR;
338 }
339
340 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
341 return EFI_NOT_FOUND;
342 }
343
344 CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
345 return EFI_SUCCESS;
346 }
347
348 /**
349 This service lets the caller get one enabled AP to execute a caller-provided
350 function. The caller can request the BSP to either wait for the completion
351 of the AP or just proceed with the next task by using the EFI event mechanism.
352 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking
353 execution support. This service may only be called from the BSP.
354
355 This function is used to dispatch one enabled AP to the function specified by
356 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent
357 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or
358 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.
359 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode
360 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,
361 then EFI_UNSUPPORTED must be returned.
362
363 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
364 from Procedure, then execution of Procedure by the AP is terminated. The AP is
365 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and
366 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
367
368 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL
369 instance.
370 @param[in] Procedure A pointer to the function to be run on
371 enabled APs of the system. See type
372 EFI_AP_PROCEDURE.
373 @param[in] ProcessorNumber The handle number of the AP. The range is
374 from 0 to the total number of logical
375 processors minus 1. The total number of
376 logical processors can be retrieved by
377 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
378 @param[in] WaitEvent The event created by the caller with CreateEvent()
379 service. If it is NULL, then execute in
380 blocking mode. BSP waits until all APs finish
381 or TimeoutInMicroseconds expires. If it's
382 not NULL, then execute in non-blocking mode.
383 BSP requests the function specified by
384 Procedure to be started on all the enabled
385 APs, and go on executing immediately. If
386 all return from Procedure or TimeoutInMicroseconds
387 expires, this event is signaled. The BSP
388 can use the CheckEvent() or WaitForEvent()
389 services to check the state of event. Type
390 EFI_EVENT is defined in CreateEvent() in
391 the Unified Extensible Firmware Interface
392 Specification.
393 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
394 APs to return from Procedure, either for
395 blocking or non-blocking mode. Zero means
396 infinity. If the timeout expires before
397 all APs return from Procedure, then Procedure
398 on the failed APs is terminated. All enabled
399 APs are available for next function assigned
400 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()
401 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().
402 If the timeout expires in blocking mode,
403 BSP returns EFI_TIMEOUT. If the timeout
404 expires in non-blocking mode, WaitEvent
405 is signaled with SignalEvent().
406 @param[in] ProcedureArgument The parameter passed into Procedure for
407 all APs.
408 @param[out] Finished If NULL, this parameter is ignored. In
409 blocking mode, this parameter is ignored.
410 In non-blocking mode, if AP returns from
411 Procedure before the timeout expires, its
412 content is set to TRUE. Otherwise, the
413 value is set to FALSE. The caller can
414 determine if the AP returned from Procedure
415 by evaluating this value.
416
417 @retval EFI_SUCCESS In blocking mode, specified AP finished before
418 the timeout expires.
419 @retval EFI_SUCCESS In non-blocking mode, the function has been
420 dispatched to specified AP.
421 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
422 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
423 signaled.
424 @retval EFI_DEVICE_ERROR The calling processor is an AP.
425 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
426 the specified AP has finished.
427 @retval EFI_NOT_READY The specified AP is busy.
428 @retval EFI_NOT_FOUND The processor with the handle specified by
429 ProcessorNumber does not exist.
430 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
431 @retval EFI_INVALID_PARAMETER Procedure is NULL.
432
433 **/
434 EFI_STATUS
435 EFIAPI
436 StartupThisAP (
437 IN EFI_MP_SERVICES_PROTOCOL *This,
438 IN EFI_AP_PROCEDURE Procedure,
439 IN UINTN ProcessorNumber,
440 IN EFI_EVENT WaitEvent OPTIONAL,
441 IN UINTN TimeoutInMicroseconds,
442 IN VOID *ProcedureArgument OPTIONAL,
443 OUT BOOLEAN *Finished OPTIONAL
444 )
445 {
446 CPU_DATA_BLOCK *CpuData;
447 EFI_STATUS Status;
448
449 CpuData = NULL;
450
451 if (Finished != NULL) {
452 *Finished = FALSE;
453 }
454
455 if (!IsBSP ()) {
456 return EFI_DEVICE_ERROR;
457 }
458
459 if (Procedure == NULL) {
460 return EFI_INVALID_PARAMETER;
461 }
462
463 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
464 return EFI_NOT_FOUND;
465 }
466
467 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
468 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||
469 !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
470 return EFI_INVALID_PARAMETER;
471 }
472
473 if (GetApState (CpuData) != CpuStateIdle) {
474 return EFI_NOT_READY;
475 }
476
477 SetApState (CpuData, CpuStateReady);
478
479 SetApProcedure (CpuData, Procedure, ProcedureArgument);
480
481 CpuData->Timeout = TimeoutInMicroseconds;
482 CpuData->WaitEvent = WaitEvent;
483 CpuData->TimeoutActive = !!(TimeoutInMicroseconds);
484 CpuData->Finished = Finished;
485
486 if (WaitEvent != NULL) {
487 //
488 // Non Blocking
489 //
490 Status = gBS->SetTimer (
491 CpuData->CheckThisAPEvent,
492 TimerPeriodic,
493 EFI_TIMER_PERIOD_MICROSECONDS (100)
494 );
495 return Status;
496 }
497
498 //
499 // Blocking
500 //
501 while (TRUE) {
502 if (GetApState (CpuData) == CpuStateFinished) {
503 SetApState (CpuData, CpuStateIdle);
504 break;
505 }
506
507 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
508 ResetProcessorToIdleState (CpuData);
509 return EFI_TIMEOUT;
510 }
511
512 gBS->Stall (gPollInterval);
513 CpuData->Timeout -= gPollInterval;
514 }
515
516 return EFI_SUCCESS;
517 }
518
519 /**
520 This service lets the caller enable or disable an AP from this point onward.
521 This service may only be called from the BSP.
522
523 This service allows the caller enable or disable an AP from this point onward.
524 The caller can optionally specify the health status of the AP by Health. If
525 an AP is being disabled, then the state of the disabled AP is implementation
526 dependent. If an AP is enabled, then the implementation must guarantee that a
527 complete initialization sequence is performed on the AP, so the AP is in a state
528 that is compatible with an MP operating system. This service may not be supported
529 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.
530
531 If the enable or disable AP operation cannot be completed prior to the return
532 from this service, then EFI_UNSUPPORTED must be returned.
533
534 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
535 @param[in] ProcessorNumber The handle number of AP that is to become the new
536 BSP. The range is from 0 to the total number of
537 logical processors minus 1. The total number of
538 logical processors can be retrieved by
539 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
540 @param[in] EnableAP Specifies the new state for the processor for
541 enabled, FALSE for disabled.
542 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
543 the new health status of the AP. This flag
544 corresponds to StatusFlag defined in
545 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
546 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
547 bits are ignored. If it is NULL, this parameter
548 is ignored.
549
550 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
551 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
552 prior to this service returning.
553 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
554 @retval EFI_DEVICE_ERROR The calling processor is an AP.
555 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
556 does not exist.
557 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
558
559 **/
560 EFI_STATUS
561 EFIAPI
562 EnableDisableAP (
563 IN EFI_MP_SERVICES_PROTOCOL *This,
564 IN UINTN ProcessorNumber,
565 IN BOOLEAN EnableAP,
566 IN UINT32 *HealthFlag OPTIONAL
567 )
568 {
569 CPU_DATA_BLOCK *CpuData;
570
571 if (!IsBSP ()) {
572 return EFI_DEVICE_ERROR;
573 }
574
575 if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {
576 return EFI_NOT_FOUND;
577 }
578
579 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
580 if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {
581 return EFI_INVALID_PARAMETER;
582 }
583
584 if (GetApState (CpuData) != CpuStateIdle) {
585 return EFI_UNSUPPORTED;
586 }
587
588 if (EnableAP) {
589 if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {
590 mMpSystemData.NumberOfEnabledProcessors++;
591 }
592 CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);
593 } else {
594 if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {
595 mMpSystemData.NumberOfEnabledProcessors--;
596 }
597 CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);
598 }
599
600 if (HealthFlag != NULL) {
601 CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);
602 CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));
603 }
604
605 return EFI_SUCCESS;
606 }
607
608 /**
609 This return the handle number for the calling processor. This service may be
610 called from the BSP and APs.
611
612 This service returns the processor handle number for the calling processor.
613 The returned value is in the range from 0 to the total number of logical
614 processors minus 1. The total number of logical processors can be retrieved
615 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be
616 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
617 is returned. Otherwise, the current processors handle number is returned in
618 ProcessorNumber, and EFI_SUCCESS is returned.
619
620 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
621 @param[out] ProcessorNumber The handle number of AP that is to become the new
622 BSP. The range is from 0 to the total number of
623 logical processors minus 1. The total number of
624 logical processors can be retrieved by
625 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().
626
627 @retval EFI_SUCCESS The current processor handle number was returned
628 in ProcessorNumber.
629 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
630
631 **/
632 EFI_STATUS
633 EFIAPI
634 WhoAmI (
635 IN EFI_MP_SERVICES_PROTOCOL *This,
636 OUT UINTN *ProcessorNumber
637 )
638 {
639 UINTN Index;
640 UINT32 ProcessorId;
641
642 if (ProcessorNumber == NULL) {
643 return EFI_INVALID_PARAMETER;
644 }
645
646 ProcessorId = GetApicId ();
647 for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {
648 if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {
649 break;
650 }
651 }
652
653 *ProcessorNumber = Index;
654 return EFI_SUCCESS;
655 }
656
657 /**
658 Terminate AP's task and set it to idle state.
659
660 This function terminates AP's task due to timeout by sending INIT-SIPI,
661 and sends it to idle state.
662
663 @param CpuData the pointer to CPU_DATA_BLOCK of specified AP
664
665 **/
666 VOID
667 ResetProcessorToIdleState (
668 IN CPU_DATA_BLOCK *CpuData
669 )
670 {
671 }
672
673 /**
674 Application Processors do loop routine
675 after switch to its own stack.
676
677 @param Context1 A pointer to the context to pass into the function.
678 @param Context2 A pointer to the context to pass into the function.
679
680 **/
681 VOID
682 ProcessorToIdleState (
683 IN VOID *Context1, OPTIONAL
684 IN VOID *Context2 OPTIONAL
685 )
686 {
687 DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));
688
689 AsmApDoneWithCommonStack ();
690
691 CpuSleep ();
692 CpuDeadLoop ();
693 }
694
695 /**
696 Checks AP' status periodically.
697
698 This function is triggerred by timer perodically to check the
699 state of AP forStartupThisAP() executed in non-blocking mode.
700
701 @param Event Event triggered.
702 @param Context Parameter passed with the event.
703
704 **/
705 VOID
706 EFIAPI
707 CheckThisAPStatus (
708 IN EFI_EVENT Event,
709 IN VOID *Context
710 )
711 {
712 CPU_DATA_BLOCK *CpuData;
713 CPU_STATE CpuState;
714
715 CpuData = (CPU_DATA_BLOCK *) Context;
716 if (CpuData->TimeoutActive) {
717 CpuData->Timeout -= gPollInterval;
718 }
719
720 CpuState = GetApState (CpuData);
721
722 if (CpuState == CpuStateFinished) {
723 if (CpuData->Finished) {
724 *CpuData->Finished = TRUE;
725 }
726 SetApState (CpuData, CpuStateIdle);
727 goto out;
728 }
729
730 if (CpuData->TimeoutActive && CpuData->Timeout < 0) {
731 if (CpuState != CpuStateIdle &&
732 CpuData->Finished) {
733 *CpuData->Finished = FALSE;
734 }
735 ResetProcessorToIdleState (CpuData);
736 goto out;
737 }
738
739 return;
740
741 out:
742 gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);
743 if (CpuData->WaitEvent) {
744 gBS->SignalEvent (CpuData->WaitEvent);
745 CpuData->WaitEvent = NULL;
746 }
747 }
748
749 /**
750 Application Processor C code entry point.
751
752 **/
753 VOID
754 EFIAPI
755 ApEntryPointInC (
756 VOID
757 )
758 {
759 VOID* TopOfApStack;
760
761 FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);
762 TopOfApStack = (UINT8*)mApStackStart + gApStackSize;
763 mApStackStart = TopOfApStack;
764
765 mMpSystemData.NumberOfProcessors++;
766
767 SwitchStack (
768 (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,
769 NULL,
770 NULL,
771 TopOfApStack);
772 }
773
774 /**
775 This function is called by all processors (both BSP and AP) once and collects MP related data.
776
777 @param Bsp TRUE if the CPU is BSP
778 @param ProcessorNumber The specific processor number
779
780 @retval EFI_SUCCESS Data for the processor collected and filled in
781
782 **/
783 EFI_STATUS
784 FillInProcessorInformation (
785 IN BOOLEAN Bsp,
786 IN UINTN ProcessorNumber
787 )
788 {
789 CPU_DATA_BLOCK *CpuData;
790 UINT32 ProcessorId;
791
792 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
793 ProcessorId = GetApicId ();
794 CpuData->Info.ProcessorId = ProcessorId;
795 CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;
796 if (Bsp) {
797 CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;
798 }
799 CpuData->Info.Location.Package = ProcessorId;
800 CpuData->Info.Location.Core = 0;
801 CpuData->Info.Location.Thread = 0;
802 CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;
803
804 CpuData->Procedure = NULL;
805 CpuData->Parameter = NULL;
806 InitializeSpinLock (&CpuData->CpuDataLock);
807
808 return EFI_SUCCESS;
809 }
810
811 /**
812 Prepare the System Data.
813
814 @retval EFI_SUCCESS the System Data finished initilization.
815
816 **/
817 EFI_STATUS
818 InitMpSystemData (
819 VOID
820 )
821 {
822 UINTN ProcessorNumber;
823 CPU_DATA_BLOCK *CpuData;
824 EFI_STATUS Status;
825
826 ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));
827
828 mMpSystemData.NumberOfProcessors = 1;
829 mMpSystemData.NumberOfEnabledProcessors = 1;
830
831 mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);
832 ASSERT(mMpSystemData.CpuDatas != NULL);
833
834 for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {
835 CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];
836 Status = gBS->CreateEvent (
837 EVT_TIMER | EVT_NOTIFY_SIGNAL,
838 TPL_CALLBACK,
839 CheckThisAPStatus,
840 (VOID *) CpuData,
841 &CpuData->CheckThisAPEvent
842 );
843 ASSERT_EFI_ERROR (Status);
844 }
845
846 //
847 // BSP
848 //
849 FillInProcessorInformation (TRUE, 0);
850
851 return EFI_SUCCESS;
852 }
853
854 /**
855 Initialize Multi-processor support.
856
857 **/
858 VOID
859 InitializeMpSupport (
860 VOID
861 )
862 {
863 gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
864 if (gMaxLogicalProcessorNumber < 1) {
865 DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));
866 return;
867 }
868
869 if (gMaxLogicalProcessorNumber == 1) {
870 return;
871 }
872
873 gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);
874 ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);
875
876 mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
877 ASSERT (mApStackStart != NULL);
878
879 //
880 // the first buffer of stack size used for common stack, when the amount of AP
881 // more than 1, we should never free the common stack which maybe used for AP reset.
882 //
883 mCommonStack = mApStackStart;
884 mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;
885 mApStackStart = mTopOfApCommonStack;
886
887 InitMpSystemData ();
888
889 if (mMpSystemData.NumberOfProcessors == 1) {
890 FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));
891 return;
892 }
893
894 if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {
895 FreePages (mApStackStart, EFI_SIZE_TO_PAGES (
896 (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *
897 gApStackSize));
898 }
899 }