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