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