]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/PiSmmCpuDxeSmm: Fix memory protection crash
[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
a6b3d753
SZ
798/**\r
799 Get available system memory below 1MB by specified size.\r
800\r
801 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
802**/\r
803VOID\r
804BackupAndPrepareWakeupBuffer(\r
805 IN CPU_MP_DATA *CpuMpData\r
806 )\r
807{\r
808 CopyMem (\r
809 (VOID *) CpuMpData->BackupBuffer,\r
810 (VOID *) CpuMpData->WakeupBuffer,\r
811 CpuMpData->BackupBufferSize\r
812 );\r
813 CopyMem (\r
814 (VOID *) CpuMpData->WakeupBuffer,\r
815 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
816 CpuMpData->AddressMap.RendezvousFunnelSize\r
817 );\r
818}\r
819\r
820/**\r
821 Restore wakeup buffer data.\r
822\r
823 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
824**/\r
825VOID\r
826RestoreWakeupBuffer(\r
827 IN CPU_MP_DATA *CpuMpData\r
828 )\r
829{\r
830 CopyMem (\r
831 (VOID *) CpuMpData->WakeupBuffer,\r
832 (VOID *) CpuMpData->BackupBuffer,\r
833 CpuMpData->BackupBufferSize\r
834 );\r
835}\r
836\r
837/**\r
838 Allocate reset vector buffer.\r
839\r
840 @param[in, out] CpuMpData The pointer to CPU MP Data structure.\r
841**/\r
842VOID\r
843AllocateResetVector (\r
844 IN OUT CPU_MP_DATA *CpuMpData\r
845 )\r
846{\r
847 UINTN ApResetVectorSize;\r
848\r
849 if (CpuMpData->WakeupBuffer == (UINTN) -1) {\r
850 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +\r
851 sizeof (MP_CPU_EXCHANGE_INFO);\r
852\r
853 CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize);\r
854 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)\r
855 (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);\r
856 }\r
857 BackupAndPrepareWakeupBuffer (CpuMpData);\r
858}\r
859\r
860/**\r
861 Free AP reset vector buffer.\r
862\r
863 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
864**/\r
865VOID\r
866FreeResetVector (\r
867 IN CPU_MP_DATA *CpuMpData\r
868 )\r
869{\r
870 RestoreWakeupBuffer (CpuMpData);\r
871}\r
872\r
96f5920d
JF
873/**\r
874 This function will be called by BSP to wakeup AP.\r
875\r
876 @param[in] CpuMpData Pointer to CPU MP Data\r
877 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
878 FALSE: Send IPI to AP by ApicId\r
879 @param[in] ProcessorNumber The handle number of specified processor\r
880 @param[in] Procedure The function to be invoked by AP\r
881 @param[in] ProcedureArgument The argument to be passed into AP function\r
882**/\r
883VOID\r
884WakeUpAP (\r
885 IN CPU_MP_DATA *CpuMpData,\r
886 IN BOOLEAN Broadcast,\r
887 IN UINTN ProcessorNumber,\r
888 IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
889 IN VOID *ProcedureArgument OPTIONAL\r
890 )\r
891{\r
892 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
893 UINTN Index;\r
894 CPU_AP_DATA *CpuData;\r
895 BOOLEAN ResetVectorRequired;\r
31a1e4da 896 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96f5920d
JF
897\r
898 CpuMpData->FinishedCount = 0;\r
899 ResetVectorRequired = FALSE;\r
900\r
901 if (CpuMpData->ApLoopMode == ApInHltLoop ||\r
902 CpuMpData->InitFlag != ApInitDone) {\r
903 ResetVectorRequired = TRUE;\r
904 AllocateResetVector (CpuMpData);\r
905 FillExchangeInfoData (CpuMpData);\r
ffab2442 906 SaveLocalApicTimerSetting (CpuMpData);\r
96f5920d
JF
907 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
908 //\r
909 // Get AP target C-state each time when waking up AP,\r
910 // for it maybe updated by platform again\r
911 //\r
912 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
913 }\r
914\r
915 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
916\r
917 if (Broadcast) {\r
918 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
919 if (Index != CpuMpData->BspNumber) {\r
920 CpuData = &CpuMpData->CpuData[Index];\r
921 CpuData->ApFunction = (UINTN) Procedure;\r
922 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
923 SetApState (CpuData, CpuStateReady);\r
924 if (CpuMpData->InitFlag != ApInitConfig) {\r
925 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
926 }\r
927 }\r
928 }\r
929 if (ResetVectorRequired) {\r
930 //\r
931 // Wakeup all APs\r
932 //\r
933 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
934 }\r
c1192210
JF
935 if (CpuMpData->InitFlag == ApInitConfig) {\r
936 //\r
937 // Wait for all potential APs waken up in one specified period\r
938 //\r
6e1987f1
LE
939 TimedWaitForApFinish (\r
940 CpuMpData,\r
941 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
942 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
943 );\r
c1192210 944 } else {\r
96f5920d
JF
945 //\r
946 // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
947 //\r
948 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
949 CpuData = &CpuMpData->CpuData[Index];\r
950 if (Index != CpuMpData->BspNumber) {\r
951 WaitApWakeup (CpuData->StartupApSignal);\r
952 }\r
953 }\r
954 }\r
955 } else {\r
956 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
957 CpuData->ApFunction = (UINTN) Procedure;\r
958 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
959 SetApState (CpuData, CpuStateReady);\r
960 //\r
961 // Wakeup specified AP\r
962 //\r
963 ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
964 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
965 if (ResetVectorRequired) {\r
31a1e4da 966 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
96f5920d 967 SendInitSipiSipi (\r
31a1e4da 968 CpuInfoInHob[ProcessorNumber].ApicId,\r
96f5920d
JF
969 (UINT32) ExchangeInfo->BufferStart\r
970 );\r
971 }\r
972 //\r
973 // Wait specified AP waken up\r
974 //\r
975 WaitApWakeup (CpuData->StartupApSignal);\r
976 }\r
977\r
978 if (ResetVectorRequired) {\r
979 FreeResetVector (CpuMpData);\r
980 }\r
981}\r
982\r
08085f08
JF
983/**\r
984 Calculate timeout value and return the current performance counter value.\r
985\r
986 Calculate the number of performance counter ticks required for a timeout.\r
987 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
988 as infinity.\r
989\r
990 @param[in] TimeoutInMicroseconds Timeout value in microseconds.\r
991 @param[out] CurrentTime Returns the current value of the performance counter.\r
992\r
993 @return Expected time stamp counter for timeout.\r
994 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
995 as infinity.\r
996\r
997**/\r
998UINT64\r
999CalculateTimeout (\r
1000 IN UINTN TimeoutInMicroseconds,\r
1001 OUT UINT64 *CurrentTime\r
1002 )\r
1003{\r
1004 //\r
1005 // Read the current value of the performance counter\r
1006 //\r
1007 *CurrentTime = GetPerformanceCounter ();\r
1008\r
1009 //\r
1010 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
1011 // as infinity.\r
1012 //\r
1013 if (TimeoutInMicroseconds == 0) {\r
1014 return 0;\r
1015 }\r
1016\r
1017 //\r
1018 // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
1019 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide\r
1020 // it by 1,000,000, to get the number of ticks for the timeout value.\r
1021 //\r
1022 return DivU64x32 (\r
1023 MultU64x64 (\r
1024 GetPerformanceCounterProperties (NULL, NULL),\r
1025 TimeoutInMicroseconds\r
1026 ),\r
1027 1000000\r
1028 );\r
1029}\r
1030\r
1031/**\r
1032 Checks whether timeout expires.\r
1033\r
1034 Check whether the number of elapsed performance counter ticks required for\r
1035 a timeout condition has been reached.\r
1036 If Timeout is zero, which means infinity, return value is always FALSE.\r
1037\r
1038 @param[in, out] PreviousTime On input, the value of the performance counter\r
1039 when it was last read.\r
1040 On output, the current value of the performance\r
1041 counter\r
1042 @param[in] TotalTime The total amount of elapsed time in performance\r
1043 counter ticks.\r
1044 @param[in] Timeout The number of performance counter ticks required\r
1045 to reach a timeout condition.\r
1046\r
1047 @retval TRUE A timeout condition has been reached.\r
1048 @retval FALSE A timeout condition has not been reached.\r
1049\r
1050**/\r
1051BOOLEAN\r
1052CheckTimeout (\r
1053 IN OUT UINT64 *PreviousTime,\r
1054 IN UINT64 *TotalTime,\r
1055 IN UINT64 Timeout\r
1056 )\r
1057{\r
1058 UINT64 Start;\r
1059 UINT64 End;\r
1060 UINT64 CurrentTime;\r
1061 INT64 Delta;\r
1062 INT64 Cycle;\r
1063\r
1064 if (Timeout == 0) {\r
1065 return FALSE;\r
1066 }\r
1067 GetPerformanceCounterProperties (&Start, &End);\r
1068 Cycle = End - Start;\r
1069 if (Cycle < 0) {\r
1070 Cycle = -Cycle;\r
1071 }\r
1072 Cycle++;\r
1073 CurrentTime = GetPerformanceCounter();\r
1074 Delta = (INT64) (CurrentTime - *PreviousTime);\r
1075 if (Start > End) {\r
1076 Delta = -Delta;\r
1077 }\r
1078 if (Delta < 0) {\r
1079 Delta += Cycle;\r
1080 }\r
1081 *TotalTime += Delta;\r
1082 *PreviousTime = CurrentTime;\r
1083 if (*TotalTime > Timeout) {\r
1084 return TRUE;\r
1085 }\r
1086 return FALSE;\r
1087}\r
1088\r
6e1987f1
LE
1089/**\r
1090 Helper function that waits until the finished AP count reaches the specified\r
1091 limit, or the specified timeout elapses (whichever comes first).\r
1092\r
1093 @param[in] CpuMpData Pointer to CPU MP Data.\r
1094 @param[in] FinishedApLimit The number of finished APs to wait for.\r
1095 @param[in] TimeLimit The number of microseconds to wait for.\r
1096**/\r
1097VOID\r
1098TimedWaitForApFinish (\r
1099 IN CPU_MP_DATA *CpuMpData,\r
1100 IN UINT32 FinishedApLimit,\r
1101 IN UINT32 TimeLimit\r
1102 )\r
1103{\r
1104 //\r
1105 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
1106 // "infinity", so check for (TimeLimit == 0) explicitly.\r
1107 //\r
1108 if (TimeLimit == 0) {\r
1109 return;\r
1110 }\r
1111\r
1112 CpuMpData->TotalTime = 0;\r
1113 CpuMpData->ExpectedTime = CalculateTimeout (\r
1114 TimeLimit,\r
1115 &CpuMpData->CurrentTime\r
1116 );\r
1117 while (CpuMpData->FinishedCount < FinishedApLimit &&\r
1118 !CheckTimeout (\r
1119 &CpuMpData->CurrentTime,\r
1120 &CpuMpData->TotalTime,\r
1121 CpuMpData->ExpectedTime\r
1122 )) {\r
1123 CpuPause ();\r
1124 }\r
1125\r
1126 if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
1127 DEBUG ((\r
1128 DEBUG_VERBOSE,\r
1129 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
1130 __FUNCTION__,\r
1131 FinishedApLimit,\r
1132 DivU64x64Remainder (\r
1133 MultU64x32 (CpuMpData->TotalTime, 1000000),\r
1134 GetPerformanceCounterProperties (NULL, NULL),\r
1135 NULL\r
1136 )\r
1137 ));\r
1138 }\r
1139}\r
1140\r
08085f08
JF
1141/**\r
1142 Reset an AP to Idle state.\r
1143\r
1144 Any task being executed by the AP will be aborted and the AP\r
1145 will be waiting for a new task in Wait-For-SIPI state.\r
1146\r
1147 @param[in] ProcessorNumber The handle number of processor.\r
1148**/\r
1149VOID\r
1150ResetProcessorToIdleState (\r
1151 IN UINTN ProcessorNumber\r
1152 )\r
1153{\r
1154 CPU_MP_DATA *CpuMpData;\r
1155\r
1156 CpuMpData = GetCpuMpData ();\r
1157\r
cb33bde4 1158 CpuMpData->InitFlag = ApInitReconfig;\r
08085f08 1159 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);\r
cb33bde4
JF
1160 while (CpuMpData->FinishedCount < 1) {\r
1161 CpuPause ();\r
1162 }\r
1163 CpuMpData->InitFlag = ApInitDone;\r
08085f08
JF
1164\r
1165 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1166}\r
1167\r
1168/**\r
1169 Searches for the next waiting AP.\r
1170\r
1171 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
1172\r
1173 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
1174\r
1175 @retval EFI_SUCCESS The next waiting AP has been found.\r
1176 @retval EFI_NOT_FOUND No waiting AP exists.\r
1177\r
1178**/\r
1179EFI_STATUS\r
1180GetNextWaitingProcessorNumber (\r
1181 OUT UINTN *NextProcessorNumber\r
1182 )\r
1183{\r
1184 UINTN ProcessorNumber;\r
1185 CPU_MP_DATA *CpuMpData;\r
1186\r
1187 CpuMpData = GetCpuMpData ();\r
1188\r
1189 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1190 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1191 *NextProcessorNumber = ProcessorNumber;\r
1192 return EFI_SUCCESS;\r
1193 }\r
1194 }\r
1195\r
1196 return EFI_NOT_FOUND;\r
1197}\r
1198\r
1199/** Checks status of specified AP.\r
1200\r
1201 This function checks whether the specified AP has finished the task assigned\r
1202 by StartupThisAP(), and whether timeout expires.\r
1203\r
1204 @param[in] ProcessorNumber The handle number of processor.\r
1205\r
1206 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
1207 @retval EFI_TIMEOUT The timeout expires.\r
1208 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
1209**/\r
1210EFI_STATUS\r
1211CheckThisAP (\r
1212 IN UINTN ProcessorNumber\r
1213 )\r
1214{\r
1215 CPU_MP_DATA *CpuMpData;\r
1216 CPU_AP_DATA *CpuData;\r
1217\r
1218 CpuMpData = GetCpuMpData ();\r
1219 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1220\r
1221 //\r
1222 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
1223 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
1224 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
1225 //\r
1226 //\r
1227 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.\r
1228 //\r
1229 if (GetApState(CpuData) == CpuStateFinished) {\r
1230 if (CpuData->Finished != NULL) {\r
1231 *(CpuData->Finished) = TRUE;\r
1232 }\r
1233 SetApState (CpuData, CpuStateIdle);\r
1234 return EFI_SUCCESS;\r
1235 } else {\r
1236 //\r
1237 // If timeout expires for StartupThisAP(), report timeout.\r
1238 //\r
1239 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
1240 if (CpuData->Finished != NULL) {\r
1241 *(CpuData->Finished) = FALSE;\r
1242 }\r
1243 //\r
1244 // Reset failed AP to idle state\r
1245 //\r
1246 ResetProcessorToIdleState (ProcessorNumber);\r
1247\r
1248 return EFI_TIMEOUT;\r
1249 }\r
1250 }\r
1251 return EFI_NOT_READY;\r
1252}\r
1253\r
1254/**\r
1255 Checks status of all APs.\r
1256\r
1257 This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
1258 and whether timeout expires.\r
1259\r
1260 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
1261 @retval EFI_TIMEOUT The timeout expires.\r
1262 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
1263**/\r
1264EFI_STATUS\r
1265CheckAllAPs (\r
1266 VOID\r
1267 )\r
1268{\r
1269 UINTN ProcessorNumber;\r
1270 UINTN NextProcessorNumber;\r
1271 UINTN ListIndex;\r
1272 EFI_STATUS Status;\r
1273 CPU_MP_DATA *CpuMpData;\r
1274 CPU_AP_DATA *CpuData;\r
1275\r
1276 CpuMpData = GetCpuMpData ();\r
1277\r
1278 NextProcessorNumber = 0;\r
1279\r
1280 //\r
1281 // Go through all APs that are responsible for the StartupAllAPs().\r
1282 //\r
1283 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1284 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1285 continue;\r
1286 }\r
1287\r
1288 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1289 //\r
1290 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
1291 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
1292 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
1293 //\r
1294 if (GetApState(CpuData) == CpuStateFinished) {\r
1295 CpuMpData->RunningCount ++;\r
1296 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
1297 SetApState(CpuData, CpuStateIdle);\r
1298\r
1299 //\r
1300 // If in Single Thread mode, then search for the next waiting AP for execution.\r
1301 //\r
1302 if (CpuMpData->SingleThread) {\r
1303 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
1304\r
1305 if (!EFI_ERROR (Status)) {\r
1306 WakeUpAP (\r
1307 CpuMpData,\r
1308 FALSE,\r
1309 (UINT32) NextProcessorNumber,\r
1310 CpuMpData->Procedure,\r
1311 CpuMpData->ProcArguments\r
1312 );\r
1313 }\r
1314 }\r
1315 }\r
1316 }\r
1317\r
1318 //\r
1319 // If all APs finish, return EFI_SUCCESS.\r
1320 //\r
1321 if (CpuMpData->RunningCount == CpuMpData->StartCount) {\r
1322 return EFI_SUCCESS;\r
1323 }\r
1324\r
1325 //\r
1326 // If timeout expires, report timeout.\r
1327 //\r
1328 if (CheckTimeout (\r
1329 &CpuMpData->CurrentTime,\r
1330 &CpuMpData->TotalTime,\r
1331 CpuMpData->ExpectedTime)\r
1332 ) {\r
1333 //\r
1334 // If FailedCpuList is not NULL, record all failed APs in it.\r
1335 //\r
1336 if (CpuMpData->FailedCpuList != NULL) {\r
1337 *CpuMpData->FailedCpuList =\r
1338 AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));\r
1339 ASSERT (*CpuMpData->FailedCpuList != NULL);\r
1340 }\r
1341 ListIndex = 0;\r
1342\r
1343 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1344 //\r
1345 // Check whether this processor is responsible for StartupAllAPs().\r
1346 //\r
1347 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1348 //\r
1349 // Reset failed APs to idle state\r
1350 //\r
1351 ResetProcessorToIdleState (ProcessorNumber);\r
1352 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
1353 if (CpuMpData->FailedCpuList != NULL) {\r
1354 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;\r
1355 }\r
1356 }\r
1357 }\r
1358 if (CpuMpData->FailedCpuList != NULL) {\r
1359 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
1360 }\r
1361 return EFI_TIMEOUT;\r
1362 }\r
1363 return EFI_NOT_READY;\r
1364}\r
1365\r
3e8ad6bd
JF
1366/**\r
1367 MP Initialize Library initialization.\r
1368\r
1369 This service will allocate AP reset vector and wakeup all APs to do APs\r
1370 initialization.\r
1371\r
1372 This service must be invoked before all other MP Initialize Library\r
1373 service are invoked.\r
1374\r
1375 @retval EFI_SUCCESS MP initialization succeeds.\r
1376 @retval Others MP initialization fails.\r
1377\r
1378**/\r
1379EFI_STATUS\r
1380EFIAPI\r
1381MpInitLibInitialize (\r
1382 VOID\r
1383 )\r
1384{\r
6a2ee2bb
JF
1385 CPU_MP_DATA *OldCpuMpData;\r
1386 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e59f8f6b
JF
1387 UINT32 MaxLogicalProcessorNumber;\r
1388 UINT32 ApStackSize;\r
f7f85d83 1389 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
e59f8f6b 1390 UINTN BufferSize;\r
9ebcf0f4 1391 UINT32 MonitorFilterSize;\r
e59f8f6b
JF
1392 VOID *MpBuffer;\r
1393 UINTN Buffer;\r
1394 CPU_MP_DATA *CpuMpData;\r
9ebcf0f4 1395 UINT8 ApLoopMode;\r
e59f8f6b 1396 UINT8 *MonitorBuffer;\r
03a1a925 1397 UINTN Index;\r
f7f85d83 1398 UINTN ApResetVectorSize;\r
e59f8f6b 1399 UINTN BackupBufferAddr;\r
6a2ee2bb
JF
1400\r
1401 OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
1402 if (OldCpuMpData == NULL) {\r
1403 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
1404 } else {\r
1405 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
1406 }\r
14e8137c 1407 ASSERT (MaxLogicalProcessorNumber != 0);\r
f7f85d83
JF
1408\r
1409 AsmGetAddressMap (&AddressMap);\r
1410 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
e59f8f6b 1411 ApStackSize = PcdGet32(PcdCpuApStackSize);\r
9ebcf0f4
JF
1412 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
1413\r
e59f8f6b
JF
1414 BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
1415 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
1416 BufferSize += sizeof (CPU_MP_DATA);\r
1417 BufferSize += ApResetVectorSize;\r
1418 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
1419 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1420 ASSERT (MpBuffer != NULL);\r
1421 ZeroMem (MpBuffer, BufferSize);\r
1422 Buffer = (UINTN) MpBuffer;\r
1423\r
1424 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
1425 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
1426 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
1427 CpuMpData->Buffer = Buffer;\r
1428 CpuMpData->CpuApStackSize = ApStackSize;\r
1429 CpuMpData->BackupBuffer = BackupBufferAddr;\r
1430 CpuMpData->BackupBufferSize = ApResetVectorSize;\r
e59f8f6b
JF
1431 CpuMpData->WakeupBuffer = (UINTN) -1;\r
1432 CpuMpData->CpuCount = 1;\r
1433 CpuMpData->BspNumber = 0;\r
1434 CpuMpData->WaitEvent = NULL;\r
41be0da5 1435 CpuMpData->SwitchBspFlag = FALSE;\r
e59f8f6b
JF
1436 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);\r
1437 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
1438 InitializeSpinLock(&CpuMpData->MpLock);\r
1439 //\r
68cb9330
JF
1440 // Save BSP's Control registers to APs\r
1441 //\r
1442 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
1443 //\r
03a1a925
JF
1444 // Set BSP basic information\r
1445 //\r
845c5be1 1446 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
03a1a925 1447 //\r
e59f8f6b
JF
1448 // Save assembly code information\r
1449 //\r
1450 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
1451 //\r
1452 // Finally set AP loop mode\r
1453 //\r
1454 CpuMpData->ApLoopMode = ApLoopMode;\r
1455 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
1456 //\r
03a1a925
JF
1457 // Set up APs wakeup signal buffer\r
1458 //\r
1459 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
1460 CpuMpData->CpuData[Index].StartupApSignal =\r
1461 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
1462 }\r
94f63c76
JF
1463 //\r
1464 // Load Microcode on BSP\r
1465 //\r
1466 MicrocodeDetect (CpuMpData);\r
1467 //\r
e59f8f6b
JF
1468 // Store BSP's MTRR setting\r
1469 //\r
1470 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
9d64a9fd
JF
1471 //\r
1472 // Enable the local APIC for Virtual Wire Mode.\r
1473 //\r
1474 ProgramVirtualWireMode ();\r
e59f8f6b 1475\r
6a2ee2bb 1476 if (OldCpuMpData == NULL) {\r
14e8137c
JF
1477 if (MaxLogicalProcessorNumber > 1) {\r
1478 //\r
1479 // Wakeup all APs and calculate the processor count in system\r
1480 //\r
1481 CollectProcessorCount (CpuMpData);\r
1482 }\r
6a2ee2bb
JF
1483 } else {\r
1484 //\r
1485 // APs have been wakeup before, just get the CPU Information\r
1486 // from HOB\r
1487 //\r
1488 CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
1489 CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
1490 CpuMpData->InitFlag = ApInitReconfig;\r
31a1e4da
JF
1491 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
1492 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
6a2ee2bb
JF
1493 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1494 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
71d8226a 1495 if (CpuInfoInHob[Index].InitialApicId >= 255 || Index > 254) {\r
6a2ee2bb
JF
1496 CpuMpData->X2ApicEnable = TRUE;\r
1497 }\r
31a1e4da 1498 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
6a2ee2bb
JF
1499 CpuMpData->CpuData[Index].ApFunction = 0;\r
1500 CopyMem (\r
1501 &CpuMpData->CpuData[Index].VolatileRegisters,\r
1502 &CpuMpData->CpuData[0].VolatileRegisters,\r
1503 sizeof (CPU_VOLATILE_REGISTERS)\r
1504 );\r
1505 }\r
14e8137c
JF
1506 if (MaxLogicalProcessorNumber > 1) {\r
1507 //\r
1508 // Wakeup APs to do some AP initialize sync\r
1509 //\r
1510 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
1511 //\r
1512 // Wait for all APs finished initialization\r
1513 //\r
1514 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
1515 CpuPause ();\r
1516 }\r
1517 CpuMpData->InitFlag = ApInitDone;\r
1518 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1519 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
1520 }\r
6a2ee2bb
JF
1521 }\r
1522 }\r
93ca4c0f
JF
1523\r
1524 //\r
1525 // Initialize global data for MP support\r
1526 //\r
1527 InitMpGlobalData (CpuMpData);\r
1528\r
f7f85d83 1529 return EFI_SUCCESS;\r
3e8ad6bd
JF
1530}\r
1531\r
1532/**\r
1533 Gets detailed MP-related information on the requested processor at the\r
1534 instant this call is made. This service may only be called from the BSP.\r
1535\r
1536 @param[in] ProcessorNumber The handle number of processor.\r
1537 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
1538 the requested processor is deposited.\r
1539 @param[out] HealthData Return processor health data.\r
1540\r
1541 @retval EFI_SUCCESS Processor information was returned.\r
1542 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1543 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
1544 @retval EFI_NOT_FOUND The processor with the handle specified by\r
1545 ProcessorNumber does not exist in the platform.\r
1546 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1547\r
1548**/\r
1549EFI_STATUS\r
1550EFIAPI\r
1551MpInitLibGetProcessorInfo (\r
1552 IN UINTN ProcessorNumber,\r
1553 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,\r
1554 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL\r
1555 )\r
1556{\r
ad52f25e
JF
1557 CPU_MP_DATA *CpuMpData;\r
1558 UINTN CallerNumber;\r
31a1e4da 1559 CPU_INFO_IN_HOB *CpuInfoInHob;\r
ad52f25e
JF
1560\r
1561 CpuMpData = GetCpuMpData ();\r
31a1e4da 1562 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
ad52f25e
JF
1563\r
1564 //\r
1565 // Check whether caller processor is BSP\r
1566 //\r
1567 MpInitLibWhoAmI (&CallerNumber);\r
1568 if (CallerNumber != CpuMpData->BspNumber) {\r
1569 return EFI_DEVICE_ERROR;\r
1570 }\r
1571\r
1572 if (ProcessorInfoBuffer == NULL) {\r
1573 return EFI_INVALID_PARAMETER;\r
1574 }\r
1575\r
1576 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1577 return EFI_NOT_FOUND;\r
1578 }\r
1579\r
31a1e4da 1580 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
ad52f25e
JF
1581 ProcessorInfoBuffer->StatusFlag = 0;\r
1582 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1583 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1584 }\r
1585 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
1586 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
1587 }\r
1588 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
1589 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
1590 } else {\r
1591 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
1592 }\r
1593\r
1594 //\r
1595 // Get processor location information\r
1596 //\r
262128e5 1597 GetProcessorLocationByApicId (\r
31a1e4da 1598 CpuInfoInHob[ProcessorNumber].ApicId,\r
73152f19
LD
1599 &ProcessorInfoBuffer->Location.Package,\r
1600 &ProcessorInfoBuffer->Location.Core,\r
1601 &ProcessorInfoBuffer->Location.Thread\r
1602 );\r
ad52f25e
JF
1603\r
1604 if (HealthData != NULL) {\r
31a1e4da 1605 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
ad52f25e
JF
1606 }\r
1607\r
1608 return EFI_SUCCESS;\r
3e8ad6bd 1609}\r
ad52f25e 1610\r
41be0da5
JF
1611/**\r
1612 Worker function to switch the requested AP to be the BSP from that point onward.\r
1613\r
1614 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
1615 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
1616 enabled AP. Otherwise, it will be disabled.\r
1617\r
1618 @retval EFI_SUCCESS BSP successfully switched.\r
1619 @retval others Failed to switch BSP. \r
1620\r
1621**/\r
1622EFI_STATUS\r
1623SwitchBSPWorker (\r
1624 IN UINTN ProcessorNumber,\r
1625 IN BOOLEAN EnableOldBSP\r
1626 )\r
1627{\r
1628 CPU_MP_DATA *CpuMpData;\r
1629 UINTN CallerNumber;\r
1630 CPU_STATE State;\r
1631 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
a8d75a18 1632 BOOLEAN OldInterruptState;\r
26b43433 1633 BOOLEAN OldTimerInterruptState;\r
a8d75a18 1634\r
26b43433
JF
1635 //\r
1636 // Save and Disable Local APIC timer interrupt\r
1637 //\r
1638 OldTimerInterruptState = GetApicTimerInterruptState ();\r
1639 DisableApicTimerInterrupt ();\r
a8d75a18
JF
1640 //\r
1641 // Before send both BSP and AP to a procedure to exchange their roles,\r
1642 // interrupt must be disabled. This is because during the exchange role\r
1643 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
1644 // be corrupted, since interrupt return address will be pushed to stack\r
1645 // by hardware.\r
1646 //\r
1647 OldInterruptState = SaveAndDisableInterrupts ();\r
1648\r
1649 //\r
1650 // Mask LINT0 & LINT1 for the old BSP\r
1651 //\r
1652 DisableLvtInterrupts ();\r
41be0da5
JF
1653\r
1654 CpuMpData = GetCpuMpData ();\r
1655\r
1656 //\r
1657 // Check whether caller processor is BSP\r
1658 //\r
1659 MpInitLibWhoAmI (&CallerNumber);\r
1660 if (CallerNumber != CpuMpData->BspNumber) {\r
5e72dacc 1661 return EFI_DEVICE_ERROR;\r
41be0da5
JF
1662 }\r
1663\r
1664 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1665 return EFI_NOT_FOUND;\r
1666 }\r
1667\r
1668 //\r
1669 // Check whether specified AP is disabled\r
1670 //\r
1671 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
1672 if (State == CpuStateDisabled) {\r
1673 return EFI_INVALID_PARAMETER;\r
1674 }\r
1675\r
1676 //\r
1677 // Check whether ProcessorNumber specifies the current BSP\r
1678 //\r
1679 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1680 return EFI_INVALID_PARAMETER;\r
1681 }\r
1682\r
1683 //\r
1684 // Check whether specified AP is busy\r
1685 //\r
1686 if (State == CpuStateBusy) {\r
1687 return EFI_NOT_READY;\r
1688 }\r
1689\r
1690 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
1691 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
1692 CpuMpData->SwitchBspFlag = TRUE;\r
b3775af2 1693 CpuMpData->NewBspNumber = ProcessorNumber;\r
41be0da5
JF
1694\r
1695 //\r
1696 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
1697 //\r
1698 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1699 ApicBaseMsr.Bits.BSP = 0;\r
1700 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1701\r
1702 //\r
1703 // Need to wakeUp AP (future BSP).\r
1704 //\r
1705 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
1706\r
1707 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
1708\r
1709 //\r
1710 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
1711 //\r
1712 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1713 ApicBaseMsr.Bits.BSP = 1;\r
1714 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1715\r
1716 //\r
1717 // Wait for old BSP finished AP task\r
1718 //\r
1719 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
1720 CpuPause ();\r
1721 }\r
1722\r
1723 CpuMpData->SwitchBspFlag = FALSE;\r
1724 //\r
1725 // Set old BSP enable state\r
1726 //\r
1727 if (!EnableOldBSP) {\r
1728 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
af8ba51a
JF
1729 } else {\r
1730 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);\r
41be0da5
JF
1731 }\r
1732 //\r
1733 // Save new BSP number\r
1734 //\r
1735 CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
1736\r
a8d75a18
JF
1737 //\r
1738 // Restore interrupt state.\r
1739 //\r
1740 SetInterruptState (OldInterruptState);\r
1741\r
26b43433
JF
1742 if (OldTimerInterruptState) {\r
1743 EnableApicTimerInterrupt ();\r
1744 }\r
a8d75a18 1745\r
41be0da5
JF
1746 return EFI_SUCCESS;\r
1747}\r
ad52f25e 1748\r
e37109bc
JF
1749/**\r
1750 Worker function to let the caller enable or disable an AP from this point onward.\r
1751 This service may only be called from the BSP.\r
1752\r
1753 @param[in] ProcessorNumber The handle number of AP.\r
1754 @param[in] EnableAP Specifies the new state for the processor for\r
1755 enabled, FALSE for disabled.\r
1756 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
1757 the new health status of the AP.\r
1758\r
1759 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1760 @retval others Failed to Enable/Disable AP.\r
1761\r
1762**/\r
1763EFI_STATUS\r
1764EnableDisableApWorker (\r
1765 IN UINTN ProcessorNumber,\r
1766 IN BOOLEAN EnableAP,\r
1767 IN UINT32 *HealthFlag OPTIONAL\r
1768 )\r
1769{\r
1770 CPU_MP_DATA *CpuMpData;\r
1771 UINTN CallerNumber;\r
1772\r
1773 CpuMpData = GetCpuMpData ();\r
1774\r
1775 //\r
1776 // Check whether caller processor is BSP\r
1777 //\r
1778 MpInitLibWhoAmI (&CallerNumber);\r
1779 if (CallerNumber != CpuMpData->BspNumber) {\r
1780 return EFI_DEVICE_ERROR;\r
1781 }\r
1782\r
1783 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1784 return EFI_INVALID_PARAMETER;\r
1785 }\r
1786\r
1787 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1788 return EFI_NOT_FOUND;\r
1789 }\r
1790\r
1791 if (!EnableAP) {\r
1792 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
1793 } else {\r
1794 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1795 }\r
1796\r
1797 if (HealthFlag != NULL) {\r
1798 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
1799 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
1800 }\r
1801\r
1802 return EFI_SUCCESS;\r
1803}\r
1804\r
3e8ad6bd
JF
1805/**\r
1806 This return the handle number for the calling processor. This service may be\r
1807 called from the BSP and APs.\r
1808\r
1809 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
1810 The range is from 0 to the total number of\r
1811 logical processors minus 1. The total number of\r
1812 logical processors can be retrieved by\r
1813 MpInitLibGetNumberOfProcessors().\r
1814\r
1815 @retval EFI_SUCCESS The current processor handle number was returned\r
1816 in ProcessorNumber.\r
1817 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1818 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1819\r
1820**/\r
1821EFI_STATUS\r
1822EFIAPI\r
1823MpInitLibWhoAmI (\r
1824 OUT UINTN *ProcessorNumber\r
1825 )\r
1826{\r
5c9e0997
JF
1827 CPU_MP_DATA *CpuMpData;\r
1828\r
1829 if (ProcessorNumber == NULL) {\r
1830 return EFI_INVALID_PARAMETER;\r
1831 }\r
1832\r
1833 CpuMpData = GetCpuMpData ();\r
1834\r
1835 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 1836}\r
809213a6 1837\r
3e8ad6bd
JF
1838/**\r
1839 Retrieves the number of logical processor in the platform and the number of\r
1840 those logical processors that are enabled on this boot. This service may only\r
1841 be called from the BSP.\r
1842\r
1843 @param[out] NumberOfProcessors Pointer to the total number of logical\r
1844 processors in the system, including the BSP\r
1845 and disabled APs.\r
1846 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
1847 processors that exist in system, including\r
1848 the BSP.\r
1849\r
1850 @retval EFI_SUCCESS The number of logical processors and enabled\r
1851 logical processors was retrieved.\r
1852 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1853 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
1854 is NULL.\r
1855 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1856\r
1857**/\r
1858EFI_STATUS\r
1859EFIAPI\r
1860MpInitLibGetNumberOfProcessors (\r
1861 OUT UINTN *NumberOfProcessors, OPTIONAL\r
1862 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
1863 )\r
1864{\r
809213a6
JF
1865 CPU_MP_DATA *CpuMpData;\r
1866 UINTN CallerNumber;\r
1867 UINTN ProcessorNumber;\r
1868 UINTN EnabledProcessorNumber;\r
1869 UINTN Index;\r
1870\r
1871 CpuMpData = GetCpuMpData ();\r
1872\r
1873 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
1874 return EFI_INVALID_PARAMETER;\r
1875 }\r
1876\r
1877 //\r
1878 // Check whether caller processor is BSP\r
1879 //\r
1880 MpInitLibWhoAmI (&CallerNumber);\r
1881 if (CallerNumber != CpuMpData->BspNumber) {\r
1882 return EFI_DEVICE_ERROR;\r
1883 }\r
1884\r
1885 ProcessorNumber = CpuMpData->CpuCount;\r
1886 EnabledProcessorNumber = 0;\r
1887 for (Index = 0; Index < ProcessorNumber; Index++) {\r
1888 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
1889 EnabledProcessorNumber ++;\r
1890 }\r
1891 }\r
1892\r
1893 if (NumberOfProcessors != NULL) {\r
1894 *NumberOfProcessors = ProcessorNumber;\r
1895 }\r
1896 if (NumberOfEnabledProcessors != NULL) {\r
1897 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
1898 }\r
1899\r
1900 return EFI_SUCCESS;\r
3e8ad6bd 1901}\r
6a2ee2bb 1902\r
809213a6 1903\r
86efe976
JF
1904/**\r
1905 Worker function to execute a caller provided function on all enabled APs.\r
1906\r
1907 @param[in] Procedure A pointer to the function to be run on\r
1908 enabled APs of the system.\r
1909 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
1910 the function specified by Procedure one by\r
1911 one, in ascending order of processor handle\r
1912 number. If FALSE, then all the enabled APs\r
1913 execute the function specified by Procedure\r
1914 simultaneously.\r
1915 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1916 service.\r
367284e7 1917 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
86efe976
JF
1918 APs to return from Procedure, either for\r
1919 blocking or non-blocking mode.\r
1920 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1921 all APs.\r
1922 @param[out] FailedCpuList If all APs finish successfully, then its\r
1923 content is set to NULL. If not all APs\r
1924 finish before timeout expires, then its\r
1925 content is set to address of the buffer\r
1926 holding handle numbers of the failed APs.\r
1927\r
1928 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1929 the timeout expired.\r
1930 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1931 to all enabled APs.\r
1932 @retval others Failed to Startup all APs.\r
1933\r
1934**/\r
1935EFI_STATUS\r
1936StartupAllAPsWorker (\r
1937 IN EFI_AP_PROCEDURE Procedure,\r
1938 IN BOOLEAN SingleThread,\r
1939 IN EFI_EVENT WaitEvent OPTIONAL,\r
1940 IN UINTN TimeoutInMicroseconds,\r
1941 IN VOID *ProcedureArgument OPTIONAL,\r
1942 OUT UINTN **FailedCpuList OPTIONAL\r
1943 )\r
1944{\r
1945 EFI_STATUS Status;\r
1946 CPU_MP_DATA *CpuMpData;\r
1947 UINTN ProcessorCount;\r
1948 UINTN ProcessorNumber;\r
1949 UINTN CallerNumber;\r
1950 CPU_AP_DATA *CpuData;\r
1951 BOOLEAN HasEnabledAp;\r
1952 CPU_STATE ApState;\r
1953\r
1954 CpuMpData = GetCpuMpData ();\r
1955\r
1956 if (FailedCpuList != NULL) {\r
1957 *FailedCpuList = NULL;\r
1958 }\r
1959\r
1960 if (CpuMpData->CpuCount == 1) {\r
1961 return EFI_NOT_STARTED;\r
1962 }\r
1963\r
1964 if (Procedure == NULL) {\r
1965 return EFI_INVALID_PARAMETER;\r
1966 }\r
1967\r
1968 //\r
1969 // Check whether caller processor is BSP\r
1970 //\r
1971 MpInitLibWhoAmI (&CallerNumber);\r
1972 if (CallerNumber != CpuMpData->BspNumber) {\r
1973 return EFI_DEVICE_ERROR;\r
1974 }\r
1975\r
1976 //\r
1977 // Update AP state\r
1978 //\r
1979 CheckAndUpdateApsStatus ();\r
1980\r
1981 ProcessorCount = CpuMpData->CpuCount;\r
1982 HasEnabledAp = FALSE;\r
1983 //\r
1984 // Check whether all enabled APs are idle.\r
1985 // If any enabled AP is not idle, return EFI_NOT_READY.\r
1986 //\r
1987 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1988 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1989 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1990 ApState = GetApState (CpuData);\r
1991 if (ApState != CpuStateDisabled) {\r
1992 HasEnabledAp = TRUE;\r
1993 if (ApState != CpuStateIdle) {\r
1994 //\r
1995 // If any enabled APs are busy, return EFI_NOT_READY.\r
1996 //\r
1997 return EFI_NOT_READY;\r
1998 }\r
1999 }\r
2000 }\r
2001 }\r
2002\r
2003 if (!HasEnabledAp) {\r
2004 //\r
2005 // If no enabled AP exists, return EFI_NOT_STARTED.\r
2006 //\r
2007 return EFI_NOT_STARTED;\r
2008 }\r
2009\r
2010 CpuMpData->StartCount = 0;\r
2011 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
2012 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
2013 CpuData->Waiting = FALSE;\r
2014 if (ProcessorNumber != CpuMpData->BspNumber) {\r
2015 if (CpuData->State == CpuStateIdle) {\r
2016 //\r
2017 // Mark this processor as responsible for current calling.\r
2018 //\r
2019 CpuData->Waiting = TRUE;\r
2020 CpuMpData->StartCount++;\r
2021 }\r
2022 }\r
2023 }\r
2024\r
2025 CpuMpData->Procedure = Procedure;\r
2026 CpuMpData->ProcArguments = ProcedureArgument;\r
2027 CpuMpData->SingleThread = SingleThread;\r
2028 CpuMpData->FinishedCount = 0;\r
2029 CpuMpData->RunningCount = 0;\r
2030 CpuMpData->FailedCpuList = FailedCpuList;\r
2031 CpuMpData->ExpectedTime = CalculateTimeout (\r
2032 TimeoutInMicroseconds,\r
2033 &CpuMpData->CurrentTime\r
2034 );\r
2035 CpuMpData->TotalTime = 0;\r
2036 CpuMpData->WaitEvent = WaitEvent;\r
2037\r
2038 if (!SingleThread) {\r
2039 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
2040 } else {\r
2041 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
2042 if (ProcessorNumber == CallerNumber) {\r
2043 continue;\r
2044 }\r
2045 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
2046 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
2047 break;\r
2048 }\r
2049 }\r
2050 }\r
2051\r
2052 Status = EFI_SUCCESS;\r
2053 if (WaitEvent == NULL) {\r
2054 do {\r
2055 Status = CheckAllAPs ();\r
2056 } while (Status == EFI_NOT_READY);\r
2057 }\r
2058\r
2059 return Status;\r
2060}\r
2061\r
20ae5774
JF
2062/**\r
2063 Worker function to let the caller get one enabled AP to execute a caller-provided\r
2064 function.\r
2065\r
2066 @param[in] Procedure A pointer to the function to be run on\r
2067 enabled APs of the system.\r
2068 @param[in] ProcessorNumber The handle number of the AP.\r
2069 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
2070 service.\r
367284e7 2071 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
20ae5774
JF
2072 APs to return from Procedure, either for\r
2073 blocking or non-blocking mode.\r
2074 @param[in] ProcedureArgument The parameter passed into Procedure for\r
2075 all APs.\r
2076 @param[out] Finished If AP returns from Procedure before the\r
2077 timeout expires, its content is set to TRUE.\r
2078 Otherwise, the value is set to FALSE.\r
2079\r
2080 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
2081 the timeout expires.\r
2082 @retval others Failed to Startup AP.\r
2083\r
2084**/\r
2085EFI_STATUS\r
2086StartupThisAPWorker (\r
2087 IN EFI_AP_PROCEDURE Procedure,\r
2088 IN UINTN ProcessorNumber,\r
2089 IN EFI_EVENT WaitEvent OPTIONAL,\r
2090 IN UINTN TimeoutInMicroseconds,\r
2091 IN VOID *ProcedureArgument OPTIONAL,\r
2092 OUT BOOLEAN *Finished OPTIONAL\r
2093 )\r
2094{\r
2095 EFI_STATUS Status;\r
2096 CPU_MP_DATA *CpuMpData;\r
2097 CPU_AP_DATA *CpuData;\r
2098 UINTN CallerNumber;\r
2099\r
2100 CpuMpData = GetCpuMpData ();\r
2101\r
2102 if (Finished != NULL) {\r
2103 *Finished = FALSE;\r
2104 }\r
2105\r
2106 //\r
2107 // Check whether caller processor is BSP\r
2108 //\r
2109 MpInitLibWhoAmI (&CallerNumber);\r
2110 if (CallerNumber != CpuMpData->BspNumber) {\r
2111 return EFI_DEVICE_ERROR;\r
2112 }\r
2113\r
2114 //\r
2115 // Check whether processor with the handle specified by ProcessorNumber exists\r
2116 //\r
2117 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2118 return EFI_NOT_FOUND;\r
2119 }\r
2120\r
2121 //\r
2122 // Check whether specified processor is BSP\r
2123 //\r
2124 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2125 return EFI_INVALID_PARAMETER;\r
2126 }\r
2127\r
2128 //\r
2129 // Check parameter Procedure\r
2130 //\r
2131 if (Procedure == NULL) {\r
2132 return EFI_INVALID_PARAMETER;\r
2133 }\r
2134\r
2135 //\r
2136 // Update AP state\r
2137 //\r
2138 CheckAndUpdateApsStatus ();\r
2139\r
2140 //\r
2141 // Check whether specified AP is disabled\r
2142 //\r
2143 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
2144 return EFI_INVALID_PARAMETER;\r
2145 }\r
2146\r
2147 //\r
2148 // If WaitEvent is not NULL, execute in non-blocking mode.\r
2149 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
2150 // CheckAPsStatus() will check completion and timeout periodically.\r
2151 //\r
2152 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
2153 CpuData->WaitEvent = WaitEvent;\r
2154 CpuData->Finished = Finished;\r
2155 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
2156 CpuData->TotalTime = 0;\r
2157\r
2158 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
2159\r
2160 //\r
2161 // If WaitEvent is NULL, execute in blocking mode.\r
2162 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
2163 //\r
2164 Status = EFI_SUCCESS;\r
2165 if (WaitEvent == NULL) {\r
2166 do {\r
2167 Status = CheckThisAP (ProcessorNumber);\r
2168 } while (Status == EFI_NOT_READY);\r
2169 }\r
2170\r
2171 return Status;\r
2172}\r
2173\r
93ca4c0f
JF
2174/**\r
2175 Get pointer to CPU MP Data structure from GUIDed HOB.\r
2176\r
2177 @return The pointer to CPU MP Data structure.\r
2178**/\r
2179CPU_MP_DATA *\r
2180GetCpuMpDataFromGuidedHob (\r
2181 VOID\r
2182 )\r
2183{\r
2184 EFI_HOB_GUID_TYPE *GuidHob;\r
2185 VOID *DataInHob;\r
2186 CPU_MP_DATA *CpuMpData;\r
2187\r
2188 CpuMpData = NULL;\r
2189 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
2190 if (GuidHob != NULL) {\r
2191 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
2192 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
2193 }\r
2194 return CpuMpData;\r
2195}\r
42c37b3b 2196\r