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