]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/MpInitLib/MpLib.c
IntelSiliconPkg: Add SMBIOS data HOB GUID
[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
f7f85d83
JF
1146\r
1147 AsmGetAddressMap (&AddressMap);\r
1148 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
e59f8f6b 1149 ApStackSize = PcdGet32(PcdCpuApStackSize);\r
9ebcf0f4
JF
1150 ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
1151\r
e59f8f6b
JF
1152 BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
1153 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
1154 BufferSize += sizeof (CPU_MP_DATA);\r
1155 BufferSize += ApResetVectorSize;\r
1156 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
1157 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
1158 ASSERT (MpBuffer != NULL);\r
1159 ZeroMem (MpBuffer, BufferSize);\r
1160 Buffer = (UINTN) MpBuffer;\r
1161\r
1162 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
1163 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
1164 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
1165 CpuMpData->Buffer = Buffer;\r
1166 CpuMpData->CpuApStackSize = ApStackSize;\r
1167 CpuMpData->BackupBuffer = BackupBufferAddr;\r
1168 CpuMpData->BackupBufferSize = ApResetVectorSize;\r
d11f10d1 1169 CpuMpData->SaveRestoreFlag = FALSE;\r
e59f8f6b
JF
1170 CpuMpData->WakeupBuffer = (UINTN) -1;\r
1171 CpuMpData->CpuCount = 1;\r
1172 CpuMpData->BspNumber = 0;\r
1173 CpuMpData->WaitEvent = NULL;\r
41be0da5 1174 CpuMpData->SwitchBspFlag = FALSE;\r
e59f8f6b
JF
1175 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);\r
1176 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
1177 InitializeSpinLock(&CpuMpData->MpLock);\r
1178 //\r
68cb9330
JF
1179 // Save BSP's Control registers to APs\r
1180 //\r
1181 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
1182 //\r
03a1a925
JF
1183 // Set BSP basic information\r
1184 //\r
1185 InitializeApData (CpuMpData, 0, 0);\r
1186 //\r
e59f8f6b
JF
1187 // Save assembly code information\r
1188 //\r
1189 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
1190 //\r
1191 // Finally set AP loop mode\r
1192 //\r
1193 CpuMpData->ApLoopMode = ApLoopMode;\r
1194 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
1195 //\r
03a1a925
JF
1196 // Set up APs wakeup signal buffer\r
1197 //\r
1198 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
1199 CpuMpData->CpuData[Index].StartupApSignal =\r
1200 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
1201 }\r
94f63c76
JF
1202 //\r
1203 // Load Microcode on BSP\r
1204 //\r
1205 MicrocodeDetect (CpuMpData);\r
1206 //\r
e59f8f6b
JF
1207 // Store BSP's MTRR setting\r
1208 //\r
1209 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
1210\r
6a2ee2bb
JF
1211 if (OldCpuMpData == NULL) {\r
1212 //\r
1213 // Wakeup all APs and calculate the processor count in system\r
1214 //\r
1215 CollectProcessorCount (CpuMpData);\r
1216 } else {\r
1217 //\r
1218 // APs have been wakeup before, just get the CPU Information\r
1219 // from HOB\r
1220 //\r
1221 CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
1222 CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
1223 CpuMpData->InitFlag = ApInitReconfig;\r
1224 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) OldCpuMpData->CpuInfoInHob;\r
1225 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1226 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
1227 CpuMpData->CpuData[Index].ApicId = CpuInfoInHob[Index].ApicId;\r
1228 CpuMpData->CpuData[Index].InitialApicId = CpuInfoInHob[Index].InitialApicId;\r
1229 if (CpuMpData->CpuData[Index].InitialApicId >= 255) {\r
1230 CpuMpData->X2ApicEnable = TRUE;\r
1231 }\r
1232 CpuMpData->CpuData[Index].Health = CpuInfoInHob[Index].Health;\r
1233 CpuMpData->CpuData[Index].CpuHealthy = (CpuMpData->CpuData[Index].Health == 0)? TRUE:FALSE;\r
1234 CpuMpData->CpuData[Index].ApFunction = 0;\r
1235 CopyMem (\r
1236 &CpuMpData->CpuData[Index].VolatileRegisters,\r
1237 &CpuMpData->CpuData[0].VolatileRegisters,\r
1238 sizeof (CPU_VOLATILE_REGISTERS)\r
1239 );\r
1240 }\r
1241 //\r
1242 // Wakeup APs to do some AP initialize sync\r
1243 //\r
1244 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
1245 //\r
1246 // Wait for all APs finished initialization\r
1247 //\r
1248 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
1249 CpuPause ();\r
1250 }\r
1251 CpuMpData->InitFlag = ApInitDone;\r
1252 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
1253 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
1254 }\r
1255 }\r
93ca4c0f
JF
1256\r
1257 //\r
1258 // Initialize global data for MP support\r
1259 //\r
1260 InitMpGlobalData (CpuMpData);\r
1261\r
f7f85d83 1262 return EFI_SUCCESS;\r
3e8ad6bd
JF
1263}\r
1264\r
1265/**\r
1266 Gets detailed MP-related information on the requested processor at the\r
1267 instant this call is made. This service may only be called from the BSP.\r
1268\r
1269 @param[in] ProcessorNumber The handle number of processor.\r
1270 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
1271 the requested processor is deposited.\r
1272 @param[out] HealthData Return processor health data.\r
1273\r
1274 @retval EFI_SUCCESS Processor information was returned.\r
1275 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1276 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
1277 @retval EFI_NOT_FOUND The processor with the handle specified by\r
1278 ProcessorNumber does not exist in the platform.\r
1279 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1280\r
1281**/\r
1282EFI_STATUS\r
1283EFIAPI\r
1284MpInitLibGetProcessorInfo (\r
1285 IN UINTN ProcessorNumber,\r
1286 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,\r
1287 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL\r
1288 )\r
1289{\r
ad52f25e
JF
1290 CPU_MP_DATA *CpuMpData;\r
1291 UINTN CallerNumber;\r
1292\r
1293 CpuMpData = GetCpuMpData ();\r
1294\r
1295 //\r
1296 // Check whether caller processor is BSP\r
1297 //\r
1298 MpInitLibWhoAmI (&CallerNumber);\r
1299 if (CallerNumber != CpuMpData->BspNumber) {\r
1300 return EFI_DEVICE_ERROR;\r
1301 }\r
1302\r
1303 if (ProcessorInfoBuffer == NULL) {\r
1304 return EFI_INVALID_PARAMETER;\r
1305 }\r
1306\r
1307 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1308 return EFI_NOT_FOUND;\r
1309 }\r
1310\r
1311 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuMpData->CpuData[ProcessorNumber].ApicId;\r
1312 ProcessorInfoBuffer->StatusFlag = 0;\r
1313 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1314 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
1315 }\r
1316 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
1317 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
1318 }\r
1319 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
1320 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
1321 } else {\r
1322 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
1323 }\r
1324\r
1325 //\r
1326 // Get processor location information\r
1327 //\r
73152f19
LD
1328 GetProcessorLocation (\r
1329 CpuMpData->CpuData[ProcessorNumber].ApicId,\r
1330 &ProcessorInfoBuffer->Location.Package,\r
1331 &ProcessorInfoBuffer->Location.Core,\r
1332 &ProcessorInfoBuffer->Location.Thread\r
1333 );\r
ad52f25e
JF
1334\r
1335 if (HealthData != NULL) {\r
1336 HealthData->Uint32 = CpuMpData->CpuData[ProcessorNumber].Health;\r
1337 }\r
1338\r
1339 return EFI_SUCCESS;\r
3e8ad6bd 1340}\r
ad52f25e 1341\r
41be0da5
JF
1342/**\r
1343 Worker function to switch the requested AP to be the BSP from that point onward.\r
1344\r
1345 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.\r
1346 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
1347 enabled AP. Otherwise, it will be disabled.\r
1348\r
1349 @retval EFI_SUCCESS BSP successfully switched.\r
1350 @retval others Failed to switch BSP. \r
1351\r
1352**/\r
1353EFI_STATUS\r
1354SwitchBSPWorker (\r
1355 IN UINTN ProcessorNumber,\r
1356 IN BOOLEAN EnableOldBSP\r
1357 )\r
1358{\r
1359 CPU_MP_DATA *CpuMpData;\r
1360 UINTN CallerNumber;\r
1361 CPU_STATE State;\r
1362 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
1363\r
1364 CpuMpData = GetCpuMpData ();\r
1365\r
1366 //\r
1367 // Check whether caller processor is BSP\r
1368 //\r
1369 MpInitLibWhoAmI (&CallerNumber);\r
1370 if (CallerNumber != CpuMpData->BspNumber) {\r
1371 return EFI_SUCCESS;\r
1372 }\r
1373\r
1374 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1375 return EFI_NOT_FOUND;\r
1376 }\r
1377\r
1378 //\r
1379 // Check whether specified AP is disabled\r
1380 //\r
1381 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
1382 if (State == CpuStateDisabled) {\r
1383 return EFI_INVALID_PARAMETER;\r
1384 }\r
1385\r
1386 //\r
1387 // Check whether ProcessorNumber specifies the current BSP\r
1388 //\r
1389 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1390 return EFI_INVALID_PARAMETER;\r
1391 }\r
1392\r
1393 //\r
1394 // Check whether specified AP is busy\r
1395 //\r
1396 if (State == CpuStateBusy) {\r
1397 return EFI_NOT_READY;\r
1398 }\r
1399\r
1400 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
1401 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
1402 CpuMpData->SwitchBspFlag = TRUE;\r
1403\r
1404 //\r
1405 // Clear the BSP bit of MSR_IA32_APIC_BASE\r
1406 //\r
1407 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1408 ApicBaseMsr.Bits.BSP = 0;\r
1409 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1410\r
1411 //\r
1412 // Need to wakeUp AP (future BSP).\r
1413 //\r
1414 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
1415\r
1416 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
1417\r
1418 //\r
1419 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
1420 //\r
1421 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
1422 ApicBaseMsr.Bits.BSP = 1;\r
1423 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
1424\r
1425 //\r
1426 // Wait for old BSP finished AP task\r
1427 //\r
1428 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
1429 CpuPause ();\r
1430 }\r
1431\r
1432 CpuMpData->SwitchBspFlag = FALSE;\r
1433 //\r
1434 // Set old BSP enable state\r
1435 //\r
1436 if (!EnableOldBSP) {\r
1437 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
1438 }\r
1439 //\r
1440 // Save new BSP number\r
1441 //\r
1442 CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
1443\r
1444 return EFI_SUCCESS;\r
1445}\r
ad52f25e 1446\r
e37109bc
JF
1447/**\r
1448 Worker function to let the caller enable or disable an AP from this point onward.\r
1449 This service may only be called from the BSP.\r
1450\r
1451 @param[in] ProcessorNumber The handle number of AP.\r
1452 @param[in] EnableAP Specifies the new state for the processor for\r
1453 enabled, FALSE for disabled.\r
1454 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
1455 the new health status of the AP.\r
1456\r
1457 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
1458 @retval others Failed to Enable/Disable AP.\r
1459\r
1460**/\r
1461EFI_STATUS\r
1462EnableDisableApWorker (\r
1463 IN UINTN ProcessorNumber,\r
1464 IN BOOLEAN EnableAP,\r
1465 IN UINT32 *HealthFlag OPTIONAL\r
1466 )\r
1467{\r
1468 CPU_MP_DATA *CpuMpData;\r
1469 UINTN CallerNumber;\r
1470\r
1471 CpuMpData = GetCpuMpData ();\r
1472\r
1473 //\r
1474 // Check whether caller processor is BSP\r
1475 //\r
1476 MpInitLibWhoAmI (&CallerNumber);\r
1477 if (CallerNumber != CpuMpData->BspNumber) {\r
1478 return EFI_DEVICE_ERROR;\r
1479 }\r
1480\r
1481 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1482 return EFI_INVALID_PARAMETER;\r
1483 }\r
1484\r
1485 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1486 return EFI_NOT_FOUND;\r
1487 }\r
1488\r
1489 if (!EnableAP) {\r
1490 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
1491 } else {\r
1492 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
1493 }\r
1494\r
1495 if (HealthFlag != NULL) {\r
1496 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
1497 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
1498 }\r
1499\r
1500 return EFI_SUCCESS;\r
1501}\r
1502\r
3e8ad6bd
JF
1503/**\r
1504 This return the handle number for the calling processor. This service may be\r
1505 called from the BSP and APs.\r
1506\r
1507 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
1508 The range is from 0 to the total number of\r
1509 logical processors minus 1. The total number of\r
1510 logical processors can be retrieved by\r
1511 MpInitLibGetNumberOfProcessors().\r
1512\r
1513 @retval EFI_SUCCESS The current processor handle number was returned\r
1514 in ProcessorNumber.\r
1515 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
1516 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1517\r
1518**/\r
1519EFI_STATUS\r
1520EFIAPI\r
1521MpInitLibWhoAmI (\r
1522 OUT UINTN *ProcessorNumber\r
1523 )\r
1524{\r
5c9e0997
JF
1525 CPU_MP_DATA *CpuMpData;\r
1526\r
1527 if (ProcessorNumber == NULL) {\r
1528 return EFI_INVALID_PARAMETER;\r
1529 }\r
1530\r
1531 CpuMpData = GetCpuMpData ();\r
1532\r
1533 return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
3e8ad6bd 1534}\r
809213a6 1535\r
3e8ad6bd
JF
1536/**\r
1537 Retrieves the number of logical processor in the platform and the number of\r
1538 those logical processors that are enabled on this boot. This service may only\r
1539 be called from the BSP.\r
1540\r
1541 @param[out] NumberOfProcessors Pointer to the total number of logical\r
1542 processors in the system, including the BSP\r
1543 and disabled APs.\r
1544 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
1545 processors that exist in system, including\r
1546 the BSP.\r
1547\r
1548 @retval EFI_SUCCESS The number of logical processors and enabled\r
1549 logical processors was retrieved.\r
1550 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
1551 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
1552 is NULL.\r
1553 @retval EFI_NOT_READY MP Initialize Library is not initialized.\r
1554\r
1555**/\r
1556EFI_STATUS\r
1557EFIAPI\r
1558MpInitLibGetNumberOfProcessors (\r
1559 OUT UINTN *NumberOfProcessors, OPTIONAL\r
1560 OUT UINTN *NumberOfEnabledProcessors OPTIONAL\r
1561 )\r
1562{\r
809213a6
JF
1563 CPU_MP_DATA *CpuMpData;\r
1564 UINTN CallerNumber;\r
1565 UINTN ProcessorNumber;\r
1566 UINTN EnabledProcessorNumber;\r
1567 UINTN Index;\r
1568\r
1569 CpuMpData = GetCpuMpData ();\r
1570\r
1571 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
1572 return EFI_INVALID_PARAMETER;\r
1573 }\r
1574\r
1575 //\r
1576 // Check whether caller processor is BSP\r
1577 //\r
1578 MpInitLibWhoAmI (&CallerNumber);\r
1579 if (CallerNumber != CpuMpData->BspNumber) {\r
1580 return EFI_DEVICE_ERROR;\r
1581 }\r
1582\r
1583 ProcessorNumber = CpuMpData->CpuCount;\r
1584 EnabledProcessorNumber = 0;\r
1585 for (Index = 0; Index < ProcessorNumber; Index++) {\r
1586 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
1587 EnabledProcessorNumber ++;\r
1588 }\r
1589 }\r
1590\r
1591 if (NumberOfProcessors != NULL) {\r
1592 *NumberOfProcessors = ProcessorNumber;\r
1593 }\r
1594 if (NumberOfEnabledProcessors != NULL) {\r
1595 *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
1596 }\r
1597\r
1598 return EFI_SUCCESS;\r
3e8ad6bd 1599}\r
6a2ee2bb 1600\r
809213a6 1601\r
86efe976
JF
1602/**\r
1603 Worker function to execute a caller provided function on all enabled APs.\r
1604\r
1605 @param[in] Procedure A pointer to the function to be run on\r
1606 enabled APs of the system.\r
1607 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
1608 the function specified by Procedure one by\r
1609 one, in ascending order of processor handle\r
1610 number. If FALSE, then all the enabled APs\r
1611 execute the function specified by Procedure\r
1612 simultaneously.\r
1613 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1614 service.\r
1615 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for\r
1616 APs to return from Procedure, either for\r
1617 blocking or non-blocking mode.\r
1618 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1619 all APs.\r
1620 @param[out] FailedCpuList If all APs finish successfully, then its\r
1621 content is set to NULL. If not all APs\r
1622 finish before timeout expires, then its\r
1623 content is set to address of the buffer\r
1624 holding handle numbers of the failed APs.\r
1625\r
1626 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1627 the timeout expired.\r
1628 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1629 to all enabled APs.\r
1630 @retval others Failed to Startup all APs.\r
1631\r
1632**/\r
1633EFI_STATUS\r
1634StartupAllAPsWorker (\r
1635 IN EFI_AP_PROCEDURE Procedure,\r
1636 IN BOOLEAN SingleThread,\r
1637 IN EFI_EVENT WaitEvent OPTIONAL,\r
1638 IN UINTN TimeoutInMicroseconds,\r
1639 IN VOID *ProcedureArgument OPTIONAL,\r
1640 OUT UINTN **FailedCpuList OPTIONAL\r
1641 )\r
1642{\r
1643 EFI_STATUS Status;\r
1644 CPU_MP_DATA *CpuMpData;\r
1645 UINTN ProcessorCount;\r
1646 UINTN ProcessorNumber;\r
1647 UINTN CallerNumber;\r
1648 CPU_AP_DATA *CpuData;\r
1649 BOOLEAN HasEnabledAp;\r
1650 CPU_STATE ApState;\r
1651\r
1652 CpuMpData = GetCpuMpData ();\r
1653\r
1654 if (FailedCpuList != NULL) {\r
1655 *FailedCpuList = NULL;\r
1656 }\r
1657\r
1658 if (CpuMpData->CpuCount == 1) {\r
1659 return EFI_NOT_STARTED;\r
1660 }\r
1661\r
1662 if (Procedure == NULL) {\r
1663 return EFI_INVALID_PARAMETER;\r
1664 }\r
1665\r
1666 //\r
1667 // Check whether caller processor is BSP\r
1668 //\r
1669 MpInitLibWhoAmI (&CallerNumber);\r
1670 if (CallerNumber != CpuMpData->BspNumber) {\r
1671 return EFI_DEVICE_ERROR;\r
1672 }\r
1673\r
1674 //\r
1675 // Update AP state\r
1676 //\r
1677 CheckAndUpdateApsStatus ();\r
1678\r
1679 ProcessorCount = CpuMpData->CpuCount;\r
1680 HasEnabledAp = FALSE;\r
1681 //\r
1682 // Check whether all enabled APs are idle.\r
1683 // If any enabled AP is not idle, return EFI_NOT_READY.\r
1684 //\r
1685 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1686 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1687 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1688 ApState = GetApState (CpuData);\r
1689 if (ApState != CpuStateDisabled) {\r
1690 HasEnabledAp = TRUE;\r
1691 if (ApState != CpuStateIdle) {\r
1692 //\r
1693 // If any enabled APs are busy, return EFI_NOT_READY.\r
1694 //\r
1695 return EFI_NOT_READY;\r
1696 }\r
1697 }\r
1698 }\r
1699 }\r
1700\r
1701 if (!HasEnabledAp) {\r
1702 //\r
1703 // If no enabled AP exists, return EFI_NOT_STARTED.\r
1704 //\r
1705 return EFI_NOT_STARTED;\r
1706 }\r
1707\r
1708 CpuMpData->StartCount = 0;\r
1709 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1710 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1711 CpuData->Waiting = FALSE;\r
1712 if (ProcessorNumber != CpuMpData->BspNumber) {\r
1713 if (CpuData->State == CpuStateIdle) {\r
1714 //\r
1715 // Mark this processor as responsible for current calling.\r
1716 //\r
1717 CpuData->Waiting = TRUE;\r
1718 CpuMpData->StartCount++;\r
1719 }\r
1720 }\r
1721 }\r
1722\r
1723 CpuMpData->Procedure = Procedure;\r
1724 CpuMpData->ProcArguments = ProcedureArgument;\r
1725 CpuMpData->SingleThread = SingleThread;\r
1726 CpuMpData->FinishedCount = 0;\r
1727 CpuMpData->RunningCount = 0;\r
1728 CpuMpData->FailedCpuList = FailedCpuList;\r
1729 CpuMpData->ExpectedTime = CalculateTimeout (\r
1730 TimeoutInMicroseconds,\r
1731 &CpuMpData->CurrentTime\r
1732 );\r
1733 CpuMpData->TotalTime = 0;\r
1734 CpuMpData->WaitEvent = WaitEvent;\r
1735\r
1736 if (!SingleThread) {\r
1737 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
1738 } else {\r
1739 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
1740 if (ProcessorNumber == CallerNumber) {\r
1741 continue;\r
1742 }\r
1743 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
1744 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
1745 break;\r
1746 }\r
1747 }\r
1748 }\r
1749\r
1750 Status = EFI_SUCCESS;\r
1751 if (WaitEvent == NULL) {\r
1752 do {\r
1753 Status = CheckAllAPs ();\r
1754 } while (Status == EFI_NOT_READY);\r
1755 }\r
1756\r
1757 return Status;\r
1758}\r
1759\r
20ae5774
JF
1760/**\r
1761 Worker function to let the caller get one enabled AP to execute a caller-provided\r
1762 function.\r
1763\r
1764 @param[in] Procedure A pointer to the function to be run on\r
1765 enabled APs of the system.\r
1766 @param[in] ProcessorNumber The handle number of the AP.\r
1767 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
1768 service.\r
1769 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for\r
1770 APs to return from Procedure, either for\r
1771 blocking or non-blocking mode.\r
1772 @param[in] ProcedureArgument The parameter passed into Procedure for\r
1773 all APs.\r
1774 @param[out] Finished If AP returns from Procedure before the\r
1775 timeout expires, its content is set to TRUE.\r
1776 Otherwise, the value is set to FALSE.\r
1777\r
1778 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
1779 the timeout expires.\r
1780 @retval others Failed to Startup AP.\r
1781\r
1782**/\r
1783EFI_STATUS\r
1784StartupThisAPWorker (\r
1785 IN EFI_AP_PROCEDURE Procedure,\r
1786 IN UINTN ProcessorNumber,\r
1787 IN EFI_EVENT WaitEvent OPTIONAL,\r
1788 IN UINTN TimeoutInMicroseconds,\r
1789 IN VOID *ProcedureArgument OPTIONAL,\r
1790 OUT BOOLEAN *Finished OPTIONAL\r
1791 )\r
1792{\r
1793 EFI_STATUS Status;\r
1794 CPU_MP_DATA *CpuMpData;\r
1795 CPU_AP_DATA *CpuData;\r
1796 UINTN CallerNumber;\r
1797\r
1798 CpuMpData = GetCpuMpData ();\r
1799\r
1800 if (Finished != NULL) {\r
1801 *Finished = FALSE;\r
1802 }\r
1803\r
1804 //\r
1805 // Check whether caller processor is BSP\r
1806 //\r
1807 MpInitLibWhoAmI (&CallerNumber);\r
1808 if (CallerNumber != CpuMpData->BspNumber) {\r
1809 return EFI_DEVICE_ERROR;\r
1810 }\r
1811\r
1812 //\r
1813 // Check whether processor with the handle specified by ProcessorNumber exists\r
1814 //\r
1815 if (ProcessorNumber >= CpuMpData->CpuCount) {\r
1816 return EFI_NOT_FOUND;\r
1817 }\r
1818\r
1819 //\r
1820 // Check whether specified processor is BSP\r
1821 //\r
1822 if (ProcessorNumber == CpuMpData->BspNumber) {\r
1823 return EFI_INVALID_PARAMETER;\r
1824 }\r
1825\r
1826 //\r
1827 // Check parameter Procedure\r
1828 //\r
1829 if (Procedure == NULL) {\r
1830 return EFI_INVALID_PARAMETER;\r
1831 }\r
1832\r
1833 //\r
1834 // Update AP state\r
1835 //\r
1836 CheckAndUpdateApsStatus ();\r
1837\r
1838 //\r
1839 // Check whether specified AP is disabled\r
1840 //\r
1841 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
1842 return EFI_INVALID_PARAMETER;\r
1843 }\r
1844\r
1845 //\r
1846 // If WaitEvent is not NULL, execute in non-blocking mode.\r
1847 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
1848 // CheckAPsStatus() will check completion and timeout periodically.\r
1849 //\r
1850 CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
1851 CpuData->WaitEvent = WaitEvent;\r
1852 CpuData->Finished = Finished;\r
1853 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
1854 CpuData->TotalTime = 0;\r
1855\r
1856 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
1857\r
1858 //\r
1859 // If WaitEvent is NULL, execute in blocking mode.\r
1860 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
1861 //\r
1862 Status = EFI_SUCCESS;\r
1863 if (WaitEvent == NULL) {\r
1864 do {\r
1865 Status = CheckThisAP (ProcessorNumber);\r
1866 } while (Status == EFI_NOT_READY);\r
1867 }\r
1868\r
1869 return Status;\r
1870}\r
1871\r
93ca4c0f
JF
1872/**\r
1873 Get pointer to CPU MP Data structure from GUIDed HOB.\r
1874\r
1875 @return The pointer to CPU MP Data structure.\r
1876**/\r
1877CPU_MP_DATA *\r
1878GetCpuMpDataFromGuidedHob (\r
1879 VOID\r
1880 )\r
1881{\r
1882 EFI_HOB_GUID_TYPE *GuidHob;\r
1883 VOID *DataInHob;\r
1884 CPU_MP_DATA *CpuMpData;\r
1885\r
1886 CpuMpData = NULL;\r
1887 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
1888 if (GuidHob != NULL) {\r
1889 DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
1890 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
1891 }\r
1892 return CpuMpData;\r
1893}\r
42c37b3b
JF
1894\r
1895/**\r
1896 Get available system memory below 1MB by specified size.\r
1897\r
1898 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
1899**/\r
1900VOID\r
1901BackupAndPrepareWakeupBuffer(\r
1902 IN CPU_MP_DATA *CpuMpData\r
1903 )\r
1904{\r
1905 CopyMem (\r
1906 (VOID *) CpuMpData->BackupBuffer,\r
1907 (VOID *) CpuMpData->WakeupBuffer,\r
1908 CpuMpData->BackupBufferSize\r
1909 );\r
1910 CopyMem (\r
1911 (VOID *) CpuMpData->WakeupBuffer,\r
1912 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
1913 CpuMpData->AddressMap.RendezvousFunnelSize\r
1914 );\r
1915}\r
1916\r
1917/**\r
1918 Restore wakeup buffer data.\r
1919\r
1920 @param[in] CpuMpData The pointer to CPU MP Data structure.\r
1921**/\r
1922VOID\r
1923RestoreWakeupBuffer(\r
1924 IN CPU_MP_DATA *CpuMpData\r
1925 )\r
1926{\r
1927 CopyMem (\r
1928 (VOID *) CpuMpData->WakeupBuffer,\r
1929 (VOID *) CpuMpData->BackupBuffer,\r
1930 CpuMpData->BackupBufferSize\r
1931 );\r
1932}\r