]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/MpServicesOnFrameworkMpServicesThunk/MpServicesOnFrameworkMpServicesThunk.c
1. Consume Debug Agent Library to support debugging AP code based on PI MP protocol.
[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 UINTN ApicBase;
536 UINT32 CurrentTimerValue;
537 UINT32 CurrentTimerRegister;
538 UINT32 CurrentTimerDivide;
539 UINT64 CurrentTscValue;
540 BOOLEAN OldInterruptState;
541
542 //
543 // Check whether caller processor is BSP
544 //
545 BspNumber = GetBspNumber ();
546 WhoAmI (This, &CallerNumber);
547 if (CallerNumber != BspNumber) {
548 return EFI_DEVICE_ERROR;
549 }
550
551 //
552 // Check whether processor with the handle specified by ProcessorNumber exists
553 //
554 if (ProcessorNumber >= mNumberOfProcessors) {
555 return EFI_NOT_FOUND;
556 }
557
558 //
559 // Check whether specified processor is BSP
560 //
561 if (ProcessorNumber == BspNumber) {
562 return EFI_INVALID_PARAMETER;
563 }
564
565 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
566
567 //
568 // Check whether specified AP is disabled
569 //
570 if (CpuData->State == CpuStateDisabled) {
571 return EFI_INVALID_PARAMETER;
572 }
573
574 //
575 // Check whether specified AP is busy
576 //
577 if (CpuData->State != CpuStateIdle) {
578 return EFI_NOT_READY;
579 }
580
581 //
582 // Save and disable interrupt.
583 //
584 OldInterruptState = SaveAndDisableInterrupts ();
585
586 //
587 // Record the current local APIC timer setting of BSP
588 //
589 ApicBase = (UINTN)AsmMsrBitFieldRead64 (MSR_IA32_APIC_BASE, 12, 35) << 12;
590 CurrentTimerValue = MmioRead32 (ApicBase + APIC_REGISTER_TIMER_COUNT);
591 CurrentTimerRegister = MmioRead32 (ApicBase + APIC_REGISTER_LVT_TIMER);
592 CurrentTimerDivide = MmioRead32 (ApicBase + APIC_REGISTER_TIMER_DIVIDE);
593 //
594 // Set mask bit (BIT 16) of LVT Timer Register to disable its interrupt
595 //
596 MmioBitFieldWrite32 (ApicBase + APIC_REGISTER_LVT_TIMER, 16, 16, 1);
597
598 //
599 // Record the current TSC value
600 //
601 CurrentTscValue = AsmReadTsc ();
602
603 Status = mFrameworkMpService->SwitchBSP (
604 mFrameworkMpService,
605 ProcessorNumber,
606 EnableOldBSP
607 );
608 ASSERT_EFI_ERROR (Status);
609
610 //
611 // Restore TSC value
612 //
613 AsmWriteMsr64 (MSR_IA32_TIME_STAMP_COUNTER, CurrentTscValue);
614
615 //
616 // Restore local APIC timer setting to new BSP
617 //
618 MmioWrite32 (ApicBase + APIC_REGISTER_TIMER_DIVIDE, CurrentTimerDivide);
619 MmioWrite32 (ApicBase + APIC_REGISTER_TIMER_INIT_COUNT, CurrentTimerValue);
620 MmioWrite32 (ApicBase + APIC_REGISTER_LVT_TIMER, CurrentTimerRegister);
621
622 //
623 // Restore interrupt state.
624 //
625 SetInterruptState (OldInterruptState);
626
627 ChangeCpuState (BspNumber, EnableOldBSP);
628
629 return EFI_SUCCESS;
630 }
631
632 /**
633 Implementation of EnableDisableAP() service of MP Services Protocol.
634
635 This service lets the caller enable or disable an AP.
636 This service may only be called from the BSP.
637
638 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
639 @param ProcessorNumber The handle number of processor.
640 @param EnableAP Indicates whether the newstate of the AP is enabled or disabled.
641 @param HealthFlag Indicates new health state of the AP..
642
643 @retval EFI_SUCCESS AP successfully enabled or disabled.
644 @retval EFI_DEVICE_ERROR Caller processor is AP.
645 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber does not exist.
646 @retval EFI_INVALID_PARAMETERS ProcessorNumber specifies the BSP.
647
648 **/
649 EFI_STATUS
650 EFIAPI
651 EnableDisableAP (
652 IN EFI_MP_SERVICES_PROTOCOL *This,
653 IN UINTN ProcessorNumber,
654 IN BOOLEAN EnableAP,
655 IN UINT32 *HealthFlag OPTIONAL
656 )
657 {
658 EFI_STATUS Status;
659 UINTN CallerNumber;
660 EFI_MP_HEALTH HealthState;
661 EFI_MP_HEALTH *HealthStatePointer;
662 UINTN BspNumber;
663
664 //
665 // Check whether caller processor is BSP
666 //
667 BspNumber = GetBspNumber ();
668 WhoAmI (This, &CallerNumber);
669 if (CallerNumber != BspNumber) {
670 return EFI_DEVICE_ERROR;
671 }
672
673 //
674 // Check whether processor with the handle specified by ProcessorNumber exists
675 //
676 if (ProcessorNumber >= mNumberOfProcessors) {
677 return EFI_NOT_FOUND;
678 }
679
680 //
681 // Check whether specified processor is BSP
682 //
683 if (ProcessorNumber == BspNumber) {
684 return EFI_INVALID_PARAMETER;
685 }
686
687 if (HealthFlag == NULL) {
688 HealthStatePointer = NULL;
689 } else {
690 if ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) == 0) {
691 HealthState.Flags.Uint32 = 1;
692 } else {
693 HealthState.Flags.Uint32 = 0;
694 }
695 HealthState.TestStatus = 0;
696
697 HealthStatePointer = &HealthState;
698 }
699
700 Status = mFrameworkMpService->EnableDisableAP (
701 mFrameworkMpService,
702 ProcessorNumber,
703 EnableAP,
704 HealthStatePointer
705 );
706 ASSERT_EFI_ERROR (Status);
707
708 ChangeCpuState (ProcessorNumber, EnableAP);
709
710 return EFI_SUCCESS;
711 }
712
713 /**
714 Implementation of WhoAmI() service of MP Services Protocol.
715
716 This service lets the caller processor get its handle number.
717 This service may be called from the BSP and APs.
718
719 @param This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.
720 @param ProcessorNumber Pointer to the handle number of AP.
721
722 @retval EFI_SUCCESS Processor number successfully returned.
723 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL
724
725 **/
726 EFI_STATUS
727 EFIAPI
728 WhoAmI (
729 IN EFI_MP_SERVICES_PROTOCOL *This,
730 OUT UINTN *ProcessorNumber
731 )
732 {
733 EFI_STATUS Status;
734
735 if (ProcessorNumber == NULL) {
736 return EFI_INVALID_PARAMETER;
737 }
738
739 Status = mFrameworkMpService->WhoAmI (
740 mFrameworkMpService,
741 ProcessorNumber
742 );
743 ASSERT_EFI_ERROR (Status);
744
745 return EFI_SUCCESS;
746 }
747
748 /**
749 Checks APs' status periodically.
750
751 This function is triggerred by timer perodically to check the
752 state of APs for StartupAllAPs() and StartupThisAP() executed
753 in non-blocking mode.
754
755 @param Event Event triggered.
756 @param Context Parameter passed with the event.
757
758 **/
759 VOID
760 EFIAPI
761 CheckAPsStatus (
762 IN EFI_EVENT Event,
763 IN VOID *Context
764 )
765 {
766 UINTN ProcessorNumber;
767 CPU_DATA_BLOCK *CpuData;
768 EFI_STATUS Status;
769
770 //
771 // If CheckAPsStatus() is stopped, then return immediately.
772 //
773 if (mStopCheckAPsStatus) {
774 return;
775 }
776
777 //
778 // First, check whether pending StartupAllAPs() exists.
779 //
780 if (mMPSystemData.WaitEvent != NULL) {
781
782 Status = CheckAllAPs ();
783 //
784 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it..
785 //
786 if (Status != EFI_NOT_READY) {
787 Status = gBS->SignalEvent (mMPSystemData.WaitEvent);
788 mMPSystemData.WaitEvent = NULL;
789 }
790 }
791
792 //
793 // Second, check whether pending StartupThisAPs() callings exist.
794 //
795 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
796
797 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
798
799 if (CpuData->WaitEvent == NULL) {
800 continue;
801 }
802
803 Status = CheckThisAP (ProcessorNumber);
804
805 if (Status != EFI_NOT_READY) {
806 gBS->SignalEvent (CpuData->WaitEvent);
807 CpuData->WaitEvent = NULL;
808 }
809 }
810 return ;
811 }
812
813 /**
814 Checks status of all APs.
815
816 This function checks whether all APs have finished task assigned by StartupAllAPs(),
817 and whether timeout expires.
818
819 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
820 @retval EFI_TIMEOUT The timeout expires.
821 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
822
823 **/
824 EFI_STATUS
825 CheckAllAPs (
826 VOID
827 )
828 {
829 UINTN ProcessorNumber;
830 UINTN NextProcessorNumber;
831 UINTN ListIndex;
832 EFI_STATUS Status;
833 CPU_STATE CpuState;
834 CPU_DATA_BLOCK *CpuData;
835
836 NextProcessorNumber = 0;
837
838 //
839 // Go through all APs that are responsible for the StartupAllAPs().
840 //
841 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
842 if (!mMPSystemData.CpuList[ProcessorNumber]) {
843 continue;
844 }
845
846 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
847
848 //
849 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
850 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
851 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
852 //
853 AcquireSpinLock (&CpuData->CpuDataLock);
854 CpuState = CpuData->State;
855 ReleaseSpinLock (&CpuData->CpuDataLock);
856
857 if (CpuState == CpuStateFinished) {
858 mMPSystemData.FinishCount++;
859 mMPSystemData.CpuList[ProcessorNumber] = FALSE;
860
861 AcquireSpinLock (&CpuData->CpuDataLock);
862 CpuData->State = CpuStateIdle;
863 ReleaseSpinLock (&CpuData->CpuDataLock);
864
865 //
866 // If in Single Thread mode, then search for the next waiting AP for execution.
867 //
868 if (mMPSystemData.SingleThread) {
869 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
870
871 if (!EFI_ERROR (Status)) {
872 WakeUpAp (
873 NextProcessorNumber,
874 mMPSystemData.Procedure,
875 mMPSystemData.ProcArguments
876 );
877 }
878 }
879 }
880 }
881
882 //
883 // If all APs finish, return EFI_SUCCESS.
884 //
885 if (mMPSystemData.FinishCount == mMPSystemData.StartCount) {
886 return EFI_SUCCESS;
887 }
888
889 //
890 // If timeout expires, report timeout.
891 //
892 if (CheckTimeout (&mMPSystemData.CurrentTime, &mMPSystemData.TotalTime, mMPSystemData.ExpectedTime)) {
893 //
894 // If FailedCpuList is not NULL, record all failed APs in it.
895 //
896 if (mMPSystemData.FailedCpuList != NULL) {
897 *mMPSystemData.FailedCpuList = AllocatePool ((mMPSystemData.StartCount - mMPSystemData.FinishCount + 1) * sizeof(UINTN));
898 ASSERT (*mMPSystemData.FailedCpuList != NULL);
899 }
900 ListIndex = 0;
901
902 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
903 //
904 // Check whether this processor is responsible for StartupAllAPs().
905 //
906 if (mMPSystemData.CpuList[ProcessorNumber]) {
907 //
908 // Reset failed APs to idle state
909 //
910 ResetProcessorToIdleState (ProcessorNumber);
911 mMPSystemData.CpuList[ProcessorNumber] = FALSE;
912 if (mMPSystemData.FailedCpuList != NULL) {
913 (*mMPSystemData.FailedCpuList)[ListIndex++] = ProcessorNumber;
914 }
915 }
916 }
917 if (mMPSystemData.FailedCpuList != NULL) {
918 (*mMPSystemData.FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
919 }
920 return EFI_TIMEOUT;
921 }
922 return EFI_NOT_READY;
923 }
924
925 /**
926 Checks status of specified AP.
927
928 This function checks whether specified AP has finished task assigned by StartupThisAP(),
929 and whether timeout expires.
930
931 @param ProcessorNumber The handle number of processor.
932
933 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
934 @retval EFI_TIMEOUT The timeout expires.
935 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
936
937 **/
938 EFI_STATUS
939 CheckThisAP (
940 UINTN ProcessorNumber
941 )
942 {
943 CPU_DATA_BLOCK *CpuData;
944 CPU_STATE CpuState;
945
946 ASSERT (ProcessorNumber < mNumberOfProcessors);
947 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
948
949 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
950
951 //
952 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
953 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
954 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
955 //
956 AcquireSpinLock (&CpuData->CpuDataLock);
957 CpuState = CpuData->State;
958 ReleaseSpinLock (&CpuData->CpuDataLock);
959
960 //
961 // If the APs finishes for StartupThisAP(), return EFI_SUCCESS.
962 //
963 if (CpuState == CpuStateFinished) {
964
965 AcquireSpinLock (&CpuData->CpuDataLock);
966 CpuData->State = CpuStateIdle;
967 ReleaseSpinLock (&CpuData->CpuDataLock);
968
969 if (CpuData->Finished != NULL) {
970 *(CpuData->Finished) = TRUE;
971 }
972 return EFI_SUCCESS;
973 } else {
974 //
975 // If timeout expires for StartupThisAP(), report timeout.
976 //
977 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
978
979 if (CpuData->Finished != NULL) {
980 *(CpuData->Finished) = FALSE;
981 }
982 //
983 // Reset failed AP to idle state
984 //
985 ResetProcessorToIdleState (ProcessorNumber);
986
987 return EFI_TIMEOUT;
988 }
989 }
990 return EFI_NOT_READY;
991 }
992
993 /**
994 Calculate timeout value and return the current performance counter value.
995
996 Calculate the number of performance counter ticks required for a timeout.
997 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
998 as infinity.
999
1000 @param TimeoutInMicroseconds Timeout value in microseconds.
1001 @param CurrentTime Returns the current value of the performance counter.
1002
1003 @return Expected timestamp counter for timeout.
1004 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1005 as infinity.
1006
1007 **/
1008 UINT64
1009 CalculateTimeout (
1010 IN UINTN TimeoutInMicroseconds,
1011 OUT UINT64 *CurrentTime
1012 )
1013 {
1014 //
1015 // Read the current value of the performance counter
1016 //
1017 *CurrentTime = GetPerformanceCounter ();
1018
1019 //
1020 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1021 // as infinity.
1022 //
1023 if (TimeoutInMicroseconds == 0) {
1024 return 0;
1025 }
1026
1027 //
1028 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1029 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
1030 // it by 1,000,000, to get the number of ticks for the timeout value.
1031 //
1032 return DivU64x32 (
1033 MultU64x64 (
1034 GetPerformanceCounterProperties (NULL, NULL),
1035 TimeoutInMicroseconds
1036 ),
1037 1000000
1038 );
1039 }
1040
1041 /**
1042 Checks whether timeout expires.
1043
1044 Check whether the number of ellapsed performance counter ticks required for a timeout condition
1045 has been reached. If Timeout is zero, which means infinity, return value is always FALSE.
1046
1047 @param PreviousTime On input, the value of the performance counter when it was last read.
1048 On output, the current value of the performance counter
1049 @param TotalTime The total amount of ellapsed time in performance counter ticks.
1050 @param Timeout The number of performance counter ticks required to reach a timeout condition.
1051
1052 @retval TRUE A timeout condition has been reached.
1053 @retval FALSE A timeout condition has not been reached.
1054
1055 **/
1056 BOOLEAN
1057 CheckTimeout (
1058 IN OUT UINT64 *PreviousTime,
1059 IN UINT64 *TotalTime,
1060 IN UINT64 Timeout
1061 )
1062 {
1063 UINT64 Start;
1064 UINT64 End;
1065 UINT64 CurrentTime;
1066 INT64 Delta;
1067 INT64 Cycle;
1068
1069 if (Timeout == 0) {
1070 return FALSE;
1071 }
1072 GetPerformanceCounterProperties (&Start, &End);
1073 Cycle = End - Start;
1074 if (Cycle < 0) {
1075 Cycle = -Cycle;
1076 }
1077 Cycle++;
1078 CurrentTime = GetPerformanceCounter();
1079 Delta = (INT64) (CurrentTime - *PreviousTime);
1080 if (Start > End) {
1081 Delta = -Delta;
1082 }
1083 if (Delta < 0) {
1084 Delta += Cycle;
1085 }
1086 *TotalTime += Delta;
1087 *PreviousTime = CurrentTime;
1088 if (*TotalTime > Timeout) {
1089 return TRUE;
1090 }
1091 return FALSE;
1092 }
1093
1094 /**
1095 Searches for the next waiting AP.
1096
1097 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1098
1099 @param NextProcessorNumber Pointer to the processor number of the next waiting AP.
1100
1101 @retval EFI_SUCCESS The next waiting AP has been found.
1102 @retval EFI_NOT_FOUND No waiting AP exists.
1103
1104 **/
1105 EFI_STATUS
1106 GetNextWaitingProcessorNumber (
1107 OUT UINTN *NextProcessorNumber
1108 )
1109 {
1110 UINTN ProcessorNumber;
1111
1112 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
1113
1114 if (mMPSystemData.CpuList[ProcessorNumber]) {
1115 *NextProcessorNumber = ProcessorNumber;
1116 return EFI_SUCCESS;
1117 }
1118 }
1119
1120 return EFI_NOT_FOUND;
1121 }
1122
1123 /**
1124 Programs Local APIC registers for virtual wire mode.
1125
1126 This function programs Local APIC registers for virtual wire mode.
1127
1128 @param Bsp Indicates whether the programmed processor is going to be BSP
1129
1130 **/
1131 VOID
1132 ProgramVirtualWireMode (
1133 BOOLEAN Bsp
1134 )
1135 {
1136 UINTN ApicBase;
1137 UINT32 Value;
1138
1139 ApicBase = (UINTN)AsmMsrBitFieldRead64 (MSR_IA32_APIC_BASE, 12, 35) << 12;
1140
1141 //
1142 // Program the Spurious Vector entry
1143 // Set bit 8 (APIC Software Enable/Disable) to enable local APIC,
1144 // and set Spurious Vector as 0x0F.
1145 //
1146 MmioBitFieldWrite32 (ApicBase + APIC_REGISTER_SPURIOUS_VECTOR_OFFSET, 0, 9, 0x10F);
1147
1148 //
1149 // Program the LINT0 vector entry as ExtInt
1150 // Set bits 8..10 to 7 as ExtInt Delivery Mode,
1151 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,
1152 // Trigger Mode, and Mask
1153 //
1154 if (!Bsp) {
1155 DisableInterrupts ();
1156 }
1157 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET);
1158 Value = BitFieldWrite32 (Value, 8, 10, 7);
1159 Value = BitFieldWrite32 (Value, 12, 16, 0);
1160 if (!Bsp) {
1161 //
1162 // For APs, LINT0 is masked
1163 //
1164 Value = BitFieldWrite32 (Value, 16, 16, 1);
1165 }
1166 MmioWrite32 (ApicBase + APIC_REGISTER_LINT0_VECTOR_OFFSET, Value);
1167
1168 //
1169 // Program the LINT1 vector entry as NMI
1170 // Set bits 8..10 to 4 as NMI Delivery Mode,
1171 // and clear bits for Delivery Status, Interrupt Input Pin Polarity, Remote IRR,
1172 // Trigger Mode.
1173 // For BSP clear Mask bit, and for AP set mask bit.
1174 //
1175 Value = MmioRead32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET);
1176 Value = BitFieldWrite32 (Value, 8, 10, 4);
1177 Value = BitFieldWrite32 (Value, 12, 16, 0);
1178 if (!Bsp) {
1179 //
1180 // For APs, LINT1 is masked
1181 //
1182 Value = BitFieldWrite32 (Value, 16, 16, 1);
1183 }
1184 MmioWrite32 (ApicBase + APIC_REGISTER_LINT1_VECTOR_OFFSET, Value);
1185 }
1186
1187
1188 /**
1189 Wrapper function for all procedures assigned to AP.
1190
1191 Wrapper function for all procedures assigned to AP via MP service protocol.
1192 It controls states of AP and invokes assigned precedure.
1193
1194 **/
1195 VOID
1196 ApProcWrapper (
1197 VOID
1198 )
1199 {
1200 EFI_AP_PROCEDURE Procedure;
1201 VOID *Parameter;
1202 UINTN ProcessorNumber;
1203 CPU_DATA_BLOCK *CpuData;
1204
1205 //
1206 // Program virtual wire mode for AP, since it will be lost after AP wake up
1207 //
1208 ProgramVirtualWireMode (FALSE);
1209
1210 //
1211 // Initialize Debug Agent to support source level debug on AP code.
1212 //
1213 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);
1214
1215 WhoAmI (&mMpService, &ProcessorNumber);
1216 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1217
1218 AcquireSpinLock (&CpuData->CpuDataLock);
1219 CpuData->State = CpuStateBusy;
1220 ReleaseSpinLock (&CpuData->CpuDataLock);
1221
1222 //
1223 // Now let us check it out.
1224 //
1225 AcquireSpinLock (&CpuData->CpuDataLock);
1226 Procedure = CpuData->Procedure;
1227 Parameter = CpuData->Parameter;
1228 ReleaseSpinLock (&CpuData->CpuDataLock);
1229
1230 if (Procedure != NULL) {
1231
1232 Procedure (Parameter);
1233
1234 //
1235 // if BSP is switched to AP, it continue execute from here, but it carries register state
1236 // of the old AP, so need to reload CpuData (might be stored in a register after compiler
1237 // optimization) to make sure it points to the right data
1238 //
1239 WhoAmI (&mMpService, &ProcessorNumber);
1240 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1241
1242 AcquireSpinLock (&CpuData->CpuDataLock);
1243 CpuData->Procedure = NULL;
1244 ReleaseSpinLock (&CpuData->CpuDataLock);
1245 }
1246
1247 AcquireSpinLock (&CpuData->CpuDataLock);
1248 CpuData->State = CpuStateFinished;
1249 ReleaseSpinLock (&CpuData->CpuDataLock);
1250 }
1251
1252 /**
1253 Sends INIT-SIPI-SIPI to AP.
1254
1255 This function sends INIT-SIPI-SIPI to AP, and assign procedure specified by ApFunction.
1256
1257 @param Broadcast If TRUE, broadcase IPI to all APs; otherwise, send to specified AP.
1258 @param ApicID The Local APIC ID of the specified AP. If Broadcast is TRUE, it is ignored.
1259 @param ApFunction The procedure for AP to work on.
1260
1261 **/
1262 VOID
1263 SendInitSipiSipi (
1264 IN BOOLEAN Broadcast,
1265 IN UINT32 ApicID,
1266 IN VOID *ApFunction
1267 )
1268 {
1269 UINTN ApicBase;
1270 UINT32 ICRLow;
1271 UINT32 ICRHigh;
1272
1273 UINT32 VectorNumber;
1274 UINT32 DeliveryMode;
1275
1276 mExchangeInfo->ApFunction = ApFunction;
1277 mExchangeInfo->StackStart = mStackStartAddress;
1278
1279 if (Broadcast) {
1280 ICRHigh = 0;
1281 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1282 } else {
1283 ICRHigh = ApicID << 24;
1284 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1285 }
1286
1287 VectorNumber = 0;
1288 DeliveryMode = DELIVERY_MODE_INIT;
1289 ICRLow |= VectorNumber | (DeliveryMode << 8);
1290
1291 ApicBase = (UINTN)AsmMsrBitFieldRead64 (MSR_IA32_APIC_BASE, 12, 35) << 12;;
1292
1293 //
1294 // Write Interrupt Command Registers to send INIT IPI.
1295 //
1296 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_HIGH_OFFSET, ICRHigh);
1297 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1298
1299 MicroSecondDelay (10);
1300
1301 VectorNumber = (UINT32) RShiftU64 (mStartupVector, 12);
1302 DeliveryMode = DELIVERY_MODE_SIPI;
1303 if (Broadcast) {
1304 ICRLow = BROADCAST_MODE_ALL_EXCLUDING_SELF_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1305 } else {
1306 ICRLow = SPECIFY_CPU_MODE_BIT | TRIGGER_MODE_LEVEL_BIT | ASSERT_BIT;
1307 }
1308
1309 ICRLow |= VectorNumber | (DeliveryMode << 8);
1310
1311 //
1312 // Write Interrupt Command Register to send first SIPI IPI.
1313 //
1314 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1315
1316 MicroSecondDelay (200);
1317
1318 //
1319 // Write Interrupt Command Register to send second SIPI IPI.
1320 //
1321 MmioWrite32 (ApicBase + APIC_REGISTER_ICR_LOW_OFFSET, ICRLow);
1322 }
1323
1324 /**
1325 Function to wake up a specified AP and assign procedure to it.
1326
1327 @param ProcessorNumber Handle number of the specified processor.
1328 @param Procedure Procedure to assign.
1329 @param ProcArguments Argument for Procedure.
1330
1331 **/
1332 VOID
1333 WakeUpAp (
1334 IN UINTN ProcessorNumber,
1335 IN EFI_AP_PROCEDURE Procedure,
1336 IN VOID *ProcArguments
1337 )
1338 {
1339 EFI_STATUS Status;
1340 CPU_DATA_BLOCK *CpuData;
1341 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;
1342
1343 ASSERT (ProcessorNumber < mNumberOfProcessors);
1344 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
1345
1346 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1347
1348 AcquireSpinLock (&CpuData->CpuDataLock);
1349 CpuData->Parameter = ProcArguments;
1350 CpuData->Procedure = Procedure;
1351 ReleaseSpinLock (&CpuData->CpuDataLock);
1352
1353 Status = GetProcessorInfo (
1354 &mMpService,
1355 ProcessorNumber,
1356 &ProcessorInfoBuffer
1357 );
1358 ASSERT_EFI_ERROR (Status);
1359
1360 SendInitSipiSipi (
1361 FALSE,
1362 (UINT32) ProcessorInfoBuffer.ProcessorId,
1363 (VOID *) (UINTN) ApProcWrapper
1364 );
1365 }
1366
1367 /**
1368 Terminate AP's task and set it to idle state.
1369
1370 This function terminates AP's task due to timeout by sending INIT-SIPI,
1371 and sends it to idle state.
1372
1373 @param ProcessorNumber Handle number of the specified processor.
1374
1375 **/
1376 VOID
1377 ResetProcessorToIdleState (
1378 UINTN ProcessorNumber
1379 )
1380 {
1381 EFI_STATUS Status;
1382 CPU_DATA_BLOCK *CpuData;
1383 EFI_PROCESSOR_INFORMATION ProcessorInfoBuffer;
1384
1385 Status = GetProcessorInfo (
1386 &mMpService,
1387 ProcessorNumber,
1388 &ProcessorInfoBuffer
1389 );
1390 ASSERT_EFI_ERROR (Status);
1391
1392 SendInitSipiSipi (
1393 FALSE,
1394 (UINT32) ProcessorInfoBuffer.ProcessorId,
1395 NULL
1396 );
1397
1398 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1399
1400 AcquireSpinLock (&CpuData->CpuDataLock);
1401 CpuData->State = CpuStateIdle;
1402 ReleaseSpinLock (&CpuData->CpuDataLock);
1403 }
1404
1405 /**
1406 Worker function of EnableDisableAP ()
1407
1408 Worker function of EnableDisableAP (). Changes state of specified processor.
1409
1410 @param ProcessorNumber Processor number of specified AP.
1411 @param NewState Desired state of the specified AP.
1412
1413 @retval EFI_SUCCESS AP's state successfully changed.
1414
1415 **/
1416 EFI_STATUS
1417 ChangeCpuState (
1418 IN UINTN ProcessorNumber,
1419 IN BOOLEAN NewState
1420 )
1421 {
1422 CPU_DATA_BLOCK *CpuData;
1423
1424 ASSERT (ProcessorNumber < mNumberOfProcessors);
1425 ASSERT (ProcessorNumber < MAX_CPU_NUMBER);
1426
1427 CpuData = &mMPSystemData.CpuData[ProcessorNumber];
1428
1429 if (!NewState) {
1430 AcquireSpinLock (&CpuData->CpuDataLock);
1431 CpuData->State = CpuStateDisabled;
1432 ReleaseSpinLock (&CpuData->CpuDataLock);
1433 } else {
1434 AcquireSpinLock (&CpuData->CpuDataLock);
1435 CpuData->State = CpuStateIdle;
1436 ReleaseSpinLock (&CpuData->CpuDataLock);
1437 }
1438
1439 return EFI_SUCCESS;
1440 }
1441
1442 /**
1443 Test memory region of EfiGcdMemoryTypeReserved.
1444
1445 @param Length The length of memory region to test.
1446
1447 @retval EFI_SUCCESS The memory region passes test.
1448 @retval EFI_NOT_FOUND The memory region is not reserved memory.
1449 @retval EFI_DEVICE_ERROR The memory fails on test.
1450
1451 **/
1452 EFI_STATUS
1453 TestReservedMemory (
1454 UINTN Length
1455 )
1456 {
1457 EFI_STATUS Status;
1458 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
1459 EFI_PHYSICAL_ADDRESS Address;
1460 UINTN LengthCovered;
1461 UINTN RemainingLength;
1462
1463 //
1464 // Walk through the memory descriptors covering the memory range.
1465 //
1466 Address = mStartupVector;
1467 RemainingLength = Length;
1468 while (Address < mStartupVector + Length) {
1469 Status = gDS->GetMemorySpaceDescriptor(
1470 Address,
1471 &Descriptor
1472 );
1473 if (EFI_ERROR (Status)) {
1474 return EFI_NOT_FOUND;
1475 }
1476
1477 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {
1478 return EFI_NOT_FOUND;
1479 }
1480 //
1481 // Calculated the length of the intersected range.
1482 //
1483 LengthCovered = (UINTN) (Descriptor.BaseAddress + Descriptor.Length - Address);
1484 if (LengthCovered > RemainingLength) {
1485 LengthCovered = RemainingLength;
1486 }
1487
1488 Status = mGenMemoryTest->CompatibleRangeTest (
1489 mGenMemoryTest,
1490 Address,
1491 LengthCovered
1492 );
1493 if (EFI_ERROR (Status)) {
1494 return EFI_DEVICE_ERROR;
1495 }
1496
1497 Address += LengthCovered;
1498 RemainingLength -= LengthCovered;
1499 }
1500
1501 return EFI_SUCCESS;
1502 }
1503
1504 /**
1505 Allocates startup vector for APs.
1506
1507 This function allocates Startup vector for APs.
1508
1509 @param Size The size of startup vector.
1510
1511 **/
1512 VOID
1513 AllocateStartupVector (
1514 UINTN Size
1515 )
1516 {
1517 EFI_STATUS Status;
1518
1519 Status = gBS->LocateProtocol (
1520 &gEfiGenericMemTestProtocolGuid,
1521 NULL,
1522 (VOID **) &mGenMemoryTest
1523 );
1524 if (EFI_ERROR (Status)) {
1525 mGenMemoryTest = NULL;
1526 }
1527
1528 for (mStartupVector = 0x7F000; mStartupVector >= 0x2000; mStartupVector -= EFI_PAGE_SIZE) {
1529 if (mGenMemoryTest != NULL) {
1530 //
1531 // Test memory if it is EfiGcdMemoryTypeReserved.
1532 //
1533 Status = TestReservedMemory (EFI_SIZE_TO_PAGES (Size) * EFI_PAGE_SIZE);
1534 if (Status == EFI_DEVICE_ERROR) {
1535 continue;
1536 }
1537 }
1538
1539 Status = gBS->AllocatePages (
1540 AllocateAddress,
1541 EfiBootServicesCode,
1542 EFI_SIZE_TO_PAGES (Size),
1543 &mStartupVector
1544 );
1545
1546 if (!EFI_ERROR (Status)) {
1547 break;
1548 }
1549 }
1550
1551 ASSERT_EFI_ERROR (Status);
1552 }
1553
1554 /**
1555 Prepares Startup Vector for APs.
1556
1557 This function prepares Startup Vector for APs.
1558
1559 **/
1560 VOID
1561 PrepareAPStartupVector (
1562 VOID
1563 )
1564 {
1565 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1566 IA32_DESCRIPTOR GdtrForBSP;
1567 IA32_DESCRIPTOR IdtrForBSP;
1568 EFI_PHYSICAL_ADDRESS GdtForAP;
1569 EFI_PHYSICAL_ADDRESS IdtForAP;
1570 EFI_STATUS Status;
1571
1572 //
1573 // Get the address map of startup code for AP,
1574 // including code size, and offset of long jump instructions to redirect.
1575 //
1576 AsmGetAddressMap (&AddressMap);
1577
1578 //
1579 // Allocate a 4K-aligned region under 1M for startup vector for AP.
1580 // The region contains AP startup code and exchange data between BSP and AP.
1581 //
1582 AllocateStartupVector (AddressMap.Size + sizeof (MP_CPU_EXCHANGE_INFO));
1583
1584 //
1585 // Copy AP startup code to startup vector, and then redirect the long jump
1586 // instructions for mode switching.
1587 //
1588 CopyMem ((VOID *) (UINTN) mStartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);
1589 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32) (mStartupVector + AddressMap.PModeEntryOffset);
1590 //
1591 // For IA32 mode, LongJumpOffset is filled with zero. If non-zero, then we are in X64 mode, so further redirect for long mode switch.
1592 //
1593 if (AddressMap.LongJumpOffset != 0) {
1594 *(UINT32 *) (UINTN) (mStartupVector + AddressMap.LongJumpOffset + 2) = (UINT32) (mStartupVector + AddressMap.LModeEntryOffset);
1595 }
1596
1597 //
1598 // Get the start address of exchange data between BSP and AP.
1599 //
1600 mExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (mStartupVector + AddressMap.Size);
1601
1602 ZeroMem ((VOID *) mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));
1603
1604 mStackStartAddress = AllocatePages (EFI_SIZE_TO_PAGES (MAX_CPU_NUMBER * AP_STACK_SIZE));
1605 mExchangeInfo->StackSize = AP_STACK_SIZE;
1606
1607 AsmReadGdtr (&GdtrForBSP);
1608 AsmReadIdtr (&IdtrForBSP);
1609
1610 //
1611 // Allocate memory under 4G to hold GDT for APs
1612 //
1613 GdtForAP = 0xffffffff;
1614 Status = gBS->AllocatePages (
1615 AllocateMaxAddress,
1616 EfiBootServicesData,
1617 EFI_SIZE_TO_PAGES ((GdtrForBSP.Limit + 1) + (IdtrForBSP.Limit + 1)),
1618 &GdtForAP
1619 );
1620 ASSERT_EFI_ERROR (Status);
1621
1622 IdtForAP = (UINTN) GdtForAP + GdtrForBSP.Limit + 1;
1623
1624 CopyMem ((VOID *) (UINTN) GdtForAP, (VOID *) GdtrForBSP.Base, GdtrForBSP.Limit + 1);
1625 CopyMem ((VOID *) (UINTN) IdtForAP, (VOID *) IdtrForBSP.Base, IdtrForBSP.Limit + 1);
1626
1627 mExchangeInfo->GdtrProfile.Base = (UINTN) GdtForAP;
1628 mExchangeInfo->GdtrProfile.Limit = GdtrForBSP.Limit;
1629 mExchangeInfo->IdtrProfile.Base = (UINTN) IdtForAP;
1630 mExchangeInfo->IdtrProfile.Limit = IdtrForBSP.Limit;
1631
1632 mExchangeInfo->BufferStart = (UINT32) mStartupVector;
1633 mExchangeInfo->Cr3 = (UINT32) (AsmReadCr3 ());
1634 }
1635
1636 /**
1637 Prepares memory region for processor configuration.
1638
1639 This function prepares memory region for processor configuration.
1640
1641 **/
1642 VOID
1643 PrepareMemoryForConfiguration (
1644 VOID
1645 )
1646 {
1647 UINTN Index;
1648
1649 //
1650 // Initialize Spin Locks for system
1651 //
1652 InitializeSpinLock (&mMPSystemData.APSerializeLock);
1653 for (Index = 0; Index < MAX_CPU_NUMBER; Index++) {
1654 InitializeSpinLock (&mMPSystemData.CpuData[Index].CpuDataLock);
1655 }
1656
1657 PrepareAPStartupVector ();
1658 }
1659
1660 /**
1661 Gets the processor number of BSP.
1662
1663 @return The processor number of BSP.
1664
1665 **/
1666 UINTN
1667 GetBspNumber (
1668 VOID
1669 )
1670 {
1671 UINTN ProcessorNumber;
1672 EFI_MP_PROC_CONTEXT ProcessorContextBuffer;
1673 EFI_STATUS Status;
1674 UINTN BufferSize;
1675
1676 BufferSize = sizeof (EFI_MP_PROC_CONTEXT);
1677
1678 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {
1679 Status = mFrameworkMpService->GetProcessorContext (
1680 mFrameworkMpService,
1681 ProcessorNumber,
1682 &BufferSize,
1683 &ProcessorContextBuffer
1684 );
1685 ASSERT_EFI_ERROR (Status);
1686
1687 if (ProcessorContextBuffer.Designation == EfiCpuBSP) {
1688 break;
1689 }
1690 }
1691 ASSERT (ProcessorNumber < mNumberOfProcessors);
1692
1693 return ProcessorNumber;
1694 }
1695
1696 /**
1697 Entrypoint of MP Services Protocol thunk driver.
1698
1699 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1700 @param[in] SystemTable A pointer to the EFI System Table.
1701
1702 @retval EFI_SUCCESS The entry point is executed successfully.
1703
1704 **/
1705 EFI_STATUS
1706 EFIAPI
1707 InitializeMpServicesProtocol (
1708 IN EFI_HANDLE ImageHandle,
1709 IN EFI_SYSTEM_TABLE *SystemTable
1710 )
1711 {
1712 EFI_STATUS Status;
1713
1714 PrepareMemoryForConfiguration ();
1715
1716 //
1717 // Locates Framework version MP Services Protocol
1718 //
1719 Status = gBS->LocateProtocol (
1720 &gFrameworkEfiMpServiceProtocolGuid,
1721 NULL,
1722 (VOID **) &mFrameworkMpService
1723 );
1724 ASSERT_EFI_ERROR (Status);
1725
1726 Status = mFrameworkMpService->GetGeneralMPInfo (
1727 mFrameworkMpService,
1728 &mNumberOfProcessors,
1729 NULL,
1730 NULL,
1731 NULL,
1732 NULL
1733 );
1734 ASSERT_EFI_ERROR (Status);
1735 ASSERT (mNumberOfProcessors < MAX_CPU_NUMBER);
1736
1737 //
1738 // Create timer event to check AP state for non-blocking execution.
1739 //
1740 Status = gBS->CreateEvent (
1741 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1742 TPL_CALLBACK,
1743 CheckAPsStatus,
1744 NULL,
1745 &mMPSystemData.CheckAPsEvent
1746 );
1747 ASSERT_EFI_ERROR (Status);
1748
1749 //
1750 // Now install the MP services protocol.
1751 //
1752 Status = gBS->InstallProtocolInterface (
1753 &mHandle,
1754 &gEfiMpServiceProtocolGuid,
1755 EFI_NATIVE_INTERFACE,
1756 &mMpService
1757 );
1758 ASSERT_EFI_ERROR (Status);
1759
1760 //
1761 // Launch the timer event to check AP state.
1762 //
1763 Status = gBS->SetTimer (
1764 mMPSystemData.CheckAPsEvent,
1765 TimerPeriodic,
1766 100000
1767 );
1768 ASSERT_EFI_ERROR (Status);
1769
1770 return EFI_SUCCESS;
1771 }