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