]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.c
1.Restore BSP IDT table to AP when AP wakeup.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / MpServicesOnFrameworkMpServicesThunk / MpServicesOnFrameworkMpServicesThunk.c
1 /** @file
2 Produces PI MP Services Protocol on top of Framework MP Services Protocol.
3
4 Intel's Framework MP Services Protocol is replaced by EFI_MP_SERVICES_PROTOCOL in PI 1.1.
5 This module produces PI MP Services Protocol on top of Framework MP Services Protocol.
6
7 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 Module Name:
16
17 **/
18
19 #include "MpServicesOnFrameworkMpServicesThunk.h"
20
21 EFI_HANDLE mHandle = NULL;
22 MP_SYSTEM_DATA mMPSystemData;
23 EFI_PHYSICAL_ADDRESS mStartupVector;
24 MP_CPU_EXCHANGE_INFO *mExchangeInfo;
25 VOID *mStackStartAddress;
26 BOOLEAN mStopCheckAPsStatus = FALSE;
27 UINTN mNumberOfProcessors;
28 EFI_GENERIC_MEMORY_TEST_PROTOCOL *mGenMemoryTest;
29
30 FRAMEWORK_EFI_MP_SERVICES_PROTOCOL *mFrameworkMpService;
31 EFI_MP_SERVICES_PROTOCOL mMpService = {
32 GetNumberOfProcessors,
33 GetProcessorInfo,
34 StartupAllAPs,
35 StartupThisAP,
36 SwitchBSP,
37 EnableDisableAP,
38 WhoAmI
39 };
40
41
42 /**
43 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
44
45 This service retrieves the number of logical processor in the platform
46 and the number of those logical processors that are enabled on this boot.
47 This service may only be called from the BSP.
48
49 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
50 @param NumberOfProcessors Pointer to the total number of logical processors in the system,
51 including the BSP and disabled APs.
52 @param NumberOfEnabledProcessors Pointer to the number of enabled logical processors that exist
53 in system, including the BSP.
54
55 @retval EFI_SUCCESS Number of logical processors and enabled logical processors retrieved..
56 @retval EFI_DEVICE_ERROR Caller processor is AP.
57 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL
58 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL
59
60 **/
61 EFI_STATUS
62 EFIAPI
63 GetNumberOfProcessors (
64 IN EFI_MP_SERVICES_PROTOCOL *This,
65 OUT UINTN *NumberOfProcessors,
66 OUT UINTN *NumberOfEnabledProcessors
67 )
68 {
69 EFI_STATUS Status;
70 UINTN CallerNumber;
71
72 //
73 // Check whether caller processor is BSP
74 //
75 WhoAmI (This, &CallerNumber);
76 if (CallerNumber != GetBspNumber ()) {
77 return EFI_DEVICE_ERROR;
78 }
79
80 //
81 // Check parameter NumberOfProcessors
82 //
83 if (NumberOfProcessors == NULL) {
84 return EFI_INVALID_PARAMETER;
85 }
86
87 //
88 // Check parameter NumberOfEnabledProcessors
89 //
90 if (NumberOfEnabledProcessors == NULL) {
91 return EFI_INVALID_PARAMETER;
92 }
93
94 Status = mFrameworkMpService->GetGeneralMPInfo (
95 mFrameworkMpService,
96 NumberOfProcessors,
97 NULL,
98 NumberOfEnabledProcessors,
99 NULL,
100 NULL
101 );
102 ASSERT_EFI_ERROR (Status);
103
104 return EFI_SUCCESS;
105 }
106
107 /**
108 Implementation of GetNumberOfProcessors() service of MP Services Protocol.
109
110 Gets detailed MP-related information on the requested processor at the
111 instant this call is made. This service may only be called from the BSP.
112
113 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
114 @param ProcessorNumber The handle number of processor.
115 @param ProcessorInfoBuffer A pointer to the buffer where information for the requested processor is deposited.
116
117 @retval EFI_SUCCESS Processor information successfully returned.
118 @retval EFI_DEVICE_ERROR Caller processor is AP.
119 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL
120 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 GetProcessorInfo (
126 IN EFI_MP_SERVICES_PROTOCOL *This,
127 IN UINTN ProcessorNumber,
128 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
129 )
130 {
131 EFI_STATUS Status;
132 UINTN CallerNumber;
133 UINTN BufferSize;
134 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;
135
136 //
137 // Check whether caller processor is BSP
138 //
139 WhoAmI (This, &CallerNumber);
140 if (CallerNumber != GetBspNumber ()) {
141 return EFI_DEVICE_ERROR;
142 }
143
144 //
145 // Check parameter ProcessorInfoBuffer
146 //
147 if (ProcessorInfoBuffer == NULL) {
148 return EFI_INVALID_PARAMETER;
149 }
150
151 //
152 // Check whether processor with the handle specified by ProcessorNumber exists
153 //
154 if (ProcessorNumber >= mNumberOfProcessors) {
155 return EFI_NOT_FOUND;
156 }
157
158 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);
159 Status = mFrameworkMpService->GetProcessorContext (
160 mFrameworkMpService,
161 ProcessorNumber,
162 &BufferSize,
163 &ProcessorContextBuffer
164 );
165 ASSERT_EFI_ERROR (Status);
166
167 ProcessorInfoBuffer->ProcessorId = (UINT64) ProcessorContextBuffer.ApicID;
168
169 //
170 // Get Status Flag of specified processor
171 //
172 ProcessorInfoBuffer->StatusFlag = 0;
173
174 if (ProcessorContextBuffer.Enabled) {
175 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
176 }
177
178 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {
179 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
180 }
181
182 if (ProcessorContextBuffer.Health.Flags.Uint32 == 0) {
183 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
184 }
185
186 ProcessorInfoBuffer->Location.Package = (UINT32) ProcessorContextBuffer.PackageNumber;
187 ProcessorInfoBuffer->Location.Core = (UINT32) ProcessorContextBuffer.NumberOfCores;
188 ProcessorInfoBuffer->Location.Thread = (UINT32) ProcessorContextBuffer.NumberOfThreads;
189
190 return EFI_SUCCESS;
191 }
192
193 /**
194 Implementation of StartupAllAPs() service of MP Services Protocol.
195
196 This service lets the caller get all enabled APs to execute a caller-provided function.
197 This service may only be called from the BSP.
198
199 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
200 @param Procedure A pointer to the function to be run on enabled APs of the system.
201 @param SingleThread Indicates whether to execute the function simultaneously or one by one..
202 @param WaitEvent The event created by the caller.
203 If it is NULL, then execute in blocking mode.
204 If it is not NULL, then execute in non-blocking mode.
205 @param TimeoutInMicroSeconds The time limit in microseconds for this AP to finish the function.
206 Zero means infinity.
207 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
208 @param FailedCpuList The list of processor numbers that fail to finish the function before
209 TimeoutInMicrosecsond expires.
210
211 @retval EFI_SUCCESS In blocking mode, all APs have finished before the timeout expired.
212 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to all enabled APs.
213 @retval EFI_DEVICE_ERROR Caller processor is AP.
214 @retval EFI_NOT_STARTED No enabled AP exists in the system.
215 @retval EFI_NOT_READY Any enabled AP is busy.
216 @retval EFI_TIMEOUT In blocking mode, The timeout expired before all enabled APs have finished.
217 @retval EFI_INVALID_PARAMETER Procedure is NULL.
218
219 **/
220 EFI_STATUS
221 EFIAPI
222 StartupAllAPs (
223 IN EFI_MP_SERVICES_PROTOCOL *This,
224 IN EFI_AP_PROCEDURE Procedure,
225 IN BOOLEAN SingleThread,
226 IN EFI_EVENT WaitEvent OPTIONAL,
227 IN UINTN TimeoutInMicroSeconds,
228 IN VOID *ProcedureArgument OPTIONAL,
229 OUT UINTN **FailedCpuList OPTIONAL
230 )
231 {
232 EFI_STATUS Status;
233 UINTN ProcessorNumber;
234 CPU_DATA_BLOCK *CpuData;
235 BOOLEAN Blocking;
236 UINTN BspNumber;
237
238 if (FailedCpuList != NULL) {
239 *FailedCpuList = NULL;
240 }
241
242 //
243 // Check whether caller processor is BSP
244 //
245 BspNumber = GetBspNumber ();
246 WhoAmI (This, &ProcessorNumber);
247 if (ProcessorNumber != BspNumber) {
248 return EFI_DEVICE_ERROR;
249 }
250
251 //
252 // Check parameter Procedure
253 //
254 if (Procedure == NULL) {
255 return EFI_INVALID_PARAMETER;
256 }
257
258 //
259 // Temporarily suppress CheckAPsStatus()
260 //
261 mStopCheckAPsStatus = TRUE;
262
263 //
264 // Check whether all enabled APs are idle.
265 // If any enabled AP is not idle, return EFI_NOT_READY.
266 //
267 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
268
269 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
270
271 mMPSystemData.CpuList[ProcessorNumber] = FALSE;
272 if (ProcessorNumber != BspNumber) {
273 if (CpuData->State != CpuStateDisabled) {
274 if (CpuData->State != CpuStateIdle) {
275 mStopCheckAPsStatus = FALSE;
276 return EFI_NOT_READY;
277 } else {
278 //
279 // Mark this processor as responsible for current calling.
280 //
281 mMPSystemData.CpuList[ProcessorNumber] = TRUE;
282 }
283 }
284 }
285 }
286
287 mMPSystemData.FinishCount = 0;
288 mMPSystemData.StartCount = 0;
289 Blocking = FALSE;
290 //
291 // Go through all enabled APs to wakeup them for Procedure.
292 // If in Single Thread mode, then only one AP is woken up, and others are waiting.
293 //
294 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
295
296 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
297 //
298 // Check whether this processor is responsible for current calling.
299 //
300 if (mMPSystemData.CpuList[ProcessorNumber]) {
301
302 mMPSystemData.StartCount++;
303
304 AcquireSpinLock (&CpuData->CpuDataLock);
305 CpuData->State = CpuStateReady;
306 ReleaseSpinLock (&CpuData->CpuDataLock);
307
308 if (!Blocking) {
309 WakeUpAp (
310 ProcessorNumber,
311 Procedure,
312 ProcedureArgument
313 );
314 }
315
316 if (SingleThread) {
317 Blocking = TRUE;
318 }
319 }
320 }
321
322 //
323 // If no enabled AP exists, return EFI_NOT_STARTED.
324 //
325 if (mMPSystemData.StartCount == 0) {
326 mStopCheckAPsStatus = FALSE;
327 return EFI_NOT_STARTED;
328 }
329
330 //
331 // If WaitEvent is not NULL, execute in non-blocking mode.
332 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
333 // CheckAPsStatus() will check completion and timeout periodically.
334 //
335 mMPSystemData.Procedure = Procedure;
336 mMPSystemData.ProcArguments = ProcedureArgument;
337 mMPSystemData.SingleThread = SingleThread;
338 mMPSystemData.FailedCpuList = FailedCpuList;
339 mMPSystemData.ExpectedTime = CalculateTimeout (TimeoutInMicroSeconds, &mMPSystemData.CurrentTime);
340 mMPSystemData.WaitEvent = WaitEvent;
341
342 //
343 // Allow CheckAPsStatus()
344 //
345 mStopCheckAPsStatus = FALSE;
346
347 if (WaitEvent != NULL) {
348 return EFI_SUCCESS;
349 }
350
351 //
352 // If WaitEvent is NULL, execute in blocking mode.
353 // BSP checks APs'state until all APs finish or TimeoutInMicrosecsond expires.
354 //
355 do {
356 Status = CheckAllAPs ();
357 } while (Status == EFI_NOT_READY);
358
359 return Status;
360 }
361
362 /**
363 Implementation of StartupThisAP() service of MP Services Protocol.
364
365 This service lets the caller get one enabled AP to execute a caller-provided function.
366 This service may only be called from the BSP.
367
368 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
369 @param Procedure A pointer to the function to be run on the designated AP.
370 @param ProcessorNumber The handle number of AP..
371 @param WaitEvent The event created by the caller.
372 If it is NULL, then execute in blocking mode.
373 If it is not NULL, then execute in non-blocking mode.
374 @param TimeoutInMicroseconds The time limit in microseconds for this AP to finish the function.
375 Zero means infinity.
376 @param ProcedureArgument Pointer to the optional parameter of the assigned function.
377 @param Finished Indicates whether AP has finished assigned function.
378 In blocking mode, it is ignored.
379
380 @retval EFI_SUCCESS In blocking mode, specified AP has finished before the timeout expires.
381 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched to specified AP.
382 @retval EFI_DEVICE_ERROR Caller processor is AP.
383 @retval EFI_TIMEOUT In blocking mode, the timeout expires before specified AP has finished.
384 @retval EFI_NOT_READY Specified AP is busy.
385 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
386 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
387 @retval EFI_INVALID_PARAMETER Procedure is NULL.
388
389 **/
390 EFI_STATUS
391 EFIAPI
392 StartupThisAP (
393 IN EFI_MP_SERVICES_PROTOCOL *This,
394 IN EFI_AP_PROCEDURE Procedure,
395 IN UINTN ProcessorNumber,
396 IN EFI_EVENT WaitEvent OPTIONAL,
397 IN UINTN TimeoutInMicroseconds,
398 IN VOID *ProcedureArgument OPTIONAL,
399 OUT BOOLEAN *Finished OPTIONAL
400 )
401 {
402 CPU_DATA_BLOCK *CpuData;
403 UINTN CallerNumber;
404 EFI_STATUS Status;
405 UINTN BspNumber;
406
407 if (Finished != NULL) {
408 *Finished = TRUE;
409 }
410
411 //
412 // Check whether caller processor is BSP
413 //
414 BspNumber = GetBspNumber ();
415 WhoAmI (This, &CallerNumber);
416 if (CallerNumber != BspNumber) {
417 return EFI_DEVICE_ERROR;
418 }
419
420 //
421 // Check whether processor with the handle specified by ProcessorNumber exists
422 //
423 if (ProcessorNumber >= mNumberOfProcessors) {
424 return EFI_NOT_FOUND;
425 }
426
427 //
428 // Check whether specified processor is BSP
429 //
430 if (ProcessorNumber == BspNumber) {
431 return EFI_INVALID_PARAMETER;
432 }
433
434 //
435 // Check parameter Procedure
436 //
437 if (Procedure == NULL) {
438 return EFI_INVALID_PARAMETER;
439 }
440
441 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
442
443 //
444 // Temporarily suppress CheckAPsStatus()
445 //
446 mStopCheckAPsStatus = TRUE;
447
448 //
449 // Check whether specified AP is disabled
450 //
451 if (CpuData->State == CpuStateDisabled) {
452 mStopCheckAPsStatus = FALSE;
453 return EFI_INVALID_PARAMETER;
454 }
455
456 //
457 // Check whether specified AP is busy
458 //
459 if (CpuData->State != CpuStateIdle) {
460 mStopCheckAPsStatus = FALSE;
461 return EFI_NOT_READY;
462 }
463
464 //
465 // Wakeup specified AP for Procedure.
466 //
467 AcquireSpinLock (&CpuData->CpuDataLock);
468 CpuData->State = CpuStateReady;
469 ReleaseSpinLock (&CpuData->CpuDataLock);
470
471 WakeUpAp (
472 ProcessorNumber,
473 Procedure,
474 ProcedureArgument
475 );
476
477 //
478 // If WaitEvent is not NULL, execute in non-blocking mode.
479 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
480 // CheckAPsStatus() will check completion and timeout periodically.
481 //
482 CpuData->WaitEvent = WaitEvent;
483 CpuData->Finished = Finished;
484 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
485
486 //
487 // Allow CheckAPsStatus()
488 //
489 mStopCheckAPsStatus = FALSE;
490
491 if (WaitEvent != NULL) {
492 return EFI_SUCCESS;
493 }
494
495 //
496 // If WaitEvent is NULL, execute in blocking mode.
497 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
498 //
499 do {
500 Status = CheckThisAP (ProcessorNumber);
501 } while (Status == EFI_NOT_READY);
502
503 return Status;
504 }
505
506 /**
507 Implementation of SwitchBSP() service of MP Services Protocol.
508
509 This service switches the requested AP to be the BSP from that point onward.
510 This service may only be called from the current BSP.
511
512 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
513 @param ProcessorNumber The handle number of processor.
514 @param EnableOldBSP Whether to enable or disable the original BSP.
515
516 @retval EFI_SUCCESS BSP successfully switched.
517 @retval EFI_DEVICE_ERROR Caller processor is AP.
518 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
519 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
520 @retval EFI_NOT_READY Specified AP is busy.
521
522 **/
523 EFI_STATUS
524 EFIAPI
525 SwitchBSP (
526 IN EFI_MP_SERVICES_PROTOCOL *This,
527 IN UINTN ProcessorNumber,
528 IN BOOLEAN EnableOldBSP
529 )
530 {
531 EFI_STATUS Status;
532 CPU_DATA_BLOCK *CpuData;
533 UINTN CallerNumber;
534 UINTN BspNumber;
535
536 //
537 // Check whether caller processor is BSP
538 //
539 BspNumber = GetBspNumber ();
540 WhoAmI (This, &CallerNumber);
541 if (CallerNumber != BspNumber) {
542 return EFI_DEVICE_ERROR;
543 }
544
545 //
546 // Check whether processor with the handle specified by ProcessorNumber exists
547 //
548 if (ProcessorNumber >= mNumberOfProcessors) {
549 return EFI_NOT_FOUND;
550 }
551
552 //
553 // Check whether specified processor is BSP
554 //
555 if (ProcessorNumber == BspNumber) {
556 return EFI_INVALID_PARAMETER;
557 }
558
559 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
560
561 //
562 // Check whether specified AP is disabled
563 //
564 if (CpuData->State == CpuStateDisabled) {
565 return EFI_INVALID_PARAMETER;
566 }
567
568 //
569 // Check whether specified AP is busy
570 //
571 if (CpuData->State != CpuStateIdle) {
572 return EFI_NOT_READY;
573 }
574
575 Status = mFrameworkMpService->SwitchBSP (
576 mFrameworkMpService,
577 ProcessorNumber,
578 EnableOldBSP
579 );
580 ASSERT_EFI_ERROR (Status);
581
582 ChangeCpuState (BspNumber, EnableOldBSP);
583
584 return EFI_SUCCESS;
585 }
586
587 /**
588 Implementation of EnableDisableAP() service of MP Services Protocol.
589
590 This service lets the caller enable or disable an AP.
591 This service may only be called from the BSP.
592
593 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
594 @param ProcessorNumber The handle number of processor.
595 @param EnableAP Indicates whether the newstate of the AP is enabled or disabled.
596 @param HealthFlag Indicates new health state of the AP..
597
598 @retval EFI_SUCCESS AP successfully enabled or disabled.
599 @retval EFI_DEVICE_ERROR Caller processor is AP.
600 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
601 @retval EFI_INVALID_PARAMETERS ProcessorNumber specifies the BSP.
602
603 **/
604 EFI_STATUS
605 EFIAPI
606 EnableDisableAP (
607 IN EFI_MP_SERVICES_PROTOCOL *This,
608 IN UINTN ProcessorNumber,
609 IN BOOLEAN EnableAP,
610 IN UINT32 *HealthFlag OPTIONAL
611 )
612 {
613 EFI_STATUS Status;
614 UINTN CallerNumber;
615 EFI_MP_HEALTH HealthState;
616 EFI_MP_HEALTH *HealthStatePointer;
617 UINTN BspNumber;
618
619 //
620 // Check whether caller processor is BSP
621 //
622 BspNumber = GetBspNumber ();
623 WhoAmI (This, &CallerNumber);
624 if (CallerNumber != BspNumber) {
625 return EFI_DEVICE_ERROR;
626 }
627
628 //
629 // Check whether processor with the handle specified by ProcessorNumber exists
630 //
631 if (ProcessorNumber >= mNumberOfProcessors) {
632 return EFI_NOT_FOUND;
633 }
634
635 //
636 // Check whether specified processor is BSP
637 //
638 if (ProcessorNumber == BspNumber) {
639 return EFI_INVALID_PARAMETER;
640 }
641
642 if (HealthFlag == NULL) {
643 HealthStatePointer = NULL;
644 } else {
645 if ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) == 0) {
646 HealthState.Flags.Uint32 = 1;
647 } else {
648 HealthState.Flags.Uint32 = 0;
649 }
650 HealthState.TestStatus = 0;
651
652 HealthStatePointer = &HealthState;
653 }
654
655 Status = mFrameworkMpService->EnableDisableAP (
656 mFrameworkMpService,
657 ProcessorNumber,
658 EnableAP,
659 HealthStatePointer
660 );
661 ASSERT_EFI_ERROR (Status);
662
663 ChangeCpuState (ProcessorNumber, EnableAP);
664
665 return EFI_SUCCESS;
666 }
667
668 /**
669 Implementation of WhoAmI() service of MP Services Protocol.
670
671 This service lets the caller processor get its handle number.
672 This service may be called from the BSP and APs.
673
674 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
675 @param ProcessorNumber Pointer to the handle number of AP.
676
677 @retval EFI_SUCCESS Processor number successfully returned.
678 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL
679
680 **/
681 EFI_STATUS
682 EFIAPI
683 WhoAmI (
684 IN EFI_MP_SERVICES_PROTOCOL *This,
685 OUT UINTN *ProcessorNumber
686 )
687 {
688 EFI_STATUS Status;
689
690 if (ProcessorNumber == NULL) {
691 return EFI_INVALID_PARAMETER;
692 }
693
694 Status = mFrameworkMpService->WhoAmI (
695 mFrameworkMpService,
696 ProcessorNumber
697 );
698 ASSERT_EFI_ERROR (Status);
699
700 return EFI_SUCCESS;
701 }
702
703 /**
704 Checks APs' status periodically.
705
706 This function is triggerred by timer perodically to check the
707 state of APs for StartupAllAPs() and StartupThisAP() executed
708 in non-blocking mode.
709
710 @param Event Event triggered.
711 @param Context Parameter passed with the event.
712
713 **/
714 VOID
715 EFIAPI
716 CheckAPsStatus (
717 IN EFI_EVENT Event,
718 IN VOID *Context
719 )
720 {
721 UINTN ProcessorNumber;
722 CPU_DATA_BLOCK *CpuData;
723 EFI_STATUS Status;
724
725 //
726 // If CheckAPsStatus() is stopped, then return immediately.
727 //
728 if (mStopCheckAPsStatus) {
729 return;
730 }
731
732 //
733 // First, check whether pending StartupAllAPs() exists.
734 //
735 if (mMPSystemData.WaitEvent != NULL) {
736
737 Status = CheckAllAPs ();
738 //
739 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it..
740 //
741 if (Status != EFI_NOT_READY) {
742 Status = gBS->SignalEvent (mMPSystemData.WaitEvent);
743 mMPSystemData.WaitEvent = NULL;
744 }
745 }
746
747 //
748 // Second, check whether pending StartupThisAPs() callings exist.
749 //
750 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
751
752 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
753
754 if (CpuData->WaitEvent == NULL) {
755 continue;
756 }
757
758 Status = CheckThisAP (ProcessorNumber);
759
760 if (Status != EFI_NOT_READY) {
761 gBS->SignalEvent (CpuData->WaitEvent);
762 CpuData->WaitEvent = NULL;
763 }
764 }
765 return ;
766 }
767
768 /**
769 Checks status of all APs.
770
771 This function checks whether all APs have finished task assigned by StartupAllAPs(),
772 and whether timeout expires.
773
774 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
775 @retval EFI_TIMEOUT The timeout expires.
776 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
777
778 **/
779 EFI_STATUS
780 CheckAllAPs (
781 VOID
782 )
783 {
784 UINTN ProcessorNumber;
785 UINTN NextProcessorNumber;
786 UINTN ListIndex;
787 EFI_STATUS Status;
788 CPU_STATE CpuState;
789 CPU_DATA_BLOCK *CpuData;
790
791 NextProcessorNumber = 0;
792
793 //
794 // Go through all APs that are responsible for the StartupAllAPs().
795 //
796 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
797 if (!mMPSystemData.CpuList[ProcessorNumber]) {
798 continue;
799 }
800
801 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
802
803 //
804 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
805 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
806 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
807 //
808 AcquireSpinLock (&CpuData->CpuDataLock);
809 CpuState = CpuData->State;
810 ReleaseSpinLock (&CpuData->CpuDataLock);
811
812 if (CpuState == CpuStateFinished) {
813 mMPSystemData.FinishCount++;
814 mMPSystemData.CpuList[ProcessorNumber] = FALSE;
815
816 AcquireSpinLock (&CpuData->CpuDataLock);
817 CpuData->State = CpuStateIdle;
818 ReleaseSpinLock (&CpuData->CpuDataLock);
819
820 //
821 // If in Single Thread mode, then search for the next waiting AP for execution.
822 //
823 if (mMPSystemData.SingleThread) {
824 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
825
826 if (!EFI_ERROR (Status)) {
827 WakeUpAp (
828 NextProcessorNumber,
829 mMPSystemData.Procedure,
830 mMPSystemData.ProcArguments
831 );
832 }
833 }
834 }
835 }
836
837 //
838 // If all APs finish, return EFI_SUCCESS.
839 //
840 if (mMPSystemData.FinishCount == mMPSystemData.StartCount) {
841 return EFI_SUCCESS;
842 }
843
844 //
845 // If timeout expires, report timeout.
846 //
847 if (CheckTimeout (&mMPSystemData.CurrentTime, &mMPSystemData.TotalTime, mMPSystemData.ExpectedTime)) {
848 //
849 // If FailedCpuList is not NULL, record all failed APs in it.
850 //
851 if (mMPSystemData.FailedCpuList != NULL) {
852 *mMPSystemData.FailedCpuList = AllocatePool ((mMPSystemData.StartCount - mMPSystemData.FinishCount + 1) * sizeof(UINTN));
853 ASSERT (*mMPSystemData.FailedCpuList != NULL);
854 }
855 ListIndex = 0;
856
857 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
858 //
859 // Check whether this processor is responsible for StartupAllAPs().
860 //
861 if (mMPSystemData.CpuList[ProcessorNumber]) {
862 //
863 // Reset failed APs to idle state
864 //
865 ResetProcessorToIdleState (ProcessorNumber);
866 mMPSystemData.CpuList[ProcessorNumber] = FALSE;
867 if (mMPSystemData.FailedCpuList != NULL) {
868 (*mMPSystemData.FailedCpuList)[ListIndex++] = ProcessorNumber;
869 }
870 }
871 }
872 if (mMPSystemData.FailedCpuList != NULL) {
873 (*mMPSystemData.FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
874 }
875 return EFI_TIMEOUT;
876 }
877 return EFI_NOT_READY;
878 }
879
880 /**
881 Checks status of specified AP.
882
883 This function checks whether specified AP has finished task assigned by StartupThisAP(),
884 and whether timeout expires.
885
886 @param ProcessorNumber The handle number of processor.
887
888 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
889 @retval EFI_TIMEOUT The timeout expires.
890 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
891
892 **/
893 EFI_STATUS
894 CheckThisAP (
895 UINTN ProcessorNumber
896 )
897 {
898 CPU_DATA_BLOCK *CpuData;
899 CPU_STATE CpuState;
900
901 ASSERT (ProcessorNumber < mNumberOfProcessors);
902 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
903
904 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
905
906 //
907 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
908 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
909 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
910 //
911 AcquireSpinLock (&CpuData->CpuDataLock);
912 CpuState = CpuData->State;
913 ReleaseSpinLock (&CpuData->CpuDataLock);
914
915 //
916 // If the APs finishes for StartupThisAP(), return EFI_SUCCESS.
917 //
918 if (CpuState == CpuStateFinished) {
919
920 AcquireSpinLock (&CpuData->CpuDataLock);
921 CpuData->State = CpuStateIdle;
922 ReleaseSpinLock (&CpuData->CpuDataLock);
923
924 if (CpuData->Finished != NULL) {
925 *(CpuData->Finished) = TRUE;
926 }
927 return EFI_SUCCESS;
928 } else {
929 //
930 // If timeout expires for StartupThisAP(), report timeout.
931 //
932 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
933
934 if (CpuData->Finished != NULL) {
935 *(CpuData->Finished) = FALSE;
936 }
937 //
938 // Reset failed AP to idle state
939 //
940 ResetProcessorToIdleState (ProcessorNumber);
941
942 return EFI_TIMEOUT;
943 }
944 }
945 return EFI_NOT_READY;
946 }
947
948 /**
949 Calculate timeout value and return the current performance counter value.
950
951 Calculate the number of performance counter ticks required for a timeout.
952 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
953 as infinity.
954
955 @param TimeoutInMicroseconds Timeout value in microseconds.
956 @param CurrentTime Returns the current value of the performance counter.
957
958 @return Expected timestamp counter for timeout.
959 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
960 as infinity.
961
962 **/
963 UINT64
964 CalculateTimeout (
965 IN UINTN TimeoutInMicroseconds,
966 OUT UINT64 *CurrentTime
967 )
968 {
969 //
970 // Read the current value of the performance counter
971 //
972 *CurrentTime = GetPerformanceCounter ();
973
974 //
975 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
976 // as infinity.
977 //
978 if (TimeoutInMicroseconds == 0) {
979 return 0;
980 }
981
982 //
983 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
984 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
985 // it by 1,000,000, to get the number of ticks for the timeout value.
986 //
987 return DivU64x32 (
988 MultU64x64 (
989 GetPerformanceCounterProperties (NULL, NULL),
990 TimeoutInMicroseconds
991 ),
992 1000000
993 );
994 }
995
996 /**
997 Checks whether timeout expires.
998
999 Check whether the number of ellapsed performance counter ticks required for a timeout condition
1000 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.
1001
1002 @param PreviousTime On input, the value of the performance counter when it was last read.
1003 On output, the current value of the performance counter
1004 @param TotalTime The total amount of ellapsed time in performance counter ticks.
1005 @param Timeout The number of performance counter ticks required to reach a timeout condition.
1006
1007 @retval TRUE A timeout condition has been reached.
1008 @retval FALSE A timeout condition has not been reached.
1009
1010 **/
1011 BOOLEAN
1012 CheckTimeout (
1013 IN OUT UINT64 *PreviousTime,
1014 IN UINT64 *TotalTime,
1015 IN UINT64 Timeout
1016 )
1017 {
1018 UINT64 Start;
1019 UINT64 End;
1020 UINT64 CurrentTime;
1021 INT64 Delta;
1022 INT64 Cycle;
1023
1024 if (Timeout == 0) {
1025 return FALSE;
1026 }
1027 GetPerformanceCounterProperties (&Start, &End);
1028 Cycle = End - Start;
1029 if (Cycle < 0) {
1030 Cycle = -Cycle;
1031 }
1032 Cycle++;
1033 CurrentTime = GetPerformanceCounter();
1034 Delta = (INT64) (CurrentTime - *PreviousTime);
1035 if (Start > End) {
1036 Delta = -Delta;
1037 }
1038 if (Delta < 0) {
1039 Delta += Cycle;
1040 }
1041 *TotalTime += Delta;
1042 *PreviousTime = CurrentTime;
1043 if (*TotalTime > Timeout) {
1044 return TRUE;
1045 }
1046 return FALSE;
1047 }
1048
1049 /**
1050 Searches for the next waiting AP.
1051
1052 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1053
1054 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.
1055
1056 @retval EFI_SUCCESS The next waiting AP has been found.
1057 @retval EFI_NOT_FOUND No waiting AP exists.
1058
1059 **/
1060 EFI_STATUS
1061 GetNextWaitingProcessorNumber (
1062 OUT UINTN *NextProcessorNumber
1063 )
1064 {
1065 UINTN ProcessorNumber;
1066
1067 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
1068
1069 if (mMPSystemData.CpuList[ProcessorNumber]) {
1070 *NextProcessorNumber = ProcessorNumber;
1071 return EFI_SUCCESS;
1072 }
1073 }
1074
1075 return EFI_NOT_FOUND;
1076 }
1077
1078 /**
1079 Programs Local APIC registers for virtual wire mode.
1080
1081 This function programs Local APIC registers for virtual wire mode.
1082
1083 @param Bsp Indicates whether the programmed processor is going to be BSP
1084
1085 **/
1086 VOID
1087 ProgramVirtualWireMode (
1088 BOOLEAN Bsp
1089 )
1090 {
1091 UINTN ApicBase;
1092 UINT32 Value;
1093
1094 ApicBase = (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;
1095
1096 //
1097 // Program the Spurious Vector entry
1098 // Set bit 8 (APIC Software Enable/Disable) to enable local APIC,
1099 // and set Spurious Vector as 0x0F.
1100 //
1101 MmioBitFieldWrite32 (ApicBase + APIC_REGISTER_SPURIOUS_VECTOR_OFFSET, 0, 9, 0x10F);
1102
1103 //
1104 // Program the LINT0 vector entry as ExtInt
1105 // Set bits 8..10 to 7 as ExtInt Delivery Mode,
1106 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,
1107 // Trigger Mode, and Mask
1108 //
1109 if (!Bsp) {
1110 DisableInterrupts ();
1111 }
1112 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET);
1113 Value = BitFieldWrite32 (Value, 8, 10, 7);
1114 Value = BitFieldWrite32 (Value, 12, 16, 0);
1115 if (!Bsp) {
1116 //
1117 // For APs, LINT0 is masked
1118 //
1119 Value = BitFieldWrite32 (Value, 16, 16, 1);
1120 }
1121 MmioWrite32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET, Value);
1122
1123 //
1124 // Program the LINT1 vector entry as NMI
1125 // Set bits 8..10 to 4 as NMI Delivery Mode,
1126 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,
1127 // Trigger Mode.
1128 // For BSP clear Mask bit, and for AP set mask bit.
1129 //
1130 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET);
1131 Value = BitFieldWrite32 (Value, 8, 10, 4);
1132 Value = BitFieldWrite32 (Value, 12, 16, 0);
1133 if (!Bsp) {
1134 //
1135 // For APs, LINT1 is masked
1136 //
1137 Value = BitFieldWrite32 (Value, 16, 16, 1);
1138 }
1139 MmioWrite32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET, Value);
1140 }
1141
1142
1143 /**
1144 Wrapper function for all procedures assigned to AP.
1145
1146 Wrapper function for all procedures assigned to AP via MP service protocol.
1147 It controls states of AP and invokes assigned precedure.
1148
1149 **/
1150 VOID
1151 ApProcWrapper (
1152 VOID
1153 )
1154 {
1155 EFI_AP_PROCEDURE Procedure;
1156 VOID *Parameter;
1157 UINTN ProcessorNumber;
1158 CPU_DATA_BLOCK *CpuData;
1159
1160 ProgramVirtualWireMode (FALSE);
1161
1162 WhoAmI (&mMpService, &ProcessorNumber);
1163 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1164
1165 AcquireSpinLock (&CpuData->CpuDataLock);
1166 CpuData->State = CpuStateBusy;
1167 ReleaseSpinLock (&CpuData->CpuDataLock);
1168
1169 //
1170 // Now let us check it out.
1171 //
1172 AcquireSpinLock (&CpuData->CpuDataLock);
1173 Procedure = CpuData->Procedure;
1174 Parameter = CpuData->Parameter;
1175 ReleaseSpinLock (&CpuData->CpuDataLock);
1176
1177 if (Procedure != NULL) {
1178
1179 Procedure (Parameter);
1180
1181 //
1182 // if BSP is switched to AP, it continue execute from here, but it carries register state
1183 // of the old AP, so need to reload CpuData (might be stored in a register after compiler
1184 // optimization) to make sure it points to the right data
1185 //
1186 WhoAmI (&mMpService, &ProcessorNumber);
1187 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1188
1189 AcquireSpinLock (&CpuData->CpuDataLock);
1190 CpuData->Procedure = NULL;
1191 ReleaseSpinLock (&CpuData->CpuDataLock);
1192 }
1193
1194 AcquireSpinLock (&CpuData->CpuDataLock);
1195 CpuData->State = CpuStateFinished;
1196 ReleaseSpinLock (&CpuData->CpuDataLock);
1197 }
1198
1199 /**
1200 Sends INIT-SIPI-SIPI to AP.
1201
1202 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.
1203
1204 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.
1205 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.
1206 @param ApFunction The procedure for AP to work on.
1207
1208 **/
1209 VOID
1210 SendInitSipiSipi (
1211 IN BOOLEAN Broadcast,
1212 IN UINT32 ApicID,
1213 IN VOID *ApFunction
1214 )
1215 {
1216 UINTN ApicBase;
1217 UINT32 ICRLow;
1218 UINT32 ICRHigh;
1219
1220 UINT32 VectorNumber;
1221 UINT32 DeliveryMode;
1222
1223 mExchangeInfo->ApFunction = ApFunction;
1224 mExchangeInfo->StackStart = mStackStartAddress;
1225
1226 if (Broadcast) {
1227 ICRHigh = 0;
1228 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1229 } else {
1230 ICRHigh = ApicID << 24;
1231 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1232 }
1233
1234 VectorNumber = 0;
1235 DeliveryMode = DELIVERY_MODE_INIT;
1236 ICRLow |= VectorNumber | (DeliveryMode << 8);
1237
1238 ApicBase = 0xfee00000;
1239
1240 //
1241 // Write Interrupt Command Registers to send INIT IPI.
1242 //
1243 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_HIGH_OFFSET, ICRHigh);
1244 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1245
1246 MicroSecondDelay (10);
1247
1248 VectorNumber = (UINT32) RShiftU64 (mStartupVector, 12);
1249 DeliveryMode = DELIVERY_MODE_SIPI;
1250 if (Broadcast) {
1251 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1252 } else {
1253 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1254 }
1255
1256 ICRLow |= VectorNumber | (DeliveryMode << 8);
1257
1258 //
1259 // Write Interrupt Command Register to send first SIPI IPI.
1260 //
1261 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1262
1263 MicroSecondDelay (200);
1264
1265 //
1266 // Write Interrupt Command Register to send second SIPI IPI.
1267 //
1268 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1269 }
1270
1271 /**
1272 Function to wake up a specified AP and assign procedure to it.
1273
1274 @param ProcessorNumber Handle number of the specified processor.
1275 @param Procedure Procedure to assign.
1276 @param ProcArguments Argument for Procedure.
1277
1278 **/
1279 VOID
1280 WakeUpAp (
1281 IN UINTN ProcessorNumber,
1282 IN EFI_AP_PROCEDURE Procedure,
1283 IN VOID *ProcArguments
1284 )
1285 {
1286 EFI_STATUS Status;
1287 CPU_DATA_BLOCK *CpuData;
1288 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;
1289
1290 ASSERT (ProcessorNumber < mNumberOfProcessors);
1291 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
1292
1293 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1294
1295 AcquireSpinLock (&CpuData->CpuDataLock);
1296 CpuData->Parameter = ProcArguments;
1297 CpuData->Procedure = Procedure;
1298 ReleaseSpinLock (&CpuData->CpuDataLock);
1299
1300 Status = GetProcessorInfo (
1301 &mMpService,
1302 ProcessorNumber,
1303 &ProcessorInfoBuffer
1304 );
1305 ASSERT_EFI_ERROR (Status);
1306
1307 SendInitSipiSipi (
1308 FALSE,
1309 (UINT32) ProcessorInfoBuffer.ProcessorId,
1310 (VOID *) (UINTN) ApProcWrapper
1311 );
1312 }
1313
1314 /**
1315 Terminate AP's task and set it to idle state.
1316
1317 This function terminates AP's task due to timeout by sending INIT-SIPI,
1318 and sends it to idle state.
1319
1320 @param ProcessorNumber Handle number of the specified processor.
1321
1322 **/
1323 VOID
1324 ResetProcessorToIdleState (
1325 UINTN ProcessorNumber
1326 )
1327 {
1328 EFI_STATUS Status;
1329 CPU_DATA_BLOCK *CpuData;
1330 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;
1331
1332 Status = GetProcessorInfo (
1333 &mMpService,
1334 ProcessorNumber,
1335 &ProcessorInfoBuffer
1336 );
1337 ASSERT_EFI_ERROR (Status);
1338
1339 SendInitSipiSipi (
1340 FALSE,
1341 (UINT32) ProcessorInfoBuffer.ProcessorId,
1342 NULL
1343 );
1344
1345 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1346
1347 AcquireSpinLock (&CpuData->CpuDataLock);
1348 CpuData->State = CpuStateIdle;
1349 ReleaseSpinLock (&CpuData->CpuDataLock);
1350 }
1351
1352 /**
1353 Worker function of EnableDisableAP ()
1354
1355 Worker function of EnableDisableAP (). Changes state of specified processor.
1356
1357 @param ProcessorNumber Processor number of specified AP.
1358 @param NewState Desired state of the specified AP.
1359
1360 @retval EFI_SUCCESS AP's state successfully changed.
1361
1362 **/
1363 EFI_STATUS
1364 ChangeCpuState (
1365 IN UINTN ProcessorNumber,
1366 IN BOOLEAN NewState
1367 )
1368 {
1369 CPU_DATA_BLOCK *CpuData;
1370
1371 ASSERT (ProcessorNumber < mNumberOfProcessors);
1372 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
1373
1374 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1375
1376 if (!NewState) {
1377 AcquireSpinLock (&CpuData->CpuDataLock);
1378 CpuData->State = CpuStateDisabled;
1379 ReleaseSpinLock (&CpuData->CpuDataLock);
1380 } else {
1381 AcquireSpinLock (&CpuData->CpuDataLock);
1382 CpuData->State = CpuStateIdle;
1383 ReleaseSpinLock (&CpuData->CpuDataLock);
1384 }
1385
1386 return EFI_SUCCESS;
1387 }
1388
1389 /**
1390 Test memory region of EfiGcdMemoryTypeReserved.
1391
1392 @param Length The length of memory region to test.
1393
1394 @retval EFI_SUCCESS The memory region passes test.
1395 @retval EFI_NOT_FOUND The memory region is not reserved memory.
1396 @retval EFI_DEVICE_ERROR The memory fails on test.
1397
1398 **/
1399 EFI_STATUS
1400 TestReservedMemory (
1401 UINTN Length
1402 )
1403 {
1404 EFI_STATUS Status;
1405 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
1406 EFI_PHYSICAL_ADDRESS Address;
1407 UINTN LengthCovered;
1408 UINTN RemainingLength;
1409
1410 //
1411 // Walk through the memory descriptors covering the memory range.
1412 //
1413 Address = mStartupVector;
1414 RemainingLength = Length;
1415 while (Address < mStartupVector + Length) {
1416 Status = gDS->GetMemorySpaceDescriptor(
1417 Address,
1418 &Descriptor
1419 );
1420 if (EFI_ERROR (Status)) {
1421 return EFI_NOT_FOUND;
1422 }
1423
1424 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {
1425 return EFI_NOT_FOUND;
1426 }
1427 //
1428 // Calculated the length of the intersected range.
1429 //
1430 LengthCovered = (UINTN) (Descriptor.BaseAddress + Descriptor.Length - Address);
1431 if (LengthCovered > RemainingLength) {
1432 LengthCovered = RemainingLength;
1433 }
1434
1435 Status = mGenMemoryTest->CompatibleRangeTest (
1436 mGenMemoryTest,
1437 Address,
1438 LengthCovered
1439 );
1440 if (EFI_ERROR (Status)) {
1441 return EFI_DEVICE_ERROR;
1442 }
1443
1444 Address += LengthCovered;
1445 RemainingLength -= LengthCovered;
1446 }
1447
1448 return EFI_SUCCESS;
1449 }
1450
1451 /**
1452 Allocates startup vector for APs.
1453
1454 This function allocates Startup vector for APs.
1455
1456 @param Size The size of startup vector.
1457
1458 **/
1459 VOID
1460 AllocateStartupVector (
1461 UINTN Size
1462 )
1463 {
1464 EFI_STATUS Status;
1465
1466 Status = gBS->LocateProtocol (
1467 &gEfiGenericMemTestProtocolGuid,
1468 NULL,
1469 (VOID **) &mGenMemoryTest
1470 );
1471 if (EFI_ERROR (Status)) {
1472 mGenMemoryTest = NULL;
1473 }
1474
1475 for (mStartupVector = 0x7F000; mStartupVector >= 0x2000; mStartupVector -= EFI_PAGE_SIZE) {
1476 if (mGenMemoryTest != NULL) {
1477 //
1478 // Test memory if it is EfiGcdMemoryTypeReserved.
1479 //
1480 Status = TestReservedMemory (EFI_SIZE_TO_PAGES (Size) * EFI_PAGE_SIZE);
1481 if (Status == EFI_DEVICE_ERROR) {
1482 continue;
1483 }
1484 }
1485
1486 Status = gBS->AllocatePages (
1487 AllocateAddress,
1488 EfiBootServicesCode,
1489 EFI_SIZE_TO_PAGES (Size),
1490 &mStartupVector
1491 );
1492
1493 if (!EFI_ERROR (Status)) {
1494 break;
1495 }
1496 }
1497
1498 ASSERT_EFI_ERROR (Status);
1499 }
1500
1501 /**
1502 Prepares Startup Vector for APs.
1503
1504 This function prepares Startup Vector for APs.
1505
1506 **/
1507 VOID
1508 PrepareAPStartupVector (
1509 VOID
1510 )
1511 {
1512 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1513 IA32_DESCRIPTOR GdtrForBSP;
1514 IA32_DESCRIPTOR IdtrForBSP;
1515 EFI_PHYSICAL_ADDRESS GdtForAP;
1516 EFI_PHYSICAL_ADDRESS IdtForAP;
1517 EFI_STATUS Status;
1518
1519 //
1520 // Get the address map of startup code for AP,
1521 // including code size, and offset of long jump instructions to redirect.
1522 //
1523 AsmGetAddressMap (&AddressMap);
1524
1525 //
1526 // Allocate a 4K-aligned region under 1M for startup vector for AP.
1527 // The region contains AP startup code and exchange data between BSP and AP.
1528 //
1529 AllocateStartupVector (AddressMap.Size + sizeof (MP_CPU_EXCHANGE_INFO));
1530
1531 //
1532 // Copy AP startup code to startup vector, and then redirect the long jump
1533 // instructions for mode switching.
1534 //
1535 CopyMem ((VOID *) (UINTN) mStartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);
1536 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32) (mStartupVector + AddressMap.PModeEntryOffset);
1537 //
1538 // For IA32 mode, LongJumpOffset is filled with zero. If non-zero, then we are in X64 mode, so further redirect for long mode switch.
1539 //
1540 if (AddressMap.LongJumpOffset != 0) {
1541 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.LongJumpOffset + 2) = (UINT32) (mStartupVector + AddressMap.LModeEntryOffset);
1542 }
1543
1544 //
1545 // Get the start address of exchange data between BSP and AP.
1546 //
1547 mExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (mStartupVector + AddressMap.Size);
1548
1549 ZeroMem ((VOID *) mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));
1550
1551 mStackStartAddress = AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER * AP_STACK_SIZE));
1552 mExchangeInfo->StackSize = AP_STACK_SIZE;
1553
1554 AsmReadGdtr (&GdtrForBSP);
1555 AsmReadIdtr (&IdtrForBSP);
1556
1557 //
1558 // Allocate memory under 4G to hold GDT for APs
1559 //
1560 GdtForAP = 0xffffffff;
1561 Status = gBS->AllocatePages (
1562 AllocateMaxAddress,
1563 EfiBootServicesData,
1564 EFI_SIZE_TO_PAGES ((GdtrForBSP.Limit + 1) + (IdtrForBSP.Limit + 1)),
1565 &GdtForAP
1566 );
1567 ASSERT_EFI_ERROR (Status);
1568
1569 IdtForAP = (UINTN) GdtForAP + GdtrForBSP.Limit + 1;
1570
1571 CopyMem ((VOID *) (UINTN) GdtForAP, (VOID *) GdtrForBSP.Base, GdtrForBSP.Limit + 1);
1572 CopyMem ((VOID *) (UINTN) IdtForAP, (VOID *) IdtrForBSP.Base, IdtrForBSP.Limit + 1);
1573
1574 mExchangeInfo->GdtrProfile.Base = (UINTN) GdtForAP;
1575 mExchangeInfo->GdtrProfile.Limit = GdtrForBSP.Limit;
1576 mExchangeInfo->IdtrProfile.Base = (UINTN) IdtForAP;
1577 mExchangeInfo->IdtrProfile.Limit = IdtrForBSP.Limit;
1578
1579 mExchangeInfo->BufferStart = (UINT32) mStartupVector;
1580 mExchangeInfo->Cr3 = (UINT32) (AsmReadCr3 ());
1581 }
1582
1583 /**
1584 Prepares memory region for processor configuration.
1585
1586 This function prepares memory region for processor configuration.
1587
1588 **/
1589 VOID
1590 PrepareMemoryForConfiguration (
1591 VOID
1592 )
1593 {
1594 UINTN Index;
1595
1596 //
1597 // Initialize Spin Locks for system
1598 //
1599 InitializeSpinLock (&mMPSystemData.APSerializeLock);
1600 for (Index = 0; Index < MAX_CPU_NUMBER; Index++) {
1601 InitializeSpinLock (&mMPSystemData.CpuData[Index].CpuDataLock);
1602 }
1603
1604 PrepareAPStartupVector ();
1605 }
1606
1607 /**
1608 Gets the processor number of BSP.
1609
1610 @return The processor number of BSP.
1611
1612 **/
1613 UINTN
1614 GetBspNumber (
1615 VOID
1616 )
1617 {
1618 UINTN ProcessorNumber;
1619 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;
1620 EFI_STATUS Status;
1621 UINTN BufferSize;
1622
1623 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);
1624
1625 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
1626 Status = mFrameworkMpService->GetProcessorContext (
1627 mFrameworkMpService,
1628 ProcessorNumber,
1629 &BufferSize,
1630 &ProcessorContextBuffer
1631 );
1632 ASSERT_EFI_ERROR (Status);
1633
1634 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {
1635 break;
1636 }
1637 }
1638 ASSERT (ProcessorNumber < mNumberOfProcessors);
1639
1640 return ProcessorNumber;
1641 }
1642
1643 /**
1644 Entrypoint of MP Services Protocol thunk driver.
1645
1646 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1647 @param[in] SystemTable A pointer to the EFI System Table.
1648
1649 @retval EFI_SUCCESS The entry point is executed successfully.
1650
1651 **/
1652 EFI_STATUS
1653 EFIAPI
1654 InitializeMpServicesProtocol (
1655 IN EFI_HANDLE ImageHandle,
1656 IN EFI_SYSTEM_TABLE *SystemTable
1657 )
1658 {
1659 EFI_STATUS Status;
1660
1661 PrepareMemoryForConfiguration ();
1662
1663 //
1664 // Locates Framework version MP Services Protocol
1665 //
1666 Status = gBS->LocateProtocol (
1667 &gFrameworkEfiMpServiceProtocolGuid,
1668 NULL,
1669 (VOID **) &mFrameworkMpService
1670 );
1671 ASSERT_EFI_ERROR (Status);
1672
1673 Status = mFrameworkMpService->GetGeneralMPInfo (
1674 mFrameworkMpService,
1675 &mNumberOfProcessors,
1676 NULL,
1677 NULL,
1678 NULL,
1679 NULL
1680 );
1681 ASSERT_EFI_ERROR (Status);
1682 ASSERT (mNumberOfProcessors < MAX_CPU_NUMBER);
1683
1684 //
1685 // Create timer event to check AP state for non-blocking execution.
1686 //
1687 Status = gBS->CreateEvent (
1688 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1689 TPL_CALLBACK,
1690 CheckAPsStatus,
1691 NULL,
1692 &mMPSystemData.CheckAPsEvent
1693 );
1694 ASSERT_EFI_ERROR (Status);
1695
1696 //
1697 // Now install the MP services protocol.
1698 //
1699 Status = gBS->InstallProtocolInterface (
1700 &mHandle,
1701 &gEfiMpServiceProtocolGuid,
1702 EFI_NATIVE_INTERFACE,
1703 &mMpService
1704 );
1705 ASSERT_EFI_ERROR (Status);
1706
1707 //
1708 // Launch the timer event to check AP state.
1709 //
1710 Status = gBS->SetTimer (
1711 mMPSystemData.CheckAPsEvent,
1712 TimerPeriodic,
1713 100000
1714 );
1715 ASSERT_EFI_ERROR (Status);
1716
1717 return EFI_SUCCESS;
1718 }