]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
MpInitLib: Move the Above1Mb vector allocation to MpInitLibInitialize
[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
283ab943 958 ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh;\r
69dfa8d8
JW
959\r
960 ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory +\r
053e878b
MK
961 (UINT32)ExchangeInfo->ModeOffset -\r
962 (UINT32)CpuMpData->AddressMap.ModeTransitionOffset;\r
69dfa8d8 963 ExchangeInfo->ModeHighSegment = (UINT16)ExchangeInfo->CodeSegment;\r
7c3f2a12
JF
964}\r
965\r
6e1987f1
LE
966/**\r
967 Helper function that waits until the finished AP count reaches the specified\r
968 limit, or the specified timeout elapses (whichever comes first).\r
969\r
970 @param[in] CpuMpData Pointer to CPU MP Data.\r
971 @param[in] FinishedApLimit The number of finished APs to wait for.\r
972 @param[in] TimeLimit The number of microseconds to wait for.\r
973**/\r
974VOID\r
975TimedWaitForApFinish (\r
053e878b
MK
976 IN CPU_MP_DATA *CpuMpData,\r
977 IN UINT32 FinishedApLimit,\r
978 IN UINT32 TimeLimit\r
6e1987f1
LE
979 );\r
980\r
a6b3d753
SZ
981/**\r
982 Get available system memory below 1MB by specified size.\r
983\r
984 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
985**/\r
986VOID\r
053e878b
MK
987BackupAndPrepareWakeupBuffer (\r
988 IN CPU_MP_DATA *CpuMpData\r
a6b3d753
SZ
989 )\r
990{\r
991 CopyMem (\r
053e878b
MK
992 (VOID *)CpuMpData->BackupBuffer,\r
993 (VOID *)CpuMpData->WakeupBuffer,\r
a6b3d753
SZ
994 CpuMpData->BackupBufferSize\r
995 );\r
996 CopyMem (\r
053e878b
MK
997 (VOID *)CpuMpData->WakeupBuffer,\r
998 (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress,\r
283ab943 999 CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO)\r
a6b3d753
SZ
1000 );\r
1001}\r
1002\r
1003/**\r
1004 Restore wakeup buffer data.\r
1005\r
1006 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
1007**/\r
1008VOID\r
053e878b
MK
1009RestoreWakeupBuffer (\r
1010 IN CPU_MP_DATA *CpuMpData\r
a6b3d753
SZ
1011 )\r
1012{\r
1013 CopyMem (\r
053e878b
MK
1014 (VOID *)CpuMpData->WakeupBuffer,\r
1015 (VOID *)CpuMpData->BackupBuffer,\r
a6b3d753
SZ
1016 CpuMpData->BackupBufferSize\r
1017 );\r
1018}\r
1019\r
1020/**\r
1021 Allocate reset vector buffer.\r
1022\r
1023 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
1024**/\r
1025VOID\r
ccc26975 1026AllocateResetVectorBelow1Mb (\r
053e878b 1027 IN OUT CPU_MP_DATA *CpuMpData\r
a6b3d753
SZ
1028 )\r
1029{\r
053e878b 1030 UINTN ApResetStackSize;\r
a6b3d753 1031\r
053e878b 1032 if (CpuMpData->WakeupBuffer == (UINTN)-1) {\r
ccc26975 1033 CpuMpData->WakeupBuffer = GetWakeupBuffer (CpuMpData->BackupBufferSize);\r
053e878b 1034 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN)\r
ccc26975
RN
1035 (CpuMpData->WakeupBuffer + CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO));\r
1036 DEBUG ((\r
1037 DEBUG_INFO,\r
1038 "AP Vector: 16-bit = %p/%x, ExchangeInfo = %p/%x\n",\r
1039 CpuMpData->WakeupBuffer,\r
1040 CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO),\r
1041 CpuMpData->MpCpuExchangeInfo,\r
1042 sizeof (MP_CPU_EXCHANGE_INFO)\r
1043 ));\r
7b7508ad 1044 //\r
dbc22a17 1045 // The AP reset stack is only used by SEV-ES guests. Do not allocate it\r
06544455
TL
1046 // if SEV-ES is not enabled. An SEV-SNP guest is also considered\r
1047 // an SEV-ES guest, but uses a different method of AP startup, eliminating\r
1048 // the need for the allocation.\r
7b7508ad 1049 //\r
06544455
TL
1050 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) &&\r
1051 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp))\r
1052 {\r
dbc22a17
TL
1053 //\r
1054 // Stack location is based on ProcessorNumber, so use the total number\r
1055 // of processors for calculating the total stack area.\r
1056 //\r
1057 ApResetStackSize = (AP_RESET_STACK_SIZE *\r
1058 PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
1059\r
1060 //\r
1061 // Invoke GetWakeupBuffer a second time to allocate the stack area\r
1062 // below 1MB. The returned buffer will be page aligned and sized and\r
1063 // below the previously allocated buffer.\r
1064 //\r
1065 CpuMpData->SevEsAPResetStackStart = GetWakeupBuffer (ApResetStackSize);\r
1066\r
1067 //\r
1068 // Check to be sure that the "allocate below" behavior hasn't changed.\r
1069 // This will also catch a failed allocation, as "-1" is returned on\r
1070 // failure.\r
1071 //\r
1072 if (CpuMpData->SevEsAPResetStackStart >= CpuMpData->WakeupBuffer) {\r
1073 DEBUG ((\r
1074 DEBUG_ERROR,\r
1075 "SEV-ES AP reset stack is not below wakeup buffer\n"\r
1076 ));\r
1077\r
1078 ASSERT (FALSE);\r
1079 CpuDeadLoop ();\r
1080 }\r
1081 }\r
a6b3d753 1082 }\r
053e878b 1083\r
a6b3d753
SZ
1084 BackupAndPrepareWakeupBuffer (CpuMpData);\r
1085}\r
1086\r
1087/**\r
1088 Free AP reset vector buffer.\r
1089\r
1090 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
1091**/\r
1092VOID\r
1093FreeResetVector (\r
053e878b 1094 IN CPU_MP_DATA *CpuMpData\r
a6b3d753
SZ
1095 )\r
1096{\r
7b7508ad
TL
1097 //\r
1098 // If SEV-ES is enabled, the reset area is needed for AP parking and\r
1099 // and AP startup in the OS, so the reset area is reserved. Do not\r
1100 // perform the restore as this will overwrite memory which has data\r
1101 // needed by SEV-ES.\r
1102 //\r
06544455 1103 if (!CpuMpData->UseSevEsAPMethod) {\r
7b7508ad
TL
1104 RestoreWakeupBuffer (CpuMpData);\r
1105 }\r
1106}\r
1107\r
96f5920d
JF
1108/**\r
1109 This function will be called by BSP to wakeup AP.\r
1110\r
1111 @param[in] CpuMpData Pointer to CPU MP Data\r
1112 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
1113 FALSE: Send IPI to AP by ApicId\r
1114 @param[in] ProcessorNumber The handle number of specified processor\r
1115 @param[in] Procedure The function to be invoked by AP\r
1116 @param[in] ProcedureArgument The argument to be passed into AP function\r
cf4e79e4 1117 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.\r
96f5920d
JF
1118**/\r
1119VOID\r
1120WakeUpAP (\r
053e878b
MK
1121 IN CPU_MP_DATA *CpuMpData,\r
1122 IN BOOLEAN Broadcast,\r
1123 IN UINTN ProcessorNumber,\r
1124 IN EFI_AP_PROCEDURE Procedure OPTIONAL,\r
1125 IN VOID *ProcedureArgument OPTIONAL,\r
1126 IN BOOLEAN WakeUpDisabledAps\r
96f5920d
JF
1127 )\r
1128{\r
053e878b
MK
1129 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
1130 UINTN Index;\r
1131 CPU_AP_DATA *CpuData;\r
1132 BOOLEAN ResetVectorRequired;\r
1133 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96f5920d
JF
1134\r
1135 CpuMpData->FinishedCount = 0;\r
053e878b 1136 ResetVectorRequired = FALSE;\r
96f5920d 1137\r
58942277 1138 if (CpuMpData->WakeUpByInitSipiSipi ||\r
053e878b
MK
1139 (CpuMpData->InitFlag != ApInitDone))\r
1140 {\r
96f5920d 1141 ResetVectorRequired = TRUE;\r
ccc26975 1142 AllocateResetVectorBelow1Mb (CpuMpData);\r
7b7508ad 1143 AllocateSevEsAPMemory (CpuMpData);\r
96f5920d 1144 FillExchangeInfoData (CpuMpData);\r
ffab2442 1145 SaveLocalApicTimerSetting (CpuMpData);\r
58942277
ED
1146 }\r
1147\r
1148 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
96f5920d
JF
1149 //\r
1150 // Get AP target C-state each time when waking up AP,\r
1151 // for it maybe updated by platform again\r
1152 //\r
1153 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
1154 }\r
1155\r
1156 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
1157\r
1158 if (Broadcast) {\r
1159 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1160 if (Index != CpuMpData->BspNumber) {\r
1161 CpuData = &CpuMpData->CpuData[Index];\r
cf4e79e4
ED
1162 //\r
1163 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but\r
e23d9c3e 1164 // the AP procedure will be skipped for disabled AP because AP state\r
cf4e79e4
ED
1165 // is not CpuStateReady.\r
1166 //\r
053e878b 1167 if ((GetApState (CpuData) == CpuStateDisabled) && !WakeUpDisabledAps) {\r
cf4e79e4
ED
1168 continue;\r
1169 }\r
1170\r
053e878b
MK
1171 CpuData->ApFunction = (UINTN)Procedure;\r
1172 CpuData->ApFunctionArgument = (UINTN)ProcedureArgument;\r
96f5920d
JF
1173 SetApState (CpuData, CpuStateReady);\r
1174 if (CpuMpData->InitFlag != ApInitConfig) {\r
053e878b 1175 *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
96f5920d
JF
1176 }\r
1177 }\r
1178 }\r
053e878b 1179\r
96f5920d 1180 if (ResetVectorRequired) {\r
7b7508ad 1181 //\r
06544455 1182 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by\r
7b7508ad
TL
1183 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address\r
1184 // from the original INIT-SIPI-SIPI.\r
1185 //\r
1186 if (CpuMpData->SevEsIsEnabled) {\r
1187 SetSevEsJumpTable (ExchangeInfo->BufferStart);\r
1188 }\r
1189\r
96f5920d
JF
1190 //\r
1191 // Wakeup all APs\r
06544455
TL
1192 // Must use the INIT-SIPI-SIPI method for initial configuration in\r
1193 // order to obtain the APIC ID.\r
96f5920d 1194 //\r
06544455
TL
1195 if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag != ApInitConfig)) {\r
1196 SevSnpCreateAP (CpuMpData, -1);\r
1197 } else {\r
1198 SendInitSipiSipiAllExcludingSelf ((UINT32)ExchangeInfo->BufferStart);\r
1199 }\r
96f5920d 1200 }\r
053e878b 1201\r
c1192210 1202 if (CpuMpData->InitFlag == ApInitConfig) {\r
778832bc
LE
1203 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) {\r
1204 //\r
1205 // The AP enumeration algorithm below is suitable only when the\r
1206 // platform can tell us the *exact* boot CPU count in advance.\r
1207 //\r
1208 // The wait below finishes only when the detected AP count reaches\r
1209 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that\r
1210 // takes. If at least one AP fails to check in (meaning a platform\r
1211 // hardware bug), the detection hangs forever, by design. If the actual\r
1212 // boot CPU count in the system is higher than\r
1213 // PcdCpuBootLogicalProcessorNumber (meaning a platform\r
1214 // misconfiguration), then some APs may complete initialization after\r
1215 // the wait finishes, and cause undefined behavior.\r
1216 //\r
1217 TimedWaitForApFinish (\r
1218 CpuMpData,\r
1219 PcdGet32 (PcdCpuBootLogicalProcessorNumber) - 1,\r
1220 MAX_UINT32 // approx. 71 minutes\r
1221 );\r
1222 } else {\r
1223 //\r
1224 // The AP enumeration algorithm below is suitable for two use cases.\r
1225 //\r
1226 // (1) The check-in time for an individual AP is bounded, and APs run\r
1227 // through their initialization routines strongly concurrently. In\r
1228 // particular, the number of concurrently running APs\r
1229 // ("NumApsExecuting") is never expected to fall to zero\r
1230 // *temporarily* -- it is expected to fall to zero only when all\r
1231 // APs have checked-in.\r
1232 //\r
1233 // In this case, the platform is supposed to set\r
1234 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long\r
1235 // enough for one AP to start initialization). The timeout will be\r
1236 // reached soon, and remaining APs are collected by watching\r
1237 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero\r
1238 // mid-process, while some APs have not completed initialization,\r
1239 // the behavior is undefined.\r
1240 //\r
1241 // (2) The check-in time for an individual AP is unbounded, and/or APs\r
1242 // may complete their initializations widely spread out. In\r
1243 // particular, some APs may finish initialization before some APs\r
1244 // even start.\r
1245 //\r
1246 // In this case, the platform is supposed to set\r
1247 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP\r
1248 // enumeration will always take that long (except when the boot CPU\r
1249 // count happens to be maximal, that is,\r
1250 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to\r
1251 // check-in before the timeout, and NumApsExecuting is assumed zero\r
1252 // at timeout. APs that miss the time-out may cause undefined\r
1253 // behavior.\r
1254 //\r
1255 TimedWaitForApFinish (\r
1256 CpuMpData,\r
1257 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
1258 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
1259 );\r
0594ec41 1260\r
778832bc 1261 while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {\r
053e878b 1262 CpuPause ();\r
778832bc 1263 }\r
0594ec41 1264 }\r
c1192210 1265 } else {\r
96f5920d
JF
1266 //\r
1267 // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
1268 //\r
1269 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1270 CpuData = &CpuMpData->CpuData[Index];\r
1271 if (Index != CpuMpData->BspNumber) {\r
1272 WaitApWakeup (CpuData->StartupApSignal);\r
1273 }\r
1274 }\r
1275 }\r
1276 } else {\r
053e878b
MK
1277 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1278 CpuData->ApFunction = (UINTN)Procedure;\r
1279 CpuData->ApFunctionArgument = (UINTN)ProcedureArgument;\r
96f5920d
JF
1280 SetApState (CpuData, CpuStateReady);\r
1281 //\r
1282 // Wakeup specified AP\r
1283 //\r
1284 ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
053e878b 1285 *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
96f5920d 1286 if (ResetVectorRequired) {\r
053e878b 1287 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
7b7508ad
TL
1288\r
1289 //\r
06544455 1290 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by\r
7b7508ad
TL
1291 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address\r
1292 // from the original INIT-SIPI-SIPI.\r
1293 //\r
1294 if (CpuMpData->SevEsIsEnabled) {\r
1295 SetSevEsJumpTable (ExchangeInfo->BufferStart);\r
1296 }\r
1297\r
06544455
TL
1298 if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag != ApInitConfig)) {\r
1299 SevSnpCreateAP (CpuMpData, (INTN)ProcessorNumber);\r
1300 } else {\r
1301 SendInitSipiSipi (\r
1302 CpuInfoInHob[ProcessorNumber].ApicId,\r
1303 (UINT32)ExchangeInfo->BufferStart\r
1304 );\r
1305 }\r
96f5920d 1306 }\r
053e878b 1307\r
96f5920d
JF
1308 //\r
1309 // Wait specified AP waken up\r
1310 //\r
1311 WaitApWakeup (CpuData->StartupApSignal);\r
1312 }\r
1313\r
1314 if (ResetVectorRequired) {\r
1315 FreeResetVector (CpuMpData);\r
1316 }\r
58942277
ED
1317\r
1318 //\r
1319 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with\r
1320 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by\r
1321 // S3SmmInitDone Ppi.\r
1322 //\r
1323 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);\r
96f5920d
JF
1324}\r
1325\r
08085f08
JF
1326/**\r
1327 Calculate timeout value and return the current performance counter value.\r
1328\r
1329 Calculate the number of performance counter ticks required for a timeout.\r
1330 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
1331 as infinity.\r
1332\r
1333 @param[in] TimeoutInMicroseconds Timeout value in microseconds.\r
1334 @param[out] CurrentTime Returns the current value of the performance counter.\r
1335\r
1336 @return Expected time stamp counter for timeout.\r
1337 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
1338 as infinity.\r
1339\r
1340**/\r
1341UINT64\r
1342CalculateTimeout (\r
1343 IN UINTN TimeoutInMicroseconds,\r
1344 OUT UINT64 *CurrentTime\r
1345 )\r
1346{\r
053e878b
MK
1347 UINT64 TimeoutInSeconds;\r
1348 UINT64 TimestampCounterFreq;\r
48cfb7c0 1349\r
08085f08
JF
1350 //\r
1351 // Read the current value of the performance counter\r
1352 //\r
1353 *CurrentTime = GetPerformanceCounter ();\r
1354\r
1355 //\r
1356 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
1357 // as infinity.\r
1358 //\r
1359 if (TimeoutInMicroseconds == 0) {\r
1360 return 0;\r
1361 }\r
1362\r
1363 //\r
1364 // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
7367cc6c 1365 // in Hz.\r
48cfb7c0
ED
1366 //\r
1367 TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);\r
1368\r
08085f08 1369 //\r
48cfb7c0
ED
1370 // Check the potential overflow before calculate the number of ticks for the timeout value.\r
1371 //\r
1372 if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {\r
1373 //\r
1374 // Convert microseconds into seconds if direct multiplication overflows\r
1375 //\r
1376 TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);\r
1377 //\r
1378 // Assertion if the final tick count exceeds MAX_UINT64\r
1379 //\r
1380 ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);\r
1381 return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);\r
1382 } else {\r
1383 //\r
1384 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide\r
1385 // it by 1,000,000, to get the number of ticks for the timeout value.\r
1386 //\r
1387 return DivU64x32 (\r
1388 MultU64x64 (\r
1389 TimestampCounterFreq,\r
1390 TimeoutInMicroseconds\r
1391 ),\r
1392 1000000\r
1393 );\r
1394 }\r
08085f08
JF
1395}\r
1396\r
1397/**\r
1398 Checks whether timeout expires.\r
1399\r
1400 Check whether the number of elapsed performance counter ticks required for\r
1401 a timeout condition has been reached.\r
1402 If Timeout is zero, which means infinity, return value is always FALSE.\r
1403\r
1404 @param[in, out] PreviousTime On input, the value of the performance counter\r
1405 when it was last read.\r
1406 On output, the current value of the performance\r
1407 counter\r
1408 @param[in] TotalTime The total amount of elapsed time in performance\r
1409 counter ticks.\r
1410 @param[in] Timeout The number of performance counter ticks required\r
1411 to reach a timeout condition.\r
1412\r
1413 @retval TRUE A timeout condition has been reached.\r
1414 @retval FALSE A timeout condition has not been reached.\r
1415\r
1416**/\r
1417BOOLEAN\r
1418CheckTimeout (\r
1419 IN OUT UINT64 *PreviousTime,\r
1420 IN UINT64 *TotalTime,\r
1421 IN UINT64 Timeout\r
1422 )\r
1423{\r
1424 UINT64 Start;\r
1425 UINT64 End;\r
1426 UINT64 CurrentTime;\r
1427 INT64 Delta;\r
1428 INT64 Cycle;\r
1429\r
1430 if (Timeout == 0) {\r
1431 return FALSE;\r
1432 }\r
053e878b 1433\r
08085f08
JF
1434 GetPerformanceCounterProperties (&Start, &End);\r
1435 Cycle = End - Start;\r
1436 if (Cycle < 0) {\r
1437 Cycle = -Cycle;\r
1438 }\r
053e878b 1439\r
08085f08 1440 Cycle++;\r
053e878b
MK
1441 CurrentTime = GetPerformanceCounter ();\r
1442 Delta = (INT64)(CurrentTime - *PreviousTime);\r
08085f08
JF
1443 if (Start > End) {\r
1444 Delta = -Delta;\r
1445 }\r
053e878b 1446\r
08085f08
JF
1447 if (Delta < 0) {\r
1448 Delta += Cycle;\r
1449 }\r
053e878b
MK
1450\r
1451 *TotalTime += Delta;\r
08085f08
JF
1452 *PreviousTime = CurrentTime;\r
1453 if (*TotalTime > Timeout) {\r
1454 return TRUE;\r
1455 }\r
053e878b 1456\r
08085f08
JF
1457 return FALSE;\r
1458}\r
1459\r
6e1987f1
LE
1460/**\r
1461 Helper function that waits until the finished AP count reaches the specified\r
1462 limit, or the specified timeout elapses (whichever comes first).\r
1463\r
1464 @param[in] CpuMpData Pointer to CPU MP Data.\r
1465 @param[in] FinishedApLimit The number of finished APs to wait for.\r
1466 @param[in] TimeLimit The number of microseconds to wait for.\r
1467**/\r
1468VOID\r
1469TimedWaitForApFinish (\r
053e878b
MK
1470 IN CPU_MP_DATA *CpuMpData,\r
1471 IN UINT32 FinishedApLimit,\r
1472 IN UINT32 TimeLimit\r
6e1987f1
LE
1473 )\r
1474{\r
1475 //\r
1476 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
1477 // "infinity", so check for (TimeLimit == 0) explicitly.\r
1478 //\r
1479 if (TimeLimit == 0) {\r
1480 return;\r
1481 }\r
1482\r
053e878b 1483 CpuMpData->TotalTime = 0;\r
6e1987f1
LE
1484 CpuMpData->ExpectedTime = CalculateTimeout (\r
1485 TimeLimit,\r
1486 &CpuMpData->CurrentTime\r
1487 );\r
1488 while (CpuMpData->FinishedCount < FinishedApLimit &&\r
1489 !CheckTimeout (\r
1490 &CpuMpData->CurrentTime,\r
1491 &CpuMpData->TotalTime,\r
1492 CpuMpData->ExpectedTime\r
053e878b
MK
1493 ))\r
1494 {\r
6e1987f1
LE
1495 CpuPause ();\r
1496 }\r
1497\r
1498 if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
1499 DEBUG ((\r
1500 DEBUG_VERBOSE,\r
1501 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
1502 __FUNCTION__,\r
1503 FinishedApLimit,\r
1504 DivU64x64Remainder (\r
1505 MultU64x32 (CpuMpData->TotalTime, 1000000),\r
1506 GetPerformanceCounterProperties (NULL, NULL),\r
1507 NULL\r
1508 )\r
1509 ));\r
1510 }\r
1511}\r
1512\r
08085f08
JF
1513/**\r
1514 Reset an AP to Idle state.\r
1515\r
1516 Any task being executed by the AP will be aborted and the AP\r
1517 will be waiting for a new task in Wait-For-SIPI state.\r
1518\r
1519 @param[in] ProcessorNumber The handle number of processor.\r
1520**/\r
1521VOID\r
1522ResetProcessorToIdleState (\r
053e878b 1523 IN UINTN ProcessorNumber\r
08085f08
JF
1524 )\r
1525{\r
053e878b 1526 CPU_MP_DATA *CpuMpData;\r
08085f08
JF
1527\r
1528 CpuMpData = GetCpuMpData ();\r
1529\r
cb33bde4 1530 CpuMpData->InitFlag = ApInitReconfig;\r
cf4e79e4 1531 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL, TRUE);\r
cb33bde4
JF
1532 while (CpuMpData->FinishedCount < 1) {\r
1533 CpuPause ();\r
1534 }\r
053e878b 1535\r
cb33bde4 1536 CpuMpData->InitFlag = ApInitDone;\r
08085f08
JF
1537\r
1538 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1539}\r
1540\r
1541/**\r
1542 Searches for the next waiting AP.\r
1543\r
1544 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
1545\r
1546 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
1547\r
1548 @retval EFI_SUCCESS The next waiting AP has been found.\r
1549 @retval EFI_NOT_FOUND No waiting AP exists.\r
1550\r
1551**/\r
1552EFI_STATUS\r
1553GetNextWaitingProcessorNumber (\r
053e878b 1554 OUT UINTN *NextProcessorNumber\r
08085f08
JF
1555 )\r
1556{\r
053e878b
MK
1557 UINTN ProcessorNumber;\r
1558 CPU_MP_DATA *CpuMpData;\r
08085f08
JF
1559\r
1560 CpuMpData = GetCpuMpData ();\r
1561\r
1562 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1563 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1564 *NextProcessorNumber = ProcessorNumber;\r
1565 return EFI_SUCCESS;\r
1566 }\r
1567 }\r
1568\r
1569 return EFI_NOT_FOUND;\r
1570}\r
1571\r
1572/** Checks status of specified AP.\r
1573\r
1574 This function checks whether the specified AP has finished the task assigned\r
1575 by StartupThisAP(), and whether timeout expires.\r
1576\r
1577 @param[in] ProcessorNumber The handle number of processor.\r
1578\r
1579 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
1580 @retval EFI_TIMEOUT The timeout expires.\r
1581 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
1582**/\r
1583EFI_STATUS\r
1584CheckThisAP (\r
053e878b 1585 IN UINTN ProcessorNumber\r
08085f08
JF
1586 )\r
1587{\r
053e878b
MK
1588 CPU_MP_DATA *CpuMpData;\r
1589 CPU_AP_DATA *CpuData;\r
08085f08
JF
1590\r
1591 CpuMpData = GetCpuMpData ();\r
1592 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1593\r
1594 //\r
2a5997f8 1595 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.\r
08085f08 1596 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
2a5997f8 1597 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.\r
08085f08
JF
1598 //\r
1599 //\r
1600 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.\r
1601 //\r
053e878b 1602 if (GetApState (CpuData) == CpuStateFinished) {\r
08085f08
JF
1603 if (CpuData->Finished != NULL) {\r
1604 *(CpuData->Finished) = TRUE;\r
1605 }\r
053e878b 1606\r
e048ce88 1607 SetApState (CpuData, CpuStateIdle);\r
08085f08
JF
1608 return EFI_SUCCESS;\r
1609 } else {\r
1610 //\r
1611 // If timeout expires for StartupThisAP(), report timeout.\r
1612 //\r
1613 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
1614 if (CpuData->Finished != NULL) {\r
1615 *(CpuData->Finished) = FALSE;\r
1616 }\r
053e878b 1617\r
08085f08
JF
1618 //\r
1619 // Reset failed AP to idle state\r
1620 //\r
1621 ResetProcessorToIdleState (ProcessorNumber);\r
1622\r
1623 return EFI_TIMEOUT;\r
1624 }\r
1625 }\r
053e878b 1626\r
08085f08
JF
1627 return EFI_NOT_READY;\r
1628}\r
1629\r
1630/**\r
1631 Checks status of all APs.\r
1632\r
1633 This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
1634 and whether timeout expires.\r
1635\r
1636 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
1637 @retval EFI_TIMEOUT The timeout expires.\r
1638 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
1639**/\r
1640EFI_STATUS\r
1641CheckAllAPs (\r
1642 VOID\r
1643 )\r
1644{\r
053e878b
MK
1645 UINTN ProcessorNumber;\r
1646 UINTN NextProcessorNumber;\r
1647 UINTN ListIndex;\r
1648 EFI_STATUS Status;\r
1649 CPU_MP_DATA *CpuMpData;\r
1650 CPU_AP_DATA *CpuData;\r
08085f08
JF
1651\r
1652 CpuMpData = GetCpuMpData ();\r
1653\r
1654 NextProcessorNumber = 0;\r
1655\r
1656 //\r
1657 // Go through all APs that are responsible for the StartupAllAPs().\r
1658 //\r
1659 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1660 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1661 continue;\r
1662 }\r
1663\r
1664 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1665 //\r
2a5997f8 1666 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.\r
08085f08 1667 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
2a5997f8 1668 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.\r
08085f08 1669 //\r
053e878b
MK
1670 if (GetApState (CpuData) == CpuStateFinished) {\r
1671 CpuMpData->RunningCount--;\r
08085f08 1672 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
053e878b 1673 SetApState (CpuData, CpuStateIdle);\r
08085f08
JF
1674\r
1675 //\r
1676 // If in Single Thread mode, then search for the next waiting AP for execution.\r
1677 //\r
1678 if (CpuMpData->SingleThread) {\r
1679 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
1680\r
1681 if (!EFI_ERROR (Status)) {\r
1682 WakeUpAP (\r
1683 CpuMpData,\r
1684 FALSE,\r
053e878b 1685 (UINT32)NextProcessorNumber,\r
08085f08 1686 CpuMpData->Procedure,\r
cf4e79e4
ED
1687 CpuMpData->ProcArguments,\r
1688 TRUE\r
08085f08 1689 );\r
053e878b 1690 }\r
08085f08
JF
1691 }\r
1692 }\r
1693 }\r
1694\r
1695 //\r
1696 // If all APs finish, return EFI_SUCCESS.\r
1697 //\r
2da3e96c 1698 if (CpuMpData->RunningCount == 0) {\r
08085f08
JF
1699 return EFI_SUCCESS;\r
1700 }\r
1701\r
1702 //\r
1703 // If timeout expires, report timeout.\r
1704 //\r
1705 if (CheckTimeout (\r
053e878b
MK
1706 &CpuMpData->CurrentTime,\r
1707 &CpuMpData->TotalTime,\r
1708 CpuMpData->ExpectedTime\r
1709 )\r
1710 )\r
1711 {\r
08085f08
JF
1712 //\r
1713 // If FailedCpuList is not NULL, record all failed APs in it.\r
1714 //\r
1715 if (CpuMpData->FailedCpuList != NULL) {\r
1716 *CpuMpData->FailedCpuList =\r
053e878b 1717 AllocatePool ((CpuMpData->RunningCount + 1) * sizeof (UINTN));\r
08085f08
JF
1718 ASSERT (*CpuMpData->FailedCpuList != NULL);\r
1719 }\r
053e878b 1720\r
08085f08
JF
1721 ListIndex = 0;\r
1722\r
1723 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1724 //\r
1725 // Check whether this processor is responsible for StartupAllAPs().\r
1726 //\r
1727 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1728 //\r
1729 // Reset failed APs to idle state\r
1730 //\r
1731 ResetProcessorToIdleState (ProcessorNumber);\r
1732 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
1733 if (CpuMpData->FailedCpuList != NULL) {\r
1734 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;\r
1735 }\r
1736 }\r
1737 }\r
053e878b 1738\r
08085f08
JF
1739 if (CpuMpData->FailedCpuList != NULL) {\r
1740 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
1741 }\r
053e878b 1742\r
08085f08
JF
1743 return EFI_TIMEOUT;\r
1744 }\r
053e878b 1745\r
08085f08
JF
1746 return EFI_NOT_READY;\r
1747}\r
1748\r
3e8ad6bd
JF
1749/**\r
1750 MP Initialize Library initialization.\r
1751\r
1752 This service will allocate AP reset vector and wakeup all APs to do APs\r
1753 initialization.\r
1754\r
1755 This service must be invoked before all other MP Initialize Library\r
1756 service are invoked.\r
1757\r
1758 @retval EFI_SUCCESS MP initialization succeeds.\r
1759 @retval Others MP initialization fails.\r
1760\r
1761**/\r
1762EFI_STATUS\r
1763EFIAPI\r
1764MpInitLibInitialize (\r
1765 VOID\r
1766 )\r
1767{\r
6a2ee2bb
JF
1768 CPU_MP_DATA *OldCpuMpData;\r
1769 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e59f8f6b
JF
1770 UINT32 MaxLogicalProcessorNumber;\r
1771 UINT32 ApStackSize;\r
f7f85d83 1772 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
c563077a 1773 CPU_VOLATILE_REGISTERS VolatileRegisters;\r
e59f8f6b 1774 UINTN BufferSize;\r
9ebcf0f4 1775 UINT32 MonitorFilterSize;\r
e59f8f6b
JF
1776 VOID *MpBuffer;\r
1777 UINTN Buffer;\r
1778 CPU_MP_DATA *CpuMpData;\r
9ebcf0f4 1779 UINT8 ApLoopMode;\r
e59f8f6b 1780 UINT8 *MonitorBuffer;\r
03a1a925 1781 UINTN Index;\r
283ab943 1782 UINTN ApResetVectorSizeBelow1Mb;\r
ccc26975 1783 UINTN ApResetVectorSizeAbove1Mb;\r
e59f8f6b 1784 UINTN BackupBufferAddr;\r
c563077a 1785 UINTN ApIdtBase;\r
6a2ee2bb
JF
1786\r
1787 OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
1788 if (OldCpuMpData == NULL) {\r
053e878b 1789 MaxLogicalProcessorNumber = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
6a2ee2bb
JF
1790 } else {\r
1791 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
1792 }\r
053e878b 1793\r
14e8137c 1794 ASSERT (MaxLogicalProcessorNumber != 0);\r
f7f85d83
JF
1795\r
1796 AsmGetAddressMap (&AddressMap);\r
ccc26975
RN
1797 GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, &ApResetVectorSizeAbove1Mb);\r
1798 ApStackSize = PcdGet32 (PcdCpuApStackSize);\r
1799 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
9ebcf0f4 1800\r
c563077a 1801 //\r
e09b6b59 1802 // Save BSP's Control registers for APs.\r
c563077a
RN
1803 //\r
1804 SaveVolatileRegisters (&VolatileRegisters);\r
1805\r
e59f8f6b
JF
1806 BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
1807 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
283ab943 1808 BufferSize += ApResetVectorSizeBelow1Mb;\r
c563077a
RN
1809 BufferSize = ALIGN_VALUE (BufferSize, 8);\r
1810 BufferSize += VolatileRegisters.Idtr.Limit + 1;\r
1811 BufferSize += sizeof (CPU_MP_DATA);\r
e59f8f6b
JF
1812 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
1813 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1814 ASSERT (MpBuffer != NULL);\r
1815 ZeroMem (MpBuffer, BufferSize);\r
053e878b 1816 Buffer = (UINTN)MpBuffer;\r
e59f8f6b 1817\r
c563077a
RN
1818 //\r
1819 // The layout of the Buffer is as below:\r
1820 //\r
1821 // +--------------------+ <-- Buffer\r
1822 // AP Stacks (N)\r
1823 // +--------------------+ <-- MonitorBuffer\r
1824 // AP Monitor Filters (N)\r
1825 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)\r
1826 // Backup Buffer\r
1827 // +--------------------+\r
1828 // Padding\r
1829 // +--------------------+ <-- ApIdtBase (8-byte boundary)\r
1830 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.\r
1831 // +--------------------+ <-- CpuMpData\r
1832 // CPU_MP_DATA\r
1833 // +--------------------+ <-- CpuMpData->CpuData\r
1834 // CPU_AP_DATA (N)\r
1835 // +--------------------+ <-- CpuMpData->CpuInfoInHob\r
1836 // CPU_INFO_IN_HOB (N)\r
1837 // +--------------------+\r
1838 //\r
053e878b
MK
1839 MonitorBuffer = (UINT8 *)(Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
1840 BackupBufferAddr = (UINTN)MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
283ab943 1841 ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSizeBelow1Mb, 8);\r
053e878b 1842 CpuMpData = (CPU_MP_DATA *)(ApIdtBase + VolatileRegisters.Idtr.Limit + 1);\r
e59f8f6b
JF
1843 CpuMpData->Buffer = Buffer;\r
1844 CpuMpData->CpuApStackSize = ApStackSize;\r
1845 CpuMpData->BackupBuffer = BackupBufferAddr;\r
283ab943 1846 CpuMpData->BackupBufferSize = ApResetVectorSizeBelow1Mb;\r
053e878b 1847 CpuMpData->WakeupBuffer = (UINTN)-1;\r
e59f8f6b
JF
1848 CpuMpData->CpuCount = 1;\r
1849 CpuMpData->BspNumber = 0;\r
1850 CpuMpData->WaitEvent = NULL;\r
41be0da5 1851 CpuMpData->SwitchBspFlag = FALSE;\r
053e878b
MK
1852 CpuMpData->CpuData = (CPU_AP_DATA *)(CpuMpData + 1);\r
1853 CpuMpData->CpuInfoInHob = (UINT64)(UINTN)(CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
1854 InitializeSpinLock (&CpuMpData->MpLock);\r
06544455
TL
1855 CpuMpData->SevEsIsEnabled = ConfidentialComputingGuestHas (CCAttrAmdSevEs);\r
1856 CpuMpData->SevSnpIsEnabled = ConfidentialComputingGuestHas (CCAttrAmdSevSnp);\r
1857 CpuMpData->SevEsAPBuffer = (UINTN)-1;\r
1858 CpuMpData->GhcbBase = PcdGet64 (PcdGhcbBase);\r
1859 CpuMpData->UseSevEsAPMethod = CpuMpData->SevEsIsEnabled && !CpuMpData->SevSnpIsEnabled;\r
1860\r
1861 if (CpuMpData->SevSnpIsEnabled) {\r
1862 ASSERT ((PcdGet64 (PcdGhcbHypervisorFeatures) & GHCB_HV_FEATURES_SNP_AP_CREATE) == GHCB_HV_FEATURES_SNP_AP_CREATE);\r
1863 }\r
c563077a
RN
1864\r
1865 //\r
1866 // Make sure no memory usage outside of the allocated buffer.\r
e59f8f6b 1867 //\r
053e878b
MK
1868 ASSERT (\r
1869 (CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) ==\r
1870 Buffer + BufferSize\r
1871 );\r
c563077a
RN
1872\r
1873 //\r
1874 // Duplicate BSP's IDT to APs.\r
1875 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1\r
68cb9330 1876 //\r
c563077a
RN
1877 CopyMem ((VOID *)ApIdtBase, (VOID *)VolatileRegisters.Idtr.Base, VolatileRegisters.Idtr.Limit + 1);\r
1878 VolatileRegisters.Idtr.Base = ApIdtBase;\r
e09b6b59
JW
1879 //\r
1880 // Don't pass BSP's TR to APs to avoid AP init failure.\r
1881 //\r
1882 VolatileRegisters.Tr = 0;\r
c563077a 1883 CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &VolatileRegisters, sizeof (VolatileRegisters));\r
68cb9330 1884 //\r
03a1a925
JF
1885 // Set BSP basic information\r
1886 //\r
f2655dcf 1887 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSize);\r
03a1a925 1888 //\r
e59f8f6b
JF
1889 // Save assembly code information\r
1890 //\r
1891 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
1892 //\r
1893 // Finally set AP loop mode\r
1894 //\r
1895 CpuMpData->ApLoopMode = ApLoopMode;\r
1896 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
58942277
ED
1897\r
1898 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);\r
1899\r
e59f8f6b 1900 //\r
03a1a925
JF
1901 // Set up APs wakeup signal buffer\r
1902 //\r
1903 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
1904 CpuMpData->CpuData[Index].StartupApSignal =\r
1905 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
1906 }\r
053e878b 1907\r
ccc26975
RN
1908 //\r
1909 // Copy all 32-bit code and 64-bit code into memory with type of\r
1910 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.\r
1911 //\r
1912 CpuMpData->WakeupBufferHigh = AllocateCodeBuffer (ApResetVectorSizeAbove1Mb);\r
1913 CopyMem (\r
1914 (VOID *)CpuMpData->WakeupBufferHigh,\r
1915 CpuMpData->AddressMap.RendezvousFunnelAddress +\r
1916 CpuMpData->AddressMap.ModeTransitionOffset,\r
1917 ApResetVectorSizeAbove1Mb\r
1918 );\r
1919 DEBUG ((DEBUG_INFO, "AP Vector: non-16-bit = %p/%x\n", CpuMpData->WakeupBufferHigh, ApResetVectorSizeAbove1Mb));\r
1920\r
94f63c76 1921 //\r
9d64a9fd
JF
1922 // Enable the local APIC for Virtual Wire Mode.\r
1923 //\r
1924 ProgramVirtualWireMode ();\r
e59f8f6b 1925\r
6a2ee2bb 1926 if (OldCpuMpData == NULL) {\r
14e8137c
JF
1927 if (MaxLogicalProcessorNumber > 1) {\r
1928 //\r
1929 // Wakeup all APs and calculate the processor count in system\r
1930 //\r
1931 CollectProcessorCount (CpuMpData);\r
1932 }\r
6a2ee2bb
JF
1933 } else {\r
1934 //\r
1935 // APs have been wakeup before, just get the CPU Information\r
1936 // from HOB\r
1937 //\r
7b7508ad 1938 OldCpuMpData->NewCpuMpData = CpuMpData;\r
053e878b
MK
1939 CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
1940 CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
1941 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
1942 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
6a2ee2bb 1943 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
053e878b
MK
1944 InitializeSpinLock (&CpuMpData->CpuData[Index].ApLock);\r
1945 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0) ? TRUE : FALSE;\r
6a2ee2bb 1946 CpuMpData->CpuData[Index].ApFunction = 0;\r
6a2ee2bb 1947 }\r
d786a172
HW
1948 }\r
1949\r
348a34d9
HW
1950 if (!GetMicrocodePatchInfoFromHob (\r
1951 &CpuMpData->MicrocodePatchAddress,\r
1952 &CpuMpData->MicrocodePatchRegionSize\r
053e878b
MK
1953 ))\r
1954 {\r
348a34d9
HW
1955 //\r
1956 // The microcode patch information cache HOB does not exist, which means\r
1957 // the microcode patches data has not been loaded into memory yet\r
1958 //\r
1959 ShadowMicrocodeUpdatePatch (CpuMpData);\r
1960 }\r
1961\r
d786a172
HW
1962 //\r
1963 // Detect and apply Microcode on BSP\r
1964 //\r
e1ed5573 1965 MicrocodeDetect (CpuMpData, CpuMpData->BspNumber);\r
d786a172
HW
1966 //\r
1967 // Store BSP's MTRR setting\r
1968 //\r
1969 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
1970\r
1971 //\r
1972 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)\r
1973 //\r
1974 if (CpuMpData->CpuCount > 1) {\r
f07fb43b
ED
1975 if (OldCpuMpData != NULL) {\r
1976 //\r
1977 // Only needs to use this flag for DXE phase to update the wake up\r
1978 // buffer. Wakeup buffer allocated in PEI phase is no longer valid\r
1979 // in DXE.\r
1980 //\r
1981 CpuMpData->InitFlag = ApInitReconfig;\r
1982 }\r
053e878b 1983\r
d786a172 1984 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE);\r
18fcb375
HW
1985 //\r
1986 // Wait for all APs finished initialization\r
1987 //\r
d786a172
HW
1988 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
1989 CpuPause ();\r
1990 }\r
053e878b 1991\r
f07fb43b
ED
1992 if (OldCpuMpData != NULL) {\r
1993 CpuMpData->InitFlag = ApInitDone;\r
1994 }\r
053e878b 1995\r
d786a172
HW
1996 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1997 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
6a2ee2bb
JF
1998 }\r
1999 }\r
93ca4c0f 2000\r
030ba309
RN
2001 //\r
2002 // Dump the microcode revision for each core.\r
2003 //\r
7c2a6033 2004 DEBUG_CODE_BEGIN ();\r
053e878b
MK
2005 UINT32 ThreadId;\r
2006 UINT32 ExpectedMicrocodeRevision;\r
2007\r
2008 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
2009 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
2010 GetProcessorLocationByApicId (CpuInfoInHob[Index].InitialApicId, NULL, NULL, &ThreadId);\r
2011 if (ThreadId == 0) {\r
2012 //\r
2013 // MicrocodeDetect() loads microcode in first thread of each core, so,\r
2014 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.\r
2015 //\r
2016 ExpectedMicrocodeRevision = 0;\r
2017 if (CpuMpData->CpuData[Index].MicrocodeEntryAddr != 0) {\r
2018 ExpectedMicrocodeRevision = ((CPU_MICROCODE_HEADER *)(UINTN)CpuMpData->CpuData[Index].MicrocodeEntryAddr)->UpdateRevision;\r
030ba309 2019 }\r
053e878b
MK
2020\r
2021 DEBUG ((\r
2022 DEBUG_INFO,\r
2023 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",\r
2024 Index,\r
2025 CpuMpData->CpuData[Index].MicrocodeRevision,\r
2026 ExpectedMicrocodeRevision\r
2027 ));\r
030ba309 2028 }\r
053e878b
MK
2029 }\r
2030\r
7c2a6033 2031 DEBUG_CODE_END ();\r
93ca4c0f
JF
2032 //\r
2033 // Initialize global data for MP support\r
2034 //\r
2035 InitMpGlobalData (CpuMpData);\r
2036\r
f7f85d83 2037 return EFI_SUCCESS;\r
3e8ad6bd
JF
2038}\r
2039\r
2040/**\r
2041 Gets detailed MP-related information on the requested processor at the\r
2042 instant this call is made. This service may only be called from the BSP.\r
2043\r
2044 @param[in] ProcessorNumber The handle number of processor.\r
2045 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
2046 the requested processor is deposited.\r
2047 @param[out] HealthData Return processor health data.\r
2048\r
2049 @retval EFI_SUCCESS Processor information was returned.\r
2050 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
2051 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
2052 @retval EFI_NOT_FOUND The processor with the handle specified by\r
2053 ProcessorNumber does not exist in the platform.\r
2054 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
2055\r
2056**/\r
2057EFI_STATUS\r
2058EFIAPI\r
2059MpInitLibGetProcessorInfo (\r
2060 IN UINTN ProcessorNumber,\r
2061 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,\r
2062 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL\r
2063 )\r
2064{\r
053e878b
MK
2065 CPU_MP_DATA *CpuMpData;\r
2066 UINTN CallerNumber;\r
2067 CPU_INFO_IN_HOB *CpuInfoInHob;\r
2068 UINTN OriginalProcessorNumber;\r
ad52f25e 2069\r
053e878b
MK
2070 CpuMpData = GetCpuMpData ();\r
2071 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
ad52f25e 2072\r
9099dcbd
RN
2073 //\r
2074 // Lower 24 bits contains the actual processor number.\r
2075 //\r
2076 OriginalProcessorNumber = ProcessorNumber;\r
053e878b 2077 ProcessorNumber &= BIT24 - 1;\r
9099dcbd 2078\r
ad52f25e
JF
2079 //\r
2080 // Check whether caller processor is BSP\r
2081 //\r
2082 MpInitLibWhoAmI (&CallerNumber);\r
2083 if (CallerNumber != CpuMpData->BspNumber) {\r
2084 return EFI_DEVICE_ERROR;\r
2085 }\r
2086\r
2087 if (ProcessorInfoBuffer == NULL) {\r
2088 return EFI_INVALID_PARAMETER;\r
2089 }\r
2090\r
2091 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2092 return EFI_NOT_FOUND;\r
2093 }\r
2094\r
053e878b 2095 ProcessorInfoBuffer->ProcessorId = (UINT64)CpuInfoInHob[ProcessorNumber].ApicId;\r
ad52f25e
JF
2096 ProcessorInfoBuffer->StatusFlag = 0;\r
2097 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2098 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
2099 }\r
053e878b 2100\r
ad52f25e
JF
2101 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
2102 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
2103 }\r
053e878b 2104\r
ad52f25e
JF
2105 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
2106 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
2107 } else {\r
2108 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
2109 }\r
2110\r
2111 //\r
2112 // Get processor location information\r
2113 //\r
262128e5 2114 GetProcessorLocationByApicId (\r
31a1e4da 2115 CpuInfoInHob[ProcessorNumber].ApicId,\r
73152f19
LD
2116 &ProcessorInfoBuffer->Location.Package,\r
2117 &ProcessorInfoBuffer->Location.Core,\r
2118 &ProcessorInfoBuffer->Location.Thread\r
2119 );\r
ad52f25e 2120\r
9099dcbd
RN
2121 if ((OriginalProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) {\r
2122 GetProcessorLocation2ByApicId (\r
2123 CpuInfoInHob[ProcessorNumber].ApicId,\r
2124 &ProcessorInfoBuffer->ExtendedInformation.Location2.Package,\r
2125 &ProcessorInfoBuffer->ExtendedInformation.Location2.Die,\r
2126 &ProcessorInfoBuffer->ExtendedInformation.Location2.Tile,\r
2127 &ProcessorInfoBuffer->ExtendedInformation.Location2.Module,\r
2128 &ProcessorInfoBuffer->ExtendedInformation.Location2.Core,\r
2129 &ProcessorInfoBuffer->ExtendedInformation.Location2.Thread\r
2130 );\r
2131 }\r
2132\r
ad52f25e 2133 if (HealthData != NULL) {\r
31a1e4da 2134 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
ad52f25e
JF
2135 }\r
2136\r
2137 return EFI_SUCCESS;\r
3e8ad6bd 2138}\r
ad52f25e 2139\r
41be0da5
JF
2140/**\r
2141 Worker function to switch the requested AP to be the BSP from that point onward.\r
2142\r
2143 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
2144 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
2145 enabled AP. Otherwise, it will be disabled.\r
2146\r
2147 @retval EFI_SUCCESS BSP successfully switched.\r
7367cc6c 2148 @retval others Failed to switch BSP.\r
41be0da5
JF
2149\r
2150**/\r
2151EFI_STATUS\r
2152SwitchBSPWorker (\r
053e878b
MK
2153 IN UINTN ProcessorNumber,\r
2154 IN BOOLEAN EnableOldBSP\r
41be0da5
JF
2155 )\r
2156{\r
2157 CPU_MP_DATA *CpuMpData;\r
2158 UINTN CallerNumber;\r
2159 CPU_STATE State;\r
2160 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
a8d75a18 2161 BOOLEAN OldInterruptState;\r
26b43433 2162 BOOLEAN OldTimerInterruptState;\r
a8d75a18 2163\r
26b43433
JF
2164 //\r
2165 // Save and Disable Local APIC timer interrupt\r
2166 //\r
2167 OldTimerInterruptState = GetApicTimerInterruptState ();\r
2168 DisableApicTimerInterrupt ();\r
a8d75a18
JF
2169 //\r
2170 // Before send both BSP and AP to a procedure to exchange their roles,\r
2171 // interrupt must be disabled. This is because during the exchange role\r
2172 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
2173 // be corrupted, since interrupt return address will be pushed to stack\r
2174 // by hardware.\r
2175 //\r
2176 OldInterruptState = SaveAndDisableInterrupts ();\r
2177\r
2178 //\r
2179 // Mask LINT0 & LINT1 for the old BSP\r
2180 //\r
2181 DisableLvtInterrupts ();\r
41be0da5
JF
2182\r
2183 CpuMpData = GetCpuMpData ();\r
2184\r
2185 //\r
2186 // Check whether caller processor is BSP\r
2187 //\r
2188 MpInitLibWhoAmI (&CallerNumber);\r
2189 if (CallerNumber != CpuMpData->BspNumber) {\r
5e72dacc 2190 return EFI_DEVICE_ERROR;\r
41be0da5
JF
2191 }\r
2192\r
2193 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2194 return EFI_NOT_FOUND;\r
2195 }\r
2196\r
2197 //\r
2198 // Check whether specified AP is disabled\r
2199 //\r
2200 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
2201 if (State == CpuStateDisabled) {\r
2202 return EFI_INVALID_PARAMETER;\r
2203 }\r
2204\r
2205 //\r
2206 // Check whether ProcessorNumber specifies the current BSP\r
2207 //\r
2208 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2209 return EFI_INVALID_PARAMETER;\r
2210 }\r
2211\r
2212 //\r
2213 // Check whether specified AP is busy\r
2214 //\r
2215 if (State == CpuStateBusy) {\r
2216 return EFI_NOT_READY;\r
2217 }\r
2218\r
2219 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
2220 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
2221 CpuMpData->SwitchBspFlag = TRUE;\r
b3775af2 2222 CpuMpData->NewBspNumber = ProcessorNumber;\r
41be0da5
JF
2223\r
2224 //\r
2225 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
2226 //\r
053e878b 2227 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
41be0da5
JF
2228 ApicBaseMsr.Bits.BSP = 0;\r
2229 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
2230\r
2231 //\r
2232 // Need to wakeUp AP (future BSP).\r
2233 //\r
cf4e79e4 2234 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData, TRUE);\r
41be0da5
JF
2235\r
2236 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
2237\r
2238 //\r
2239 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
2240 //\r
053e878b 2241 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
41be0da5
JF
2242 ApicBaseMsr.Bits.BSP = 1;\r
2243 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
9c6961d5 2244 ProgramVirtualWireMode ();\r
41be0da5
JF
2245\r
2246 //\r
2247 // Wait for old BSP finished AP task\r
2248 //\r
e048ce88 2249 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
41be0da5
JF
2250 CpuPause ();\r
2251 }\r
2252\r
2253 CpuMpData->SwitchBspFlag = FALSE;\r
2254 //\r
2255 // Set old BSP enable state\r
2256 //\r
2257 if (!EnableOldBSP) {\r
2258 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
af8ba51a
JF
2259 } else {\r
2260 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);\r
41be0da5 2261 }\r
053e878b 2262\r
41be0da5
JF
2263 //\r
2264 // Save new BSP number\r
2265 //\r
053e878b 2266 CpuMpData->BspNumber = (UINT32)ProcessorNumber;\r
41be0da5 2267\r
a8d75a18
JF
2268 //\r
2269 // Restore interrupt state.\r
2270 //\r
2271 SetInterruptState (OldInterruptState);\r
2272\r
26b43433
JF
2273 if (OldTimerInterruptState) {\r
2274 EnableApicTimerInterrupt ();\r
2275 }\r
a8d75a18 2276\r
41be0da5
JF
2277 return EFI_SUCCESS;\r
2278}\r
ad52f25e 2279\r
e37109bc
JF
2280/**\r
2281 Worker function to let the caller enable or disable an AP from this point onward.\r
2282 This service may only be called from the BSP.\r
2283\r
2284 @param[in] ProcessorNumber The handle number of AP.\r
2285 @param[in] EnableAP Specifies the new state for the processor for\r
2286 enabled, FALSE for disabled.\r
2287 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
2288 the new health status of the AP.\r
2289\r
2290 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
2291 @retval others Failed to Enable/Disable AP.\r
2292\r
2293**/\r
2294EFI_STATUS\r
2295EnableDisableApWorker (\r
053e878b
MK
2296 IN UINTN ProcessorNumber,\r
2297 IN BOOLEAN EnableAP,\r
2298 IN UINT32 *HealthFlag OPTIONAL\r
e37109bc
JF
2299 )\r
2300{\r
053e878b
MK
2301 CPU_MP_DATA *CpuMpData;\r
2302 UINTN CallerNumber;\r
e37109bc
JF
2303\r
2304 CpuMpData = GetCpuMpData ();\r
2305\r
2306 //\r
2307 // Check whether caller processor is BSP\r
2308 //\r
2309 MpInitLibWhoAmI (&CallerNumber);\r
2310 if (CallerNumber != CpuMpData->BspNumber) {\r
2311 return EFI_DEVICE_ERROR;\r
2312 }\r
2313\r
2314 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2315 return EFI_INVALID_PARAMETER;\r
2316 }\r
2317\r
2318 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2319 return EFI_NOT_FOUND;\r
2320 }\r
2321\r
2322 if (!EnableAP) {\r
2323 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
2324 } else {\r
d5fdae96 2325 ResetProcessorToIdleState (ProcessorNumber);\r
e37109bc
JF
2326 }\r
2327\r
2328 if (HealthFlag != NULL) {\r
2329 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
053e878b 2330 (BOOLEAN)((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
e37109bc
JF
2331 }\r
2332\r
2333 return EFI_SUCCESS;\r
2334}\r
2335\r
3e8ad6bd
JF
2336/**\r
2337 This return the handle number for the calling processor. This service may be\r
2338 called from the BSP and APs.\r
2339\r
2340 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
2341 The range is from 0 to the total number of\r
2342 logical processors minus 1. The total number of\r
2343 logical processors can be retrieved by\r
2344 MpInitLibGetNumberOfProcessors().\r
2345\r
2346 @retval EFI_SUCCESS The current processor handle number was returned\r
2347 in ProcessorNumber.\r
2348 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
2349 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
2350\r
2351**/\r
2352EFI_STATUS\r
2353EFIAPI\r
2354MpInitLibWhoAmI (\r
053e878b 2355 OUT UINTN *ProcessorNumber\r
3e8ad6bd
JF
2356 )\r
2357{\r
053e878b 2358 CPU_MP_DATA *CpuMpData;\r
5c9e0997
JF
2359\r
2360 if (ProcessorNumber == NULL) {\r
2361 return EFI_INVALID_PARAMETER;\r
2362 }\r
2363\r
2364 CpuMpData = GetCpuMpData ();\r
2365\r
2366 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 2367}\r
809213a6 2368\r
3e8ad6bd
JF
2369/**\r
2370 Retrieves the number of logical processor in the platform and the number of\r
2371 those logical processors that are enabled on this boot. This service may only\r
2372 be called from the BSP.\r
2373\r
2374 @param[out] NumberOfProcessors Pointer to the total number of logical\r
2375 processors in the system, including the BSP\r
2376 and disabled APs.\r
2377 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
2378 processors that exist in system, including\r
2379 the BSP.\r
2380\r
2381 @retval EFI_SUCCESS The number of logical processors and enabled\r
2382 logical processors was retrieved.\r
2383 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
2384 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
2385 is NULL.\r
2386 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
2387\r
2388**/\r
2389EFI_STATUS\r
2390EFIAPI\r
2391MpInitLibGetNumberOfProcessors (\r
053e878b
MK
2392 OUT UINTN *NumberOfProcessors OPTIONAL,\r
2393 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
3e8ad6bd
JF
2394 )\r
2395{\r
053e878b
MK
2396 CPU_MP_DATA *CpuMpData;\r
2397 UINTN CallerNumber;\r
2398 UINTN ProcessorNumber;\r
2399 UINTN EnabledProcessorNumber;\r
2400 UINTN Index;\r
809213a6 2401\r
b5cd30a7
MX
2402 CpuMpData = GetCpuMpData ();\r
2403\r
809213a6
JF
2404 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
2405 return EFI_INVALID_PARAMETER;\r
2406 }\r
2407\r
2408 //\r
2409 // Check whether caller processor is BSP\r
2410 //\r
2411 MpInitLibWhoAmI (&CallerNumber);\r
2412 if (CallerNumber != CpuMpData->BspNumber) {\r
2413 return EFI_DEVICE_ERROR;\r
2414 }\r
2415\r
2416 ProcessorNumber = CpuMpData->CpuCount;\r
2417 EnabledProcessorNumber = 0;\r
2418 for (Index = 0; Index < ProcessorNumber; Index++) {\r
2419 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
053e878b 2420 EnabledProcessorNumber++;\r
809213a6
JF
2421 }\r
2422 }\r
2423\r
2424 if (NumberOfProcessors != NULL) {\r
2425 *NumberOfProcessors = ProcessorNumber;\r
2426 }\r
053e878b 2427\r
809213a6
JF
2428 if (NumberOfEnabledProcessors != NULL) {\r
2429 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
2430 }\r
2431\r
2432 return EFI_SUCCESS;\r
3e8ad6bd 2433}\r
6a2ee2bb 2434\r
86efe976
JF
2435/**\r
2436 Worker function to execute a caller provided function on all enabled APs.\r
2437\r
2438 @param[in] Procedure A pointer to the function to be run on\r
2439 enabled APs of the system.\r
2440 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
2441 the function specified by Procedure one by\r
2442 one, in ascending order of processor handle\r
2443 number. If FALSE, then all the enabled APs\r
2444 execute the function specified by Procedure\r
2445 simultaneously.\r
ee0c39fa 2446 @param[in] ExcludeBsp Whether let BSP also trig this task.\r
86efe976
JF
2447 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
2448 service.\r
367284e7 2449 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
86efe976
JF
2450 APs to return from Procedure, either for\r
2451 blocking or non-blocking mode.\r
2452 @param[in] ProcedureArgument The parameter passed into Procedure for\r
2453 all APs.\r
2454 @param[out] FailedCpuList If all APs finish successfully, then its\r
2455 content is set to NULL. If not all APs\r
2456 finish before timeout expires, then its\r
2457 content is set to address of the buffer\r
2458 holding handle numbers of the failed APs.\r
2459\r
2460 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
2461 the timeout expired.\r
2462 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
2463 to all enabled APs.\r
2464 @retval others Failed to Startup all APs.\r
2465\r
2466**/\r
2467EFI_STATUS\r
ee0c39fa 2468StartupAllCPUsWorker (\r
053e878b
MK
2469 IN EFI_AP_PROCEDURE Procedure,\r
2470 IN BOOLEAN SingleThread,\r
2471 IN BOOLEAN ExcludeBsp,\r
2472 IN EFI_EVENT WaitEvent OPTIONAL,\r
2473 IN UINTN TimeoutInMicroseconds,\r
2474 IN VOID *ProcedureArgument OPTIONAL,\r
2475 OUT UINTN **FailedCpuList OPTIONAL\r
86efe976
JF
2476 )\r
2477{\r
053e878b
MK
2478 EFI_STATUS Status;\r
2479 CPU_MP_DATA *CpuMpData;\r
2480 UINTN ProcessorCount;\r
2481 UINTN ProcessorNumber;\r
2482 UINTN CallerNumber;\r
2483 CPU_AP_DATA *CpuData;\r
2484 BOOLEAN HasEnabledAp;\r
2485 CPU_STATE ApState;\r
86efe976 2486\r
b5cd30a7
MX
2487 CpuMpData = GetCpuMpData ();\r
2488\r
86efe976
JF
2489 if (FailedCpuList != NULL) {\r
2490 *FailedCpuList = NULL;\r
2491 }\r
2492\r
b5cd30a7 2493 if ((CpuMpData->CpuCount == 1) && ExcludeBsp) {\r
86efe976
JF
2494 return EFI_NOT_STARTED;\r
2495 }\r
2496\r
2497 if (Procedure == NULL) {\r
2498 return EFI_INVALID_PARAMETER;\r
2499 }\r
2500\r
2501 //\r
2502 // Check whether caller processor is BSP\r
2503 //\r
2504 MpInitLibWhoAmI (&CallerNumber);\r
2505 if (CallerNumber != CpuMpData->BspNumber) {\r
2506 return EFI_DEVICE_ERROR;\r
2507 }\r
2508\r
2509 //\r
2510 // Update AP state\r
2511 //\r
2512 CheckAndUpdateApsStatus ();\r
2513\r
2514 ProcessorCount = CpuMpData->CpuCount;\r
2515 HasEnabledAp = FALSE;\r
2516 //\r
2517 // Check whether all enabled APs are idle.\r
2518 // If any enabled AP is not idle, return EFI_NOT_READY.\r
2519 //\r
2520 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
2521 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
2522 if (ProcessorNumber != CpuMpData->BspNumber) {\r
2523 ApState = GetApState (CpuData);\r
2524 if (ApState != CpuStateDisabled) {\r
2525 HasEnabledAp = TRUE;\r
2526 if (ApState != CpuStateIdle) {\r
2527 //\r
2528 // If any enabled APs are busy, return EFI_NOT_READY.\r
2529 //\r
2530 return EFI_NOT_READY;\r
2531 }\r
2532 }\r
2533 }\r
2534 }\r
2535\r
ee0c39fa 2536 if (!HasEnabledAp && ExcludeBsp) {\r
86efe976 2537 //\r
ee0c39fa 2538 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.\r
86efe976
JF
2539 //\r
2540 return EFI_NOT_STARTED;\r
2541 }\r
2542\r
2da3e96c 2543 CpuMpData->RunningCount = 0;\r
86efe976 2544 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
053e878b 2545 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
86efe976
JF
2546 CpuData->Waiting = FALSE;\r
2547 if (ProcessorNumber != CpuMpData->BspNumber) {\r
2548 if (CpuData->State == CpuStateIdle) {\r
2549 //\r
2550 // Mark this processor as responsible for current calling.\r
2551 //\r
2552 CpuData->Waiting = TRUE;\r
2da3e96c 2553 CpuMpData->RunningCount++;\r
86efe976
JF
2554 }\r
2555 }\r
2556 }\r
2557\r
2558 CpuMpData->Procedure = Procedure;\r
2559 CpuMpData->ProcArguments = ProcedureArgument;\r
2560 CpuMpData->SingleThread = SingleThread;\r
2561 CpuMpData->FinishedCount = 0;\r
86efe976
JF
2562 CpuMpData->FailedCpuList = FailedCpuList;\r
2563 CpuMpData->ExpectedTime = CalculateTimeout (\r
2564 TimeoutInMicroseconds,\r
2565 &CpuMpData->CurrentTime\r
2566 );\r
053e878b
MK
2567 CpuMpData->TotalTime = 0;\r
2568 CpuMpData->WaitEvent = WaitEvent;\r
86efe976
JF
2569\r
2570 if (!SingleThread) {\r
cf4e79e4 2571 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument, FALSE);\r
86efe976
JF
2572 } else {\r
2573 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
2574 if (ProcessorNumber == CallerNumber) {\r
2575 continue;\r
2576 }\r
053e878b 2577\r
86efe976 2578 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
cf4e79e4 2579 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);\r
86efe976
JF
2580 break;\r
2581 }\r
2582 }\r
2583 }\r
2584\r
ee0c39fa
ED
2585 if (!ExcludeBsp) {\r
2586 //\r
2587 // Start BSP.\r
2588 //\r
2589 Procedure (ProcedureArgument);\r
2590 }\r
2591\r
86efe976
JF
2592 Status = EFI_SUCCESS;\r
2593 if (WaitEvent == NULL) {\r
2594 do {\r
2595 Status = CheckAllAPs ();\r
2596 } while (Status == EFI_NOT_READY);\r
2597 }\r
2598\r
2599 return Status;\r
2600}\r
2601\r
20ae5774
JF
2602/**\r
2603 Worker function to let the caller get one enabled AP to execute a caller-provided\r
2604 function.\r
2605\r
2606 @param[in] Procedure A pointer to the function to be run on\r
2607 enabled APs of the system.\r
2608 @param[in] ProcessorNumber The handle number of the AP.\r
2609 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
2610 service.\r
367284e7 2611 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
20ae5774
JF
2612 APs to return from Procedure, either for\r
2613 blocking or non-blocking mode.\r
2614 @param[in] ProcedureArgument The parameter passed into Procedure for\r
2615 all APs.\r
2616 @param[out] Finished If AP returns from Procedure before the\r
2617 timeout expires, its content is set to TRUE.\r
2618 Otherwise, the value is set to FALSE.\r
2619\r
2620 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
2621 the timeout expires.\r
2622 @retval others Failed to Startup AP.\r
2623\r
2624**/\r
2625EFI_STATUS\r
2626StartupThisAPWorker (\r
053e878b
MK
2627 IN EFI_AP_PROCEDURE Procedure,\r
2628 IN UINTN ProcessorNumber,\r
2629 IN EFI_EVENT WaitEvent OPTIONAL,\r
2630 IN UINTN TimeoutInMicroseconds,\r
2631 IN VOID *ProcedureArgument OPTIONAL,\r
2632 OUT BOOLEAN *Finished OPTIONAL\r
20ae5774
JF
2633 )\r
2634{\r
053e878b
MK
2635 EFI_STATUS Status;\r
2636 CPU_MP_DATA *CpuMpData;\r
2637 CPU_AP_DATA *CpuData;\r
2638 UINTN CallerNumber;\r
20ae5774
JF
2639\r
2640 CpuMpData = GetCpuMpData ();\r
2641\r
2642 if (Finished != NULL) {\r
2643 *Finished = FALSE;\r
2644 }\r
2645\r
2646 //\r
2647 // Check whether caller processor is BSP\r
2648 //\r
2649 MpInitLibWhoAmI (&CallerNumber);\r
2650 if (CallerNumber != CpuMpData->BspNumber) {\r
2651 return EFI_DEVICE_ERROR;\r
2652 }\r
2653\r
2654 //\r
2655 // Check whether processor with the handle specified by ProcessorNumber exists\r
2656 //\r
2657 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2658 return EFI_NOT_FOUND;\r
2659 }\r
2660\r
2661 //\r
2662 // Check whether specified processor is BSP\r
2663 //\r
2664 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2665 return EFI_INVALID_PARAMETER;\r
2666 }\r
2667\r
2668 //\r
2669 // Check parameter Procedure\r
2670 //\r
2671 if (Procedure == NULL) {\r
2672 return EFI_INVALID_PARAMETER;\r
2673 }\r
2674\r
2675 //\r
2676 // Update AP state\r
2677 //\r
2678 CheckAndUpdateApsStatus ();\r
2679\r
2680 //\r
2681 // Check whether specified AP is disabled\r
2682 //\r
2683 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
2684 return EFI_INVALID_PARAMETER;\r
2685 }\r
2686\r
2687 //\r
2688 // If WaitEvent is not NULL, execute in non-blocking mode.\r
2689 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
2690 // CheckAPsStatus() will check completion and timeout periodically.\r
2691 //\r
053e878b 2692 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
20ae5774
JF
2693 CpuData->WaitEvent = WaitEvent;\r
2694 CpuData->Finished = Finished;\r
2695 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
2696 CpuData->TotalTime = 0;\r
2697\r
cf4e79e4 2698 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);\r
20ae5774
JF
2699\r
2700 //\r
2701 // If WaitEvent is NULL, execute in blocking mode.\r
2702 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
2703 //\r
2704 Status = EFI_SUCCESS;\r
2705 if (WaitEvent == NULL) {\r
2706 do {\r
2707 Status = CheckThisAP (ProcessorNumber);\r
2708 } while (Status == EFI_NOT_READY);\r
2709 }\r
2710\r
2711 return Status;\r
2712}\r
2713\r
93ca4c0f
JF
2714/**\r
2715 Get pointer to CPU MP Data structure from GUIDed HOB.\r
2716\r
2717 @return The pointer to CPU MP Data structure.\r
2718**/\r
2719CPU_MP_DATA *\r
2720GetCpuMpDataFromGuidedHob (\r
2721 VOID\r
2722 )\r
2723{\r
053e878b
MK
2724 EFI_HOB_GUID_TYPE *GuidHob;\r
2725 VOID *DataInHob;\r
2726 CPU_MP_DATA *CpuMpData;\r
93ca4c0f
JF
2727\r
2728 CpuMpData = NULL;\r
053e878b 2729 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
93ca4c0f
JF
2730 if (GuidHob != NULL) {\r
2731 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
053e878b 2732 CpuMpData = (CPU_MP_DATA *)(*(UINTN *)DataInHob);\r
93ca4c0f 2733 }\r
053e878b 2734\r
93ca4c0f
JF
2735 return CpuMpData;\r
2736}\r
42c37b3b 2737\r
ee0c39fa
ED
2738/**\r
2739 This service executes a caller provided function on all enabled CPUs.\r
2740\r
2741 @param[in] Procedure A pointer to the function to be run on\r
2742 enabled APs of the system. See type\r
2743 EFI_AP_PROCEDURE.\r
2744 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
2745 APs to return from Procedure, either for\r
2746 blocking or non-blocking mode. Zero means\r
2747 infinity. TimeoutInMicroseconds is ignored\r
2748 for BSP.\r
2749 @param[in] ProcedureArgument The parameter passed into Procedure for\r
2750 all APs.\r
2751\r
2752 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before\r
2753 the timeout expired.\r
2754 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
2755 to all enabled CPUs.\r
2756 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
2757 @retval EFI_NOT_READY Any enabled APs are busy.\r
2758 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
2759 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
2760 all enabled APs have finished.\r
2761 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
2762\r
2763**/\r
2764EFI_STATUS\r
2765EFIAPI\r
2766MpInitLibStartupAllCPUs (\r
053e878b
MK
2767 IN EFI_AP_PROCEDURE Procedure,\r
2768 IN UINTN TimeoutInMicroseconds,\r
2769 IN VOID *ProcedureArgument OPTIONAL\r
ee0c39fa
ED
2770 )\r
2771{\r
2772 return StartupAllCPUsWorker (\r
2773 Procedure,\r
2774 FALSE,\r
2775 FALSE,\r
2776 NULL,\r
2777 TimeoutInMicroseconds,\r
2778 ProcedureArgument,\r
2779 NULL\r
2780 );\r
2781}\r
b95908e0
BS
2782\r
2783/**\r
2784 The function check if the specified Attr is set.\r
2785\r
2786 @param[in] CurrentAttr The current attribute.\r
2787 @param[in] Attr The attribute to check.\r
2788\r
2789 @retval TRUE The specified Attr is set.\r
2790 @retval FALSE The specified Attr is not set.\r
2791\r
2792**/\r
2793STATIC\r
2794BOOLEAN\r
2795AmdMemEncryptionAttrCheck (\r
2796 IN UINT64 CurrentAttr,\r
2797 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr\r
2798 )\r
2799{\r
2800 switch (Attr) {\r
2801 case CCAttrAmdSev:\r
2802 //\r
2803 // SEV is automatically enabled if SEV-ES or SEV-SNP is active.\r
2804 //\r
2805 return CurrentAttr >= CCAttrAmdSev;\r
2806 case CCAttrAmdSevEs:\r
2807 //\r
2808 // SEV-ES is automatically enabled if SEV-SNP is active.\r
2809 //\r
2810 return CurrentAttr >= CCAttrAmdSevEs;\r
2811 case CCAttrAmdSevSnp:\r
2812 return CurrentAttr == CCAttrAmdSevSnp;\r
2813 default:\r
2814 return FALSE;\r
2815 }\r
2816}\r
2817\r
2818/**\r
2819 Check if the specified confidential computing attribute is active.\r
2820\r
2821 @param[in] Attr The attribute to check.\r
2822\r
2823 @retval TRUE The specified Attr is active.\r
2824 @retval FALSE The specified Attr is not active.\r
2825\r
2826**/\r
2827BOOLEAN\r
2828EFIAPI\r
2829ConfidentialComputingGuestHas (\r
2830 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr\r
2831 )\r
2832{\r
2833 UINT64 CurrentAttr;\r
2834\r
2835 //\r
2836 // Get the current CC attribute.\r
2837 //\r
2838 CurrentAttr = PcdGet64 (PcdConfidentialComputingGuestAttr);\r
2839\r
2840 //\r
2841 // If attr is for the AMD group then call AMD specific checks.\r
2842 //\r
2843 if (((RShiftU64 (CurrentAttr, 8)) & 0xff) == 1) {\r
2844 return AmdMemEncryptionAttrCheck (CurrentAttr, Attr);\r
2845 }\r
2846\r
2847 return (CurrentAttr == Attr);\r
2848}\r