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