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