]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpInitLib: Implementation of MpInitLibEnableDisableAP()
[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
ad52f25e
JF
60/**\r
61 Get CPU Package/Core/Thread location information.\r
62\r
63 @param[in] InitialApicId CPU APIC ID\r
64 @param[out] Location Pointer to CPU location information\r
65**/\r
66VOID\r
67ExtractProcessorLocation (\r
68 IN UINT32 InitialApicId,\r
69 OUT EFI_CPU_PHYSICAL_LOCATION *Location\r
70 )\r
71{\r
72 BOOLEAN TopologyLeafSupported;\r
73 UINTN ThreadBits;\r
74 UINTN CoreBits;\r
75 CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
76 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
77 CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
78 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
79 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
80 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
81 UINT32 MaxCpuIdIndex;\r
82 UINT32 SubIndex;\r
83 UINTN LevelType;\r
84 UINT32 MaxLogicProcessorsPerPackage;\r
85 UINT32 MaxCoresPerPackage;\r
86\r
87 //\r
88 // Check if the processor is capable of supporting more than one logical processor.\r
89 //\r
90 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
91 if (VersionInfoEdx.Bits.HTT == 0) {\r
92 Location->Thread = 0;\r
93 Location->Core = 0;\r
94 Location->Package = 0;\r
95 return;\r
96 }\r
97\r
98 ThreadBits = 0;\r
99 CoreBits = 0;\r
100\r
101 //\r
102 // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
103 //\r
104\r
105 TopologyLeafSupported = FALSE;\r
106 //\r
107 // Get the max index of basic CPUID\r
108 //\r
109 AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
110\r
111 //\r
112 // If the extended topology enumeration leaf is available, it\r
113 // is the preferred mechanism for enumerating topology.\r
114 //\r
115 if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
116 AsmCpuidEx (\r
117 CPUID_EXTENDED_TOPOLOGY,\r
118 0,\r
119 &ExtendedTopologyEax.Uint32,\r
120 &ExtendedTopologyEbx.Uint32,\r
121 &ExtendedTopologyEcx.Uint32,\r
122 NULL\r
123 );\r
124 //\r
125 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
126 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
127 // supported on that processor.\r
128 //\r
129 if (ExtendedTopologyEbx.Uint32 != 0) {\r
130 TopologyLeafSupported = TRUE;\r
131\r
132 //\r
133 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
134 // the SMT sub-field of x2APIC ID.\r
135 //\r
136 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
137 ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
138 ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
139\r
140 //\r
141 // Software must not assume any "level type" encoding\r
142 // value to be related to any sub-leaf index, except sub-leaf 0.\r
143 //\r
144 SubIndex = 1;\r
145 do {\r
146 AsmCpuidEx (\r
147 CPUID_EXTENDED_TOPOLOGY,\r
148 SubIndex,\r
149 &ExtendedTopologyEax.Uint32,\r
150 NULL,\r
151 &ExtendedTopologyEcx.Uint32,\r
152 NULL\r
153 );\r
154 LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
155 if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
156 CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
157 break;\r
158 }\r
159 SubIndex++;\r
160 } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
161 }\r
162 }\r
163\r
164 if (!TopologyLeafSupported) {\r
165 AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
166 MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
167 if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
168 AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
169 MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
170 } else {\r
171 //\r
172 // Must be a single-core processor.\r
173 //\r
174 MaxCoresPerPackage = 1;\r
175 }\r
176\r
177 ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
178 CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);\r
179 }\r
180\r
181 Location->Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
182 Location->Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
183 Location->Package = (InitialApicId >> (ThreadBits + CoreBits));\r
184}\r
185\r
41be0da5
JF
186/**\r
187 Worker function for SwitchBSP().\r
188\r
189 Worker function for SwitchBSP(), assigned to the AP which is intended\r
190 to become BSP.\r
191\r
192 @param[in] Buffer Pointer to CPU MP Data\r
193**/\r
194VOID\r
195EFIAPI\r
196FutureBSPProc (\r
197 IN VOID *Buffer\r
198 )\r
199{\r
200 CPU_MP_DATA *DataInHob;\r
201\r
202 DataInHob = (CPU_MP_DATA *) Buffer;\r
203 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);\r
204}\r
205\r
03a1a925
JF
206/**\r
207 Get the Application Processors state.\r
208\r
209 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP\r
210\r
211 @return The AP status\r
212**/\r
213CPU_STATE\r
214GetApState (\r
215 IN CPU_AP_DATA *CpuData\r
216 )\r
217{\r
218 return CpuData->State;\r
219}\r
220\r
221/**\r
222 Set the Application Processors state.\r
223\r
224 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP\r
225 @param[in] State The AP status\r
226**/\r
227VOID\r
228SetApState (\r
229 IN CPU_AP_DATA *CpuData,\r
230 IN CPU_STATE State\r
231 )\r
232{\r
233 AcquireSpinLock (&CpuData->ApLock);\r
234 CpuData->State = State;\r
235 ReleaseSpinLock (&CpuData->ApLock);\r
236}\r
3e8ad6bd 237\r
68cb9330
JF
238/**\r
239 Save the volatile registers required to be restored following INIT IPI.\r
240\r
241 @param[out] VolatileRegisters Returns buffer saved the volatile resisters\r
242**/\r
243VOID\r
244SaveVolatileRegisters (\r
245 OUT CPU_VOLATILE_REGISTERS *VolatileRegisters\r
246 )\r
247{\r
248 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
249\r
250 VolatileRegisters->Cr0 = AsmReadCr0 ();\r
251 VolatileRegisters->Cr3 = AsmReadCr3 ();\r
252 VolatileRegisters->Cr4 = AsmReadCr4 ();\r
253\r
254 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
255 if (VersionInfoEdx.Bits.DE != 0) {\r
256 //\r
257 // If processor supports Debugging Extensions feature\r
258 // by CPUID.[EAX=01H]:EDX.BIT2\r
259 //\r
260 VolatileRegisters->Dr0 = AsmReadDr0 ();\r
261 VolatileRegisters->Dr1 = AsmReadDr1 ();\r
262 VolatileRegisters->Dr2 = AsmReadDr2 ();\r
263 VolatileRegisters->Dr3 = AsmReadDr3 ();\r
264 VolatileRegisters->Dr6 = AsmReadDr6 ();\r
265 VolatileRegisters->Dr7 = AsmReadDr7 ();\r
266 }\r
267}\r
268\r
269/**\r
270 Restore the volatile registers following INIT IPI.\r
271\r
272 @param[in] VolatileRegisters Pointer to volatile resisters\r
273 @param[in] IsRestoreDr TRUE: Restore DRx if supported\r
274 FALSE: Do not restore DRx\r
275**/\r
276VOID\r
277RestoreVolatileRegisters (\r
278 IN CPU_VOLATILE_REGISTERS *VolatileRegisters,\r
279 IN BOOLEAN IsRestoreDr\r
280 )\r
281{\r
282 CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
283\r
284 AsmWriteCr0 (VolatileRegisters->Cr0);\r
285 AsmWriteCr3 (VolatileRegisters->Cr3);\r
286 AsmWriteCr4 (VolatileRegisters->Cr4);\r
287\r
288 if (IsRestoreDr) {\r
289 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
290 if (VersionInfoEdx.Bits.DE != 0) {\r
291 //\r
292 // If processor supports Debugging Extensions feature\r
293 // by CPUID.[EAX=01H]:EDX.BIT2\r
294 //\r
295 AsmWriteDr0 (VolatileRegisters->Dr0);\r
296 AsmWriteDr1 (VolatileRegisters->Dr1);\r
297 AsmWriteDr2 (VolatileRegisters->Dr2);\r
298 AsmWriteDr3 (VolatileRegisters->Dr3);\r
299 AsmWriteDr6 (VolatileRegisters->Dr6);\r
300 AsmWriteDr7 (VolatileRegisters->Dr7);\r
301 }\r
302 }\r
303}\r
304\r
9ebcf0f4
JF
305/**\r
306 Detect whether Mwait-monitor feature is supported.\r
307\r
308 @retval TRUE Mwait-monitor feature is supported.\r
309 @retval FALSE Mwait-monitor feature is not supported.\r
310**/\r
311BOOLEAN\r
312IsMwaitSupport (\r
313 VOID\r
314 )\r
315{\r
316 CPUID_VERSION_INFO_ECX VersionInfoEcx;\r
317\r
318 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);\r
319 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;\r
320}\r
321\r
322/**\r
323 Get AP loop mode.\r
324\r
325 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.\r
326\r
327 @return The AP loop mode.\r
328**/\r
329UINT8\r
330GetApLoopMode (\r
331 OUT UINT32 *MonitorFilterSize\r
332 )\r
333{\r
334 UINT8 ApLoopMode;\r
335 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;\r
336\r
337 ASSERT (MonitorFilterSize != NULL);\r
338\r
339 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
340 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);\r
341 if (ApLoopMode == ApInMwaitLoop) {\r
342 if (!IsMwaitSupport ()) {\r
343 //\r
344 // If processor does not support MONITOR/MWAIT feature,\r
345 // force AP in Hlt-loop mode\r
346 //\r
347 ApLoopMode = ApInHltLoop;\r
348 }\r
349 }\r
350\r
351 if (ApLoopMode != ApInMwaitLoop) {\r
352 *MonitorFilterSize = sizeof (UINT32);\r
353 } else {\r
354 //\r
355 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes\r
356 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT\r
357 //\r
358 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);\r
359 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;\r
360 }\r
361\r
362 return ApLoopMode;\r
363}\r
b8b04307 364\r
8a2d564b
JF
365/**\r
366 Sort the APIC ID of all processors.\r
367\r
368 This function sorts the APIC ID of all processors so that processor number is\r
369 assigned in the ascending order of APIC ID which eases MP debugging.\r
370\r
371 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
372**/\r
373VOID\r
374SortApicId (\r
375 IN CPU_MP_DATA *CpuMpData\r
376 )\r
377{\r
378 UINTN Index1;\r
379 UINTN Index2;\r
380 UINTN Index3;\r
381 UINT32 ApicId;\r
382 CPU_AP_DATA CpuData;\r
383 UINT32 ApCount;\r
384 CPU_INFO_IN_HOB *CpuInfoInHob;\r
385\r
386 ApCount = CpuMpData->CpuCount - 1;\r
387\r
388 if (ApCount != 0) {\r
389 for (Index1 = 0; Index1 < ApCount; Index1++) {\r
390 Index3 = Index1;\r
391 //\r
392 // Sort key is the hardware default APIC ID\r
393 //\r
394 ApicId = CpuMpData->CpuData[Index1].ApicId;\r
395 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
396 if (ApicId > CpuMpData->CpuData[Index2].ApicId) {\r
397 Index3 = Index2;\r
398 ApicId = CpuMpData->CpuData[Index2].ApicId;\r
399 }\r
400 }\r
401 if (Index3 != Index1) {\r
402 CopyMem (&CpuData, &CpuMpData->CpuData[Index3], sizeof (CPU_AP_DATA));\r
403 CopyMem (\r
404 &CpuMpData->CpuData[Index3],\r
405 &CpuMpData->CpuData[Index1],\r
406 sizeof (CPU_AP_DATA)\r
407 );\r
408 CopyMem (&CpuMpData->CpuData[Index1], &CpuData, sizeof (CPU_AP_DATA));\r
409 }\r
410 }\r
411\r
412 //\r
413 // Get the processor number for the BSP\r
414 //\r
415 ApicId = GetInitialApicId ();\r
416 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
417 if (CpuMpData->CpuData[Index1].ApicId == ApicId) {\r
418 CpuMpData->BspNumber = (UINT32) Index1;\r
419 break;\r
420 }\r
421 }\r
422\r
423 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
424 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
425 CpuInfoInHob[Index1].InitialApicId = CpuMpData->CpuData[Index1].InitialApicId;\r
426 CpuInfoInHob[Index1].ApicId = CpuMpData->CpuData[Index1].ApicId;\r
427 CpuInfoInHob[Index1].Health = CpuMpData->CpuData[Index1].Health;\r
428 }\r
429 }\r
430}\r
431\r
fe627769
JF
432/**\r
433 Enable x2APIC mode on APs.\r
434\r
435 @param[in, out] Buffer Pointer to private data buffer.\r
436**/\r
437VOID\r
438EFIAPI\r
439ApFuncEnableX2Apic (\r
440 IN OUT VOID *Buffer\r
441 )\r
442{\r
443 SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
444}\r
445\r
b8b04307
JF
446/**\r
447 Do sync on APs.\r
448\r
449 @param[in, out] Buffer Pointer to private data buffer.\r
450**/\r
451VOID\r
452EFIAPI\r
453ApInitializeSync (\r
454 IN OUT VOID *Buffer\r
455 )\r
456{\r
457 CPU_MP_DATA *CpuMpData;\r
458\r
459 CpuMpData = (CPU_MP_DATA *) Buffer;\r
460 //\r
461 // Sync BSP's MTRR table to AP\r
462 //\r
463 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);\r
464 //\r
465 // Load microcode on AP\r
466 //\r
467 MicrocodeDetect (CpuMpData);\r
468}\r
469\r
470/**\r
471 Find the current Processor number by APIC ID.\r
472\r
473 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
474 @param[in] ProcessorNumber Return the pocessor number found\r
475\r
476 @retval EFI_SUCCESS ProcessorNumber is found and returned.\r
477 @retval EFI_NOT_FOUND ProcessorNumber is not found.\r
478**/\r
479EFI_STATUS\r
480GetProcessorNumber (\r
481 IN CPU_MP_DATA *CpuMpData,\r
482 OUT UINTN *ProcessorNumber\r
483 )\r
484{\r
485 UINTN TotalProcessorNumber;\r
486 UINTN Index;\r
487\r
488 TotalProcessorNumber = CpuMpData->CpuCount;\r
489 for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
490 if (CpuMpData->CpuData[Index].ApicId == GetApicId ()) {\r
491 *ProcessorNumber = Index;\r
492 return EFI_SUCCESS;\r
493 }\r
494 }\r
495 return EFI_NOT_FOUND;\r
496}\r
497\r
03434dff
JF
498/**\r
499 This function will get CPU count in the system.\r
500\r
501 @param[in] CpuMpData Pointer to PEI CPU MP Data\r
502\r
503 @return CPU count detected\r
504**/\r
505UINTN\r
506CollectProcessorCount (\r
507 IN CPU_MP_DATA *CpuMpData\r
508 )\r
509{\r
510 //\r
511 // Send 1st broadcast IPI to APs to wakeup APs\r
512 //\r
513 CpuMpData->InitFlag = ApInitConfig;\r
514 CpuMpData->X2ApicEnable = FALSE;\r
515 WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);\r
516 //\r
517 // Wait for AP task to complete and then exit.\r
518 //\r
519 MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds));\r
520 CpuMpData->InitFlag = ApInitDone;\r
521 ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
522 //\r
523 // Wait for all APs finished the initialization\r
524 //\r
525 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
526 CpuPause ();\r
527 }\r
528\r
fe627769
JF
529 if (CpuMpData->X2ApicEnable) {\r
530 DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));\r
531 //\r
532 // Wakeup all APs to enable x2APIC mode\r
533 //\r
534 WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL);\r
535 //\r
536 // Wait for all known APs finished\r
537 //\r
538 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
539 CpuPause ();\r
540 }\r
541 //\r
542 // Enable x2APIC on BSP\r
543 //\r
544 SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
545 }\r
546 DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));\r
8a2d564b
JF
547 //\r
548 // Sort BSP/Aps by CPU APIC ID in ascending order\r
549 //\r
550 SortApicId (CpuMpData);\r
551\r
03434dff
JF
552 DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));\r
553\r
554 return CpuMpData->CpuCount;\r
555}\r
556\r
03a1a925
JF
557/*\r
558 Initialize CPU AP Data when AP is wakeup at the first time.\r
559\r
560 @param[in, out] CpuMpData Pointer to PEI CPU MP Data\r
561 @param[in] ProcessorNumber The handle number of processor\r
562 @param[in] BistData Processor BIST data\r
563\r
564**/\r
565VOID\r
566InitializeApData (\r
567 IN OUT CPU_MP_DATA *CpuMpData,\r
568 IN UINTN ProcessorNumber,\r
569 IN UINT32 BistData\r
570 )\r
571{\r
572 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
573 CpuMpData->CpuData[ProcessorNumber].Health = BistData;\r
574 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
575 CpuMpData->CpuData[ProcessorNumber].ApicId = GetApicId ();\r
576 CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
577 if (CpuMpData->CpuData[ProcessorNumber].InitialApicId >= 0xFF) {\r
578 //\r
579 // Set x2APIC mode if there are any logical processor reporting\r
580 // an Initial APIC ID of 255 or greater.\r
581 //\r
582 AcquireSpinLock(&CpuMpData->MpLock);\r
583 CpuMpData->X2ApicEnable = TRUE;\r
584 ReleaseSpinLock(&CpuMpData->MpLock);\r
585 }\r
586\r
587 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);\r
588 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
589}\r
590\r
b8b04307
JF
591/**\r
592 This function will be called from AP reset code if BSP uses WakeUpAP.\r
593\r
594 @param[in] ExchangeInfo Pointer to the MP exchange info buffer\r
595 @param[in] NumApsExecuting Number of current executing AP\r
596**/\r
597VOID\r
598EFIAPI\r
599ApWakeupFunction (\r
600 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,\r
601 IN UINTN NumApsExecuting\r
602 )\r
603{\r
604 CPU_MP_DATA *CpuMpData;\r
605 UINTN ProcessorNumber;\r
606 EFI_AP_PROCEDURE Procedure;\r
607 VOID *Parameter;\r
608 UINT32 BistData;\r
609 volatile UINT32 *ApStartupSignalBuffer;\r
610\r
611 //\r
612 // AP finished assembly code and begin to execute C code\r
613 //\r
614 CpuMpData = ExchangeInfo->CpuMpData;\r
615\r
616 ProgramVirtualWireMode (); \r
617\r
618 while (TRUE) {\r
619 if (CpuMpData->InitFlag == ApInitConfig) {\r
620 //\r
621 // Add CPU number\r
622 //\r
623 InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);\r
624 ProcessorNumber = NumApsExecuting;\r
625 //\r
626 // This is first time AP wakeup, get BIST information from AP stack\r
627 //\r
628 BistData = *(UINT32 *) (CpuMpData->Buffer + ProcessorNumber * CpuMpData->CpuApStackSize - sizeof (UINTN));\r
629 //\r
630 // Do some AP initialize sync\r
631 //\r
632 ApInitializeSync (CpuMpData);\r
633 //\r
634 // Sync BSP's Control registers to APs\r
635 //\r
636 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);\r
637 InitializeApData (CpuMpData, ProcessorNumber, BistData);\r
638 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
639 } else {\r
640 //\r
641 // Execute AP function if AP is ready\r
642 //\r
643 GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
644 //\r
645 // Clear AP start-up signal when AP waken up\r
646 //\r
647 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
648 InterlockedCompareExchange32 (\r
649 (UINT32 *) ApStartupSignalBuffer,\r
650 WAKEUP_AP_SIGNAL,\r
651 0\r
652 );\r
653 if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
654 //\r
655 // Restore AP's volatile registers saved\r
656 //\r
657 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);\r
658 }\r
659\r
660 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {\r
661 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;\r
662 Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;\r
663 if (Procedure != NULL) {\r
664 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);\r
665 //\r
666 // Invoke AP function here\r
667 //\r
668 Procedure (Parameter);\r
41be0da5
JF
669 if (CpuMpData->SwitchBspFlag) {\r
670 //\r
671 // Re-get the processor number due to BSP/AP maybe exchange in AP function\r
672 //\r
673 GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
674 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;\r
675 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;\r
676 } else {\r
677 //\r
678 // Re-get the CPU APICID and Initial APICID\r
679 //\r
680 CpuMpData->CpuData[ProcessorNumber].ApicId = GetApicId ();\r
681 CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
682 }\r
b8b04307
JF
683 }\r
684 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);\r
685 }\r
686 }\r
687\r
688 //\r
689 // AP finished executing C code\r
690 //\r
691 InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);\r
692\r
693 //\r
694 // Place AP is specified loop mode\r
695 //\r
696 if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
697 //\r
698 // Save AP volatile registers\r
699 //\r
700 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);\r
701 //\r
702 // Place AP in HLT-loop\r
703 //\r
704 while (TRUE) {\r
705 DisableInterrupts ();\r
706 CpuSleep ();\r
707 CpuPause ();\r
708 }\r
709 }\r
710 while (TRUE) {\r
711 DisableInterrupts ();\r
712 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
713 //\r
714 // Place AP in MWAIT-loop\r
715 //\r
716 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);\r
717 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {\r
718 //\r
719 // Check AP start-up signal again.\r
720 // If AP start-up signal is not set, place AP into\r
721 // the specified C-state\r
722 //\r
723 AsmMwait (CpuMpData->ApTargetCState << 4, 0);\r
724 }\r
725 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {\r
726 //\r
727 // Place AP in Run-loop\r
728 //\r
729 CpuPause ();\r
730 } else {\r
731 ASSERT (FALSE);\r
732 }\r
733\r
734 //\r
735 // If AP start-up signal is written, AP is waken up\r
736 // otherwise place AP in loop again\r
737 //\r
738 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {\r
739 break;\r
740 }\r
741 }\r
742 }\r
743}\r
744\r
96f5920d
JF
745/**\r
746 Wait for AP wakeup and write AP start-up signal till AP is waken up.\r
747\r
748 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal\r
749**/\r
750VOID\r
751WaitApWakeup (\r
752 IN volatile UINT32 *ApStartupSignalBuffer\r
753 )\r
754{\r
755 //\r
756 // If AP is waken up, StartupApSignal should be cleared.\r
757 // Otherwise, write StartupApSignal again till AP waken up.\r
758 //\r
759 while (InterlockedCompareExchange32 (\r
760 (UINT32 *) ApStartupSignalBuffer,\r
761 WAKEUP_AP_SIGNAL,\r
762 WAKEUP_AP_SIGNAL\r
763 ) != 0) {\r
764 CpuPause ();\r
765 }\r
766}\r
767\r
7c3f2a12
JF
768/**\r
769 This function will fill the exchange info structure.\r
770\r
771 @param[in] CpuMpData Pointer to CPU MP Data\r
772\r
773**/\r
774VOID\r
775FillExchangeInfoData (\r
776 IN CPU_MP_DATA *CpuMpData\r
777 )\r
778{\r
779 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
780\r
781 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
782 ExchangeInfo->Lock = 0;\r
783 ExchangeInfo->StackStart = CpuMpData->Buffer;\r
784 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;\r
785 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;\r
786 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;\r
787\r
788 ExchangeInfo->CodeSegment = AsmReadCs ();\r
789 ExchangeInfo->DataSegment = AsmReadDs ();\r
790\r
791 ExchangeInfo->Cr3 = AsmReadCr3 ();\r
792\r
793 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;\r
794 ExchangeInfo->NumApsExecuting = 0;\r
795 ExchangeInfo->CpuMpData = CpuMpData;\r
796\r
797 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();\r
798\r
799 //\r
800 // Get the BSP's data of GDT and IDT\r
801 //\r
802 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);\r
803 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
804}\r
805\r
96f5920d
JF
806/**\r
807 This function will be called by BSP to wakeup AP.\r
808\r
809 @param[in] CpuMpData Pointer to CPU MP Data\r
810 @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
811 FALSE: Send IPI to AP by ApicId\r
812 @param[in] ProcessorNumber The handle number of specified processor\r
813 @param[in] Procedure The function to be invoked by AP\r
814 @param[in] ProcedureArgument The argument to be passed into AP function\r
815**/\r
816VOID\r
817WakeUpAP (\r
818 IN CPU_MP_DATA *CpuMpData,\r
819 IN BOOLEAN Broadcast,\r
820 IN UINTN ProcessorNumber,\r
821 IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
822 IN VOID *ProcedureArgument OPTIONAL\r
823 )\r
824{\r
825 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\r
826 UINTN Index;\r
827 CPU_AP_DATA *CpuData;\r
828 BOOLEAN ResetVectorRequired;\r
829\r
830 CpuMpData->FinishedCount = 0;\r
831 ResetVectorRequired = FALSE;\r
832\r
833 if (CpuMpData->ApLoopMode == ApInHltLoop ||\r
834 CpuMpData->InitFlag != ApInitDone) {\r
835 ResetVectorRequired = TRUE;\r
836 AllocateResetVector (CpuMpData);\r
837 FillExchangeInfoData (CpuMpData);\r
838 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
839 //\r
840 // Get AP target C-state each time when waking up AP,\r
841 // for it maybe updated by platform again\r
842 //\r
843 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
844 }\r
845\r
846 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
847\r
848 if (Broadcast) {\r
849 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
850 if (Index != CpuMpData->BspNumber) {\r
851 CpuData = &CpuMpData->CpuData[Index];\r
852 CpuData->ApFunction = (UINTN) Procedure;\r
853 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
854 SetApState (CpuData, CpuStateReady);\r
855 if (CpuMpData->InitFlag != ApInitConfig) {\r
856 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
857 }\r
858 }\r
859 }\r
860 if (ResetVectorRequired) {\r
861 //\r
862 // Wakeup all APs\r
863 //\r
864 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
865 }\r
866 if (CpuMpData->InitFlag != ApInitConfig) {\r
867 //\r
868 // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
869 //\r
870 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
871 CpuData = &CpuMpData->CpuData[Index];\r
872 if (Index != CpuMpData->BspNumber) {\r
873 WaitApWakeup (CpuData->StartupApSignal);\r
874 }\r
875 }\r
876 }\r
877 } else {\r
878 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
879 CpuData->ApFunction = (UINTN) Procedure;\r
880 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
881 SetApState (CpuData, CpuStateReady);\r
882 //\r
883 // Wakeup specified AP\r
884 //\r
885 ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
886 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
887 if (ResetVectorRequired) {\r
888 SendInitSipiSipi (\r
889 CpuData->ApicId,\r
890 (UINT32) ExchangeInfo->BufferStart\r
891 );\r
892 }\r
893 //\r
894 // Wait specified AP waken up\r
895 //\r
896 WaitApWakeup (CpuData->StartupApSignal);\r
897 }\r
898\r
899 if (ResetVectorRequired) {\r
900 FreeResetVector (CpuMpData);\r
901 }\r
902}\r
903\r
3e8ad6bd
JF
904/**\r
905 MP Initialize Library initialization.\r
906\r
907 This service will allocate AP reset vector and wakeup all APs to do APs\r
908 initialization.\r
909\r
910 This service must be invoked before all other MP Initialize Library\r
911 service are invoked.\r
912\r
913 @retval EFI_SUCCESS MP initialization succeeds.\r
914 @retval Others MP initialization fails.\r
915\r
916**/\r
917EFI_STATUS\r
918EFIAPI\r
919MpInitLibInitialize (\r
920 VOID\r
921 )\r
922{\r
6a2ee2bb
JF
923 CPU_MP_DATA *OldCpuMpData;\r
924 CPU_INFO_IN_HOB *CpuInfoInHob;\r
e59f8f6b
JF
925 UINT32 MaxLogicalProcessorNumber;\r
926 UINT32 ApStackSize;\r
f7f85d83 927 MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
e59f8f6b 928 UINTN BufferSize;\r
9ebcf0f4 929 UINT32 MonitorFilterSize;\r
e59f8f6b
JF
930 VOID *MpBuffer;\r
931 UINTN Buffer;\r
932 CPU_MP_DATA *CpuMpData;\r
9ebcf0f4 933 UINT8 ApLoopMode;\r
e59f8f6b 934 UINT8 *MonitorBuffer;\r
03a1a925 935 UINTN Index;\r
f7f85d83 936 UINTN ApResetVectorSize;\r
e59f8f6b 937 UINTN BackupBufferAddr;\r
6a2ee2bb
JF
938\r
939 OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
940 if (OldCpuMpData == NULL) {\r
941 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
942 } else {\r
943 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
944 }\r
f7f85d83
JF
945\r
946 AsmGetAddressMap (&AddressMap);\r
947 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
e59f8f6b 948 ApStackSize = PcdGet32(PcdCpuApStackSize);\r
9ebcf0f4
JF
949 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
950\r
e59f8f6b
JF
951 BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
952 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
953 BufferSize += sizeof (CPU_MP_DATA);\r
954 BufferSize += ApResetVectorSize;\r
955 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
956 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
957 ASSERT (MpBuffer != NULL);\r
958 ZeroMem (MpBuffer, BufferSize);\r
959 Buffer = (UINTN) MpBuffer;\r
960\r
961 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
962 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
963 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
964 CpuMpData->Buffer = Buffer;\r
965 CpuMpData->CpuApStackSize = ApStackSize;\r
966 CpuMpData->BackupBuffer = BackupBufferAddr;\r
967 CpuMpData->BackupBufferSize = ApResetVectorSize;\r
968 CpuMpData->EndOfPeiFlag = FALSE;\r
969 CpuMpData->WakeupBuffer = (UINTN) -1;\r
970 CpuMpData->CpuCount = 1;\r
971 CpuMpData->BspNumber = 0;\r
972 CpuMpData->WaitEvent = NULL;\r
41be0da5 973 CpuMpData->SwitchBspFlag = FALSE;\r
e59f8f6b
JF
974 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);\r
975 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
976 InitializeSpinLock(&CpuMpData->MpLock);\r
977 //\r
68cb9330
JF
978 // Save BSP's Control registers to APs\r
979 //\r
980 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
981 //\r
03a1a925
JF
982 // Set BSP basic information\r
983 //\r
984 InitializeApData (CpuMpData, 0, 0);\r
985 //\r
e59f8f6b
JF
986 // Save assembly code information\r
987 //\r
988 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
989 //\r
990 // Finally set AP loop mode\r
991 //\r
992 CpuMpData->ApLoopMode = ApLoopMode;\r
993 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
994 //\r
03a1a925
JF
995 // Set up APs wakeup signal buffer\r
996 //\r
997 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
998 CpuMpData->CpuData[Index].StartupApSignal =\r
999 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
1000 }\r
94f63c76
JF
1001 //\r
1002 // Load Microcode on BSP\r
1003 //\r
1004 MicrocodeDetect (CpuMpData);\r
1005 //\r
e59f8f6b
JF
1006 // Store BSP's MTRR setting\r
1007 //\r
1008 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
1009\r
6a2ee2bb
JF
1010 if (OldCpuMpData == NULL) {\r
1011 //\r
1012 // Wakeup all APs and calculate the processor count in system\r
1013 //\r
1014 CollectProcessorCount (CpuMpData);\r
1015 } else {\r
1016 //\r
1017 // APs have been wakeup before, just get the CPU Information\r
1018 // from HOB\r
1019 //\r
1020 CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
1021 CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
1022 CpuMpData->InitFlag = ApInitReconfig;\r
1023 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) OldCpuMpData->CpuInfoInHob;\r
1024 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1025 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
1026 CpuMpData->CpuData[Index].ApicId = CpuInfoInHob[Index].ApicId;\r
1027 CpuMpData->CpuData[Index].InitialApicId = CpuInfoInHob[Index].InitialApicId;\r
1028 if (CpuMpData->CpuData[Index].InitialApicId >= 255) {\r
1029 CpuMpData->X2ApicEnable = TRUE;\r
1030 }\r
1031 CpuMpData->CpuData[Index].Health = CpuInfoInHob[Index].Health;\r
1032 CpuMpData->CpuData[Index].CpuHealthy = (CpuMpData->CpuData[Index].Health == 0)? TRUE:FALSE;\r
1033 CpuMpData->CpuData[Index].ApFunction = 0;\r
1034 CopyMem (\r
1035 &CpuMpData->CpuData[Index].VolatileRegisters,\r
1036 &CpuMpData->CpuData[0].VolatileRegisters,\r
1037 sizeof (CPU_VOLATILE_REGISTERS)\r
1038 );\r
1039 }\r
1040 //\r
1041 // Wakeup APs to do some AP initialize sync\r
1042 //\r
1043 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
1044 //\r
1045 // Wait for all APs finished initialization\r
1046 //\r
1047 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
1048 CpuPause ();\r
1049 }\r
1050 CpuMpData->InitFlag = ApInitDone;\r
1051 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1052 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
1053 }\r
1054 }\r
93ca4c0f
JF
1055\r
1056 //\r
1057 // Initialize global data for MP support\r
1058 //\r
1059 InitMpGlobalData (CpuMpData);\r
1060\r
f7f85d83 1061 return EFI_SUCCESS;\r
3e8ad6bd
JF
1062}\r
1063\r
1064/**\r
1065 Gets detailed MP-related information on the requested processor at the\r
1066 instant this call is made. This service may only be called from the BSP.\r
1067\r
1068 @param[in] ProcessorNumber The handle number of processor.\r
1069 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
1070 the requested processor is deposited.\r
1071 @param[out] HealthData Return processor health data.\r
1072\r
1073 @retval EFI_SUCCESS Processor information was returned.\r
1074 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1075 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
1076 @retval EFI_NOT_FOUND The processor with the handle specified by\r
1077 ProcessorNumber does not exist in the platform.\r
1078 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1079\r
1080**/\r
1081EFI_STATUS\r
1082EFIAPI\r
1083MpInitLibGetProcessorInfo (\r
1084 IN UINTN ProcessorNumber,\r
1085 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,\r
1086 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL\r
1087 )\r
1088{\r
ad52f25e
JF
1089 CPU_MP_DATA *CpuMpData;\r
1090 UINTN CallerNumber;\r
1091\r
1092 CpuMpData = GetCpuMpData ();\r
1093\r
1094 //\r
1095 // Check whether caller processor is BSP\r
1096 //\r
1097 MpInitLibWhoAmI (&CallerNumber);\r
1098 if (CallerNumber != CpuMpData->BspNumber) {\r
1099 return EFI_DEVICE_ERROR;\r
1100 }\r
1101\r
1102 if (ProcessorInfoBuffer == NULL) {\r
1103 return EFI_INVALID_PARAMETER;\r
1104 }\r
1105\r
1106 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1107 return EFI_NOT_FOUND;\r
1108 }\r
1109\r
1110 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuMpData->CpuData[ProcessorNumber].ApicId;\r
1111 ProcessorInfoBuffer->StatusFlag = 0;\r
1112 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1113 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1114 }\r
1115 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
1116 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
1117 }\r
1118 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
1119 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
1120 } else {\r
1121 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
1122 }\r
1123\r
1124 //\r
1125 // Get processor location information\r
1126 //\r
1127 ExtractProcessorLocation (CpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);\r
1128\r
1129 if (HealthData != NULL) {\r
1130 HealthData->Uint32 = CpuMpData->CpuData[ProcessorNumber].Health;\r
1131 }\r
1132\r
1133 return EFI_SUCCESS;\r
3e8ad6bd 1134}\r
ad52f25e 1135\r
41be0da5
JF
1136/**\r
1137 Worker function to switch the requested AP to be the BSP from that point onward.\r
1138\r
1139 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
1140 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
1141 enabled AP. Otherwise, it will be disabled.\r
1142\r
1143 @retval EFI_SUCCESS BSP successfully switched.\r
1144 @retval others Failed to switch BSP. \r
1145\r
1146**/\r
1147EFI_STATUS\r
1148SwitchBSPWorker (\r
1149 IN UINTN ProcessorNumber,\r
1150 IN BOOLEAN EnableOldBSP\r
1151 )\r
1152{\r
1153 CPU_MP_DATA *CpuMpData;\r
1154 UINTN CallerNumber;\r
1155 CPU_STATE State;\r
1156 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
1157\r
1158 CpuMpData = GetCpuMpData ();\r
1159\r
1160 //\r
1161 // Check whether caller processor is BSP\r
1162 //\r
1163 MpInitLibWhoAmI (&CallerNumber);\r
1164 if (CallerNumber != CpuMpData->BspNumber) {\r
1165 return EFI_SUCCESS;\r
1166 }\r
1167\r
1168 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1169 return EFI_NOT_FOUND;\r
1170 }\r
1171\r
1172 //\r
1173 // Check whether specified AP is disabled\r
1174 //\r
1175 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
1176 if (State == CpuStateDisabled) {\r
1177 return EFI_INVALID_PARAMETER;\r
1178 }\r
1179\r
1180 //\r
1181 // Check whether ProcessorNumber specifies the current BSP\r
1182 //\r
1183 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1184 return EFI_INVALID_PARAMETER;\r
1185 }\r
1186\r
1187 //\r
1188 // Check whether specified AP is busy\r
1189 //\r
1190 if (State == CpuStateBusy) {\r
1191 return EFI_NOT_READY;\r
1192 }\r
1193\r
1194 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
1195 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
1196 CpuMpData->SwitchBspFlag = TRUE;\r
1197\r
1198 //\r
1199 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
1200 //\r
1201 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1202 ApicBaseMsr.Bits.BSP = 0;\r
1203 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1204\r
1205 //\r
1206 // Need to wakeUp AP (future BSP).\r
1207 //\r
1208 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
1209\r
1210 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
1211\r
1212 //\r
1213 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
1214 //\r
1215 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1216 ApicBaseMsr.Bits.BSP = 1;\r
1217 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1218\r
1219 //\r
1220 // Wait for old BSP finished AP task\r
1221 //\r
1222 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
1223 CpuPause ();\r
1224 }\r
1225\r
1226 CpuMpData->SwitchBspFlag = FALSE;\r
1227 //\r
1228 // Set old BSP enable state\r
1229 //\r
1230 if (!EnableOldBSP) {\r
1231 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
1232 }\r
1233 //\r
1234 // Save new BSP number\r
1235 //\r
1236 CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
1237\r
1238 return EFI_SUCCESS;\r
1239}\r
ad52f25e 1240\r
e37109bc
JF
1241/**\r
1242 Worker function to let the caller enable or disable an AP from this point onward.\r
1243 This service may only be called from the BSP.\r
1244\r
1245 @param[in] ProcessorNumber The handle number of AP.\r
1246 @param[in] EnableAP Specifies the new state for the processor for\r
1247 enabled, FALSE for disabled.\r
1248 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
1249 the new health status of the AP.\r
1250\r
1251 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1252 @retval others Failed to Enable/Disable AP.\r
1253\r
1254**/\r
1255EFI_STATUS\r
1256EnableDisableApWorker (\r
1257 IN UINTN ProcessorNumber,\r
1258 IN BOOLEAN EnableAP,\r
1259 IN UINT32 *HealthFlag OPTIONAL\r
1260 )\r
1261{\r
1262 CPU_MP_DATA *CpuMpData;\r
1263 UINTN CallerNumber;\r
1264\r
1265 CpuMpData = GetCpuMpData ();\r
1266\r
1267 //\r
1268 // Check whether caller processor is BSP\r
1269 //\r
1270 MpInitLibWhoAmI (&CallerNumber);\r
1271 if (CallerNumber != CpuMpData->BspNumber) {\r
1272 return EFI_DEVICE_ERROR;\r
1273 }\r
1274\r
1275 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1276 return EFI_INVALID_PARAMETER;\r
1277 }\r
1278\r
1279 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1280 return EFI_NOT_FOUND;\r
1281 }\r
1282\r
1283 if (!EnableAP) {\r
1284 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
1285 } else {\r
1286 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1287 }\r
1288\r
1289 if (HealthFlag != NULL) {\r
1290 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
1291 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
1292 }\r
1293\r
1294 return EFI_SUCCESS;\r
1295}\r
1296\r
3e8ad6bd
JF
1297/**\r
1298 This return the handle number for the calling processor. This service may be\r
1299 called from the BSP and APs.\r
1300\r
1301 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
1302 The range is from 0 to the total number of\r
1303 logical processors minus 1. The total number of\r
1304 logical processors can be retrieved by\r
1305 MpInitLibGetNumberOfProcessors().\r
1306\r
1307 @retval EFI_SUCCESS The current processor handle number was returned\r
1308 in ProcessorNumber.\r
1309 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1310 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1311\r
1312**/\r
1313EFI_STATUS\r
1314EFIAPI\r
1315MpInitLibWhoAmI (\r
1316 OUT UINTN *ProcessorNumber\r
1317 )\r
1318{\r
5c9e0997
JF
1319 CPU_MP_DATA *CpuMpData;\r
1320\r
1321 if (ProcessorNumber == NULL) {\r
1322 return EFI_INVALID_PARAMETER;\r
1323 }\r
1324\r
1325 CpuMpData = GetCpuMpData ();\r
1326\r
1327 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 1328}\r
809213a6 1329\r
3e8ad6bd
JF
1330/**\r
1331 Retrieves the number of logical processor in the platform and the number of\r
1332 those logical processors that are enabled on this boot. This service may only\r
1333 be called from the BSP.\r
1334\r
1335 @param[out] NumberOfProcessors Pointer to the total number of logical\r
1336 processors in the system, including the BSP\r
1337 and disabled APs.\r
1338 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
1339 processors that exist in system, including\r
1340 the BSP.\r
1341\r
1342 @retval EFI_SUCCESS The number of logical processors and enabled\r
1343 logical processors was retrieved.\r
1344 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1345 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
1346 is NULL.\r
1347 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1348\r
1349**/\r
1350EFI_STATUS\r
1351EFIAPI\r
1352MpInitLibGetNumberOfProcessors (\r
1353 OUT UINTN *NumberOfProcessors, OPTIONAL\r
1354 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
1355 )\r
1356{\r
809213a6
JF
1357 CPU_MP_DATA *CpuMpData;\r
1358 UINTN CallerNumber;\r
1359 UINTN ProcessorNumber;\r
1360 UINTN EnabledProcessorNumber;\r
1361 UINTN Index;\r
1362\r
1363 CpuMpData = GetCpuMpData ();\r
1364\r
1365 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
1366 return EFI_INVALID_PARAMETER;\r
1367 }\r
1368\r
1369 //\r
1370 // Check whether caller processor is BSP\r
1371 //\r
1372 MpInitLibWhoAmI (&CallerNumber);\r
1373 if (CallerNumber != CpuMpData->BspNumber) {\r
1374 return EFI_DEVICE_ERROR;\r
1375 }\r
1376\r
1377 ProcessorNumber = CpuMpData->CpuCount;\r
1378 EnabledProcessorNumber = 0;\r
1379 for (Index = 0; Index < ProcessorNumber; Index++) {\r
1380 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
1381 EnabledProcessorNumber ++;\r
1382 }\r
1383 }\r
1384\r
1385 if (NumberOfProcessors != NULL) {\r
1386 *NumberOfProcessors = ProcessorNumber;\r
1387 }\r
1388 if (NumberOfEnabledProcessors != NULL) {\r
1389 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
1390 }\r
1391\r
1392 return EFI_SUCCESS;\r
3e8ad6bd 1393}\r
6a2ee2bb 1394\r
809213a6 1395\r
93ca4c0f
JF
1396/**\r
1397 Get pointer to CPU MP Data structure from GUIDed HOB.\r
1398\r
1399 @return The pointer to CPU MP Data structure.\r
1400**/\r
1401CPU_MP_DATA *\r
1402GetCpuMpDataFromGuidedHob (\r
1403 VOID\r
1404 )\r
1405{\r
1406 EFI_HOB_GUID_TYPE *GuidHob;\r
1407 VOID *DataInHob;\r
1408 CPU_MP_DATA *CpuMpData;\r
1409\r
1410 CpuMpData = NULL;\r
1411 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
1412 if (GuidHob != NULL) {\r
1413 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
1414 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
1415 }\r
1416 return CpuMpData;\r
1417}\r