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