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