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