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