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