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