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