]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpInitLib: Check APIC mode change around AP function
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
CommitLineData
3e8ad6bd
JF
1/** @file\r
2 CPU MP Initialize Library common functions.\r
3\r
844b2d07 4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
3e8ad6bd
JF
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
844b2d07
JF
21\r
22 DxeIpl may have enabled Execute Disable for BSP, APs need to\r
23 get the status and sync up the settings.\r
24 If BSP's CR0.Paging is not set, BSP execute Disble feature is\r
25 not working actually.\r
7c3f2a12
JF
26\r
27 @retval TRUE BSP Execute Disable is enabled.\r
28 @retval FALSE BSP Execute Disable is not enabled.\r
29**/\r
30BOOLEAN\r
31IsBspExecuteDisableEnabled (\r
32 VOID\r
33 )\r
34{\r
35 UINT32 Eax;\r
36 CPUID_EXTENDED_CPU_SIG_EDX Edx;\r
37 MSR_IA32_EFER_REGISTER EferMsr;\r
38 BOOLEAN Enabled;\r
844b2d07 39 IA32_CR0 Cr0;\r
7c3f2a12
JF
40\r
41 Enabled = FALSE;\r
844b2d07
JF
42 Cr0.UintN = AsmReadCr0 ();\r
43 if (Cr0.Bits.PG != 0) {\r
7c3f2a12 44 //\r
844b2d07 45 // If CR0 Paging bit is set\r
7c3f2a12 46 //\r
844b2d07
JF
47 AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);\r
48 if (Eax >= CPUID_EXTENDED_CPU_SIG) {\r
49 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &Edx.Uint32);\r
7c3f2a12 50 //\r
844b2d07
JF
51 // CPUID 0x80000001\r
52 // Bit 20: Execute Disable Bit available.\r
7c3f2a12 53 //\r
844b2d07
JF
54 if (Edx.Bits.NX != 0) {\r
55 EferMsr.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);\r
56 //\r
57 // MSR 0xC0000080\r
58 // Bit 11: Execute Disable Bit enable.\r
59 //\r
60 if (EferMsr.Bits.NXE != 0) {\r
61 Enabled = TRUE;\r
62 }\r
7c3f2a12
JF
63 }\r
64 }\r
65 }\r
66\r
67 return Enabled;\r
68}\r
69\r
41be0da5
JF
70/**\r
71 Worker function for SwitchBSP().\r
72\r
73 Worker function for SwitchBSP(), assigned to the AP which is intended\r
74 to become BSP.\r
75\r
76 @param[in] Buffer Pointer to CPU MP Data\r
77**/\r
78VOID\r
79EFIAPI\r
80FutureBSPProc (\r
81 IN VOID *Buffer\r
82 )\r
83{\r
84 CPU_MP_DATA *DataInHob;\r
85\r
86 DataInHob = (CPU_MP_DATA *) Buffer;\r
87 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);\r
88}\r
89\r
03a1a925
JF
90/**\r
91 Get the Application Processors state.\r
92\r
93 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP\r
94\r
95 @return The AP status\r
96**/\r
97CPU_STATE\r
98GetApState (\r
99 IN CPU_AP_DATA *CpuData\r
100 )\r
101{\r
102 return CpuData->State;\r
103}\r
104\r
105/**\r
106 Set the Application Processors state.\r
107\r
108 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP\r
109 @param[in] State The AP status\r
110**/\r
111VOID\r
112SetApState (\r
113 IN CPU_AP_DATA *CpuData,\r
114 IN CPU_STATE State\r
115 )\r
116{\r
117 AcquireSpinLock (&CpuData->ApLock);\r
118 CpuData->State = State;\r
119 ReleaseSpinLock (&CpuData->ApLock);\r
120}\r
3e8ad6bd 121\r
ffab2442 122/**\r
f70174d6 123 Save BSP's local APIC timer setting.\r
ffab2442
JF
124\r
125 @param[in] CpuMpData Pointer to CPU MP Data\r
126**/\r
127VOID\r
128SaveLocalApicTimerSetting (\r
129 IN CPU_MP_DATA *CpuMpData\r
130 )\r
131{\r
132 //\r
133 // Record the current local APIC timer setting of BSP\r
134 //\r
135 GetApicTimerState (\r
136 &CpuMpData->DivideValue,\r
137 &CpuMpData->PeriodicMode,\r
138 &CpuMpData->Vector\r
139 );\r
140 CpuMpData->CurrentTimerCount = GetApicTimerCurrentCount ();\r
141 CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();\r
142}\r
143\r
144/**\r
145 Sync local APIC timer setting from BSP to AP.\r
146\r
147 @param[in] CpuMpData Pointer to CPU MP Data\r
148**/\r
149VOID\r
150SyncLocalApicTimerSetting (\r
151 IN CPU_MP_DATA *CpuMpData\r
152 )\r
153{\r
154 //\r
155 // Sync local APIC timer setting from BSP to AP\r
156 //\r
157 InitializeApicTimer (\r
158 CpuMpData->DivideValue,\r
159 CpuMpData->CurrentTimerCount,\r
160 CpuMpData->PeriodicMode,\r
161 CpuMpData->Vector\r
162 );\r
163 //\r
164 // Disable AP's local APIC timer interrupt\r
165 //\r
166 DisableApicTimerInterrupt ();\r
167}\r
168\r
68cb9330
JF
169/**\r
170 Save the volatile registers required to be restored following INIT IPI.\r
171\r
172 @param[out] VolatileRegisters Returns buffer saved the volatile resisters\r
173**/\r
174VOID\r
175SaveVolatileRegisters (\r
176 OUT CPU_VOLATILE_REGISTERS *VolatileRegisters\r
177 )\r
178{\r
179 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
180\r
181 VolatileRegisters->Cr0 = AsmReadCr0 ();\r
182 VolatileRegisters->Cr3 = AsmReadCr3 ();\r
183 VolatileRegisters->Cr4 = AsmReadCr4 ();\r
184\r
185 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
186 if (VersionInfoEdx.Bits.DE != 0) {\r
187 //\r
188 // If processor supports Debugging Extensions feature\r
189 // by CPUID.[EAX=01H]:EDX.BIT2\r
190 //\r
191 VolatileRegisters->Dr0 = AsmReadDr0 ();\r
192 VolatileRegisters->Dr1 = AsmReadDr1 ();\r
193 VolatileRegisters->Dr2 = AsmReadDr2 ();\r
194 VolatileRegisters->Dr3 = AsmReadDr3 ();\r
195 VolatileRegisters->Dr6 = AsmReadDr6 ();\r
196 VolatileRegisters->Dr7 = AsmReadDr7 ();\r
197 }\r
198}\r
199\r
200/**\r
201 Restore the volatile registers following INIT IPI.\r
202\r
203 @param[in] VolatileRegisters Pointer to volatile resisters\r
204 @param[in] IsRestoreDr TRUE: Restore DRx if supported\r
205 FALSE: Do not restore DRx\r
206**/\r
207VOID\r
208RestoreVolatileRegisters (\r
209 IN CPU_VOLATILE_REGISTERS *VolatileRegisters,\r
210 IN BOOLEAN IsRestoreDr\r
211 )\r
212{\r
213 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
214\r
215 AsmWriteCr0 (VolatileRegisters->Cr0);\r
216 AsmWriteCr3 (VolatileRegisters->Cr3);\r
217 AsmWriteCr4 (VolatileRegisters->Cr4);\r
218\r
219 if (IsRestoreDr) {\r
220 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
221 if (VersionInfoEdx.Bits.DE != 0) {\r
222 //\r
223 // If processor supports Debugging Extensions feature\r
224 // by CPUID.[EAX=01H]:EDX.BIT2\r
225 //\r
226 AsmWriteDr0 (VolatileRegisters->Dr0);\r
227 AsmWriteDr1 (VolatileRegisters->Dr1);\r
228 AsmWriteDr2 (VolatileRegisters->Dr2);\r
229 AsmWriteDr3 (VolatileRegisters->Dr3);\r
230 AsmWriteDr6 (VolatileRegisters->Dr6);\r
231 AsmWriteDr7 (VolatileRegisters->Dr7);\r
232 }\r
233 }\r
234}\r
235\r
9ebcf0f4
JF
236/**\r
237 Detect whether Mwait-monitor feature is supported.\r
238\r
239 @retval TRUE Mwait-monitor feature is supported.\r
240 @retval FALSE Mwait-monitor feature is not supported.\r
241**/\r
242BOOLEAN\r
243IsMwaitSupport (\r
244 VOID\r
245 )\r
246{\r
247 CPUID_VERSION_INFO_ECX VersionInfoEcx;\r
248\r
249 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);\r
250 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;\r
251}\r
252\r
253/**\r
254 Get AP loop mode.\r
255\r
256 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.\r
257\r
258 @return The AP loop mode.\r
259**/\r
260UINT8\r
261GetApLoopMode (\r
262 OUT UINT32 *MonitorFilterSize\r
263 )\r
264{\r
265 UINT8 ApLoopMode;\r
266 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;\r
267\r
268 ASSERT (MonitorFilterSize != NULL);\r
269\r
270 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
271 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);\r
272 if (ApLoopMode == ApInMwaitLoop) {\r
273 if (!IsMwaitSupport ()) {\r
274 //\r
275 // If processor does not support MONITOR/MWAIT feature,\r
276 // force AP in Hlt-loop mode\r
277 //\r
278 ApLoopMode = ApInHltLoop;\r
279 }\r
280 }\r
281\r
282 if (ApLoopMode != ApInMwaitLoop) {\r
283 *MonitorFilterSize = sizeof (UINT32);\r
284 } else {\r
285 //\r
286 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes\r
287 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT\r
288 //\r
289 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);\r
290 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;\r
291 }\r
292\r
293 return ApLoopMode;\r
294}\r
b8b04307 295\r
8a2d564b
JF
296/**\r
297 Sort the APIC ID of all processors.\r
298\r
299 This function sorts the APIC ID of all processors so that processor number is\r
300 assigned in the ascending order of APIC ID which eases MP debugging.\r
301\r
302 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
303**/\r
304VOID\r
305SortApicId (\r
306 IN CPU_MP_DATA *CpuMpData\r
307 )\r
308{\r
309 UINTN Index1;\r
310 UINTN Index2;\r
311 UINTN Index3;\r
312 UINT32 ApicId;\r
31a1e4da 313 CPU_INFO_IN_HOB CpuInfo;\r
8a2d564b
JF
314 UINT32 ApCount;\r
315 CPU_INFO_IN_HOB *CpuInfoInHob;\r
316\r
317 ApCount = CpuMpData->CpuCount - 1;\r
31a1e4da 318 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
8a2d564b
JF
319 if (ApCount != 0) {\r
320 for (Index1 = 0; Index1 < ApCount; Index1++) {\r
321 Index3 = Index1;\r
322 //\r
323 // Sort key is the hardware default APIC ID\r
324 //\r
31a1e4da 325 ApicId = CpuInfoInHob[Index1].ApicId;\r
8a2d564b 326 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
31a1e4da 327 if (ApicId > CpuInfoInHob[Index2].ApicId) {\r
8a2d564b 328 Index3 = Index2;\r
31a1e4da 329 ApicId = CpuInfoInHob[Index2].ApicId;\r
8a2d564b
JF
330 }\r
331 }\r
332 if (Index3 != Index1) {\r
31a1e4da 333 CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));\r
8a2d564b 334 CopyMem (\r
31a1e4da
JF
335 &CpuInfoInHob[Index3],\r
336 &CpuInfoInHob[Index1],\r
337 sizeof (CPU_INFO_IN_HOB)\r
8a2d564b 338 );\r
31a1e4da 339 CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));\r
8a2d564b
JF
340 }\r
341 }\r
342\r
343 //\r
344 // Get the processor number for the BSP\r
345 //\r
346 ApicId = GetInitialApicId ();\r
347 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
31a1e4da 348 if (CpuInfoInHob[Index1].ApicId == ApicId) {\r
8a2d564b
JF
349 CpuMpData->BspNumber = (UINT32) Index1;\r
350 break;\r
351 }\r
352 }\r
8a2d564b
JF
353 }\r
354}\r
355\r
fe627769
JF
356/**\r
357 Enable x2APIC mode on APs.\r
358\r
359 @param[in, out] Buffer Pointer to private data buffer.\r
360**/\r
361VOID\r
362EFIAPI\r
363ApFuncEnableX2Apic (\r
364 IN OUT VOID *Buffer\r
365 )\r
366{\r
367 SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
368}\r
369\r
b8b04307
JF
370/**\r
371 Do sync on APs.\r
372\r
373 @param[in, out] Buffer Pointer to private data buffer.\r
374**/\r
375VOID\r
376EFIAPI\r
377ApInitializeSync (\r
378 IN OUT VOID *Buffer\r
379 )\r
380{\r
381 CPU_MP_DATA *CpuMpData;\r
382\r
383 CpuMpData = (CPU_MP_DATA *) Buffer;\r
384 //\r
b8b04307
JF
385 // Load microcode on AP\r
386 //\r
387 MicrocodeDetect (CpuMpData);\r
cb811673
JF
388 //\r
389 // Sync BSP's MTRR table to AP\r
390 //\r
391 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);\r
b8b04307
JF
392}\r
393\r
394/**\r
395 Find the current Processor number by APIC ID.\r
396\r
367284e7
DB
397 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
398 @param[out] ProcessorNumber Return the pocessor number found\r
b8b04307
JF
399\r
400 @retval EFI_SUCCESS ProcessorNumber is found and returned.\r
401 @retval EFI_NOT_FOUND ProcessorNumber is not found.\r
402**/\r
403EFI_STATUS\r
404GetProcessorNumber (\r
405 IN CPU_MP_DATA *CpuMpData,\r
406 OUT UINTN *ProcessorNumber\r
407 )\r
408{\r
409 UINTN TotalProcessorNumber;\r
410 UINTN Index;\r
31a1e4da
JF
411 CPU_INFO_IN_HOB *CpuInfoInHob;\r
412\r
413 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
b8b04307
JF
414\r
415 TotalProcessorNumber = CpuMpData->CpuCount;\r
416 for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
31a1e4da 417 if (CpuInfoInHob[Index].ApicId == GetApicId ()) {\r
b8b04307
JF
418 *ProcessorNumber = Index;\r
419 return EFI_SUCCESS;\r
420 }\r
421 }\r
422 return EFI_NOT_FOUND;\r
423}\r
424\r
03434dff
JF
425/**\r
426 This function will get CPU count in the system.\r
427\r
428 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
429\r
430 @return CPU count detected\r
431**/\r
432UINTN\r
433CollectProcessorCount (\r
434 IN CPU_MP_DATA *CpuMpData\r
435 )\r
436{\r
59a119f0
JF
437 UINTN Index;\r
438\r
03434dff
JF
439 //\r
440 // Send 1st broadcast IPI to APs to wakeup APs\r
441 //\r
442 CpuMpData->InitFlag = ApInitConfig;\r
443 CpuMpData->X2ApicEnable = FALSE;\r
444 WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);\r
03434dff
JF
445 CpuMpData->InitFlag = ApInitDone;\r
446 ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
447 //\r
448 // Wait for all APs finished the initialization\r
449 //\r
450 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
451 CpuPause ();\r
452 }\r
453\r
fe627769
JF
454 if (CpuMpData->X2ApicEnable) {\r
455 DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));\r
456 //\r
457 // Wakeup all APs to enable x2APIC mode\r
458 //\r
459 WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL);\r
460 //\r
461 // Wait for all known APs finished\r
462 //\r
463 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
464 CpuPause ();\r
465 }\r
466 //\r
467 // Enable x2APIC on BSP\r
468 //\r
469 SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
59a119f0
JF
470 //\r
471 // Set BSP/Aps state to IDLE\r
472 //\r
473 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
474 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
475 }\r
fe627769
JF
476 }\r
477 DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));\r
8a2d564b
JF
478 //\r
479 // Sort BSP/Aps by CPU APIC ID in ascending order\r
480 //\r
481 SortApicId (CpuMpData);\r
482\r
03434dff
JF
483 DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));\r
484\r
485 return CpuMpData->CpuCount;\r
486}\r
487\r
367284e7 488/**\r
03a1a925
JF
489 Initialize CPU AP Data when AP is wakeup at the first time.\r
490\r
491 @param[in, out] CpuMpData Pointer to PEI CPU MP Data\r
492 @param[in] ProcessorNumber The handle number of processor\r
493 @param[in] BistData Processor BIST data\r
367284e7 494 @param[in] ApTopOfStack Top of AP stack\r
03a1a925
JF
495\r
496**/\r
497VOID\r
498InitializeApData (\r
499 IN OUT CPU_MP_DATA *CpuMpData,\r
500 IN UINTN ProcessorNumber,\r
845c5be1 501 IN UINT32 BistData,\r
dd3fa0cd 502 IN UINT64 ApTopOfStack\r
03a1a925
JF
503 )\r
504{\r
31a1e4da
JF
505 CPU_INFO_IN_HOB *CpuInfoInHob;\r
506\r
507 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
508 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
509 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();\r
510 CpuInfoInHob[ProcessorNumber].Health = BistData;\r
dd3fa0cd 511 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;\r
31a1e4da 512\r
03a1a925 513 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
03a1a925 514 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
31a1e4da 515 if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {\r
03a1a925
JF
516 //\r
517 // Set x2APIC mode if there are any logical processor reporting\r
518 // an Initial APIC ID of 255 or greater.\r
519 //\r
520 AcquireSpinLock(&CpuMpData->MpLock);\r
521 CpuMpData->X2ApicEnable = TRUE;\r
522 ReleaseSpinLock(&CpuMpData->MpLock);\r
523 }\r
524\r
525 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);\r
526 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
527}\r
528\r
b8b04307
JF
529/**\r
530 This function will be called from AP reset code if BSP uses WakeUpAP.\r
531\r
532 @param[in] ExchangeInfo Pointer to the MP exchange info buffer\r
533 @param[in] NumApsExecuting Number of current executing AP\r
534**/\r
535VOID\r
536EFIAPI\r
537ApWakeupFunction (\r
538 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,\r
539 IN UINTN NumApsExecuting\r
540 )\r
541{\r
542 CPU_MP_DATA *CpuMpData;\r
543 UINTN ProcessorNumber;\r
544 EFI_AP_PROCEDURE Procedure;\r
545 VOID *Parameter;\r
546 UINT32 BistData;\r
547 volatile UINT32 *ApStartupSignalBuffer;\r
31a1e4da 548 CPU_INFO_IN_HOB *CpuInfoInHob;\r
dd3fa0cd 549 UINT64 ApTopOfStack;\r
c6b0feb3 550 UINTN CurrentApicMode;\r
b8b04307
JF
551\r
552 //\r
553 // AP finished assembly code and begin to execute C code\r
554 //\r
555 CpuMpData = ExchangeInfo->CpuMpData;\r
556\r
ffab2442
JF
557 //\r
558 // AP's local APIC settings will be lost after received INIT IPI\r
559 // We need to re-initialize them at here\r
560 //\r
561 ProgramVirtualWireMode ();\r
562 SyncLocalApicTimerSetting (CpuMpData);\r
b8b04307 563\r
c6b0feb3 564 CurrentApicMode = GetApicMode ();\r
b8b04307
JF
565 while (TRUE) {\r
566 if (CpuMpData->InitFlag == ApInitConfig) {\r
567 //\r
568 // Add CPU number\r
569 //\r
570 InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);\r
571 ProcessorNumber = NumApsExecuting;\r
572 //\r
573 // This is first time AP wakeup, get BIST information from AP stack\r
574 //\r
845c5be1 575 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;\r
dd3fa0cd 576 BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));\r
b8b04307
JF
577 //\r
578 // Do some AP initialize sync\r
579 //\r
580 ApInitializeSync (CpuMpData);\r
581 //\r
582 // Sync BSP's Control registers to APs\r
583 //\r
584 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);\r
845c5be1 585 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);\r
b8b04307
JF
586 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
587 } else {\r
588 //\r
589 // Execute AP function if AP is ready\r
590 //\r
591 GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
592 //\r
593 // Clear AP start-up signal when AP waken up\r
594 //\r
595 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
596 InterlockedCompareExchange32 (\r
597 (UINT32 *) ApStartupSignalBuffer,\r
598 WAKEUP_AP_SIGNAL,\r
599 0\r
600 );\r
601 if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
602 //\r
603 // Restore AP's volatile registers saved\r
604 //\r
605 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);\r
606 }\r
607\r
608 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {\r
609 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;\r
610 Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;\r
611 if (Procedure != NULL) {\r
612 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);\r
613 //\r
43c9fdcc
JF
614 // Enable source debugging on AP function\r
615 // \r
616 EnableDebugAgent ();\r
617 //\r
b8b04307
JF
618 // Invoke AP function here\r
619 //\r
620 Procedure (Parameter);\r
31a1e4da 621 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
41be0da5
JF
622 if (CpuMpData->SwitchBspFlag) {\r
623 //\r
624 // Re-get the processor number due to BSP/AP maybe exchange in AP function\r
625 //\r
626 GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
627 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;\r
628 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;\r
b3775af2
JF
629 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
630 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;\r
41be0da5 631 } else {\r
c6b0feb3
JF
632 if (CpuInfoInHob[ProcessorNumber].ApicId != GetApicId () ||\r
633 CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ()) {\r
634 if (CurrentApicMode != GetApicMode ()) {\r
635 //\r
636 // If APIC mode change happened during AP function execution,\r
637 // we do not support APIC ID value changed.\r
638 //\r
639 ASSERT (FALSE);\r
640 CpuDeadLoop ();\r
641 } else {\r
642 //\r
643 // Re-get the CPU APICID and Initial APICID if they are changed\r
644 //\r
645 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();\r
646 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
647 }\r
648 }\r
41be0da5 649 }\r
b8b04307
JF
650 }\r
651 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);\r
652 }\r
653 }\r
654\r
655 //\r
656 // AP finished executing C code\r
657 //\r
658 InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);\r
659\r
660 //\r
661 // Place AP is specified loop mode\r
662 //\r
663 if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
664 //\r
665 // Save AP volatile registers\r
666 //\r
667 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);\r
668 //\r
669 // Place AP in HLT-loop\r
670 //\r
671 while (TRUE) {\r
672 DisableInterrupts ();\r
673 CpuSleep ();\r
674 CpuPause ();\r
675 }\r
676 }\r
677 while (TRUE) {\r
678 DisableInterrupts ();\r
679 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
680 //\r
681 // Place AP in MWAIT-loop\r
682 //\r
683 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);\r
684 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {\r
685 //\r
686 // Check AP start-up signal again.\r
687 // If AP start-up signal is not set, place AP into\r
688 // the specified C-state\r
689 //\r
690 AsmMwait (CpuMpData->ApTargetCState << 4, 0);\r
691 }\r
692 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {\r
693 //\r
694 // Place AP in Run-loop\r
695 //\r
696 CpuPause ();\r
697 } else {\r
698 ASSERT (FALSE);\r
699 }\r
700\r
701 //\r
702 // If AP start-up signal is written, AP is waken up\r
703 // otherwise place AP in loop again\r
704 //\r
705 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {\r
706 break;\r
707 }\r
708 }\r
709 }\r
710}\r
711\r
96f5920d
JF
712/**\r
713 Wait for AP wakeup and write AP start-up signal till AP is waken up.\r
714\r
715 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal\r
716**/\r
717VOID\r
718WaitApWakeup (\r
719 IN volatile UINT32 *ApStartupSignalBuffer\r
720 )\r
721{\r
722 //\r
723 // If AP is waken up, StartupApSignal should be cleared.\r
724 // Otherwise, write StartupApSignal again till AP waken up.\r
725 //\r
726 while (InterlockedCompareExchange32 (\r
727 (UINT32 *) ApStartupSignalBuffer,\r
728 WAKEUP_AP_SIGNAL,\r
729 WAKEUP_AP_SIGNAL\r
730 ) != 0) {\r
731 CpuPause ();\r
732 }\r
733}\r
734\r
7c3f2a12
JF
735/**\r
736 This function will fill the exchange info structure.\r
737\r
738 @param[in] CpuMpData Pointer to CPU MP Data\r
739\r
740**/\r
741VOID\r
742FillExchangeInfoData (\r
743 IN CPU_MP_DATA *CpuMpData\r
744 )\r
745{\r
746 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
747\r
748 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
749 ExchangeInfo->Lock = 0;\r
750 ExchangeInfo->StackStart = CpuMpData->Buffer;\r
751 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;\r
752 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;\r
753 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;\r
754\r
755 ExchangeInfo->CodeSegment = AsmReadCs ();\r
756 ExchangeInfo->DataSegment = AsmReadDs ();\r
757\r
758 ExchangeInfo->Cr3 = AsmReadCr3 ();\r
759\r
760 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;\r
761 ExchangeInfo->NumApsExecuting = 0;\r
46d4b885
JF
762 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;\r
763 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
7c3f2a12
JF
764 ExchangeInfo->CpuMpData = CpuMpData;\r
765\r
766 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();\r
767\r
3b2928b4
MK
768 ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;\r
769\r
7c3f2a12
JF
770 //\r
771 // Get the BSP's data of GDT and IDT\r
772 //\r
773 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);\r
774 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
775}\r
776\r
6e1987f1
LE
777/**\r
778 Helper function that waits until the finished AP count reaches the specified\r
779 limit, or the specified timeout elapses (whichever comes first).\r
780\r
781 @param[in] CpuMpData Pointer to CPU MP Data.\r
782 @param[in] FinishedApLimit The number of finished APs to wait for.\r
783 @param[in] TimeLimit The number of microseconds to wait for.\r
784**/\r
785VOID\r
786TimedWaitForApFinish (\r
787 IN CPU_MP_DATA *CpuMpData,\r
788 IN UINT32 FinishedApLimit,\r
789 IN UINT32 TimeLimit\r
790 );\r
791\r
96f5920d
JF
792/**\r
793 This function will be called by BSP to wakeup AP.\r
794\r
795 @param[in] CpuMpData Pointer to CPU MP Data\r
796 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
797 FALSE: Send IPI to AP by ApicId\r
798 @param[in] ProcessorNumber The handle number of specified processor\r
799 @param[in] Procedure The function to be invoked by AP\r
800 @param[in] ProcedureArgument The argument to be passed into AP function\r
801**/\r
802VOID\r
803WakeUpAP (\r
804 IN CPU_MP_DATA *CpuMpData,\r
805 IN BOOLEAN Broadcast,\r
806 IN UINTN ProcessorNumber,\r
807 IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
808 IN VOID *ProcedureArgument OPTIONAL\r
809 )\r
810{\r
811 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
812 UINTN Index;\r
813 CPU_AP_DATA *CpuData;\r
814 BOOLEAN ResetVectorRequired;\r
31a1e4da 815 CPU_INFO_IN_HOB *CpuInfoInHob;\r
96f5920d
JF
816\r
817 CpuMpData->FinishedCount = 0;\r
818 ResetVectorRequired = FALSE;\r
819\r
820 if (CpuMpData->ApLoopMode == ApInHltLoop ||\r
821 CpuMpData->InitFlag != ApInitDone) {\r
822 ResetVectorRequired = TRUE;\r
823 AllocateResetVector (CpuMpData);\r
824 FillExchangeInfoData (CpuMpData);\r
ffab2442 825 SaveLocalApicTimerSetting (CpuMpData);\r
96f5920d
JF
826 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
827 //\r
828 // Get AP target C-state each time when waking up AP,\r
829 // for it maybe updated by platform again\r
830 //\r
831 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
832 }\r
833\r
834 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
835\r
836 if (Broadcast) {\r
837 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
838 if (Index != CpuMpData->BspNumber) {\r
839 CpuData = &CpuMpData->CpuData[Index];\r
840 CpuData->ApFunction = (UINTN) Procedure;\r
841 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
842 SetApState (CpuData, CpuStateReady);\r
843 if (CpuMpData->InitFlag != ApInitConfig) {\r
844 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
845 }\r
846 }\r
847 }\r
848 if (ResetVectorRequired) {\r
849 //\r
850 // Wakeup all APs\r
851 //\r
852 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
853 }\r
c1192210
JF
854 if (CpuMpData->InitFlag == ApInitConfig) {\r
855 //\r
856 // Wait for all potential APs waken up in one specified period\r
857 //\r
6e1987f1
LE
858 TimedWaitForApFinish (\r
859 CpuMpData,\r
860 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
861 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
862 );\r
c1192210 863 } else {\r
96f5920d
JF
864 //\r
865 // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
866 //\r
867 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
868 CpuData = &CpuMpData->CpuData[Index];\r
869 if (Index != CpuMpData->BspNumber) {\r
870 WaitApWakeup (CpuData->StartupApSignal);\r
871 }\r
872 }\r
873 }\r
874 } else {\r
875 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
876 CpuData->ApFunction = (UINTN) Procedure;\r
877 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
878 SetApState (CpuData, CpuStateReady);\r
879 //\r
880 // Wakeup specified AP\r
881 //\r
882 ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
883 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
884 if (ResetVectorRequired) {\r
31a1e4da 885 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
96f5920d 886 SendInitSipiSipi (\r
31a1e4da 887 CpuInfoInHob[ProcessorNumber].ApicId,\r
96f5920d
JF
888 (UINT32) ExchangeInfo->BufferStart\r
889 );\r
890 }\r
891 //\r
892 // Wait specified AP waken up\r
893 //\r
894 WaitApWakeup (CpuData->StartupApSignal);\r
895 }\r
896\r
897 if (ResetVectorRequired) {\r
898 FreeResetVector (CpuMpData);\r
899 }\r
900}\r
901\r
08085f08
JF
902/**\r
903 Calculate timeout value and return the current performance counter value.\r
904\r
905 Calculate the number of performance counter ticks required for a timeout.\r
906 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
907 as infinity.\r
908\r
909 @param[in] TimeoutInMicroseconds Timeout value in microseconds.\r
910 @param[out] CurrentTime Returns the current value of the performance counter.\r
911\r
912 @return Expected time stamp counter for timeout.\r
913 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
914 as infinity.\r
915\r
916**/\r
917UINT64\r
918CalculateTimeout (\r
919 IN UINTN TimeoutInMicroseconds,\r
920 OUT UINT64 *CurrentTime\r
921 )\r
922{\r
923 //\r
924 // Read the current value of the performance counter\r
925 //\r
926 *CurrentTime = GetPerformanceCounter ();\r
927\r
928 //\r
929 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
930 // as infinity.\r
931 //\r
932 if (TimeoutInMicroseconds == 0) {\r
933 return 0;\r
934 }\r
935\r
936 //\r
937 // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
938 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide\r
939 // it by 1,000,000, to get the number of ticks for the timeout value.\r
940 //\r
941 return DivU64x32 (\r
942 MultU64x64 (\r
943 GetPerformanceCounterProperties (NULL, NULL),\r
944 TimeoutInMicroseconds\r
945 ),\r
946 1000000\r
947 );\r
948}\r
949\r
950/**\r
951 Checks whether timeout expires.\r
952\r
953 Check whether the number of elapsed performance counter ticks required for\r
954 a timeout condition has been reached.\r
955 If Timeout is zero, which means infinity, return value is always FALSE.\r
956\r
957 @param[in, out] PreviousTime On input, the value of the performance counter\r
958 when it was last read.\r
959 On output, the current value of the performance\r
960 counter\r
961 @param[in] TotalTime The total amount of elapsed time in performance\r
962 counter ticks.\r
963 @param[in] Timeout The number of performance counter ticks required\r
964 to reach a timeout condition.\r
965\r
966 @retval TRUE A timeout condition has been reached.\r
967 @retval FALSE A timeout condition has not been reached.\r
968\r
969**/\r
970BOOLEAN\r
971CheckTimeout (\r
972 IN OUT UINT64 *PreviousTime,\r
973 IN UINT64 *TotalTime,\r
974 IN UINT64 Timeout\r
975 )\r
976{\r
977 UINT64 Start;\r
978 UINT64 End;\r
979 UINT64 CurrentTime;\r
980 INT64 Delta;\r
981 INT64 Cycle;\r
982\r
983 if (Timeout == 0) {\r
984 return FALSE;\r
985 }\r
986 GetPerformanceCounterProperties (&Start, &End);\r
987 Cycle = End - Start;\r
988 if (Cycle < 0) {\r
989 Cycle = -Cycle;\r
990 }\r
991 Cycle++;\r
992 CurrentTime = GetPerformanceCounter();\r
993 Delta = (INT64) (CurrentTime - *PreviousTime);\r
994 if (Start > End) {\r
995 Delta = -Delta;\r
996 }\r
997 if (Delta < 0) {\r
998 Delta += Cycle;\r
999 }\r
1000 *TotalTime += Delta;\r
1001 *PreviousTime = CurrentTime;\r
1002 if (*TotalTime > Timeout) {\r
1003 return TRUE;\r
1004 }\r
1005 return FALSE;\r
1006}\r
1007\r
6e1987f1
LE
1008/**\r
1009 Helper function that waits until the finished AP count reaches the specified\r
1010 limit, or the specified timeout elapses (whichever comes first).\r
1011\r
1012 @param[in] CpuMpData Pointer to CPU MP Data.\r
1013 @param[in] FinishedApLimit The number of finished APs to wait for.\r
1014 @param[in] TimeLimit The number of microseconds to wait for.\r
1015**/\r
1016VOID\r
1017TimedWaitForApFinish (\r
1018 IN CPU_MP_DATA *CpuMpData,\r
1019 IN UINT32 FinishedApLimit,\r
1020 IN UINT32 TimeLimit\r
1021 )\r
1022{\r
1023 //\r
1024 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
1025 // "infinity", so check for (TimeLimit == 0) explicitly.\r
1026 //\r
1027 if (TimeLimit == 0) {\r
1028 return;\r
1029 }\r
1030\r
1031 CpuMpData->TotalTime = 0;\r
1032 CpuMpData->ExpectedTime = CalculateTimeout (\r
1033 TimeLimit,\r
1034 &CpuMpData->CurrentTime\r
1035 );\r
1036 while (CpuMpData->FinishedCount < FinishedApLimit &&\r
1037 !CheckTimeout (\r
1038 &CpuMpData->CurrentTime,\r
1039 &CpuMpData->TotalTime,\r
1040 CpuMpData->ExpectedTime\r
1041 )) {\r
1042 CpuPause ();\r
1043 }\r
1044\r
1045 if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
1046 DEBUG ((\r
1047 DEBUG_VERBOSE,\r
1048 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
1049 __FUNCTION__,\r
1050 FinishedApLimit,\r
1051 DivU64x64Remainder (\r
1052 MultU64x32 (CpuMpData->TotalTime, 1000000),\r
1053 GetPerformanceCounterProperties (NULL, NULL),\r
1054 NULL\r
1055 )\r
1056 ));\r
1057 }\r
1058}\r
1059\r
08085f08
JF
1060/**\r
1061 Reset an AP to Idle state.\r
1062\r
1063 Any task being executed by the AP will be aborted and the AP\r
1064 will be waiting for a new task in Wait-For-SIPI state.\r
1065\r
1066 @param[in] ProcessorNumber The handle number of processor.\r
1067**/\r
1068VOID\r
1069ResetProcessorToIdleState (\r
1070 IN UINTN ProcessorNumber\r
1071 )\r
1072{\r
1073 CPU_MP_DATA *CpuMpData;\r
1074\r
1075 CpuMpData = GetCpuMpData ();\r
1076\r
cb33bde4 1077 CpuMpData->InitFlag = ApInitReconfig;\r
08085f08 1078 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);\r
cb33bde4
JF
1079 while (CpuMpData->FinishedCount < 1) {\r
1080 CpuPause ();\r
1081 }\r
1082 CpuMpData->InitFlag = ApInitDone;\r
08085f08
JF
1083\r
1084 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1085}\r
1086\r
1087/**\r
1088 Searches for the next waiting AP.\r
1089\r
1090 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
1091\r
1092 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
1093\r
1094 @retval EFI_SUCCESS The next waiting AP has been found.\r
1095 @retval EFI_NOT_FOUND No waiting AP exists.\r
1096\r
1097**/\r
1098EFI_STATUS\r
1099GetNextWaitingProcessorNumber (\r
1100 OUT UINTN *NextProcessorNumber\r
1101 )\r
1102{\r
1103 UINTN ProcessorNumber;\r
1104 CPU_MP_DATA *CpuMpData;\r
1105\r
1106 CpuMpData = GetCpuMpData ();\r
1107\r
1108 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1109 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1110 *NextProcessorNumber = ProcessorNumber;\r
1111 return EFI_SUCCESS;\r
1112 }\r
1113 }\r
1114\r
1115 return EFI_NOT_FOUND;\r
1116}\r
1117\r
1118/** Checks status of specified AP.\r
1119\r
1120 This function checks whether the specified AP has finished the task assigned\r
1121 by StartupThisAP(), and whether timeout expires.\r
1122\r
1123 @param[in] ProcessorNumber The handle number of processor.\r
1124\r
1125 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
1126 @retval EFI_TIMEOUT The timeout expires.\r
1127 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
1128**/\r
1129EFI_STATUS\r
1130CheckThisAP (\r
1131 IN UINTN ProcessorNumber\r
1132 )\r
1133{\r
1134 CPU_MP_DATA *CpuMpData;\r
1135 CPU_AP_DATA *CpuData;\r
1136\r
1137 CpuMpData = GetCpuMpData ();\r
1138 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1139\r
1140 //\r
1141 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
1142 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
1143 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
1144 //\r
1145 //\r
1146 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.\r
1147 //\r
1148 if (GetApState(CpuData) == CpuStateFinished) {\r
1149 if (CpuData->Finished != NULL) {\r
1150 *(CpuData->Finished) = TRUE;\r
1151 }\r
1152 SetApState (CpuData, CpuStateIdle);\r
1153 return EFI_SUCCESS;\r
1154 } else {\r
1155 //\r
1156 // If timeout expires for StartupThisAP(), report timeout.\r
1157 //\r
1158 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
1159 if (CpuData->Finished != NULL) {\r
1160 *(CpuData->Finished) = FALSE;\r
1161 }\r
1162 //\r
1163 // Reset failed AP to idle state\r
1164 //\r
1165 ResetProcessorToIdleState (ProcessorNumber);\r
1166\r
1167 return EFI_TIMEOUT;\r
1168 }\r
1169 }\r
1170 return EFI_NOT_READY;\r
1171}\r
1172\r
1173/**\r
1174 Checks status of all APs.\r
1175\r
1176 This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
1177 and whether timeout expires.\r
1178\r
1179 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
1180 @retval EFI_TIMEOUT The timeout expires.\r
1181 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
1182**/\r
1183EFI_STATUS\r
1184CheckAllAPs (\r
1185 VOID\r
1186 )\r
1187{\r
1188 UINTN ProcessorNumber;\r
1189 UINTN NextProcessorNumber;\r
1190 UINTN ListIndex;\r
1191 EFI_STATUS Status;\r
1192 CPU_MP_DATA *CpuMpData;\r
1193 CPU_AP_DATA *CpuData;\r
1194\r
1195 CpuMpData = GetCpuMpData ();\r
1196\r
1197 NextProcessorNumber = 0;\r
1198\r
1199 //\r
1200 // Go through all APs that are responsible for the StartupAllAPs().\r
1201 //\r
1202 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1203 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1204 continue;\r
1205 }\r
1206\r
1207 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1208 //\r
1209 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
1210 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
1211 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
1212 //\r
1213 if (GetApState(CpuData) == CpuStateFinished) {\r
1214 CpuMpData->RunningCount ++;\r
1215 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
1216 SetApState(CpuData, CpuStateIdle);\r
1217\r
1218 //\r
1219 // If in Single Thread mode, then search for the next waiting AP for execution.\r
1220 //\r
1221 if (CpuMpData->SingleThread) {\r
1222 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
1223\r
1224 if (!EFI_ERROR (Status)) {\r
1225 WakeUpAP (\r
1226 CpuMpData,\r
1227 FALSE,\r
1228 (UINT32) NextProcessorNumber,\r
1229 CpuMpData->Procedure,\r
1230 CpuMpData->ProcArguments\r
1231 );\r
1232 }\r
1233 }\r
1234 }\r
1235 }\r
1236\r
1237 //\r
1238 // If all APs finish, return EFI_SUCCESS.\r
1239 //\r
1240 if (CpuMpData->RunningCount == CpuMpData->StartCount) {\r
1241 return EFI_SUCCESS;\r
1242 }\r
1243\r
1244 //\r
1245 // If timeout expires, report timeout.\r
1246 //\r
1247 if (CheckTimeout (\r
1248 &CpuMpData->CurrentTime,\r
1249 &CpuMpData->TotalTime,\r
1250 CpuMpData->ExpectedTime)\r
1251 ) {\r
1252 //\r
1253 // If FailedCpuList is not NULL, record all failed APs in it.\r
1254 //\r
1255 if (CpuMpData->FailedCpuList != NULL) {\r
1256 *CpuMpData->FailedCpuList =\r
1257 AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));\r
1258 ASSERT (*CpuMpData->FailedCpuList != NULL);\r
1259 }\r
1260 ListIndex = 0;\r
1261\r
1262 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
1263 //\r
1264 // Check whether this processor is responsible for StartupAllAPs().\r
1265 //\r
1266 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1267 //\r
1268 // Reset failed APs to idle state\r
1269 //\r
1270 ResetProcessorToIdleState (ProcessorNumber);\r
1271 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
1272 if (CpuMpData->FailedCpuList != NULL) {\r
1273 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;\r
1274 }\r
1275 }\r
1276 }\r
1277 if (CpuMpData->FailedCpuList != NULL) {\r
1278 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
1279 }\r
1280 return EFI_TIMEOUT;\r
1281 }\r
1282 return EFI_NOT_READY;\r
1283}\r
1284\r
3e8ad6bd
JF
1285/**\r
1286 MP Initialize Library initialization.\r
1287\r
1288 This service will allocate AP reset vector and wakeup all APs to do APs\r
1289 initialization.\r
1290\r
1291 This service must be invoked before all other MP Initialize Library\r
1292 service are invoked.\r
1293\r
1294 @retval EFI_SUCCESS MP initialization succeeds.\r
1295 @retval Others MP initialization fails.\r
1296\r
1297**/\r
1298EFI_STATUS\r
1299EFIAPI\r
1300MpInitLibInitialize (\r
1301 VOID\r
1302 )\r
1303{\r
6a2ee2bb
JF
1304 CPU_MP_DATA *OldCpuMpData;\r
1305 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e59f8f6b
JF
1306 UINT32 MaxLogicalProcessorNumber;\r
1307 UINT32 ApStackSize;\r
f7f85d83 1308 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
e59f8f6b 1309 UINTN BufferSize;\r
9ebcf0f4 1310 UINT32 MonitorFilterSize;\r
e59f8f6b
JF
1311 VOID *MpBuffer;\r
1312 UINTN Buffer;\r
1313 CPU_MP_DATA *CpuMpData;\r
9ebcf0f4 1314 UINT8 ApLoopMode;\r
e59f8f6b 1315 UINT8 *MonitorBuffer;\r
03a1a925 1316 UINTN Index;\r
f7f85d83 1317 UINTN ApResetVectorSize;\r
e59f8f6b 1318 UINTN BackupBufferAddr;\r
6a2ee2bb
JF
1319\r
1320 OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
1321 if (OldCpuMpData == NULL) {\r
1322 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
1323 } else {\r
1324 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
1325 }\r
14e8137c 1326 ASSERT (MaxLogicalProcessorNumber != 0);\r
f7f85d83
JF
1327\r
1328 AsmGetAddressMap (&AddressMap);\r
1329 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
e59f8f6b 1330 ApStackSize = PcdGet32(PcdCpuApStackSize);\r
9ebcf0f4
JF
1331 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
1332\r
e59f8f6b
JF
1333 BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
1334 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
1335 BufferSize += sizeof (CPU_MP_DATA);\r
1336 BufferSize += ApResetVectorSize;\r
1337 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
1338 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1339 ASSERT (MpBuffer != NULL);\r
1340 ZeroMem (MpBuffer, BufferSize);\r
1341 Buffer = (UINTN) MpBuffer;\r
1342\r
1343 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
1344 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
1345 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
1346 CpuMpData->Buffer = Buffer;\r
1347 CpuMpData->CpuApStackSize = ApStackSize;\r
1348 CpuMpData->BackupBuffer = BackupBufferAddr;\r
1349 CpuMpData->BackupBufferSize = ApResetVectorSize;\r
d11f10d1 1350 CpuMpData->SaveRestoreFlag = FALSE;\r
e59f8f6b
JF
1351 CpuMpData->WakeupBuffer = (UINTN) -1;\r
1352 CpuMpData->CpuCount = 1;\r
1353 CpuMpData->BspNumber = 0;\r
1354 CpuMpData->WaitEvent = NULL;\r
41be0da5 1355 CpuMpData->SwitchBspFlag = FALSE;\r
e59f8f6b
JF
1356 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);\r
1357 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
1358 InitializeSpinLock(&CpuMpData->MpLock);\r
1359 //\r
68cb9330
JF
1360 // Save BSP's Control registers to APs\r
1361 //\r
1362 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
1363 //\r
03a1a925
JF
1364 // Set BSP basic information\r
1365 //\r
845c5be1 1366 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
03a1a925 1367 //\r
e59f8f6b
JF
1368 // Save assembly code information\r
1369 //\r
1370 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
1371 //\r
1372 // Finally set AP loop mode\r
1373 //\r
1374 CpuMpData->ApLoopMode = ApLoopMode;\r
1375 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
1376 //\r
03a1a925
JF
1377 // Set up APs wakeup signal buffer\r
1378 //\r
1379 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
1380 CpuMpData->CpuData[Index].StartupApSignal =\r
1381 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
1382 }\r
94f63c76
JF
1383 //\r
1384 // Load Microcode on BSP\r
1385 //\r
1386 MicrocodeDetect (CpuMpData);\r
1387 //\r
e59f8f6b
JF
1388 // Store BSP's MTRR setting\r
1389 //\r
1390 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
9d64a9fd
JF
1391 //\r
1392 // Enable the local APIC for Virtual Wire Mode.\r
1393 //\r
1394 ProgramVirtualWireMode ();\r
e59f8f6b 1395\r
6a2ee2bb 1396 if (OldCpuMpData == NULL) {\r
14e8137c
JF
1397 if (MaxLogicalProcessorNumber > 1) {\r
1398 //\r
1399 // Wakeup all APs and calculate the processor count in system\r
1400 //\r
1401 CollectProcessorCount (CpuMpData);\r
1402 }\r
6a2ee2bb
JF
1403 } else {\r
1404 //\r
1405 // APs have been wakeup before, just get the CPU Information\r
1406 // from HOB\r
1407 //\r
1408 CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
1409 CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
1410 CpuMpData->InitFlag = ApInitReconfig;\r
31a1e4da
JF
1411 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
1412 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
6a2ee2bb
JF
1413 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1414 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
31a1e4da 1415 if (CpuInfoInHob[Index].InitialApicId >= 255) {\r
6a2ee2bb
JF
1416 CpuMpData->X2ApicEnable = TRUE;\r
1417 }\r
31a1e4da 1418 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
6a2ee2bb
JF
1419 CpuMpData->CpuData[Index].ApFunction = 0;\r
1420 CopyMem (\r
1421 &CpuMpData->CpuData[Index].VolatileRegisters,\r
1422 &CpuMpData->CpuData[0].VolatileRegisters,\r
1423 sizeof (CPU_VOLATILE_REGISTERS)\r
1424 );\r
1425 }\r
14e8137c
JF
1426 if (MaxLogicalProcessorNumber > 1) {\r
1427 //\r
1428 // Wakeup APs to do some AP initialize sync\r
1429 //\r
1430 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
1431 //\r
1432 // Wait for all APs finished initialization\r
1433 //\r
1434 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
1435 CpuPause ();\r
1436 }\r
1437 CpuMpData->InitFlag = ApInitDone;\r
1438 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1439 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
1440 }\r
6a2ee2bb
JF
1441 }\r
1442 }\r
93ca4c0f
JF
1443\r
1444 //\r
1445 // Initialize global data for MP support\r
1446 //\r
1447 InitMpGlobalData (CpuMpData);\r
1448\r
f7f85d83 1449 return EFI_SUCCESS;\r
3e8ad6bd
JF
1450}\r
1451\r
1452/**\r
1453 Gets detailed MP-related information on the requested processor at the\r
1454 instant this call is made. This service may only be called from the BSP.\r
1455\r
1456 @param[in] ProcessorNumber The handle number of processor.\r
1457 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
1458 the requested processor is deposited.\r
1459 @param[out] HealthData Return processor health data.\r
1460\r
1461 @retval EFI_SUCCESS Processor information was returned.\r
1462 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1463 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
1464 @retval EFI_NOT_FOUND The processor with the handle specified by\r
1465 ProcessorNumber does not exist in the platform.\r
1466 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1467\r
1468**/\r
1469EFI_STATUS\r
1470EFIAPI\r
1471MpInitLibGetProcessorInfo (\r
1472 IN UINTN ProcessorNumber,\r
1473 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,\r
1474 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL\r
1475 )\r
1476{\r
ad52f25e
JF
1477 CPU_MP_DATA *CpuMpData;\r
1478 UINTN CallerNumber;\r
31a1e4da 1479 CPU_INFO_IN_HOB *CpuInfoInHob;\r
ad52f25e
JF
1480\r
1481 CpuMpData = GetCpuMpData ();\r
31a1e4da 1482 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
ad52f25e
JF
1483\r
1484 //\r
1485 // Check whether caller processor is BSP\r
1486 //\r
1487 MpInitLibWhoAmI (&CallerNumber);\r
1488 if (CallerNumber != CpuMpData->BspNumber) {\r
1489 return EFI_DEVICE_ERROR;\r
1490 }\r
1491\r
1492 if (ProcessorInfoBuffer == NULL) {\r
1493 return EFI_INVALID_PARAMETER;\r
1494 }\r
1495\r
1496 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1497 return EFI_NOT_FOUND;\r
1498 }\r
1499\r
31a1e4da 1500 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
ad52f25e
JF
1501 ProcessorInfoBuffer->StatusFlag = 0;\r
1502 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1503 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1504 }\r
1505 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
1506 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
1507 }\r
1508 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
1509 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
1510 } else {\r
1511 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
1512 }\r
1513\r
1514 //\r
1515 // Get processor location information\r
1516 //\r
262128e5 1517 GetProcessorLocationByApicId (\r
31a1e4da 1518 CpuInfoInHob[ProcessorNumber].ApicId,\r
73152f19
LD
1519 &ProcessorInfoBuffer->Location.Package,\r
1520 &ProcessorInfoBuffer->Location.Core,\r
1521 &ProcessorInfoBuffer->Location.Thread\r
1522 );\r
ad52f25e
JF
1523\r
1524 if (HealthData != NULL) {\r
31a1e4da 1525 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
ad52f25e
JF
1526 }\r
1527\r
1528 return EFI_SUCCESS;\r
3e8ad6bd 1529}\r
ad52f25e 1530\r
41be0da5
JF
1531/**\r
1532 Worker function to switch the requested AP to be the BSP from that point onward.\r
1533\r
1534 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
1535 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
1536 enabled AP. Otherwise, it will be disabled.\r
1537\r
1538 @retval EFI_SUCCESS BSP successfully switched.\r
1539 @retval others Failed to switch BSP. \r
1540\r
1541**/\r
1542EFI_STATUS\r
1543SwitchBSPWorker (\r
1544 IN UINTN ProcessorNumber,\r
1545 IN BOOLEAN EnableOldBSP\r
1546 )\r
1547{\r
1548 CPU_MP_DATA *CpuMpData;\r
1549 UINTN CallerNumber;\r
1550 CPU_STATE State;\r
1551 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
a8d75a18 1552 BOOLEAN OldInterruptState;\r
26b43433 1553 BOOLEAN OldTimerInterruptState;\r
a8d75a18 1554\r
26b43433
JF
1555 //\r
1556 // Save and Disable Local APIC timer interrupt\r
1557 //\r
1558 OldTimerInterruptState = GetApicTimerInterruptState ();\r
1559 DisableApicTimerInterrupt ();\r
a8d75a18
JF
1560 //\r
1561 // Before send both BSP and AP to a procedure to exchange their roles,\r
1562 // interrupt must be disabled. This is because during the exchange role\r
1563 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
1564 // be corrupted, since interrupt return address will be pushed to stack\r
1565 // by hardware.\r
1566 //\r
1567 OldInterruptState = SaveAndDisableInterrupts ();\r
1568\r
1569 //\r
1570 // Mask LINT0 & LINT1 for the old BSP\r
1571 //\r
1572 DisableLvtInterrupts ();\r
41be0da5
JF
1573\r
1574 CpuMpData = GetCpuMpData ();\r
1575\r
1576 //\r
1577 // Check whether caller processor is BSP\r
1578 //\r
1579 MpInitLibWhoAmI (&CallerNumber);\r
1580 if (CallerNumber != CpuMpData->BspNumber) {\r
1581 return EFI_SUCCESS;\r
1582 }\r
1583\r
1584 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1585 return EFI_NOT_FOUND;\r
1586 }\r
1587\r
1588 //\r
1589 // Check whether specified AP is disabled\r
1590 //\r
1591 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
1592 if (State == CpuStateDisabled) {\r
1593 return EFI_INVALID_PARAMETER;\r
1594 }\r
1595\r
1596 //\r
1597 // Check whether ProcessorNumber specifies the current BSP\r
1598 //\r
1599 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1600 return EFI_INVALID_PARAMETER;\r
1601 }\r
1602\r
1603 //\r
1604 // Check whether specified AP is busy\r
1605 //\r
1606 if (State == CpuStateBusy) {\r
1607 return EFI_NOT_READY;\r
1608 }\r
1609\r
1610 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
1611 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
1612 CpuMpData->SwitchBspFlag = TRUE;\r
b3775af2 1613 CpuMpData->NewBspNumber = ProcessorNumber;\r
41be0da5
JF
1614\r
1615 //\r
1616 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
1617 //\r
1618 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1619 ApicBaseMsr.Bits.BSP = 0;\r
1620 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1621\r
1622 //\r
1623 // Need to wakeUp AP (future BSP).\r
1624 //\r
1625 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
1626\r
1627 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
1628\r
1629 //\r
1630 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
1631 //\r
1632 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1633 ApicBaseMsr.Bits.BSP = 1;\r
1634 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1635\r
1636 //\r
1637 // Wait for old BSP finished AP task\r
1638 //\r
1639 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
1640 CpuPause ();\r
1641 }\r
1642\r
1643 CpuMpData->SwitchBspFlag = FALSE;\r
1644 //\r
1645 // Set old BSP enable state\r
1646 //\r
1647 if (!EnableOldBSP) {\r
1648 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
af8ba51a
JF
1649 } else {\r
1650 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);\r
41be0da5
JF
1651 }\r
1652 //\r
1653 // Save new BSP number\r
1654 //\r
1655 CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
1656\r
a8d75a18
JF
1657 //\r
1658 // Restore interrupt state.\r
1659 //\r
1660 SetInterruptState (OldInterruptState);\r
1661\r
26b43433
JF
1662 if (OldTimerInterruptState) {\r
1663 EnableApicTimerInterrupt ();\r
1664 }\r
a8d75a18 1665\r
41be0da5
JF
1666 return EFI_SUCCESS;\r
1667}\r
ad52f25e 1668\r
e37109bc
JF
1669/**\r
1670 Worker function to let the caller enable or disable an AP from this point onward.\r
1671 This service may only be called from the BSP.\r
1672\r
1673 @param[in] ProcessorNumber The handle number of AP.\r
1674 @param[in] EnableAP Specifies the new state for the processor for\r
1675 enabled, FALSE for disabled.\r
1676 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
1677 the new health status of the AP.\r
1678\r
1679 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1680 @retval others Failed to Enable/Disable AP.\r
1681\r
1682**/\r
1683EFI_STATUS\r
1684EnableDisableApWorker (\r
1685 IN UINTN ProcessorNumber,\r
1686 IN BOOLEAN EnableAP,\r
1687 IN UINT32 *HealthFlag OPTIONAL\r
1688 )\r
1689{\r
1690 CPU_MP_DATA *CpuMpData;\r
1691 UINTN CallerNumber;\r
1692\r
1693 CpuMpData = GetCpuMpData ();\r
1694\r
1695 //\r
1696 // Check whether caller processor is BSP\r
1697 //\r
1698 MpInitLibWhoAmI (&CallerNumber);\r
1699 if (CallerNumber != CpuMpData->BspNumber) {\r
1700 return EFI_DEVICE_ERROR;\r
1701 }\r
1702\r
1703 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1704 return EFI_INVALID_PARAMETER;\r
1705 }\r
1706\r
1707 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1708 return EFI_NOT_FOUND;\r
1709 }\r
1710\r
1711 if (!EnableAP) {\r
1712 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
1713 } else {\r
1714 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1715 }\r
1716\r
1717 if (HealthFlag != NULL) {\r
1718 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
1719 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
1720 }\r
1721\r
1722 return EFI_SUCCESS;\r
1723}\r
1724\r
3e8ad6bd
JF
1725/**\r
1726 This return the handle number for the calling processor. This service may be\r
1727 called from the BSP and APs.\r
1728\r
1729 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
1730 The range is from 0 to the total number of\r
1731 logical processors minus 1. The total number of\r
1732 logical processors can be retrieved by\r
1733 MpInitLibGetNumberOfProcessors().\r
1734\r
1735 @retval EFI_SUCCESS The current processor handle number was returned\r
1736 in ProcessorNumber.\r
1737 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1738 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1739\r
1740**/\r
1741EFI_STATUS\r
1742EFIAPI\r
1743MpInitLibWhoAmI (\r
1744 OUT UINTN *ProcessorNumber\r
1745 )\r
1746{\r
5c9e0997
JF
1747 CPU_MP_DATA *CpuMpData;\r
1748\r
1749 if (ProcessorNumber == NULL) {\r
1750 return EFI_INVALID_PARAMETER;\r
1751 }\r
1752\r
1753 CpuMpData = GetCpuMpData ();\r
1754\r
1755 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 1756}\r
809213a6 1757\r
3e8ad6bd
JF
1758/**\r
1759 Retrieves the number of logical processor in the platform and the number of\r
1760 those logical processors that are enabled on this boot. This service may only\r
1761 be called from the BSP.\r
1762\r
1763 @param[out] NumberOfProcessors Pointer to the total number of logical\r
1764 processors in the system, including the BSP\r
1765 and disabled APs.\r
1766 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
1767 processors that exist in system, including\r
1768 the BSP.\r
1769\r
1770 @retval EFI_SUCCESS The number of logical processors and enabled\r
1771 logical processors was retrieved.\r
1772 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1773 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
1774 is NULL.\r
1775 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1776\r
1777**/\r
1778EFI_STATUS\r
1779EFIAPI\r
1780MpInitLibGetNumberOfProcessors (\r
1781 OUT UINTN *NumberOfProcessors, OPTIONAL\r
1782 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
1783 )\r
1784{\r
809213a6
JF
1785 CPU_MP_DATA *CpuMpData;\r
1786 UINTN CallerNumber;\r
1787 UINTN ProcessorNumber;\r
1788 UINTN EnabledProcessorNumber;\r
1789 UINTN Index;\r
1790\r
1791 CpuMpData = GetCpuMpData ();\r
1792\r
1793 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
1794 return EFI_INVALID_PARAMETER;\r
1795 }\r
1796\r
1797 //\r
1798 // Check whether caller processor is BSP\r
1799 //\r
1800 MpInitLibWhoAmI (&CallerNumber);\r
1801 if (CallerNumber != CpuMpData->BspNumber) {\r
1802 return EFI_DEVICE_ERROR;\r
1803 }\r
1804\r
1805 ProcessorNumber = CpuMpData->CpuCount;\r
1806 EnabledProcessorNumber = 0;\r
1807 for (Index = 0; Index < ProcessorNumber; Index++) {\r
1808 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
1809 EnabledProcessorNumber ++;\r
1810 }\r
1811 }\r
1812\r
1813 if (NumberOfProcessors != NULL) {\r
1814 *NumberOfProcessors = ProcessorNumber;\r
1815 }\r
1816 if (NumberOfEnabledProcessors != NULL) {\r
1817 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
1818 }\r
1819\r
1820 return EFI_SUCCESS;\r
3e8ad6bd 1821}\r
6a2ee2bb 1822\r
809213a6 1823\r
86efe976
JF
1824/**\r
1825 Worker function to execute a caller provided function on all enabled APs.\r
1826\r
1827 @param[in] Procedure A pointer to the function to be run on\r
1828 enabled APs of the system.\r
1829 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
1830 the function specified by Procedure one by\r
1831 one, in ascending order of processor handle\r
1832 number. If FALSE, then all the enabled APs\r
1833 execute the function specified by Procedure\r
1834 simultaneously.\r
1835 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1836 service.\r
367284e7 1837 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
86efe976
JF
1838 APs to return from Procedure, either for\r
1839 blocking or non-blocking mode.\r
1840 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1841 all APs.\r
1842 @param[out] FailedCpuList If all APs finish successfully, then its\r
1843 content is set to NULL. If not all APs\r
1844 finish before timeout expires, then its\r
1845 content is set to address of the buffer\r
1846 holding handle numbers of the failed APs.\r
1847\r
1848 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1849 the timeout expired.\r
1850 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1851 to all enabled APs.\r
1852 @retval others Failed to Startup all APs.\r
1853\r
1854**/\r
1855EFI_STATUS\r
1856StartupAllAPsWorker (\r
1857 IN EFI_AP_PROCEDURE Procedure,\r
1858 IN BOOLEAN SingleThread,\r
1859 IN EFI_EVENT WaitEvent OPTIONAL,\r
1860 IN UINTN TimeoutInMicroseconds,\r
1861 IN VOID *ProcedureArgument OPTIONAL,\r
1862 OUT UINTN **FailedCpuList OPTIONAL\r
1863 )\r
1864{\r
1865 EFI_STATUS Status;\r
1866 CPU_MP_DATA *CpuMpData;\r
1867 UINTN ProcessorCount;\r
1868 UINTN ProcessorNumber;\r
1869 UINTN CallerNumber;\r
1870 CPU_AP_DATA *CpuData;\r
1871 BOOLEAN HasEnabledAp;\r
1872 CPU_STATE ApState;\r
1873\r
1874 CpuMpData = GetCpuMpData ();\r
1875\r
1876 if (FailedCpuList != NULL) {\r
1877 *FailedCpuList = NULL;\r
1878 }\r
1879\r
1880 if (CpuMpData->CpuCount == 1) {\r
1881 return EFI_NOT_STARTED;\r
1882 }\r
1883\r
1884 if (Procedure == NULL) {\r
1885 return EFI_INVALID_PARAMETER;\r
1886 }\r
1887\r
1888 //\r
1889 // Check whether caller processor is BSP\r
1890 //\r
1891 MpInitLibWhoAmI (&CallerNumber);\r
1892 if (CallerNumber != CpuMpData->BspNumber) {\r
1893 return EFI_DEVICE_ERROR;\r
1894 }\r
1895\r
1896 //\r
1897 // Update AP state\r
1898 //\r
1899 CheckAndUpdateApsStatus ();\r
1900\r
1901 ProcessorCount = CpuMpData->CpuCount;\r
1902 HasEnabledAp = FALSE;\r
1903 //\r
1904 // Check whether all enabled APs are idle.\r
1905 // If any enabled AP is not idle, return EFI_NOT_READY.\r
1906 //\r
1907 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1908 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1909 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1910 ApState = GetApState (CpuData);\r
1911 if (ApState != CpuStateDisabled) {\r
1912 HasEnabledAp = TRUE;\r
1913 if (ApState != CpuStateIdle) {\r
1914 //\r
1915 // If any enabled APs are busy, return EFI_NOT_READY.\r
1916 //\r
1917 return EFI_NOT_READY;\r
1918 }\r
1919 }\r
1920 }\r
1921 }\r
1922\r
1923 if (!HasEnabledAp) {\r
1924 //\r
1925 // If no enabled AP exists, return EFI_NOT_STARTED.\r
1926 //\r
1927 return EFI_NOT_STARTED;\r
1928 }\r
1929\r
1930 CpuMpData->StartCount = 0;\r
1931 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1932 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1933 CpuData->Waiting = FALSE;\r
1934 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1935 if (CpuData->State == CpuStateIdle) {\r
1936 //\r
1937 // Mark this processor as responsible for current calling.\r
1938 //\r
1939 CpuData->Waiting = TRUE;\r
1940 CpuMpData->StartCount++;\r
1941 }\r
1942 }\r
1943 }\r
1944\r
1945 CpuMpData->Procedure = Procedure;\r
1946 CpuMpData->ProcArguments = ProcedureArgument;\r
1947 CpuMpData->SingleThread = SingleThread;\r
1948 CpuMpData->FinishedCount = 0;\r
1949 CpuMpData->RunningCount = 0;\r
1950 CpuMpData->FailedCpuList = FailedCpuList;\r
1951 CpuMpData->ExpectedTime = CalculateTimeout (\r
1952 TimeoutInMicroseconds,\r
1953 &CpuMpData->CurrentTime\r
1954 );\r
1955 CpuMpData->TotalTime = 0;\r
1956 CpuMpData->WaitEvent = WaitEvent;\r
1957\r
1958 if (!SingleThread) {\r
1959 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
1960 } else {\r
1961 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1962 if (ProcessorNumber == CallerNumber) {\r
1963 continue;\r
1964 }\r
1965 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1966 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
1967 break;\r
1968 }\r
1969 }\r
1970 }\r
1971\r
1972 Status = EFI_SUCCESS;\r
1973 if (WaitEvent == NULL) {\r
1974 do {\r
1975 Status = CheckAllAPs ();\r
1976 } while (Status == EFI_NOT_READY);\r
1977 }\r
1978\r
1979 return Status;\r
1980}\r
1981\r
20ae5774
JF
1982/**\r
1983 Worker function to let the caller get one enabled AP to execute a caller-provided\r
1984 function.\r
1985\r
1986 @param[in] Procedure A pointer to the function to be run on\r
1987 enabled APs of the system.\r
1988 @param[in] ProcessorNumber The handle number of the AP.\r
1989 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1990 service.\r
367284e7 1991 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
20ae5774
JF
1992 APs to return from Procedure, either for\r
1993 blocking or non-blocking mode.\r
1994 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1995 all APs.\r
1996 @param[out] Finished If AP returns from Procedure before the\r
1997 timeout expires, its content is set to TRUE.\r
1998 Otherwise, the value is set to FALSE.\r
1999\r
2000 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
2001 the timeout expires.\r
2002 @retval others Failed to Startup AP.\r
2003\r
2004**/\r
2005EFI_STATUS\r
2006StartupThisAPWorker (\r
2007 IN EFI_AP_PROCEDURE Procedure,\r
2008 IN UINTN ProcessorNumber,\r
2009 IN EFI_EVENT WaitEvent OPTIONAL,\r
2010 IN UINTN TimeoutInMicroseconds,\r
2011 IN VOID *ProcedureArgument OPTIONAL,\r
2012 OUT BOOLEAN *Finished OPTIONAL\r
2013 )\r
2014{\r
2015 EFI_STATUS Status;\r
2016 CPU_MP_DATA *CpuMpData;\r
2017 CPU_AP_DATA *CpuData;\r
2018 UINTN CallerNumber;\r
2019\r
2020 CpuMpData = GetCpuMpData ();\r
2021\r
2022 if (Finished != NULL) {\r
2023 *Finished = FALSE;\r
2024 }\r
2025\r
2026 //\r
2027 // Check whether caller processor is BSP\r
2028 //\r
2029 MpInitLibWhoAmI (&CallerNumber);\r
2030 if (CallerNumber != CpuMpData->BspNumber) {\r
2031 return EFI_DEVICE_ERROR;\r
2032 }\r
2033\r
2034 //\r
2035 // Check whether processor with the handle specified by ProcessorNumber exists\r
2036 //\r
2037 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
2038 return EFI_NOT_FOUND;\r
2039 }\r
2040\r
2041 //\r
2042 // Check whether specified processor is BSP\r
2043 //\r
2044 if (ProcessorNumber == CpuMpData->BspNumber) {\r
2045 return EFI_INVALID_PARAMETER;\r
2046 }\r
2047\r
2048 //\r
2049 // Check parameter Procedure\r
2050 //\r
2051 if (Procedure == NULL) {\r
2052 return EFI_INVALID_PARAMETER;\r
2053 }\r
2054\r
2055 //\r
2056 // Update AP state\r
2057 //\r
2058 CheckAndUpdateApsStatus ();\r
2059\r
2060 //\r
2061 // Check whether specified AP is disabled\r
2062 //\r
2063 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
2064 return EFI_INVALID_PARAMETER;\r
2065 }\r
2066\r
2067 //\r
2068 // If WaitEvent is not NULL, execute in non-blocking mode.\r
2069 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
2070 // CheckAPsStatus() will check completion and timeout periodically.\r
2071 //\r
2072 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
2073 CpuData->WaitEvent = WaitEvent;\r
2074 CpuData->Finished = Finished;\r
2075 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
2076 CpuData->TotalTime = 0;\r
2077\r
2078 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
2079\r
2080 //\r
2081 // If WaitEvent is NULL, execute in blocking mode.\r
2082 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
2083 //\r
2084 Status = EFI_SUCCESS;\r
2085 if (WaitEvent == NULL) {\r
2086 do {\r
2087 Status = CheckThisAP (ProcessorNumber);\r
2088 } while (Status == EFI_NOT_READY);\r
2089 }\r
2090\r
2091 return Status;\r
2092}\r
2093\r
93ca4c0f
JF
2094/**\r
2095 Get pointer to CPU MP Data structure from GUIDed HOB.\r
2096\r
2097 @return The pointer to CPU MP Data structure.\r
2098**/\r
2099CPU_MP_DATA *\r
2100GetCpuMpDataFromGuidedHob (\r
2101 VOID\r
2102 )\r
2103{\r
2104 EFI_HOB_GUID_TYPE *GuidHob;\r
2105 VOID *DataInHob;\r
2106 CPU_MP_DATA *CpuMpData;\r
2107\r
2108 CpuMpData = NULL;\r
2109 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
2110 if (GuidHob != NULL) {\r
2111 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
2112 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
2113 }\r
2114 return CpuMpData;\r
2115}\r
42c37b3b
JF
2116\r
2117/**\r
2118 Get available system memory below 1MB by specified size.\r
2119\r
2120 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
2121**/\r
2122VOID\r
2123BackupAndPrepareWakeupBuffer(\r
2124 IN CPU_MP_DATA *CpuMpData\r
2125 )\r
2126{\r
2127 CopyMem (\r
2128 (VOID *) CpuMpData->BackupBuffer,\r
2129 (VOID *) CpuMpData->WakeupBuffer,\r
2130 CpuMpData->BackupBufferSize\r
2131 );\r
2132 CopyMem (\r
2133 (VOID *) CpuMpData->WakeupBuffer,\r
2134 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
2135 CpuMpData->AddressMap.RendezvousFunnelSize\r
2136 );\r
2137}\r
2138\r
2139/**\r
2140 Restore wakeup buffer data.\r
2141\r
2142 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
2143**/\r
2144VOID\r
2145RestoreWakeupBuffer(\r
2146 IN CPU_MP_DATA *CpuMpData\r
2147 )\r
2148{\r
2149 CopyMem (\r
2150 (VOID *) CpuMpData->WakeupBuffer,\r
2151 (VOID *) CpuMpData->BackupBuffer,\r
2152 CpuMpData->BackupBufferSize\r
2153 );\r
2154}\r