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