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