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