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