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