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