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