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