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