]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
MdeModulePkg/TerminalDxe: Refine SetTerminalDevicePath
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
CommitLineData
3e8ad6bd
JF
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
93ca4c0f
JF
17EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;\r
18\r
7c3f2a12
JF
19/**\r
20 The function will check if BSP Execute Disable is enabled.\r
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
41be0da5
JF
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
03a1a925
JF
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
3e8ad6bd 111\r
ffab2442
JF
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
68cb9330
JF
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
9ebcf0f4
JF
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
b8b04307 285\r
8a2d564b
JF
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
31a1e4da 303 CPU_INFO_IN_HOB CpuInfo;\r
8a2d564b
JF
304 UINT32 ApCount;\r
305 CPU_INFO_IN_HOB *CpuInfoInHob;\r
306\r
307 ApCount = CpuMpData->CpuCount - 1;\r
31a1e4da 308 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
8a2d564b
JF
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
31a1e4da 315 ApicId = CpuInfoInHob[Index1].ApicId;\r
8a2d564b 316 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
31a1e4da 317 if (ApicId > CpuInfoInHob[Index2].ApicId) {\r
8a2d564b 318 Index3 = Index2;\r
31a1e4da 319 ApicId = CpuInfoInHob[Index2].ApicId;\r
8a2d564b
JF
320 }\r
321 }\r
322 if (Index3 != Index1) {\r
31a1e4da 323 CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));\r
8a2d564b 324 CopyMem (\r
31a1e4da
JF
325 &CpuInfoInHob[Index3],\r
326 &CpuInfoInHob[Index1],\r
327 sizeof (CPU_INFO_IN_HOB)\r
8a2d564b 328 );\r
31a1e4da 329 CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));\r
8a2d564b
JF
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
31a1e4da 338 if (CpuInfoInHob[Index1].ApicId == ApicId) {\r
8a2d564b
JF
339 CpuMpData->BspNumber = (UINT32) Index1;\r
340 break;\r
341 }\r
342 }\r
8a2d564b
JF
343 }\r
344}\r
345\r
fe627769
JF
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
b8b04307
JF
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
367284e7
DB
387 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
388 @param[out] ProcessorNumber Return the pocessor number found\r
b8b04307
JF
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
31a1e4da
JF
401 CPU_INFO_IN_HOB *CpuInfoInHob;\r
402\r
403 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
b8b04307
JF
404\r
405 TotalProcessorNumber = CpuMpData->CpuCount;\r
406 for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
31a1e4da 407 if (CpuInfoInHob[Index].ApicId == GetApicId ()) {\r
b8b04307
JF
408 *ProcessorNumber = Index;\r
409 return EFI_SUCCESS;\r
410 }\r
411 }\r
412 return EFI_NOT_FOUND;\r
413}\r
414\r
03434dff
JF
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
03434dff
JF
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
fe627769
JF
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
8a2d564b
JF
460 //\r
461 // Sort BSP/Aps by CPU APIC ID in ascending order\r
462 //\r
463 SortApicId (CpuMpData);\r
464\r
03434dff
JF
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
367284e7 470/**\r
03a1a925
JF
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
367284e7 476 @param[in] ApTopOfStack Top of AP stack\r
03a1a925
JF
477\r
478**/\r
479VOID\r
480InitializeApData (\r
481 IN OUT CPU_MP_DATA *CpuMpData,\r
482 IN UINTN ProcessorNumber,\r
845c5be1 483 IN UINT32 BistData,\r
dd3fa0cd 484 IN UINT64 ApTopOfStack\r
03a1a925
JF
485 )\r
486{\r
31a1e4da
JF
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
dd3fa0cd 493 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;\r
31a1e4da 494\r
03a1a925 495 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
03a1a925 496 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
31a1e4da 497 if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {\r
03a1a925
JF
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
b8b04307
JF
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
31a1e4da 530 CPU_INFO_IN_HOB *CpuInfoInHob;\r
dd3fa0cd 531 UINT64 ApTopOfStack;\r
b8b04307
JF
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
ffab2442
JF
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
b8b04307
JF
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
845c5be1 555 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;\r
dd3fa0cd 556 BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));\r
b8b04307
JF
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
845c5be1 565 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);\r
b8b04307
JF
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
43c9fdcc
JF
594 // Enable source debugging on AP function\r
595 // \r
596 EnableDebugAgent ();\r
597 //\r
b8b04307
JF
598 // Invoke AP function here\r
599 //\r
600 Procedure (Parameter);\r
31a1e4da 601 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
41be0da5
JF
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
b3775af2
JF
609 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
610 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;\r
41be0da5
JF
611 } else {\r
612 //\r
613 // Re-get the CPU APICID and Initial APICID\r
614 //\r
31a1e4da
JF
615 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();\r
616 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
41be0da5 617 }\r
b8b04307
JF
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
96f5920d
JF
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
7c3f2a12
JF
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
46d4b885
JF
730 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;\r
731 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
7c3f2a12
JF
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
6e1987f1
LE
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
96f5920d
JF
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
31a1e4da 781 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96f5920d
JF
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
ffab2442 791 SaveLocalApicTimerSetting (CpuMpData);\r
96f5920d
JF
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
c1192210
JF
820 if (CpuMpData->InitFlag == ApInitConfig) {\r
821 //\r
822 // Wait for all potential APs waken up in one specified period\r
823 //\r
6e1987f1
LE
824 TimedWaitForApFinish (\r
825 CpuMpData,\r
826 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
827 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
828 );\r
c1192210 829 } else {\r
96f5920d
JF
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
31a1e4da 851 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
96f5920d 852 SendInitSipiSipi (\r
31a1e4da 853 CpuInfoInHob[ProcessorNumber].ApicId,\r
96f5920d
JF
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
08085f08
JF
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
6e1987f1
LE
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
08085f08
JF
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
cb33bde4 1043 CpuMpData->InitFlag = ApInitReconfig;\r
08085f08 1044 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);\r
cb33bde4
JF
1045 while (CpuMpData->FinishedCount < 1) {\r
1046 CpuPause ();\r
1047 }\r
1048 CpuMpData->InitFlag = ApInitDone;\r
08085f08
JF
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
3e8ad6bd
JF
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
6a2ee2bb
JF
1270 CPU_MP_DATA *OldCpuMpData;\r
1271 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e59f8f6b
JF
1272 UINT32 MaxLogicalProcessorNumber;\r
1273 UINT32 ApStackSize;\r
f7f85d83 1274 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
e59f8f6b 1275 UINTN BufferSize;\r
9ebcf0f4 1276 UINT32 MonitorFilterSize;\r
e59f8f6b
JF
1277 VOID *MpBuffer;\r
1278 UINTN Buffer;\r
1279 CPU_MP_DATA *CpuMpData;\r
9ebcf0f4 1280 UINT8 ApLoopMode;\r
e59f8f6b 1281 UINT8 *MonitorBuffer;\r
03a1a925 1282 UINTN Index;\r
f7f85d83 1283 UINTN ApResetVectorSize;\r
e59f8f6b 1284 UINTN BackupBufferAddr;\r
6a2ee2bb
JF
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
14e8137c 1292 ASSERT (MaxLogicalProcessorNumber != 0);\r
f7f85d83
JF
1293\r
1294 AsmGetAddressMap (&AddressMap);\r
1295 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
e59f8f6b 1296 ApStackSize = PcdGet32(PcdCpuApStackSize);\r
9ebcf0f4
JF
1297 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
1298\r
e59f8f6b
JF
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
d11f10d1 1316 CpuMpData->SaveRestoreFlag = FALSE;\r
e59f8f6b
JF
1317 CpuMpData->WakeupBuffer = (UINTN) -1;\r
1318 CpuMpData->CpuCount = 1;\r
1319 CpuMpData->BspNumber = 0;\r
1320 CpuMpData->WaitEvent = NULL;\r
41be0da5 1321 CpuMpData->SwitchBspFlag = FALSE;\r
e59f8f6b
JF
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
68cb9330
JF
1326 // Save BSP's Control registers to APs\r
1327 //\r
1328 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
1329 //\r
03a1a925
JF
1330 // Set BSP basic information\r
1331 //\r
845c5be1 1332 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
03a1a925 1333 //\r
e59f8f6b
JF
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
03a1a925
JF
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
94f63c76
JF
1349 //\r
1350 // Load Microcode on BSP\r
1351 //\r
1352 MicrocodeDetect (CpuMpData);\r
1353 //\r
e59f8f6b
JF
1354 // Store BSP's MTRR setting\r
1355 //\r
1356 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
1357\r
6a2ee2bb 1358 if (OldCpuMpData == NULL) {\r
14e8137c
JF
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
6a2ee2bb
JF
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
31a1e4da
JF
1373 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
1374 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
6a2ee2bb
JF
1375 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1376 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
31a1e4da 1377 if (CpuInfoInHob[Index].InitialApicId >= 255) {\r
6a2ee2bb
JF
1378 CpuMpData->X2ApicEnable = TRUE;\r
1379 }\r
31a1e4da 1380 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
6a2ee2bb
JF
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
14e8137c
JF
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
6a2ee2bb
JF
1403 }\r
1404 }\r
93ca4c0f
JF
1405\r
1406 //\r
1407 // Initialize global data for MP support\r
1408 //\r
1409 InitMpGlobalData (CpuMpData);\r
1410\r
f7f85d83 1411 return EFI_SUCCESS;\r
3e8ad6bd
JF
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
ad52f25e
JF
1439 CPU_MP_DATA *CpuMpData;\r
1440 UINTN CallerNumber;\r
31a1e4da 1441 CPU_INFO_IN_HOB *CpuInfoInHob;\r
ad52f25e
JF
1442\r
1443 CpuMpData = GetCpuMpData ();\r
31a1e4da 1444 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
ad52f25e
JF
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
31a1e4da 1462 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
ad52f25e
JF
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
262128e5 1479 GetProcessorLocationByApicId (\r
31a1e4da 1480 CpuInfoInHob[ProcessorNumber].ApicId,\r
73152f19
LD
1481 &ProcessorInfoBuffer->Location.Package,\r
1482 &ProcessorInfoBuffer->Location.Core,\r
1483 &ProcessorInfoBuffer->Location.Thread\r
1484 );\r
ad52f25e
JF
1485\r
1486 if (HealthData != NULL) {\r
31a1e4da 1487 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
ad52f25e
JF
1488 }\r
1489\r
1490 return EFI_SUCCESS;\r
3e8ad6bd 1491}\r
ad52f25e 1492\r
41be0da5
JF
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
a8d75a18 1514 BOOLEAN OldInterruptState;\r
26b43433 1515 BOOLEAN OldTimerInterruptState;\r
a8d75a18 1516\r
26b43433
JF
1517 //\r
1518 // Save and Disable Local APIC timer interrupt\r
1519 //\r
1520 OldTimerInterruptState = GetApicTimerInterruptState ();\r
1521 DisableApicTimerInterrupt ();\r
a8d75a18
JF
1522 //\r
1523 // Before send both BSP and AP to a procedure to exchange their roles,\r
1524 // interrupt must be disabled. This is because during the exchange role\r
1525 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
1526 // be corrupted, since interrupt return address will be pushed to stack\r
1527 // by hardware.\r
1528 //\r
1529 OldInterruptState = SaveAndDisableInterrupts ();\r
1530\r
1531 //\r
1532 // Mask LINT0 & LINT1 for the old BSP\r
1533 //\r
1534 DisableLvtInterrupts ();\r
41be0da5
JF
1535\r
1536 CpuMpData = GetCpuMpData ();\r
1537\r
1538 //\r
1539 // Check whether caller processor is BSP\r
1540 //\r
1541 MpInitLibWhoAmI (&CallerNumber);\r
1542 if (CallerNumber != CpuMpData->BspNumber) {\r
1543 return EFI_SUCCESS;\r
1544 }\r
1545\r
1546 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1547 return EFI_NOT_FOUND;\r
1548 }\r
1549\r
1550 //\r
1551 // Check whether specified AP is disabled\r
1552 //\r
1553 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
1554 if (State == CpuStateDisabled) {\r
1555 return EFI_INVALID_PARAMETER;\r
1556 }\r
1557\r
1558 //\r
1559 // Check whether ProcessorNumber specifies the current BSP\r
1560 //\r
1561 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1562 return EFI_INVALID_PARAMETER;\r
1563 }\r
1564\r
1565 //\r
1566 // Check whether specified AP is busy\r
1567 //\r
1568 if (State == CpuStateBusy) {\r
1569 return EFI_NOT_READY;\r
1570 }\r
1571\r
1572 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
1573 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
1574 CpuMpData->SwitchBspFlag = TRUE;\r
b3775af2 1575 CpuMpData->NewBspNumber = ProcessorNumber;\r
41be0da5
JF
1576\r
1577 //\r
1578 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
1579 //\r
1580 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1581 ApicBaseMsr.Bits.BSP = 0;\r
1582 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1583\r
1584 //\r
1585 // Need to wakeUp AP (future BSP).\r
1586 //\r
1587 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
1588\r
1589 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
1590\r
1591 //\r
1592 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
1593 //\r
1594 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1595 ApicBaseMsr.Bits.BSP = 1;\r
1596 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1597\r
1598 //\r
1599 // Wait for old BSP finished AP task\r
1600 //\r
1601 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
1602 CpuPause ();\r
1603 }\r
1604\r
1605 CpuMpData->SwitchBspFlag = FALSE;\r
1606 //\r
1607 // Set old BSP enable state\r
1608 //\r
1609 if (!EnableOldBSP) {\r
1610 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
af8ba51a
JF
1611 } else {\r
1612 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);\r
41be0da5
JF
1613 }\r
1614 //\r
1615 // Save new BSP number\r
1616 //\r
1617 CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
1618\r
a8d75a18
JF
1619 //\r
1620 // Restore interrupt state.\r
1621 //\r
1622 SetInterruptState (OldInterruptState);\r
1623\r
26b43433
JF
1624 if (OldTimerInterruptState) {\r
1625 EnableApicTimerInterrupt ();\r
1626 }\r
a8d75a18 1627\r
41be0da5
JF
1628 return EFI_SUCCESS;\r
1629}\r
ad52f25e 1630\r
e37109bc
JF
1631/**\r
1632 Worker function to let the caller enable or disable an AP from this point onward.\r
1633 This service may only be called from the BSP.\r
1634\r
1635 @param[in] ProcessorNumber The handle number of AP.\r
1636 @param[in] EnableAP Specifies the new state for the processor for\r
1637 enabled, FALSE for disabled.\r
1638 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
1639 the new health status of the AP.\r
1640\r
1641 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1642 @retval others Failed to Enable/Disable AP.\r
1643\r
1644**/\r
1645EFI_STATUS\r
1646EnableDisableApWorker (\r
1647 IN UINTN ProcessorNumber,\r
1648 IN BOOLEAN EnableAP,\r
1649 IN UINT32 *HealthFlag OPTIONAL\r
1650 )\r
1651{\r
1652 CPU_MP_DATA *CpuMpData;\r
1653 UINTN CallerNumber;\r
1654\r
1655 CpuMpData = GetCpuMpData ();\r
1656\r
1657 //\r
1658 // Check whether caller processor is BSP\r
1659 //\r
1660 MpInitLibWhoAmI (&CallerNumber);\r
1661 if (CallerNumber != CpuMpData->BspNumber) {\r
1662 return EFI_DEVICE_ERROR;\r
1663 }\r
1664\r
1665 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1666 return EFI_INVALID_PARAMETER;\r
1667 }\r
1668\r
1669 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1670 return EFI_NOT_FOUND;\r
1671 }\r
1672\r
1673 if (!EnableAP) {\r
1674 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
1675 } else {\r
1676 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1677 }\r
1678\r
1679 if (HealthFlag != NULL) {\r
1680 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
1681 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
1682 }\r
1683\r
1684 return EFI_SUCCESS;\r
1685}\r
1686\r
3e8ad6bd
JF
1687/**\r
1688 This return the handle number for the calling processor. This service may be\r
1689 called from the BSP and APs.\r
1690\r
1691 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
1692 The range is from 0 to the total number of\r
1693 logical processors minus 1. The total number of\r
1694 logical processors can be retrieved by\r
1695 MpInitLibGetNumberOfProcessors().\r
1696\r
1697 @retval EFI_SUCCESS The current processor handle number was returned\r
1698 in ProcessorNumber.\r
1699 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1700 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1701\r
1702**/\r
1703EFI_STATUS\r
1704EFIAPI\r
1705MpInitLibWhoAmI (\r
1706 OUT UINTN *ProcessorNumber\r
1707 )\r
1708{\r
5c9e0997
JF
1709 CPU_MP_DATA *CpuMpData;\r
1710\r
1711 if (ProcessorNumber == NULL) {\r
1712 return EFI_INVALID_PARAMETER;\r
1713 }\r
1714\r
1715 CpuMpData = GetCpuMpData ();\r
1716\r
1717 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 1718}\r
809213a6 1719\r
3e8ad6bd
JF
1720/**\r
1721 Retrieves the number of logical processor in the platform and the number of\r
1722 those logical processors that are enabled on this boot. This service may only\r
1723 be called from the BSP.\r
1724\r
1725 @param[out] NumberOfProcessors Pointer to the total number of logical\r
1726 processors in the system, including the BSP\r
1727 and disabled APs.\r
1728 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
1729 processors that exist in system, including\r
1730 the BSP.\r
1731\r
1732 @retval EFI_SUCCESS The number of logical processors and enabled\r
1733 logical processors was retrieved.\r
1734 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1735 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
1736 is NULL.\r
1737 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1738\r
1739**/\r
1740EFI_STATUS\r
1741EFIAPI\r
1742MpInitLibGetNumberOfProcessors (\r
1743 OUT UINTN *NumberOfProcessors, OPTIONAL\r
1744 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
1745 )\r
1746{\r
809213a6
JF
1747 CPU_MP_DATA *CpuMpData;\r
1748 UINTN CallerNumber;\r
1749 UINTN ProcessorNumber;\r
1750 UINTN EnabledProcessorNumber;\r
1751 UINTN Index;\r
1752\r
1753 CpuMpData = GetCpuMpData ();\r
1754\r
1755 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
1756 return EFI_INVALID_PARAMETER;\r
1757 }\r
1758\r
1759 //\r
1760 // Check whether caller processor is BSP\r
1761 //\r
1762 MpInitLibWhoAmI (&CallerNumber);\r
1763 if (CallerNumber != CpuMpData->BspNumber) {\r
1764 return EFI_DEVICE_ERROR;\r
1765 }\r
1766\r
1767 ProcessorNumber = CpuMpData->CpuCount;\r
1768 EnabledProcessorNumber = 0;\r
1769 for (Index = 0; Index < ProcessorNumber; Index++) {\r
1770 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
1771 EnabledProcessorNumber ++;\r
1772 }\r
1773 }\r
1774\r
1775 if (NumberOfProcessors != NULL) {\r
1776 *NumberOfProcessors = ProcessorNumber;\r
1777 }\r
1778 if (NumberOfEnabledProcessors != NULL) {\r
1779 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
1780 }\r
1781\r
1782 return EFI_SUCCESS;\r
3e8ad6bd 1783}\r
6a2ee2bb 1784\r
809213a6 1785\r
86efe976
JF
1786/**\r
1787 Worker function to execute a caller provided function on all enabled APs.\r
1788\r
1789 @param[in] Procedure A pointer to the function to be run on\r
1790 enabled APs of the system.\r
1791 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
1792 the function specified by Procedure one by\r
1793 one, in ascending order of processor handle\r
1794 number. If FALSE, then all the enabled APs\r
1795 execute the function specified by Procedure\r
1796 simultaneously.\r
1797 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1798 service.\r
367284e7 1799 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
86efe976
JF
1800 APs to return from Procedure, either for\r
1801 blocking or non-blocking mode.\r
1802 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1803 all APs.\r
1804 @param[out] FailedCpuList If all APs finish successfully, then its\r
1805 content is set to NULL. If not all APs\r
1806 finish before timeout expires, then its\r
1807 content is set to address of the buffer\r
1808 holding handle numbers of the failed APs.\r
1809\r
1810 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1811 the timeout expired.\r
1812 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1813 to all enabled APs.\r
1814 @retval others Failed to Startup all APs.\r
1815\r
1816**/\r
1817EFI_STATUS\r
1818StartupAllAPsWorker (\r
1819 IN EFI_AP_PROCEDURE Procedure,\r
1820 IN BOOLEAN SingleThread,\r
1821 IN EFI_EVENT WaitEvent OPTIONAL,\r
1822 IN UINTN TimeoutInMicroseconds,\r
1823 IN VOID *ProcedureArgument OPTIONAL,\r
1824 OUT UINTN **FailedCpuList OPTIONAL\r
1825 )\r
1826{\r
1827 EFI_STATUS Status;\r
1828 CPU_MP_DATA *CpuMpData;\r
1829 UINTN ProcessorCount;\r
1830 UINTN ProcessorNumber;\r
1831 UINTN CallerNumber;\r
1832 CPU_AP_DATA *CpuData;\r
1833 BOOLEAN HasEnabledAp;\r
1834 CPU_STATE ApState;\r
1835\r
1836 CpuMpData = GetCpuMpData ();\r
1837\r
1838 if (FailedCpuList != NULL) {\r
1839 *FailedCpuList = NULL;\r
1840 }\r
1841\r
1842 if (CpuMpData->CpuCount == 1) {\r
1843 return EFI_NOT_STARTED;\r
1844 }\r
1845\r
1846 if (Procedure == NULL) {\r
1847 return EFI_INVALID_PARAMETER;\r
1848 }\r
1849\r
1850 //\r
1851 // Check whether caller processor is BSP\r
1852 //\r
1853 MpInitLibWhoAmI (&CallerNumber);\r
1854 if (CallerNumber != CpuMpData->BspNumber) {\r
1855 return EFI_DEVICE_ERROR;\r
1856 }\r
1857\r
1858 //\r
1859 // Update AP state\r
1860 //\r
1861 CheckAndUpdateApsStatus ();\r
1862\r
1863 ProcessorCount = CpuMpData->CpuCount;\r
1864 HasEnabledAp = FALSE;\r
1865 //\r
1866 // Check whether all enabled APs are idle.\r
1867 // If any enabled AP is not idle, return EFI_NOT_READY.\r
1868 //\r
1869 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1870 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1871 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1872 ApState = GetApState (CpuData);\r
1873 if (ApState != CpuStateDisabled) {\r
1874 HasEnabledAp = TRUE;\r
1875 if (ApState != CpuStateIdle) {\r
1876 //\r
1877 // If any enabled APs are busy, return EFI_NOT_READY.\r
1878 //\r
1879 return EFI_NOT_READY;\r
1880 }\r
1881 }\r
1882 }\r
1883 }\r
1884\r
1885 if (!HasEnabledAp) {\r
1886 //\r
1887 // If no enabled AP exists, return EFI_NOT_STARTED.\r
1888 //\r
1889 return EFI_NOT_STARTED;\r
1890 }\r
1891\r
1892 CpuMpData->StartCount = 0;\r
1893 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1894 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1895 CpuData->Waiting = FALSE;\r
1896 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1897 if (CpuData->State == CpuStateIdle) {\r
1898 //\r
1899 // Mark this processor as responsible for current calling.\r
1900 //\r
1901 CpuData->Waiting = TRUE;\r
1902 CpuMpData->StartCount++;\r
1903 }\r
1904 }\r
1905 }\r
1906\r
1907 CpuMpData->Procedure = Procedure;\r
1908 CpuMpData->ProcArguments = ProcedureArgument;\r
1909 CpuMpData->SingleThread = SingleThread;\r
1910 CpuMpData->FinishedCount = 0;\r
1911 CpuMpData->RunningCount = 0;\r
1912 CpuMpData->FailedCpuList = FailedCpuList;\r
1913 CpuMpData->ExpectedTime = CalculateTimeout (\r
1914 TimeoutInMicroseconds,\r
1915 &CpuMpData->CurrentTime\r
1916 );\r
1917 CpuMpData->TotalTime = 0;\r
1918 CpuMpData->WaitEvent = WaitEvent;\r
1919\r
1920 if (!SingleThread) {\r
1921 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
1922 } else {\r
1923 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1924 if (ProcessorNumber == CallerNumber) {\r
1925 continue;\r
1926 }\r
1927 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1928 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
1929 break;\r
1930 }\r
1931 }\r
1932 }\r
1933\r
1934 Status = EFI_SUCCESS;\r
1935 if (WaitEvent == NULL) {\r
1936 do {\r
1937 Status = CheckAllAPs ();\r
1938 } while (Status == EFI_NOT_READY);\r
1939 }\r
1940\r
1941 return Status;\r
1942}\r
1943\r
20ae5774
JF
1944/**\r
1945 Worker function to let the caller get one enabled AP to execute a caller-provided\r
1946 function.\r
1947\r
1948 @param[in] Procedure A pointer to the function to be run on\r
1949 enabled APs of the system.\r
1950 @param[in] ProcessorNumber The handle number of the AP.\r
1951 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1952 service.\r
367284e7 1953 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
20ae5774
JF
1954 APs to return from Procedure, either for\r
1955 blocking or non-blocking mode.\r
1956 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1957 all APs.\r
1958 @param[out] Finished If AP returns from Procedure before the\r
1959 timeout expires, its content is set to TRUE.\r
1960 Otherwise, the value is set to FALSE.\r
1961\r
1962 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
1963 the timeout expires.\r
1964 @retval others Failed to Startup AP.\r
1965\r
1966**/\r
1967EFI_STATUS\r
1968StartupThisAPWorker (\r
1969 IN EFI_AP_PROCEDURE Procedure,\r
1970 IN UINTN ProcessorNumber,\r
1971 IN EFI_EVENT WaitEvent OPTIONAL,\r
1972 IN UINTN TimeoutInMicroseconds,\r
1973 IN VOID *ProcedureArgument OPTIONAL,\r
1974 OUT BOOLEAN *Finished OPTIONAL\r
1975 )\r
1976{\r
1977 EFI_STATUS Status;\r
1978 CPU_MP_DATA *CpuMpData;\r
1979 CPU_AP_DATA *CpuData;\r
1980 UINTN CallerNumber;\r
1981\r
1982 CpuMpData = GetCpuMpData ();\r
1983\r
1984 if (Finished != NULL) {\r
1985 *Finished = FALSE;\r
1986 }\r
1987\r
1988 //\r
1989 // Check whether caller processor is BSP\r
1990 //\r
1991 MpInitLibWhoAmI (&CallerNumber);\r
1992 if (CallerNumber != CpuMpData->BspNumber) {\r
1993 return EFI_DEVICE_ERROR;\r
1994 }\r
1995\r
1996 //\r
1997 // Check whether processor with the handle specified by ProcessorNumber exists\r
1998 //\r
1999 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2000 return EFI_NOT_FOUND;\r
2001 }\r
2002\r
2003 //\r
2004 // Check whether specified processor is BSP\r
2005 //\r
2006 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2007 return EFI_INVALID_PARAMETER;\r
2008 }\r
2009\r
2010 //\r
2011 // Check parameter Procedure\r
2012 //\r
2013 if (Procedure == NULL) {\r
2014 return EFI_INVALID_PARAMETER;\r
2015 }\r
2016\r
2017 //\r
2018 // Update AP state\r
2019 //\r
2020 CheckAndUpdateApsStatus ();\r
2021\r
2022 //\r
2023 // Check whether specified AP is disabled\r
2024 //\r
2025 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
2026 return EFI_INVALID_PARAMETER;\r
2027 }\r
2028\r
2029 //\r
2030 // If WaitEvent is not NULL, execute in non-blocking mode.\r
2031 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
2032 // CheckAPsStatus() will check completion and timeout periodically.\r
2033 //\r
2034 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
2035 CpuData->WaitEvent = WaitEvent;\r
2036 CpuData->Finished = Finished;\r
2037 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
2038 CpuData->TotalTime = 0;\r
2039\r
2040 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
2041\r
2042 //\r
2043 // If WaitEvent is NULL, execute in blocking mode.\r
2044 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
2045 //\r
2046 Status = EFI_SUCCESS;\r
2047 if (WaitEvent == NULL) {\r
2048 do {\r
2049 Status = CheckThisAP (ProcessorNumber);\r
2050 } while (Status == EFI_NOT_READY);\r
2051 }\r
2052\r
2053 return Status;\r
2054}\r
2055\r
93ca4c0f
JF
2056/**\r
2057 Get pointer to CPU MP Data structure from GUIDed HOB.\r
2058\r
2059 @return The pointer to CPU MP Data structure.\r
2060**/\r
2061CPU_MP_DATA *\r
2062GetCpuMpDataFromGuidedHob (\r
2063 VOID\r
2064 )\r
2065{\r
2066 EFI_HOB_GUID_TYPE *GuidHob;\r
2067 VOID *DataInHob;\r
2068 CPU_MP_DATA *CpuMpData;\r
2069\r
2070 CpuMpData = NULL;\r
2071 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
2072 if (GuidHob != NULL) {\r
2073 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
2074 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
2075 }\r
2076 return CpuMpData;\r
2077}\r
42c37b3b
JF
2078\r
2079/**\r
2080 Get available system memory below 1MB by specified size.\r
2081\r
2082 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
2083**/\r
2084VOID\r
2085BackupAndPrepareWakeupBuffer(\r
2086 IN CPU_MP_DATA *CpuMpData\r
2087 )\r
2088{\r
2089 CopyMem (\r
2090 (VOID *) CpuMpData->BackupBuffer,\r
2091 (VOID *) CpuMpData->WakeupBuffer,\r
2092 CpuMpData->BackupBufferSize\r
2093 );\r
2094 CopyMem (\r
2095 (VOID *) CpuMpData->WakeupBuffer,\r
2096 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
2097 CpuMpData->AddressMap.RendezvousFunnelSize\r
2098 );\r
2099}\r
2100\r
2101/**\r
2102 Restore wakeup buffer data.\r
2103\r
2104 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
2105**/\r
2106VOID\r
2107RestoreWakeupBuffer(\r
2108 IN CPU_MP_DATA *CpuMpData\r
2109 )\r
2110{\r
2111 CopyMem (\r
2112 (VOID *) CpuMpData->WakeupBuffer,\r
2113 (VOID *) CpuMpData->BackupBuffer,\r
2114 CpuMpData->BackupBufferSize\r
2115 );\r
2116}\r