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