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