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