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