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