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