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