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