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