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