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