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