UefiCpuPkg/MpInitLib: Fix X64 XCODE5/NASM compatibility issues
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
1 /** @file
2 CPU MP Initialize Library common functions.
3
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "MpLib.h"
16
17 EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
18
19 /**
20 The function will check if BSP Execute Disable is enabled.
21
22 DxeIpl may have enabled Execute Disable for BSP, APs need to
23 get the status and sync up the settings.
24 If BSP's CR0.Paging is not set, BSP execute Disble feature is
25 not working actually.
26
27 @retval TRUE BSP Execute Disable is enabled.
28 @retval FALSE BSP Execute Disable is not enabled.
29 **/
30 BOOLEAN
31 IsBspExecuteDisableEnabled (
32 VOID
33 )
34 {
35 UINT32 Eax;
36 CPUID_EXTENDED_CPU_SIG_EDX Edx;
37 MSR_IA32_EFER_REGISTER EferMsr;
38 BOOLEAN Enabled;
39 IA32_CR0 Cr0;
40
41 Enabled = FALSE;
42 Cr0.UintN = AsmReadCr0 ();
43 if (Cr0.Bits.PG != 0) {
44 //
45 // If CR0 Paging bit is set
46 //
47 AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);
48 if (Eax >= CPUID_EXTENDED_CPU_SIG) {
49 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &Edx.Uint32);
50 //
51 // CPUID 0x80000001
52 // Bit 20: Execute Disable Bit available.
53 //
54 if (Edx.Bits.NX != 0) {
55 EferMsr.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);
56 //
57 // MSR 0xC0000080
58 // Bit 11: Execute Disable Bit enable.
59 //
60 if (EferMsr.Bits.NXE != 0) {
61 Enabled = TRUE;
62 }
63 }
64 }
65 }
66
67 return Enabled;
68 }
69
70 /**
71 Worker function for SwitchBSP().
72
73 Worker function for SwitchBSP(), assigned to the AP which is intended
74 to become BSP.
75
76 @param[in] Buffer Pointer to CPU MP Data
77 **/
78 VOID
79 EFIAPI
80 FutureBSPProc (
81 IN VOID *Buffer
82 )
83 {
84 CPU_MP_DATA *DataInHob;
85
86 DataInHob = (CPU_MP_DATA *) Buffer;
87 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);
88 }
89
90 /**
91 Get the Application Processors state.
92
93 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
94
95 @return The AP status
96 **/
97 CPU_STATE
98 GetApState (
99 IN CPU_AP_DATA *CpuData
100 )
101 {
102 return CpuData->State;
103 }
104
105 /**
106 Set the Application Processors state.
107
108 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
109 @param[in] State The AP status
110 **/
111 VOID
112 SetApState (
113 IN CPU_AP_DATA *CpuData,
114 IN CPU_STATE State
115 )
116 {
117 AcquireSpinLock (&CpuData->ApLock);
118 CpuData->State = State;
119 ReleaseSpinLock (&CpuData->ApLock);
120 }
121
122 /**
123 Save BSP's local APIC timer setting.
124
125 @param[in] CpuMpData Pointer to CPU MP Data
126 **/
127 VOID
128 SaveLocalApicTimerSetting (
129 IN CPU_MP_DATA *CpuMpData
130 )
131 {
132 //
133 // Record the current local APIC timer setting of BSP
134 //
135 GetApicTimerState (
136 &CpuMpData->DivideValue,
137 &CpuMpData->PeriodicMode,
138 &CpuMpData->Vector
139 );
140 CpuMpData->CurrentTimerCount = GetApicTimerCurrentCount ();
141 CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();
142 }
143
144 /**
145 Sync local APIC timer setting from BSP to AP.
146
147 @param[in] CpuMpData Pointer to CPU MP Data
148 **/
149 VOID
150 SyncLocalApicTimerSetting (
151 IN CPU_MP_DATA *CpuMpData
152 )
153 {
154 //
155 // Sync local APIC timer setting from BSP to AP
156 //
157 InitializeApicTimer (
158 CpuMpData->DivideValue,
159 CpuMpData->CurrentTimerCount,
160 CpuMpData->PeriodicMode,
161 CpuMpData->Vector
162 );
163 //
164 // Disable AP's local APIC timer interrupt
165 //
166 DisableApicTimerInterrupt ();
167 }
168
169 /**
170 Save the volatile registers required to be restored following INIT IPI.
171
172 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
173 **/
174 VOID
175 SaveVolatileRegisters (
176 OUT CPU_VOLATILE_REGISTERS *VolatileRegisters
177 )
178 {
179 CPUID_VERSION_INFO_EDX VersionInfoEdx;
180
181 VolatileRegisters->Cr0 = AsmReadCr0 ();
182 VolatileRegisters->Cr3 = AsmReadCr3 ();
183 VolatileRegisters->Cr4 = AsmReadCr4 ();
184
185 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
186 if (VersionInfoEdx.Bits.DE != 0) {
187 //
188 // If processor supports Debugging Extensions feature
189 // by CPUID.[EAX=01H]:EDX.BIT2
190 //
191 VolatileRegisters->Dr0 = AsmReadDr0 ();
192 VolatileRegisters->Dr1 = AsmReadDr1 ();
193 VolatileRegisters->Dr2 = AsmReadDr2 ();
194 VolatileRegisters->Dr3 = AsmReadDr3 ();
195 VolatileRegisters->Dr6 = AsmReadDr6 ();
196 VolatileRegisters->Dr7 = AsmReadDr7 ();
197 }
198 }
199
200 /**
201 Restore the volatile registers following INIT IPI.
202
203 @param[in] VolatileRegisters Pointer to volatile resisters
204 @param[in] IsRestoreDr TRUE: Restore DRx if supported
205 FALSE: Do not restore DRx
206 **/
207 VOID
208 RestoreVolatileRegisters (
209 IN CPU_VOLATILE_REGISTERS *VolatileRegisters,
210 IN BOOLEAN IsRestoreDr
211 )
212 {
213 CPUID_VERSION_INFO_EDX VersionInfoEdx;
214
215 AsmWriteCr0 (VolatileRegisters->Cr0);
216 AsmWriteCr3 (VolatileRegisters->Cr3);
217 AsmWriteCr4 (VolatileRegisters->Cr4);
218
219 if (IsRestoreDr) {
220 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
221 if (VersionInfoEdx.Bits.DE != 0) {
222 //
223 // If processor supports Debugging Extensions feature
224 // by CPUID.[EAX=01H]:EDX.BIT2
225 //
226 AsmWriteDr0 (VolatileRegisters->Dr0);
227 AsmWriteDr1 (VolatileRegisters->Dr1);
228 AsmWriteDr2 (VolatileRegisters->Dr2);
229 AsmWriteDr3 (VolatileRegisters->Dr3);
230 AsmWriteDr6 (VolatileRegisters->Dr6);
231 AsmWriteDr7 (VolatileRegisters->Dr7);
232 }
233 }
234 }
235
236 /**
237 Detect whether Mwait-monitor feature is supported.
238
239 @retval TRUE Mwait-monitor feature is supported.
240 @retval FALSE Mwait-monitor feature is not supported.
241 **/
242 BOOLEAN
243 IsMwaitSupport (
244 VOID
245 )
246 {
247 CPUID_VERSION_INFO_ECX VersionInfoEcx;
248
249 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);
250 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;
251 }
252
253 /**
254 Get AP loop mode.
255
256 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
257
258 @return The AP loop mode.
259 **/
260 UINT8
261 GetApLoopMode (
262 OUT UINT32 *MonitorFilterSize
263 )
264 {
265 UINT8 ApLoopMode;
266 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;
267
268 ASSERT (MonitorFilterSize != NULL);
269
270 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
271 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);
272 if (ApLoopMode == ApInMwaitLoop) {
273 if (!IsMwaitSupport ()) {
274 //
275 // If processor does not support MONITOR/MWAIT feature,
276 // force AP in Hlt-loop mode
277 //
278 ApLoopMode = ApInHltLoop;
279 }
280 }
281
282 if (ApLoopMode != ApInMwaitLoop) {
283 *MonitorFilterSize = sizeof (UINT32);
284 } else {
285 //
286 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
287 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
288 //
289 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);
290 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;
291 }
292
293 return ApLoopMode;
294 }
295
296 /**
297 Sort the APIC ID of all processors.
298
299 This function sorts the APIC ID of all processors so that processor number is
300 assigned in the ascending order of APIC ID which eases MP debugging.
301
302 @param[in] CpuMpData Pointer to PEI CPU MP Data
303 **/
304 VOID
305 SortApicId (
306 IN CPU_MP_DATA *CpuMpData
307 )
308 {
309 UINTN Index1;
310 UINTN Index2;
311 UINTN Index3;
312 UINT32 ApicId;
313 CPU_INFO_IN_HOB CpuInfo;
314 UINT32 ApCount;
315 CPU_INFO_IN_HOB *CpuInfoInHob;
316
317 ApCount = CpuMpData->CpuCount - 1;
318 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
319 if (ApCount != 0) {
320 for (Index1 = 0; Index1 < ApCount; Index1++) {
321 Index3 = Index1;
322 //
323 // Sort key is the hardware default APIC ID
324 //
325 ApicId = CpuInfoInHob[Index1].ApicId;
326 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {
327 if (ApicId > CpuInfoInHob[Index2].ApicId) {
328 Index3 = Index2;
329 ApicId = CpuInfoInHob[Index2].ApicId;
330 }
331 }
332 if (Index3 != Index1) {
333 CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));
334 CopyMem (
335 &CpuInfoInHob[Index3],
336 &CpuInfoInHob[Index1],
337 sizeof (CPU_INFO_IN_HOB)
338 );
339 CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));
340 }
341 }
342
343 //
344 // Get the processor number for the BSP
345 //
346 ApicId = GetInitialApicId ();
347 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {
348 if (CpuInfoInHob[Index1].ApicId == ApicId) {
349 CpuMpData->BspNumber = (UINT32) Index1;
350 break;
351 }
352 }
353 }
354 }
355
356 /**
357 Enable x2APIC mode on APs.
358
359 @param[in, out] Buffer Pointer to private data buffer.
360 **/
361 VOID
362 EFIAPI
363 ApFuncEnableX2Apic (
364 IN OUT VOID *Buffer
365 )
366 {
367 SetApicMode (LOCAL_APIC_MODE_X2APIC);
368 }
369
370 /**
371 Do sync on APs.
372
373 @param[in, out] Buffer Pointer to private data buffer.
374 **/
375 VOID
376 EFIAPI
377 ApInitializeSync (
378 IN OUT VOID *Buffer
379 )
380 {
381 CPU_MP_DATA *CpuMpData;
382
383 CpuMpData = (CPU_MP_DATA *) Buffer;
384 //
385 // Load microcode on AP
386 //
387 MicrocodeDetect (CpuMpData);
388 //
389 // Sync BSP's MTRR table to AP
390 //
391 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);
392 }
393
394 /**
395 Find the current Processor number by APIC ID.
396
397 @param[in] CpuMpData Pointer to PEI CPU MP Data
398 @param[out] ProcessorNumber Return the pocessor number found
399
400 @retval EFI_SUCCESS ProcessorNumber is found and returned.
401 @retval EFI_NOT_FOUND ProcessorNumber is not found.
402 **/
403 EFI_STATUS
404 GetProcessorNumber (
405 IN CPU_MP_DATA *CpuMpData,
406 OUT UINTN *ProcessorNumber
407 )
408 {
409 UINTN TotalProcessorNumber;
410 UINTN Index;
411 CPU_INFO_IN_HOB *CpuInfoInHob;
412
413 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
414
415 TotalProcessorNumber = CpuMpData->CpuCount;
416 for (Index = 0; Index < TotalProcessorNumber; Index ++) {
417 if (CpuInfoInHob[Index].ApicId == GetApicId ()) {
418 *ProcessorNumber = Index;
419 return EFI_SUCCESS;
420 }
421 }
422 return EFI_NOT_FOUND;
423 }
424
425 /**
426 This function will get CPU count in the system.
427
428 @param[in] CpuMpData Pointer to PEI CPU MP Data
429
430 @return CPU count detected
431 **/
432 UINTN
433 CollectProcessorCount (
434 IN CPU_MP_DATA *CpuMpData
435 )
436 {
437 UINTN Index;
438
439 //
440 // Send 1st broadcast IPI to APs to wakeup APs
441 //
442 CpuMpData->InitFlag = ApInitConfig;
443 CpuMpData->X2ApicEnable = FALSE;
444 WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);
445 CpuMpData->InitFlag = ApInitDone;
446 ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
447 //
448 // Wait for all APs finished the initialization
449 //
450 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
451 CpuPause ();
452 }
453
454 if (CpuMpData->X2ApicEnable) {
455 DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));
456 //
457 // Wakeup all APs to enable x2APIC mode
458 //
459 WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL);
460 //
461 // Wait for all known APs finished
462 //
463 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
464 CpuPause ();
465 }
466 //
467 // Enable x2APIC on BSP
468 //
469 SetApicMode (LOCAL_APIC_MODE_X2APIC);
470 //
471 // Set BSP/Aps state to IDLE
472 //
473 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
474 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
475 }
476 }
477 DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));
478 //
479 // Sort BSP/Aps by CPU APIC ID in ascending order
480 //
481 SortApicId (CpuMpData);
482
483 DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));
484
485 return CpuMpData->CpuCount;
486 }
487
488 /**
489 Initialize CPU AP Data when AP is wakeup at the first time.
490
491 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
492 @param[in] ProcessorNumber The handle number of processor
493 @param[in] BistData Processor BIST data
494 @param[in] ApTopOfStack Top of AP stack
495
496 **/
497 VOID
498 InitializeApData (
499 IN OUT CPU_MP_DATA *CpuMpData,
500 IN UINTN ProcessorNumber,
501 IN UINT32 BistData,
502 IN UINT64 ApTopOfStack
503 )
504 {
505 CPU_INFO_IN_HOB *CpuInfoInHob;
506
507 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
508 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
509 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
510 CpuInfoInHob[ProcessorNumber].Health = BistData;
511 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;
512
513 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
514 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;
515 if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {
516 //
517 // Set x2APIC mode if there are any logical processor reporting
518 // an Initial APIC ID of 255 or greater.
519 //
520 AcquireSpinLock(&CpuMpData->MpLock);
521 CpuMpData->X2ApicEnable = TRUE;
522 ReleaseSpinLock(&CpuMpData->MpLock);
523 }
524
525 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);
526 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
527 }
528
529 /**
530 This function will be called from AP reset code if BSP uses WakeUpAP.
531
532 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
533 @param[in] NumApsExecuting Number of current executing AP
534 **/
535 VOID
536 EFIAPI
537 ApWakeupFunction (
538 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,
539 IN UINTN NumApsExecuting
540 )
541 {
542 CPU_MP_DATA *CpuMpData;
543 UINTN ProcessorNumber;
544 EFI_AP_PROCEDURE Procedure;
545 VOID *Parameter;
546 UINT32 BistData;
547 volatile UINT32 *ApStartupSignalBuffer;
548 CPU_INFO_IN_HOB *CpuInfoInHob;
549 UINT64 ApTopOfStack;
550
551 //
552 // AP finished assembly code and begin to execute C code
553 //
554 CpuMpData = ExchangeInfo->CpuMpData;
555
556 //
557 // AP's local APIC settings will be lost after received INIT IPI
558 // We need to re-initialize them at here
559 //
560 ProgramVirtualWireMode ();
561 SyncLocalApicTimerSetting (CpuMpData);
562
563 while (TRUE) {
564 if (CpuMpData->InitFlag == ApInitConfig) {
565 //
566 // Add CPU number
567 //
568 InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);
569 ProcessorNumber = NumApsExecuting;
570 //
571 // This is first time AP wakeup, get BIST information from AP stack
572 //
573 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;
574 BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));
575 //
576 // Do some AP initialize sync
577 //
578 ApInitializeSync (CpuMpData);
579 //
580 // Sync BSP's Control registers to APs
581 //
582 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
583 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);
584 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
585 } else {
586 //
587 // Execute AP function if AP is ready
588 //
589 GetProcessorNumber (CpuMpData, &ProcessorNumber);
590 //
591 // Clear AP start-up signal when AP waken up
592 //
593 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
594 InterlockedCompareExchange32 (
595 (UINT32 *) ApStartupSignalBuffer,
596 WAKEUP_AP_SIGNAL,
597 0
598 );
599 if (CpuMpData->ApLoopMode == ApInHltLoop) {
600 //
601 // Restore AP's volatile registers saved
602 //
603 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);
604 }
605
606 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {
607 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;
608 Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;
609 if (Procedure != NULL) {
610 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);
611 //
612 // Enable source debugging on AP function
613 //
614 EnableDebugAgent ();
615 //
616 // Invoke AP function here
617 //
618 Procedure (Parameter);
619 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
620 if (CpuMpData->SwitchBspFlag) {
621 //
622 // Re-get the processor number due to BSP/AP maybe exchange in AP function
623 //
624 GetProcessorNumber (CpuMpData, &ProcessorNumber);
625 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;
626 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;
627 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
628 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;
629 } else {
630 //
631 // Re-get the CPU APICID and Initial APICID
632 //
633 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
634 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
635 }
636 }
637 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);
638 }
639 }
640
641 //
642 // AP finished executing C code
643 //
644 InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);
645
646 //
647 // Place AP is specified loop mode
648 //
649 if (CpuMpData->ApLoopMode == ApInHltLoop) {
650 //
651 // Save AP volatile registers
652 //
653 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);
654 //
655 // Place AP in HLT-loop
656 //
657 while (TRUE) {
658 DisableInterrupts ();
659 CpuSleep ();
660 CpuPause ();
661 }
662 }
663 while (TRUE) {
664 DisableInterrupts ();
665 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
666 //
667 // Place AP in MWAIT-loop
668 //
669 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);
670 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {
671 //
672 // Check AP start-up signal again.
673 // If AP start-up signal is not set, place AP into
674 // the specified C-state
675 //
676 AsmMwait (CpuMpData->ApTargetCState << 4, 0);
677 }
678 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {
679 //
680 // Place AP in Run-loop
681 //
682 CpuPause ();
683 } else {
684 ASSERT (FALSE);
685 }
686
687 //
688 // If AP start-up signal is written, AP is waken up
689 // otherwise place AP in loop again
690 //
691 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {
692 break;
693 }
694 }
695 }
696 }
697
698 /**
699 Wait for AP wakeup and write AP start-up signal till AP is waken up.
700
701 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
702 **/
703 VOID
704 WaitApWakeup (
705 IN volatile UINT32 *ApStartupSignalBuffer
706 )
707 {
708 //
709 // If AP is waken up, StartupApSignal should be cleared.
710 // Otherwise, write StartupApSignal again till AP waken up.
711 //
712 while (InterlockedCompareExchange32 (
713 (UINT32 *) ApStartupSignalBuffer,
714 WAKEUP_AP_SIGNAL,
715 WAKEUP_AP_SIGNAL
716 ) != 0) {
717 CpuPause ();
718 }
719 }
720
721 /**
722 This function will fill the exchange info structure.
723
724 @param[in] CpuMpData Pointer to CPU MP Data
725
726 **/
727 VOID
728 FillExchangeInfoData (
729 IN CPU_MP_DATA *CpuMpData
730 )
731 {
732 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
733
734 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
735 ExchangeInfo->Lock = 0;
736 ExchangeInfo->StackStart = CpuMpData->Buffer;
737 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;
738 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;
739 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;
740
741 ExchangeInfo->CodeSegment = AsmReadCs ();
742 ExchangeInfo->DataSegment = AsmReadDs ();
743
744 ExchangeInfo->Cr3 = AsmReadCr3 ();
745
746 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;
747 ExchangeInfo->NumApsExecuting = 0;
748 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;
749 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
750 ExchangeInfo->CpuMpData = CpuMpData;
751
752 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();
753
754 ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;
755
756 //
757 // Get the BSP's data of GDT and IDT
758 //
759 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);
760 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);
761 }
762
763 /**
764 Helper function that waits until the finished AP count reaches the specified
765 limit, or the specified timeout elapses (whichever comes first).
766
767 @param[in] CpuMpData Pointer to CPU MP Data.
768 @param[in] FinishedApLimit The number of finished APs to wait for.
769 @param[in] TimeLimit The number of microseconds to wait for.
770 **/
771 VOID
772 TimedWaitForApFinish (
773 IN CPU_MP_DATA *CpuMpData,
774 IN UINT32 FinishedApLimit,
775 IN UINT32 TimeLimit
776 );
777
778 /**
779 This function will be called by BSP to wakeup AP.
780
781 @param[in] CpuMpData Pointer to CPU MP Data
782 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
783 FALSE: Send IPI to AP by ApicId
784 @param[in] ProcessorNumber The handle number of specified processor
785 @param[in] Procedure The function to be invoked by AP
786 @param[in] ProcedureArgument The argument to be passed into AP function
787 **/
788 VOID
789 WakeUpAP (
790 IN CPU_MP_DATA *CpuMpData,
791 IN BOOLEAN Broadcast,
792 IN UINTN ProcessorNumber,
793 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
794 IN VOID *ProcedureArgument OPTIONAL
795 )
796 {
797 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
798 UINTN Index;
799 CPU_AP_DATA *CpuData;
800 BOOLEAN ResetVectorRequired;
801 CPU_INFO_IN_HOB *CpuInfoInHob;
802
803 CpuMpData->FinishedCount = 0;
804 ResetVectorRequired = FALSE;
805
806 if (CpuMpData->ApLoopMode == ApInHltLoop ||
807 CpuMpData->InitFlag != ApInitDone) {
808 ResetVectorRequired = TRUE;
809 AllocateResetVector (CpuMpData);
810 FillExchangeInfoData (CpuMpData);
811 SaveLocalApicTimerSetting (CpuMpData);
812 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
813 //
814 // Get AP target C-state each time when waking up AP,
815 // for it maybe updated by platform again
816 //
817 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);
818 }
819
820 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
821
822 if (Broadcast) {
823 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
824 if (Index != CpuMpData->BspNumber) {
825 CpuData = &CpuMpData->CpuData[Index];
826 CpuData->ApFunction = (UINTN) Procedure;
827 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
828 SetApState (CpuData, CpuStateReady);
829 if (CpuMpData->InitFlag != ApInitConfig) {
830 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
831 }
832 }
833 }
834 if (ResetVectorRequired) {
835 //
836 // Wakeup all APs
837 //
838 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);
839 }
840 if (CpuMpData->InitFlag == ApInitConfig) {
841 //
842 // Wait for all potential APs waken up in one specified period
843 //
844 TimedWaitForApFinish (
845 CpuMpData,
846 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,
847 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)
848 );
849 } else {
850 //
851 // Wait all APs waken up if this is not the 1st broadcast of SIPI
852 //
853 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
854 CpuData = &CpuMpData->CpuData[Index];
855 if (Index != CpuMpData->BspNumber) {
856 WaitApWakeup (CpuData->StartupApSignal);
857 }
858 }
859 }
860 } else {
861 CpuData = &CpuMpData->CpuData[ProcessorNumber];
862 CpuData->ApFunction = (UINTN) Procedure;
863 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
864 SetApState (CpuData, CpuStateReady);
865 //
866 // Wakeup specified AP
867 //
868 ASSERT (CpuMpData->InitFlag != ApInitConfig);
869 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
870 if (ResetVectorRequired) {
871 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
872 SendInitSipiSipi (
873 CpuInfoInHob[ProcessorNumber].ApicId,
874 (UINT32) ExchangeInfo->BufferStart
875 );
876 }
877 //
878 // Wait specified AP waken up
879 //
880 WaitApWakeup (CpuData->StartupApSignal);
881 }
882
883 if (ResetVectorRequired) {
884 FreeResetVector (CpuMpData);
885 }
886 }
887
888 /**
889 Calculate timeout value and return the current performance counter value.
890
891 Calculate the number of performance counter ticks required for a timeout.
892 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
893 as infinity.
894
895 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
896 @param[out] CurrentTime Returns the current value of the performance counter.
897
898 @return Expected time stamp counter for timeout.
899 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
900 as infinity.
901
902 **/
903 UINT64
904 CalculateTimeout (
905 IN UINTN TimeoutInMicroseconds,
906 OUT UINT64 *CurrentTime
907 )
908 {
909 //
910 // Read the current value of the performance counter
911 //
912 *CurrentTime = GetPerformanceCounter ();
913
914 //
915 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
916 // as infinity.
917 //
918 if (TimeoutInMicroseconds == 0) {
919 return 0;
920 }
921
922 //
923 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
924 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
925 // it by 1,000,000, to get the number of ticks for the timeout value.
926 //
927 return DivU64x32 (
928 MultU64x64 (
929 GetPerformanceCounterProperties (NULL, NULL),
930 TimeoutInMicroseconds
931 ),
932 1000000
933 );
934 }
935
936 /**
937 Checks whether timeout expires.
938
939 Check whether the number of elapsed performance counter ticks required for
940 a timeout condition has been reached.
941 If Timeout is zero, which means infinity, return value is always FALSE.
942
943 @param[in, out] PreviousTime On input, the value of the performance counter
944 when it was last read.
945 On output, the current value of the performance
946 counter
947 @param[in] TotalTime The total amount of elapsed time in performance
948 counter ticks.
949 @param[in] Timeout The number of performance counter ticks required
950 to reach a timeout condition.
951
952 @retval TRUE A timeout condition has been reached.
953 @retval FALSE A timeout condition has not been reached.
954
955 **/
956 BOOLEAN
957 CheckTimeout (
958 IN OUT UINT64 *PreviousTime,
959 IN UINT64 *TotalTime,
960 IN UINT64 Timeout
961 )
962 {
963 UINT64 Start;
964 UINT64 End;
965 UINT64 CurrentTime;
966 INT64 Delta;
967 INT64 Cycle;
968
969 if (Timeout == 0) {
970 return FALSE;
971 }
972 GetPerformanceCounterProperties (&Start, &End);
973 Cycle = End - Start;
974 if (Cycle < 0) {
975 Cycle = -Cycle;
976 }
977 Cycle++;
978 CurrentTime = GetPerformanceCounter();
979 Delta = (INT64) (CurrentTime - *PreviousTime);
980 if (Start > End) {
981 Delta = -Delta;
982 }
983 if (Delta < 0) {
984 Delta += Cycle;
985 }
986 *TotalTime += Delta;
987 *PreviousTime = CurrentTime;
988 if (*TotalTime > Timeout) {
989 return TRUE;
990 }
991 return FALSE;
992 }
993
994 /**
995 Helper function that waits until the finished AP count reaches the specified
996 limit, or the specified timeout elapses (whichever comes first).
997
998 @param[in] CpuMpData Pointer to CPU MP Data.
999 @param[in] FinishedApLimit The number of finished APs to wait for.
1000 @param[in] TimeLimit The number of microseconds to wait for.
1001 **/
1002 VOID
1003 TimedWaitForApFinish (
1004 IN CPU_MP_DATA *CpuMpData,
1005 IN UINT32 FinishedApLimit,
1006 IN UINT32 TimeLimit
1007 )
1008 {
1009 //
1010 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1011 // "infinity", so check for (TimeLimit == 0) explicitly.
1012 //
1013 if (TimeLimit == 0) {
1014 return;
1015 }
1016
1017 CpuMpData->TotalTime = 0;
1018 CpuMpData->ExpectedTime = CalculateTimeout (
1019 TimeLimit,
1020 &CpuMpData->CurrentTime
1021 );
1022 while (CpuMpData->FinishedCount < FinishedApLimit &&
1023 !CheckTimeout (
1024 &CpuMpData->CurrentTime,
1025 &CpuMpData->TotalTime,
1026 CpuMpData->ExpectedTime
1027 )) {
1028 CpuPause ();
1029 }
1030
1031 if (CpuMpData->FinishedCount >= FinishedApLimit) {
1032 DEBUG ((
1033 DEBUG_VERBOSE,
1034 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1035 __FUNCTION__,
1036 FinishedApLimit,
1037 DivU64x64Remainder (
1038 MultU64x32 (CpuMpData->TotalTime, 1000000),
1039 GetPerformanceCounterProperties (NULL, NULL),
1040 NULL
1041 )
1042 ));
1043 }
1044 }
1045
1046 /**
1047 Reset an AP to Idle state.
1048
1049 Any task being executed by the AP will be aborted and the AP
1050 will be waiting for a new task in Wait-For-SIPI state.
1051
1052 @param[in] ProcessorNumber The handle number of processor.
1053 **/
1054 VOID
1055 ResetProcessorToIdleState (
1056 IN UINTN ProcessorNumber
1057 )
1058 {
1059 CPU_MP_DATA *CpuMpData;
1060
1061 CpuMpData = GetCpuMpData ();
1062
1063 CpuMpData->InitFlag = ApInitReconfig;
1064 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);
1065 while (CpuMpData->FinishedCount < 1) {
1066 CpuPause ();
1067 }
1068 CpuMpData->InitFlag = ApInitDone;
1069
1070 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1071 }
1072
1073 /**
1074 Searches for the next waiting AP.
1075
1076 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1077
1078 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1079
1080 @retval EFI_SUCCESS The next waiting AP has been found.
1081 @retval EFI_NOT_FOUND No waiting AP exists.
1082
1083 **/
1084 EFI_STATUS
1085 GetNextWaitingProcessorNumber (
1086 OUT UINTN *NextProcessorNumber
1087 )
1088 {
1089 UINTN ProcessorNumber;
1090 CPU_MP_DATA *CpuMpData;
1091
1092 CpuMpData = GetCpuMpData ();
1093
1094 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1095 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1096 *NextProcessorNumber = ProcessorNumber;
1097 return EFI_SUCCESS;
1098 }
1099 }
1100
1101 return EFI_NOT_FOUND;
1102 }
1103
1104 /** Checks status of specified AP.
1105
1106 This function checks whether the specified AP has finished the task assigned
1107 by StartupThisAP(), and whether timeout expires.
1108
1109 @param[in] ProcessorNumber The handle number of processor.
1110
1111 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1112 @retval EFI_TIMEOUT The timeout expires.
1113 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1114 **/
1115 EFI_STATUS
1116 CheckThisAP (
1117 IN UINTN ProcessorNumber
1118 )
1119 {
1120 CPU_MP_DATA *CpuMpData;
1121 CPU_AP_DATA *CpuData;
1122
1123 CpuMpData = GetCpuMpData ();
1124 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1125
1126 //
1127 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1128 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1129 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1130 //
1131 //
1132 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1133 //
1134 if (GetApState(CpuData) == CpuStateFinished) {
1135 if (CpuData->Finished != NULL) {
1136 *(CpuData->Finished) = TRUE;
1137 }
1138 SetApState (CpuData, CpuStateIdle);
1139 return EFI_SUCCESS;
1140 } else {
1141 //
1142 // If timeout expires for StartupThisAP(), report timeout.
1143 //
1144 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
1145 if (CpuData->Finished != NULL) {
1146 *(CpuData->Finished) = FALSE;
1147 }
1148 //
1149 // Reset failed AP to idle state
1150 //
1151 ResetProcessorToIdleState (ProcessorNumber);
1152
1153 return EFI_TIMEOUT;
1154 }
1155 }
1156 return EFI_NOT_READY;
1157 }
1158
1159 /**
1160 Checks status of all APs.
1161
1162 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1163 and whether timeout expires.
1164
1165 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1166 @retval EFI_TIMEOUT The timeout expires.
1167 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1168 **/
1169 EFI_STATUS
1170 CheckAllAPs (
1171 VOID
1172 )
1173 {
1174 UINTN ProcessorNumber;
1175 UINTN NextProcessorNumber;
1176 UINTN ListIndex;
1177 EFI_STATUS Status;
1178 CPU_MP_DATA *CpuMpData;
1179 CPU_AP_DATA *CpuData;
1180
1181 CpuMpData = GetCpuMpData ();
1182
1183 NextProcessorNumber = 0;
1184
1185 //
1186 // Go through all APs that are responsible for the StartupAllAPs().
1187 //
1188 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1189 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {
1190 continue;
1191 }
1192
1193 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1194 //
1195 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1196 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1197 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1198 //
1199 if (GetApState(CpuData) == CpuStateFinished) {
1200 CpuMpData->RunningCount ++;
1201 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1202 SetApState(CpuData, CpuStateIdle);
1203
1204 //
1205 // If in Single Thread mode, then search for the next waiting AP for execution.
1206 //
1207 if (CpuMpData->SingleThread) {
1208 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
1209
1210 if (!EFI_ERROR (Status)) {
1211 WakeUpAP (
1212 CpuMpData,
1213 FALSE,
1214 (UINT32) NextProcessorNumber,
1215 CpuMpData->Procedure,
1216 CpuMpData->ProcArguments
1217 );
1218 }
1219 }
1220 }
1221 }
1222
1223 //
1224 // If all APs finish, return EFI_SUCCESS.
1225 //
1226 if (CpuMpData->RunningCount == CpuMpData->StartCount) {
1227 return EFI_SUCCESS;
1228 }
1229
1230 //
1231 // If timeout expires, report timeout.
1232 //
1233 if (CheckTimeout (
1234 &CpuMpData->CurrentTime,
1235 &CpuMpData->TotalTime,
1236 CpuMpData->ExpectedTime)
1237 ) {
1238 //
1239 // If FailedCpuList is not NULL, record all failed APs in it.
1240 //
1241 if (CpuMpData->FailedCpuList != NULL) {
1242 *CpuMpData->FailedCpuList =
1243 AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));
1244 ASSERT (*CpuMpData->FailedCpuList != NULL);
1245 }
1246 ListIndex = 0;
1247
1248 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1249 //
1250 // Check whether this processor is responsible for StartupAllAPs().
1251 //
1252 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1253 //
1254 // Reset failed APs to idle state
1255 //
1256 ResetProcessorToIdleState (ProcessorNumber);
1257 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1258 if (CpuMpData->FailedCpuList != NULL) {
1259 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;
1260 }
1261 }
1262 }
1263 if (CpuMpData->FailedCpuList != NULL) {
1264 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
1265 }
1266 return EFI_TIMEOUT;
1267 }
1268 return EFI_NOT_READY;
1269 }
1270
1271 /**
1272 MP Initialize Library initialization.
1273
1274 This service will allocate AP reset vector and wakeup all APs to do APs
1275 initialization.
1276
1277 This service must be invoked before all other MP Initialize Library
1278 service are invoked.
1279
1280 @retval EFI_SUCCESS MP initialization succeeds.
1281 @retval Others MP initialization fails.
1282
1283 **/
1284 EFI_STATUS
1285 EFIAPI
1286 MpInitLibInitialize (
1287 VOID
1288 )
1289 {
1290 CPU_MP_DATA *OldCpuMpData;
1291 CPU_INFO_IN_HOB *CpuInfoInHob;
1292 UINT32 MaxLogicalProcessorNumber;
1293 UINT32 ApStackSize;
1294 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1295 UINTN BufferSize;
1296 UINT32 MonitorFilterSize;
1297 VOID *MpBuffer;
1298 UINTN Buffer;
1299 CPU_MP_DATA *CpuMpData;
1300 UINT8 ApLoopMode;
1301 UINT8 *MonitorBuffer;
1302 UINTN Index;
1303 UINTN ApResetVectorSize;
1304 UINTN BackupBufferAddr;
1305
1306 OldCpuMpData = GetCpuMpDataFromGuidedHob ();
1307 if (OldCpuMpData == NULL) {
1308 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
1309 } else {
1310 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;
1311 }
1312 ASSERT (MaxLogicalProcessorNumber != 0);
1313
1314 AsmGetAddressMap (&AddressMap);
1315 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
1316 ApStackSize = PcdGet32(PcdCpuApStackSize);
1317 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
1318
1319 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
1320 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
1321 BufferSize += sizeof (CPU_MP_DATA);
1322 BufferSize += ApResetVectorSize;
1323 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
1324 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
1325 ASSERT (MpBuffer != NULL);
1326 ZeroMem (MpBuffer, BufferSize);
1327 Buffer = (UINTN) MpBuffer;
1328
1329 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);
1330 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
1331 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);
1332 CpuMpData->Buffer = Buffer;
1333 CpuMpData->CpuApStackSize = ApStackSize;
1334 CpuMpData->BackupBuffer = BackupBufferAddr;
1335 CpuMpData->BackupBufferSize = ApResetVectorSize;
1336 CpuMpData->SaveRestoreFlag = FALSE;
1337 CpuMpData->WakeupBuffer = (UINTN) -1;
1338 CpuMpData->CpuCount = 1;
1339 CpuMpData->BspNumber = 0;
1340 CpuMpData->WaitEvent = NULL;
1341 CpuMpData->SwitchBspFlag = FALSE;
1342 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);
1343 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);
1344 InitializeSpinLock(&CpuMpData->MpLock);
1345 //
1346 // Save BSP's Control registers to APs
1347 //
1348 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);
1349 //
1350 // Set BSP basic information
1351 //
1352 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);
1353 //
1354 // Save assembly code information
1355 //
1356 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
1357 //
1358 // Finally set AP loop mode
1359 //
1360 CpuMpData->ApLoopMode = ApLoopMode;
1361 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
1362 //
1363 // Set up APs wakeup signal buffer
1364 //
1365 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {
1366 CpuMpData->CpuData[Index].StartupApSignal =
1367 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
1368 }
1369 //
1370 // Load Microcode on BSP
1371 //
1372 MicrocodeDetect (CpuMpData);
1373 //
1374 // Store BSP's MTRR setting
1375 //
1376 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
1377 //
1378 // Enable the local APIC for Virtual Wire Mode.
1379 //
1380 ProgramVirtualWireMode ();
1381
1382 if (OldCpuMpData == NULL) {
1383 if (MaxLogicalProcessorNumber > 1) {
1384 //
1385 // Wakeup all APs and calculate the processor count in system
1386 //
1387 CollectProcessorCount (CpuMpData);
1388 }
1389 } else {
1390 //
1391 // APs have been wakeup before, just get the CPU Information
1392 // from HOB
1393 //
1394 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1395 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1396 CpuMpData->InitFlag = ApInitReconfig;
1397 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1398 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1399 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1400 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);
1401 if (CpuInfoInHob[Index].InitialApicId >= 255) {
1402 CpuMpData->X2ApicEnable = TRUE;
1403 }
1404 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;
1405 CpuMpData->CpuData[Index].ApFunction = 0;
1406 CopyMem (
1407 &CpuMpData->CpuData[Index].VolatileRegisters,
1408 &CpuMpData->CpuData[0].VolatileRegisters,
1409 sizeof (CPU_VOLATILE_REGISTERS)
1410 );
1411 }
1412 if (MaxLogicalProcessorNumber > 1) {
1413 //
1414 // Wakeup APs to do some AP initialize sync
1415 //
1416 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);
1417 //
1418 // Wait for all APs finished initialization
1419 //
1420 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1421 CpuPause ();
1422 }
1423 CpuMpData->InitFlag = ApInitDone;
1424 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1425 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1426 }
1427 }
1428 }
1429
1430 //
1431 // Initialize global data for MP support
1432 //
1433 InitMpGlobalData (CpuMpData);
1434
1435 return EFI_SUCCESS;
1436 }
1437
1438 /**
1439 Gets detailed MP-related information on the requested processor at the
1440 instant this call is made. This service may only be called from the BSP.
1441
1442 @param[in] ProcessorNumber The handle number of processor.
1443 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1444 the requested processor is deposited.
1445 @param[out] HealthData Return processor health data.
1446
1447 @retval EFI_SUCCESS Processor information was returned.
1448 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1449 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1450 @retval EFI_NOT_FOUND The processor with the handle specified by
1451 ProcessorNumber does not exist in the platform.
1452 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1453
1454 **/
1455 EFI_STATUS
1456 EFIAPI
1457 MpInitLibGetProcessorInfo (
1458 IN UINTN ProcessorNumber,
1459 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
1460 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
1461 )
1462 {
1463 CPU_MP_DATA *CpuMpData;
1464 UINTN CallerNumber;
1465 CPU_INFO_IN_HOB *CpuInfoInHob;
1466
1467 CpuMpData = GetCpuMpData ();
1468 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1469
1470 //
1471 // Check whether caller processor is BSP
1472 //
1473 MpInitLibWhoAmI (&CallerNumber);
1474 if (CallerNumber != CpuMpData->BspNumber) {
1475 return EFI_DEVICE_ERROR;
1476 }
1477
1478 if (ProcessorInfoBuffer == NULL) {
1479 return EFI_INVALID_PARAMETER;
1480 }
1481
1482 if (ProcessorNumber >= CpuMpData->CpuCount) {
1483 return EFI_NOT_FOUND;
1484 }
1485
1486 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;
1487 ProcessorInfoBuffer->StatusFlag = 0;
1488 if (ProcessorNumber == CpuMpData->BspNumber) {
1489 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
1490 }
1491 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
1492 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
1493 }
1494 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1495 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
1496 } else {
1497 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
1498 }
1499
1500 //
1501 // Get processor location information
1502 //
1503 GetProcessorLocationByApicId (
1504 CpuInfoInHob[ProcessorNumber].ApicId,
1505 &ProcessorInfoBuffer->Location.Package,
1506 &ProcessorInfoBuffer->Location.Core,
1507 &ProcessorInfoBuffer->Location.Thread
1508 );
1509
1510 if (HealthData != NULL) {
1511 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
1512 }
1513
1514 return EFI_SUCCESS;
1515 }
1516
1517 /**
1518 Worker function to switch the requested AP to be the BSP from that point onward.
1519
1520 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1521 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1522 enabled AP. Otherwise, it will be disabled.
1523
1524 @retval EFI_SUCCESS BSP successfully switched.
1525 @retval others Failed to switch BSP.
1526
1527 **/
1528 EFI_STATUS
1529 SwitchBSPWorker (
1530 IN UINTN ProcessorNumber,
1531 IN BOOLEAN EnableOldBSP
1532 )
1533 {
1534 CPU_MP_DATA *CpuMpData;
1535 UINTN CallerNumber;
1536 CPU_STATE State;
1537 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
1538 BOOLEAN OldInterruptState;
1539 BOOLEAN OldTimerInterruptState;
1540
1541 //
1542 // Save and Disable Local APIC timer interrupt
1543 //
1544 OldTimerInterruptState = GetApicTimerInterruptState ();
1545 DisableApicTimerInterrupt ();
1546 //
1547 // Before send both BSP and AP to a procedure to exchange their roles,
1548 // interrupt must be disabled. This is because during the exchange role
1549 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1550 // be corrupted, since interrupt return address will be pushed to stack
1551 // by hardware.
1552 //
1553 OldInterruptState = SaveAndDisableInterrupts ();
1554
1555 //
1556 // Mask LINT0 & LINT1 for the old BSP
1557 //
1558 DisableLvtInterrupts ();
1559
1560 CpuMpData = GetCpuMpData ();
1561
1562 //
1563 // Check whether caller processor is BSP
1564 //
1565 MpInitLibWhoAmI (&CallerNumber);
1566 if (CallerNumber != CpuMpData->BspNumber) {
1567 return EFI_SUCCESS;
1568 }
1569
1570 if (ProcessorNumber >= CpuMpData->CpuCount) {
1571 return EFI_NOT_FOUND;
1572 }
1573
1574 //
1575 // Check whether specified AP is disabled
1576 //
1577 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
1578 if (State == CpuStateDisabled) {
1579 return EFI_INVALID_PARAMETER;
1580 }
1581
1582 //
1583 // Check whether ProcessorNumber specifies the current BSP
1584 //
1585 if (ProcessorNumber == CpuMpData->BspNumber) {
1586 return EFI_INVALID_PARAMETER;
1587 }
1588
1589 //
1590 // Check whether specified AP is busy
1591 //
1592 if (State == CpuStateBusy) {
1593 return EFI_NOT_READY;
1594 }
1595
1596 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
1597 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
1598 CpuMpData->SwitchBspFlag = TRUE;
1599 CpuMpData->NewBspNumber = ProcessorNumber;
1600
1601 //
1602 // Clear the BSP bit of MSR_IA32_APIC_BASE
1603 //
1604 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1605 ApicBaseMsr.Bits.BSP = 0;
1606 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1607
1608 //
1609 // Need to wakeUp AP (future BSP).
1610 //
1611 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);
1612
1613 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
1614
1615 //
1616 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1617 //
1618 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1619 ApicBaseMsr.Bits.BSP = 1;
1620 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1621
1622 //
1623 // Wait for old BSP finished AP task
1624 //
1625 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
1626 CpuPause ();
1627 }
1628
1629 CpuMpData->SwitchBspFlag = FALSE;
1630 //
1631 // Set old BSP enable state
1632 //
1633 if (!EnableOldBSP) {
1634 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
1635 } else {
1636 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
1637 }
1638 //
1639 // Save new BSP number
1640 //
1641 CpuMpData->BspNumber = (UINT32) ProcessorNumber;
1642
1643 //
1644 // Restore interrupt state.
1645 //
1646 SetInterruptState (OldInterruptState);
1647
1648 if (OldTimerInterruptState) {
1649 EnableApicTimerInterrupt ();
1650 }
1651
1652 return EFI_SUCCESS;
1653 }
1654
1655 /**
1656 Worker function to let the caller enable or disable an AP from this point onward.
1657 This service may only be called from the BSP.
1658
1659 @param[in] ProcessorNumber The handle number of AP.
1660 @param[in] EnableAP Specifies the new state for the processor for
1661 enabled, FALSE for disabled.
1662 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1663 the new health status of the AP.
1664
1665 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1666 @retval others Failed to Enable/Disable AP.
1667
1668 **/
1669 EFI_STATUS
1670 EnableDisableApWorker (
1671 IN UINTN ProcessorNumber,
1672 IN BOOLEAN EnableAP,
1673 IN UINT32 *HealthFlag OPTIONAL
1674 )
1675 {
1676 CPU_MP_DATA *CpuMpData;
1677 UINTN CallerNumber;
1678
1679 CpuMpData = GetCpuMpData ();
1680
1681 //
1682 // Check whether caller processor is BSP
1683 //
1684 MpInitLibWhoAmI (&CallerNumber);
1685 if (CallerNumber != CpuMpData->BspNumber) {
1686 return EFI_DEVICE_ERROR;
1687 }
1688
1689 if (ProcessorNumber == CpuMpData->BspNumber) {
1690 return EFI_INVALID_PARAMETER;
1691 }
1692
1693 if (ProcessorNumber >= CpuMpData->CpuCount) {
1694 return EFI_NOT_FOUND;
1695 }
1696
1697 if (!EnableAP) {
1698 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
1699 } else {
1700 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1701 }
1702
1703 if (HealthFlag != NULL) {
1704 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
1705 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
1706 }
1707
1708 return EFI_SUCCESS;
1709 }
1710
1711 /**
1712 This return the handle number for the calling processor. This service may be
1713 called from the BSP and APs.
1714
1715 @param[out] ProcessorNumber Pointer to the handle number of AP.
1716 The range is from 0 to the total number of
1717 logical processors minus 1. The total number of
1718 logical processors can be retrieved by
1719 MpInitLibGetNumberOfProcessors().
1720
1721 @retval EFI_SUCCESS The current processor handle number was returned
1722 in ProcessorNumber.
1723 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1724 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1725
1726 **/
1727 EFI_STATUS
1728 EFIAPI
1729 MpInitLibWhoAmI (
1730 OUT UINTN *ProcessorNumber
1731 )
1732 {
1733 CPU_MP_DATA *CpuMpData;
1734
1735 if (ProcessorNumber == NULL) {
1736 return EFI_INVALID_PARAMETER;
1737 }
1738
1739 CpuMpData = GetCpuMpData ();
1740
1741 return GetProcessorNumber (CpuMpData, ProcessorNumber);
1742 }
1743
1744 /**
1745 Retrieves the number of logical processor in the platform and the number of
1746 those logical processors that are enabled on this boot. This service may only
1747 be called from the BSP.
1748
1749 @param[out] NumberOfProcessors Pointer to the total number of logical
1750 processors in the system, including the BSP
1751 and disabled APs.
1752 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1753 processors that exist in system, including
1754 the BSP.
1755
1756 @retval EFI_SUCCESS The number of logical processors and enabled
1757 logical processors was retrieved.
1758 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1759 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1760 is NULL.
1761 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1762
1763 **/
1764 EFI_STATUS
1765 EFIAPI
1766 MpInitLibGetNumberOfProcessors (
1767 OUT UINTN *NumberOfProcessors, OPTIONAL
1768 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
1769 )
1770 {
1771 CPU_MP_DATA *CpuMpData;
1772 UINTN CallerNumber;
1773 UINTN ProcessorNumber;
1774 UINTN EnabledProcessorNumber;
1775 UINTN Index;
1776
1777 CpuMpData = GetCpuMpData ();
1778
1779 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
1780 return EFI_INVALID_PARAMETER;
1781 }
1782
1783 //
1784 // Check whether caller processor is BSP
1785 //
1786 MpInitLibWhoAmI (&CallerNumber);
1787 if (CallerNumber != CpuMpData->BspNumber) {
1788 return EFI_DEVICE_ERROR;
1789 }
1790
1791 ProcessorNumber = CpuMpData->CpuCount;
1792 EnabledProcessorNumber = 0;
1793 for (Index = 0; Index < ProcessorNumber; Index++) {
1794 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
1795 EnabledProcessorNumber ++;
1796 }
1797 }
1798
1799 if (NumberOfProcessors != NULL) {
1800 *NumberOfProcessors = ProcessorNumber;
1801 }
1802 if (NumberOfEnabledProcessors != NULL) {
1803 *NumberOfEnabledProcessors = EnabledProcessorNumber;
1804 }
1805
1806 return EFI_SUCCESS;
1807 }
1808
1809
1810 /**
1811 Worker function to execute a caller provided function on all enabled APs.
1812
1813 @param[in] Procedure A pointer to the function to be run on
1814 enabled APs of the system.
1815 @param[in] SingleThread If TRUE, then all the enabled APs execute
1816 the function specified by Procedure one by
1817 one, in ascending order of processor handle
1818 number. If FALSE, then all the enabled APs
1819 execute the function specified by Procedure
1820 simultaneously.
1821 @param[in] WaitEvent The event created by the caller with CreateEvent()
1822 service.
1823 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1824 APs to return from Procedure, either for
1825 blocking or non-blocking mode.
1826 @param[in] ProcedureArgument The parameter passed into Procedure for
1827 all APs.
1828 @param[out] FailedCpuList If all APs finish successfully, then its
1829 content is set to NULL. If not all APs
1830 finish before timeout expires, then its
1831 content is set to address of the buffer
1832 holding handle numbers of the failed APs.
1833
1834 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1835 the timeout expired.
1836 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1837 to all enabled APs.
1838 @retval others Failed to Startup all APs.
1839
1840 **/
1841 EFI_STATUS
1842 StartupAllAPsWorker (
1843 IN EFI_AP_PROCEDURE Procedure,
1844 IN BOOLEAN SingleThread,
1845 IN EFI_EVENT WaitEvent OPTIONAL,
1846 IN UINTN TimeoutInMicroseconds,
1847 IN VOID *ProcedureArgument OPTIONAL,
1848 OUT UINTN **FailedCpuList OPTIONAL
1849 )
1850 {
1851 EFI_STATUS Status;
1852 CPU_MP_DATA *CpuMpData;
1853 UINTN ProcessorCount;
1854 UINTN ProcessorNumber;
1855 UINTN CallerNumber;
1856 CPU_AP_DATA *CpuData;
1857 BOOLEAN HasEnabledAp;
1858 CPU_STATE ApState;
1859
1860 CpuMpData = GetCpuMpData ();
1861
1862 if (FailedCpuList != NULL) {
1863 *FailedCpuList = NULL;
1864 }
1865
1866 if (CpuMpData->CpuCount == 1) {
1867 return EFI_NOT_STARTED;
1868 }
1869
1870 if (Procedure == NULL) {
1871 return EFI_INVALID_PARAMETER;
1872 }
1873
1874 //
1875 // Check whether caller processor is BSP
1876 //
1877 MpInitLibWhoAmI (&CallerNumber);
1878 if (CallerNumber != CpuMpData->BspNumber) {
1879 return EFI_DEVICE_ERROR;
1880 }
1881
1882 //
1883 // Update AP state
1884 //
1885 CheckAndUpdateApsStatus ();
1886
1887 ProcessorCount = CpuMpData->CpuCount;
1888 HasEnabledAp = FALSE;
1889 //
1890 // Check whether all enabled APs are idle.
1891 // If any enabled AP is not idle, return EFI_NOT_READY.
1892 //
1893 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1894 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1895 if (ProcessorNumber != CpuMpData->BspNumber) {
1896 ApState = GetApState (CpuData);
1897 if (ApState != CpuStateDisabled) {
1898 HasEnabledAp = TRUE;
1899 if (ApState != CpuStateIdle) {
1900 //
1901 // If any enabled APs are busy, return EFI_NOT_READY.
1902 //
1903 return EFI_NOT_READY;
1904 }
1905 }
1906 }
1907 }
1908
1909 if (!HasEnabledAp) {
1910 //
1911 // If no enabled AP exists, return EFI_NOT_STARTED.
1912 //
1913 return EFI_NOT_STARTED;
1914 }
1915
1916 CpuMpData->StartCount = 0;
1917 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1918 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1919 CpuData->Waiting = FALSE;
1920 if (ProcessorNumber != CpuMpData->BspNumber) {
1921 if (CpuData->State == CpuStateIdle) {
1922 //
1923 // Mark this processor as responsible for current calling.
1924 //
1925 CpuData->Waiting = TRUE;
1926 CpuMpData->StartCount++;
1927 }
1928 }
1929 }
1930
1931 CpuMpData->Procedure = Procedure;
1932 CpuMpData->ProcArguments = ProcedureArgument;
1933 CpuMpData->SingleThread = SingleThread;
1934 CpuMpData->FinishedCount = 0;
1935 CpuMpData->RunningCount = 0;
1936 CpuMpData->FailedCpuList = FailedCpuList;
1937 CpuMpData->ExpectedTime = CalculateTimeout (
1938 TimeoutInMicroseconds,
1939 &CpuMpData->CurrentTime
1940 );
1941 CpuMpData->TotalTime = 0;
1942 CpuMpData->WaitEvent = WaitEvent;
1943
1944 if (!SingleThread) {
1945 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);
1946 } else {
1947 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1948 if (ProcessorNumber == CallerNumber) {
1949 continue;
1950 }
1951 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1952 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
1953 break;
1954 }
1955 }
1956 }
1957
1958 Status = EFI_SUCCESS;
1959 if (WaitEvent == NULL) {
1960 do {
1961 Status = CheckAllAPs ();
1962 } while (Status == EFI_NOT_READY);
1963 }
1964
1965 return Status;
1966 }
1967
1968 /**
1969 Worker function to let the caller get one enabled AP to execute a caller-provided
1970 function.
1971
1972 @param[in] Procedure A pointer to the function to be run on
1973 enabled APs of the system.
1974 @param[in] ProcessorNumber The handle number of the AP.
1975 @param[in] WaitEvent The event created by the caller with CreateEvent()
1976 service.
1977 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1978 APs to return from Procedure, either for
1979 blocking or non-blocking mode.
1980 @param[in] ProcedureArgument The parameter passed into Procedure for
1981 all APs.
1982 @param[out] Finished If AP returns from Procedure before the
1983 timeout expires, its content is set to TRUE.
1984 Otherwise, the value is set to FALSE.
1985
1986 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1987 the timeout expires.
1988 @retval others Failed to Startup AP.
1989
1990 **/
1991 EFI_STATUS
1992 StartupThisAPWorker (
1993 IN EFI_AP_PROCEDURE Procedure,
1994 IN UINTN ProcessorNumber,
1995 IN EFI_EVENT WaitEvent OPTIONAL,
1996 IN UINTN TimeoutInMicroseconds,
1997 IN VOID *ProcedureArgument OPTIONAL,
1998 OUT BOOLEAN *Finished OPTIONAL
1999 )
2000 {
2001 EFI_STATUS Status;
2002 CPU_MP_DATA *CpuMpData;
2003 CPU_AP_DATA *CpuData;
2004 UINTN CallerNumber;
2005
2006 CpuMpData = GetCpuMpData ();
2007
2008 if (Finished != NULL) {
2009 *Finished = FALSE;
2010 }
2011
2012 //
2013 // Check whether caller processor is BSP
2014 //
2015 MpInitLibWhoAmI (&CallerNumber);
2016 if (CallerNumber != CpuMpData->BspNumber) {
2017 return EFI_DEVICE_ERROR;
2018 }
2019
2020 //
2021 // Check whether processor with the handle specified by ProcessorNumber exists
2022 //
2023 if (ProcessorNumber >= CpuMpData->CpuCount) {
2024 return EFI_NOT_FOUND;
2025 }
2026
2027 //
2028 // Check whether specified processor is BSP
2029 //
2030 if (ProcessorNumber == CpuMpData->BspNumber) {
2031 return EFI_INVALID_PARAMETER;
2032 }
2033
2034 //
2035 // Check parameter Procedure
2036 //
2037 if (Procedure == NULL) {
2038 return EFI_INVALID_PARAMETER;
2039 }
2040
2041 //
2042 // Update AP state
2043 //
2044 CheckAndUpdateApsStatus ();
2045
2046 //
2047 // Check whether specified AP is disabled
2048 //
2049 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2050 return EFI_INVALID_PARAMETER;
2051 }
2052
2053 //
2054 // If WaitEvent is not NULL, execute in non-blocking mode.
2055 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2056 // CheckAPsStatus() will check completion and timeout periodically.
2057 //
2058 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2059 CpuData->WaitEvent = WaitEvent;
2060 CpuData->Finished = Finished;
2061 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
2062 CpuData->TotalTime = 0;
2063
2064 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
2065
2066 //
2067 // If WaitEvent is NULL, execute in blocking mode.
2068 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2069 //
2070 Status = EFI_SUCCESS;
2071 if (WaitEvent == NULL) {
2072 do {
2073 Status = CheckThisAP (ProcessorNumber);
2074 } while (Status == EFI_NOT_READY);
2075 }
2076
2077 return Status;
2078 }
2079
2080 /**
2081 Get pointer to CPU MP Data structure from GUIDed HOB.
2082
2083 @return The pointer to CPU MP Data structure.
2084 **/
2085 CPU_MP_DATA *
2086 GetCpuMpDataFromGuidedHob (
2087 VOID
2088 )
2089 {
2090 EFI_HOB_GUID_TYPE *GuidHob;
2091 VOID *DataInHob;
2092 CPU_MP_DATA *CpuMpData;
2093
2094 CpuMpData = NULL;
2095 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
2096 if (GuidHob != NULL) {
2097 DataInHob = GET_GUID_HOB_DATA (GuidHob);
2098 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);
2099 }
2100 return CpuMpData;
2101 }
2102
2103 /**
2104 Get available system memory below 1MB by specified size.
2105
2106 @param[in] CpuMpData The pointer to CPU MP Data structure.
2107 **/
2108 VOID
2109 BackupAndPrepareWakeupBuffer(
2110 IN CPU_MP_DATA *CpuMpData
2111 )
2112 {
2113 CopyMem (
2114 (VOID *) CpuMpData->BackupBuffer,
2115 (VOID *) CpuMpData->WakeupBuffer,
2116 CpuMpData->BackupBufferSize
2117 );
2118 CopyMem (
2119 (VOID *) CpuMpData->WakeupBuffer,
2120 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
2121 CpuMpData->AddressMap.RendezvousFunnelSize
2122 );
2123 }
2124
2125 /**
2126 Restore wakeup buffer data.
2127
2128 @param[in] CpuMpData The pointer to CPU MP Data structure.
2129 **/
2130 VOID
2131 RestoreWakeupBuffer(
2132 IN CPU_MP_DATA *CpuMpData
2133 )
2134 {
2135 CopyMem (
2136 (VOID *) CpuMpData->WakeupBuffer,
2137 (VOID *) CpuMpData->BackupBuffer,
2138 CpuMpData->BackupBufferSize
2139 );
2140 }