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