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