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