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