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