]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.c
ed22ce67782ef48b1148b5aac9868b1789a30e50
[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[in] 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
430 **/
431 VOID
432 InitializeApData (
433 IN OUT CPU_MP_DATA *CpuMpData,
434 IN UINTN ProcessorNumber,
435 IN UINT32 BistData,
436 IN UINT64 ApTopOfStack
437 )
438 {
439 CPU_INFO_IN_HOB *CpuInfoInHob;
440
441 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
442 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
443 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
444 CpuInfoInHob[ProcessorNumber].Health = BistData;
445 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;
446
447 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
448 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;
449 if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {
450 //
451 // Set x2APIC mode if there are any logical processor reporting
452 // an Initial APIC ID of 255 or greater.
453 //
454 AcquireSpinLock(&CpuMpData->MpLock);
455 CpuMpData->X2ApicEnable = TRUE;
456 ReleaseSpinLock(&CpuMpData->MpLock);
457 }
458
459 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);
460 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
461 }
462
463 /**
464 This function will be called from AP reset code if BSP uses WakeUpAP.
465
466 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
467 @param[in] NumApsExecuting Number of current executing AP
468 **/
469 VOID
470 EFIAPI
471 ApWakeupFunction (
472 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,
473 IN UINTN NumApsExecuting
474 )
475 {
476 CPU_MP_DATA *CpuMpData;
477 UINTN ProcessorNumber;
478 EFI_AP_PROCEDURE Procedure;
479 VOID *Parameter;
480 UINT32 BistData;
481 volatile UINT32 *ApStartupSignalBuffer;
482 CPU_INFO_IN_HOB *CpuInfoInHob;
483 UINT64 ApTopOfStack;
484
485 //
486 // AP finished assembly code and begin to execute C code
487 //
488 CpuMpData = ExchangeInfo->CpuMpData;
489
490 ProgramVirtualWireMode ();
491
492 while (TRUE) {
493 if (CpuMpData->InitFlag == ApInitConfig) {
494 //
495 // Add CPU number
496 //
497 InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);
498 ProcessorNumber = NumApsExecuting;
499 //
500 // This is first time AP wakeup, get BIST information from AP stack
501 //
502 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;
503 BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));
504 //
505 // Do some AP initialize sync
506 //
507 ApInitializeSync (CpuMpData);
508 //
509 // Sync BSP's Control registers to APs
510 //
511 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
512 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);
513 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
514 } else {
515 //
516 // Execute AP function if AP is ready
517 //
518 GetProcessorNumber (CpuMpData, &ProcessorNumber);
519 //
520 // Clear AP start-up signal when AP waken up
521 //
522 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
523 InterlockedCompareExchange32 (
524 (UINT32 *) ApStartupSignalBuffer,
525 WAKEUP_AP_SIGNAL,
526 0
527 );
528 if (CpuMpData->ApLoopMode == ApInHltLoop) {
529 //
530 // Restore AP's volatile registers saved
531 //
532 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);
533 }
534
535 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {
536 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;
537 Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;
538 if (Procedure != NULL) {
539 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);
540 //
541 // Invoke AP function here
542 //
543 Procedure (Parameter);
544 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
545 if (CpuMpData->SwitchBspFlag) {
546 //
547 // Re-get the processor number due to BSP/AP maybe exchange in AP function
548 //
549 GetProcessorNumber (CpuMpData, &ProcessorNumber);
550 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;
551 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;
552 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
553 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;
554 } else {
555 //
556 // Re-get the CPU APICID and Initial APICID
557 //
558 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
559 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
560 }
561 }
562 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);
563 }
564 }
565
566 //
567 // AP finished executing C code
568 //
569 InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);
570
571 //
572 // Place AP is specified loop mode
573 //
574 if (CpuMpData->ApLoopMode == ApInHltLoop) {
575 //
576 // Save AP volatile registers
577 //
578 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);
579 //
580 // Place AP in HLT-loop
581 //
582 while (TRUE) {
583 DisableInterrupts ();
584 CpuSleep ();
585 CpuPause ();
586 }
587 }
588 while (TRUE) {
589 DisableInterrupts ();
590 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
591 //
592 // Place AP in MWAIT-loop
593 //
594 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);
595 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {
596 //
597 // Check AP start-up signal again.
598 // If AP start-up signal is not set, place AP into
599 // the specified C-state
600 //
601 AsmMwait (CpuMpData->ApTargetCState << 4, 0);
602 }
603 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {
604 //
605 // Place AP in Run-loop
606 //
607 CpuPause ();
608 } else {
609 ASSERT (FALSE);
610 }
611
612 //
613 // If AP start-up signal is written, AP is waken up
614 // otherwise place AP in loop again
615 //
616 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {
617 break;
618 }
619 }
620 }
621 }
622
623 /**
624 Wait for AP wakeup and write AP start-up signal till AP is waken up.
625
626 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
627 **/
628 VOID
629 WaitApWakeup (
630 IN volatile UINT32 *ApStartupSignalBuffer
631 )
632 {
633 //
634 // If AP is waken up, StartupApSignal should be cleared.
635 // Otherwise, write StartupApSignal again till AP waken up.
636 //
637 while (InterlockedCompareExchange32 (
638 (UINT32 *) ApStartupSignalBuffer,
639 WAKEUP_AP_SIGNAL,
640 WAKEUP_AP_SIGNAL
641 ) != 0) {
642 CpuPause ();
643 }
644 }
645
646 /**
647 This function will fill the exchange info structure.
648
649 @param[in] CpuMpData Pointer to CPU MP Data
650
651 **/
652 VOID
653 FillExchangeInfoData (
654 IN CPU_MP_DATA *CpuMpData
655 )
656 {
657 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
658
659 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
660 ExchangeInfo->Lock = 0;
661 ExchangeInfo->StackStart = CpuMpData->Buffer;
662 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;
663 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;
664 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;
665
666 ExchangeInfo->CodeSegment = AsmReadCs ();
667 ExchangeInfo->DataSegment = AsmReadDs ();
668
669 ExchangeInfo->Cr3 = AsmReadCr3 ();
670
671 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;
672 ExchangeInfo->NumApsExecuting = 0;
673 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;
674 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
675 ExchangeInfo->CpuMpData = CpuMpData;
676
677 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();
678
679 //
680 // Get the BSP's data of GDT and IDT
681 //
682 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);
683 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);
684 }
685
686 /**
687 Helper function that waits until the finished AP count reaches the specified
688 limit, or the specified timeout elapses (whichever comes first).
689
690 @param[in] CpuMpData Pointer to CPU MP Data.
691 @param[in] FinishedApLimit The number of finished APs to wait for.
692 @param[in] TimeLimit The number of microseconds to wait for.
693 **/
694 VOID
695 TimedWaitForApFinish (
696 IN CPU_MP_DATA *CpuMpData,
697 IN UINT32 FinishedApLimit,
698 IN UINT32 TimeLimit
699 );
700
701 /**
702 This function will be called by BSP to wakeup AP.
703
704 @param[in] CpuMpData Pointer to CPU MP Data
705 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
706 FALSE: Send IPI to AP by ApicId
707 @param[in] ProcessorNumber The handle number of specified processor
708 @param[in] Procedure The function to be invoked by AP
709 @param[in] ProcedureArgument The argument to be passed into AP function
710 **/
711 VOID
712 WakeUpAP (
713 IN CPU_MP_DATA *CpuMpData,
714 IN BOOLEAN Broadcast,
715 IN UINTN ProcessorNumber,
716 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
717 IN VOID *ProcedureArgument OPTIONAL
718 )
719 {
720 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
721 UINTN Index;
722 CPU_AP_DATA *CpuData;
723 BOOLEAN ResetVectorRequired;
724 CPU_INFO_IN_HOB *CpuInfoInHob;
725
726 CpuMpData->FinishedCount = 0;
727 ResetVectorRequired = FALSE;
728
729 if (CpuMpData->ApLoopMode == ApInHltLoop ||
730 CpuMpData->InitFlag != ApInitDone) {
731 ResetVectorRequired = TRUE;
732 AllocateResetVector (CpuMpData);
733 FillExchangeInfoData (CpuMpData);
734 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
735 //
736 // Get AP target C-state each time when waking up AP,
737 // for it maybe updated by platform again
738 //
739 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);
740 }
741
742 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
743
744 if (Broadcast) {
745 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
746 if (Index != CpuMpData->BspNumber) {
747 CpuData = &CpuMpData->CpuData[Index];
748 CpuData->ApFunction = (UINTN) Procedure;
749 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
750 SetApState (CpuData, CpuStateReady);
751 if (CpuMpData->InitFlag != ApInitConfig) {
752 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
753 }
754 }
755 }
756 if (ResetVectorRequired) {
757 //
758 // Wakeup all APs
759 //
760 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);
761 }
762 if (CpuMpData->InitFlag == ApInitConfig) {
763 //
764 // Wait for all potential APs waken up in one specified period
765 //
766 TimedWaitForApFinish (
767 CpuMpData,
768 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,
769 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)
770 );
771 } else {
772 //
773 // Wait all APs waken up if this is not the 1st broadcast of SIPI
774 //
775 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
776 CpuData = &CpuMpData->CpuData[Index];
777 if (Index != CpuMpData->BspNumber) {
778 WaitApWakeup (CpuData->StartupApSignal);
779 }
780 }
781 }
782 } else {
783 CpuData = &CpuMpData->CpuData[ProcessorNumber];
784 CpuData->ApFunction = (UINTN) Procedure;
785 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
786 SetApState (CpuData, CpuStateReady);
787 //
788 // Wakeup specified AP
789 //
790 ASSERT (CpuMpData->InitFlag != ApInitConfig);
791 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
792 if (ResetVectorRequired) {
793 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
794 SendInitSipiSipi (
795 CpuInfoInHob[ProcessorNumber].ApicId,
796 (UINT32) ExchangeInfo->BufferStart
797 );
798 }
799 //
800 // Wait specified AP waken up
801 //
802 WaitApWakeup (CpuData->StartupApSignal);
803 }
804
805 if (ResetVectorRequired) {
806 FreeResetVector (CpuMpData);
807 }
808 }
809
810 /**
811 Calculate timeout value and return the current performance counter value.
812
813 Calculate the number of performance counter ticks required for a timeout.
814 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
815 as infinity.
816
817 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
818 @param[out] CurrentTime Returns the current value of the performance counter.
819
820 @return Expected time stamp counter for timeout.
821 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
822 as infinity.
823
824 **/
825 UINT64
826 CalculateTimeout (
827 IN UINTN TimeoutInMicroseconds,
828 OUT UINT64 *CurrentTime
829 )
830 {
831 //
832 // Read the current value of the performance counter
833 //
834 *CurrentTime = GetPerformanceCounter ();
835
836 //
837 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
838 // as infinity.
839 //
840 if (TimeoutInMicroseconds == 0) {
841 return 0;
842 }
843
844 //
845 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
846 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
847 // it by 1,000,000, to get the number of ticks for the timeout value.
848 //
849 return DivU64x32 (
850 MultU64x64 (
851 GetPerformanceCounterProperties (NULL, NULL),
852 TimeoutInMicroseconds
853 ),
854 1000000
855 );
856 }
857
858 /**
859 Checks whether timeout expires.
860
861 Check whether the number of elapsed performance counter ticks required for
862 a timeout condition has been reached.
863 If Timeout is zero, which means infinity, return value is always FALSE.
864
865 @param[in, out] PreviousTime On input, the value of the performance counter
866 when it was last read.
867 On output, the current value of the performance
868 counter
869 @param[in] TotalTime The total amount of elapsed time in performance
870 counter ticks.
871 @param[in] Timeout The number of performance counter ticks required
872 to reach a timeout condition.
873
874 @retval TRUE A timeout condition has been reached.
875 @retval FALSE A timeout condition has not been reached.
876
877 **/
878 BOOLEAN
879 CheckTimeout (
880 IN OUT UINT64 *PreviousTime,
881 IN UINT64 *TotalTime,
882 IN UINT64 Timeout
883 )
884 {
885 UINT64 Start;
886 UINT64 End;
887 UINT64 CurrentTime;
888 INT64 Delta;
889 INT64 Cycle;
890
891 if (Timeout == 0) {
892 return FALSE;
893 }
894 GetPerformanceCounterProperties (&Start, &End);
895 Cycle = End - Start;
896 if (Cycle < 0) {
897 Cycle = -Cycle;
898 }
899 Cycle++;
900 CurrentTime = GetPerformanceCounter();
901 Delta = (INT64) (CurrentTime - *PreviousTime);
902 if (Start > End) {
903 Delta = -Delta;
904 }
905 if (Delta < 0) {
906 Delta += Cycle;
907 }
908 *TotalTime += Delta;
909 *PreviousTime = CurrentTime;
910 if (*TotalTime > Timeout) {
911 return TRUE;
912 }
913 return FALSE;
914 }
915
916 /**
917 Helper function that waits until the finished AP count reaches the specified
918 limit, or the specified timeout elapses (whichever comes first).
919
920 @param[in] CpuMpData Pointer to CPU MP Data.
921 @param[in] FinishedApLimit The number of finished APs to wait for.
922 @param[in] TimeLimit The number of microseconds to wait for.
923 **/
924 VOID
925 TimedWaitForApFinish (
926 IN CPU_MP_DATA *CpuMpData,
927 IN UINT32 FinishedApLimit,
928 IN UINT32 TimeLimit
929 )
930 {
931 //
932 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
933 // "infinity", so check for (TimeLimit == 0) explicitly.
934 //
935 if (TimeLimit == 0) {
936 return;
937 }
938
939 CpuMpData->TotalTime = 0;
940 CpuMpData->ExpectedTime = CalculateTimeout (
941 TimeLimit,
942 &CpuMpData->CurrentTime
943 );
944 while (CpuMpData->FinishedCount < FinishedApLimit &&
945 !CheckTimeout (
946 &CpuMpData->CurrentTime,
947 &CpuMpData->TotalTime,
948 CpuMpData->ExpectedTime
949 )) {
950 CpuPause ();
951 }
952
953 if (CpuMpData->FinishedCount >= FinishedApLimit) {
954 DEBUG ((
955 DEBUG_VERBOSE,
956 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
957 __FUNCTION__,
958 FinishedApLimit,
959 DivU64x64Remainder (
960 MultU64x32 (CpuMpData->TotalTime, 1000000),
961 GetPerformanceCounterProperties (NULL, NULL),
962 NULL
963 )
964 ));
965 }
966 }
967
968 /**
969 Reset an AP to Idle state.
970
971 Any task being executed by the AP will be aborted and the AP
972 will be waiting for a new task in Wait-For-SIPI state.
973
974 @param[in] ProcessorNumber The handle number of processor.
975 **/
976 VOID
977 ResetProcessorToIdleState (
978 IN UINTN ProcessorNumber
979 )
980 {
981 CPU_MP_DATA *CpuMpData;
982
983 CpuMpData = GetCpuMpData ();
984
985 CpuMpData->InitFlag = ApInitReconfig;
986 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);
987 while (CpuMpData->FinishedCount < 1) {
988 CpuPause ();
989 }
990 CpuMpData->InitFlag = ApInitDone;
991
992 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
993 }
994
995 /**
996 Searches for the next waiting AP.
997
998 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
999
1000 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1001
1002 @retval EFI_SUCCESS The next waiting AP has been found.
1003 @retval EFI_NOT_FOUND No waiting AP exists.
1004
1005 **/
1006 EFI_STATUS
1007 GetNextWaitingProcessorNumber (
1008 OUT UINTN *NextProcessorNumber
1009 )
1010 {
1011 UINTN ProcessorNumber;
1012 CPU_MP_DATA *CpuMpData;
1013
1014 CpuMpData = GetCpuMpData ();
1015
1016 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1017 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1018 *NextProcessorNumber = ProcessorNumber;
1019 return EFI_SUCCESS;
1020 }
1021 }
1022
1023 return EFI_NOT_FOUND;
1024 }
1025
1026 /** Checks status of specified AP.
1027
1028 This function checks whether the specified AP has finished the task assigned
1029 by StartupThisAP(), and whether timeout expires.
1030
1031 @param[in] ProcessorNumber The handle number of processor.
1032
1033 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1034 @retval EFI_TIMEOUT The timeout expires.
1035 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1036 **/
1037 EFI_STATUS
1038 CheckThisAP (
1039 IN UINTN ProcessorNumber
1040 )
1041 {
1042 CPU_MP_DATA *CpuMpData;
1043 CPU_AP_DATA *CpuData;
1044
1045 CpuMpData = GetCpuMpData ();
1046 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1047
1048 //
1049 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1050 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1051 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1052 //
1053 //
1054 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1055 //
1056 if (GetApState(CpuData) == CpuStateFinished) {
1057 if (CpuData->Finished != NULL) {
1058 *(CpuData->Finished) = TRUE;
1059 }
1060 SetApState (CpuData, CpuStateIdle);
1061 return EFI_SUCCESS;
1062 } else {
1063 //
1064 // If timeout expires for StartupThisAP(), report timeout.
1065 //
1066 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
1067 if (CpuData->Finished != NULL) {
1068 *(CpuData->Finished) = FALSE;
1069 }
1070 //
1071 // Reset failed AP to idle state
1072 //
1073 ResetProcessorToIdleState (ProcessorNumber);
1074
1075 return EFI_TIMEOUT;
1076 }
1077 }
1078 return EFI_NOT_READY;
1079 }
1080
1081 /**
1082 Checks status of all APs.
1083
1084 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1085 and whether timeout expires.
1086
1087 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1088 @retval EFI_TIMEOUT The timeout expires.
1089 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1090 **/
1091 EFI_STATUS
1092 CheckAllAPs (
1093 VOID
1094 )
1095 {
1096 UINTN ProcessorNumber;
1097 UINTN NextProcessorNumber;
1098 UINTN ListIndex;
1099 EFI_STATUS Status;
1100 CPU_MP_DATA *CpuMpData;
1101 CPU_AP_DATA *CpuData;
1102
1103 CpuMpData = GetCpuMpData ();
1104
1105 NextProcessorNumber = 0;
1106
1107 //
1108 // Go through all APs that are responsible for the StartupAllAPs().
1109 //
1110 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1111 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {
1112 continue;
1113 }
1114
1115 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1116 //
1117 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1118 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1119 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1120 //
1121 if (GetApState(CpuData) == CpuStateFinished) {
1122 CpuMpData->RunningCount ++;
1123 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1124 SetApState(CpuData, CpuStateIdle);
1125
1126 //
1127 // If in Single Thread mode, then search for the next waiting AP for execution.
1128 //
1129 if (CpuMpData->SingleThread) {
1130 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
1131
1132 if (!EFI_ERROR (Status)) {
1133 WakeUpAP (
1134 CpuMpData,
1135 FALSE,
1136 (UINT32) NextProcessorNumber,
1137 CpuMpData->Procedure,
1138 CpuMpData->ProcArguments
1139 );
1140 }
1141 }
1142 }
1143 }
1144
1145 //
1146 // If all APs finish, return EFI_SUCCESS.
1147 //
1148 if (CpuMpData->RunningCount == CpuMpData->StartCount) {
1149 return EFI_SUCCESS;
1150 }
1151
1152 //
1153 // If timeout expires, report timeout.
1154 //
1155 if (CheckTimeout (
1156 &CpuMpData->CurrentTime,
1157 &CpuMpData->TotalTime,
1158 CpuMpData->ExpectedTime)
1159 ) {
1160 //
1161 // If FailedCpuList is not NULL, record all failed APs in it.
1162 //
1163 if (CpuMpData->FailedCpuList != NULL) {
1164 *CpuMpData->FailedCpuList =
1165 AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));
1166 ASSERT (*CpuMpData->FailedCpuList != NULL);
1167 }
1168 ListIndex = 0;
1169
1170 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1171 //
1172 // Check whether this processor is responsible for StartupAllAPs().
1173 //
1174 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1175 //
1176 // Reset failed APs to idle state
1177 //
1178 ResetProcessorToIdleState (ProcessorNumber);
1179 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1180 if (CpuMpData->FailedCpuList != NULL) {
1181 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;
1182 }
1183 }
1184 }
1185 if (CpuMpData->FailedCpuList != NULL) {
1186 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
1187 }
1188 return EFI_TIMEOUT;
1189 }
1190 return EFI_NOT_READY;
1191 }
1192
1193 /**
1194 MP Initialize Library initialization.
1195
1196 This service will allocate AP reset vector and wakeup all APs to do APs
1197 initialization.
1198
1199 This service must be invoked before all other MP Initialize Library
1200 service are invoked.
1201
1202 @retval EFI_SUCCESS MP initialization succeeds.
1203 @retval Others MP initialization fails.
1204
1205 **/
1206 EFI_STATUS
1207 EFIAPI
1208 MpInitLibInitialize (
1209 VOID
1210 )
1211 {
1212 CPU_MP_DATA *OldCpuMpData;
1213 CPU_INFO_IN_HOB *CpuInfoInHob;
1214 UINT32 MaxLogicalProcessorNumber;
1215 UINT32 ApStackSize;
1216 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1217 UINTN BufferSize;
1218 UINT32 MonitorFilterSize;
1219 VOID *MpBuffer;
1220 UINTN Buffer;
1221 CPU_MP_DATA *CpuMpData;
1222 UINT8 ApLoopMode;
1223 UINT8 *MonitorBuffer;
1224 UINTN Index;
1225 UINTN ApResetVectorSize;
1226 UINTN BackupBufferAddr;
1227
1228 OldCpuMpData = GetCpuMpDataFromGuidedHob ();
1229 if (OldCpuMpData == NULL) {
1230 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
1231 } else {
1232 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;
1233 }
1234 ASSERT (MaxLogicalProcessorNumber != 0);
1235
1236 AsmGetAddressMap (&AddressMap);
1237 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
1238 ApStackSize = PcdGet32(PcdCpuApStackSize);
1239 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
1240
1241 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
1242 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
1243 BufferSize += sizeof (CPU_MP_DATA);
1244 BufferSize += ApResetVectorSize;
1245 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
1246 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
1247 ASSERT (MpBuffer != NULL);
1248 ZeroMem (MpBuffer, BufferSize);
1249 Buffer = (UINTN) MpBuffer;
1250
1251 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);
1252 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
1253 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);
1254 CpuMpData->Buffer = Buffer;
1255 CpuMpData->CpuApStackSize = ApStackSize;
1256 CpuMpData->BackupBuffer = BackupBufferAddr;
1257 CpuMpData->BackupBufferSize = ApResetVectorSize;
1258 CpuMpData->SaveRestoreFlag = FALSE;
1259 CpuMpData->WakeupBuffer = (UINTN) -1;
1260 CpuMpData->CpuCount = 1;
1261 CpuMpData->BspNumber = 0;
1262 CpuMpData->WaitEvent = NULL;
1263 CpuMpData->SwitchBspFlag = FALSE;
1264 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);
1265 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);
1266 InitializeSpinLock(&CpuMpData->MpLock);
1267 //
1268 // Save BSP's Control registers to APs
1269 //
1270 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);
1271 //
1272 // Set BSP basic information
1273 //
1274 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);
1275 //
1276 // Save assembly code information
1277 //
1278 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
1279 //
1280 // Finally set AP loop mode
1281 //
1282 CpuMpData->ApLoopMode = ApLoopMode;
1283 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
1284 //
1285 // Set up APs wakeup signal buffer
1286 //
1287 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {
1288 CpuMpData->CpuData[Index].StartupApSignal =
1289 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
1290 }
1291 //
1292 // Load Microcode on BSP
1293 //
1294 MicrocodeDetect (CpuMpData);
1295 //
1296 // Store BSP's MTRR setting
1297 //
1298 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
1299
1300 if (OldCpuMpData == NULL) {
1301 if (MaxLogicalProcessorNumber > 1) {
1302 //
1303 // Wakeup all APs and calculate the processor count in system
1304 //
1305 CollectProcessorCount (CpuMpData);
1306 }
1307 } else {
1308 //
1309 // APs have been wakeup before, just get the CPU Information
1310 // from HOB
1311 //
1312 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1313 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1314 CpuMpData->InitFlag = ApInitReconfig;
1315 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1316 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1317 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1318 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);
1319 if (CpuInfoInHob[Index].InitialApicId >= 255) {
1320 CpuMpData->X2ApicEnable = TRUE;
1321 }
1322 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;
1323 CpuMpData->CpuData[Index].ApFunction = 0;
1324 CopyMem (
1325 &CpuMpData->CpuData[Index].VolatileRegisters,
1326 &CpuMpData->CpuData[0].VolatileRegisters,
1327 sizeof (CPU_VOLATILE_REGISTERS)
1328 );
1329 }
1330 if (MaxLogicalProcessorNumber > 1) {
1331 //
1332 // Wakeup APs to do some AP initialize sync
1333 //
1334 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);
1335 //
1336 // Wait for all APs finished initialization
1337 //
1338 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1339 CpuPause ();
1340 }
1341 CpuMpData->InitFlag = ApInitDone;
1342 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1343 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1344 }
1345 }
1346 }
1347
1348 //
1349 // Initialize global data for MP support
1350 //
1351 InitMpGlobalData (CpuMpData);
1352
1353 return EFI_SUCCESS;
1354 }
1355
1356 /**
1357 Gets detailed MP-related information on the requested processor at the
1358 instant this call is made. This service may only be called from the BSP.
1359
1360 @param[in] ProcessorNumber The handle number of processor.
1361 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1362 the requested processor is deposited.
1363 @param[out] HealthData Return processor health data.
1364
1365 @retval EFI_SUCCESS Processor information was returned.
1366 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1367 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1368 @retval EFI_NOT_FOUND The processor with the handle specified by
1369 ProcessorNumber does not exist in the platform.
1370 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1371
1372 **/
1373 EFI_STATUS
1374 EFIAPI
1375 MpInitLibGetProcessorInfo (
1376 IN UINTN ProcessorNumber,
1377 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
1378 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
1379 )
1380 {
1381 CPU_MP_DATA *CpuMpData;
1382 UINTN CallerNumber;
1383 CPU_INFO_IN_HOB *CpuInfoInHob;
1384
1385 CpuMpData = GetCpuMpData ();
1386 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1387
1388 //
1389 // Check whether caller processor is BSP
1390 //
1391 MpInitLibWhoAmI (&CallerNumber);
1392 if (CallerNumber != CpuMpData->BspNumber) {
1393 return EFI_DEVICE_ERROR;
1394 }
1395
1396 if (ProcessorInfoBuffer == NULL) {
1397 return EFI_INVALID_PARAMETER;
1398 }
1399
1400 if (ProcessorNumber >= CpuMpData->CpuCount) {
1401 return EFI_NOT_FOUND;
1402 }
1403
1404 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;
1405 ProcessorInfoBuffer->StatusFlag = 0;
1406 if (ProcessorNumber == CpuMpData->BspNumber) {
1407 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
1408 }
1409 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
1410 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
1411 }
1412 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1413 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
1414 } else {
1415 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
1416 }
1417
1418 //
1419 // Get processor location information
1420 //
1421 GetProcessorLocationByApicId (
1422 CpuInfoInHob[ProcessorNumber].ApicId,
1423 &ProcessorInfoBuffer->Location.Package,
1424 &ProcessorInfoBuffer->Location.Core,
1425 &ProcessorInfoBuffer->Location.Thread
1426 );
1427
1428 if (HealthData != NULL) {
1429 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
1430 }
1431
1432 return EFI_SUCCESS;
1433 }
1434
1435 /**
1436 Worker function to switch the requested AP to be the BSP from that point onward.
1437
1438 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1439 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1440 enabled AP. Otherwise, it will be disabled.
1441
1442 @retval EFI_SUCCESS BSP successfully switched.
1443 @retval others Failed to switch BSP.
1444
1445 **/
1446 EFI_STATUS
1447 SwitchBSPWorker (
1448 IN UINTN ProcessorNumber,
1449 IN BOOLEAN EnableOldBSP
1450 )
1451 {
1452 CPU_MP_DATA *CpuMpData;
1453 UINTN CallerNumber;
1454 CPU_STATE State;
1455 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
1456
1457 CpuMpData = GetCpuMpData ();
1458
1459 //
1460 // Check whether caller processor is BSP
1461 //
1462 MpInitLibWhoAmI (&CallerNumber);
1463 if (CallerNumber != CpuMpData->BspNumber) {
1464 return EFI_SUCCESS;
1465 }
1466
1467 if (ProcessorNumber >= CpuMpData->CpuCount) {
1468 return EFI_NOT_FOUND;
1469 }
1470
1471 //
1472 // Check whether specified AP is disabled
1473 //
1474 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
1475 if (State == CpuStateDisabled) {
1476 return EFI_INVALID_PARAMETER;
1477 }
1478
1479 //
1480 // Check whether ProcessorNumber specifies the current BSP
1481 //
1482 if (ProcessorNumber == CpuMpData->BspNumber) {
1483 return EFI_INVALID_PARAMETER;
1484 }
1485
1486 //
1487 // Check whether specified AP is busy
1488 //
1489 if (State == CpuStateBusy) {
1490 return EFI_NOT_READY;
1491 }
1492
1493 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
1494 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
1495 CpuMpData->SwitchBspFlag = TRUE;
1496 CpuMpData->NewBspNumber = ProcessorNumber;
1497
1498 //
1499 // Clear the BSP bit of MSR_IA32_APIC_BASE
1500 //
1501 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1502 ApicBaseMsr.Bits.BSP = 0;
1503 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1504
1505 //
1506 // Need to wakeUp AP (future BSP).
1507 //
1508 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);
1509
1510 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
1511
1512 //
1513 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1514 //
1515 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1516 ApicBaseMsr.Bits.BSP = 1;
1517 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1518
1519 //
1520 // Wait for old BSP finished AP task
1521 //
1522 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
1523 CpuPause ();
1524 }
1525
1526 CpuMpData->SwitchBspFlag = FALSE;
1527 //
1528 // Set old BSP enable state
1529 //
1530 if (!EnableOldBSP) {
1531 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
1532 }
1533 //
1534 // Save new BSP number
1535 //
1536 CpuMpData->BspNumber = (UINT32) ProcessorNumber;
1537
1538 return EFI_SUCCESS;
1539 }
1540
1541 /**
1542 Worker function to let the caller enable or disable an AP from this point onward.
1543 This service may only be called from the BSP.
1544
1545 @param[in] ProcessorNumber The handle number of AP.
1546 @param[in] EnableAP Specifies the new state for the processor for
1547 enabled, FALSE for disabled.
1548 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1549 the new health status of the AP.
1550
1551 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1552 @retval others Failed to Enable/Disable AP.
1553
1554 **/
1555 EFI_STATUS
1556 EnableDisableApWorker (
1557 IN UINTN ProcessorNumber,
1558 IN BOOLEAN EnableAP,
1559 IN UINT32 *HealthFlag OPTIONAL
1560 )
1561 {
1562 CPU_MP_DATA *CpuMpData;
1563 UINTN CallerNumber;
1564
1565 CpuMpData = GetCpuMpData ();
1566
1567 //
1568 // Check whether caller processor is BSP
1569 //
1570 MpInitLibWhoAmI (&CallerNumber);
1571 if (CallerNumber != CpuMpData->BspNumber) {
1572 return EFI_DEVICE_ERROR;
1573 }
1574
1575 if (ProcessorNumber == CpuMpData->BspNumber) {
1576 return EFI_INVALID_PARAMETER;
1577 }
1578
1579 if (ProcessorNumber >= CpuMpData->CpuCount) {
1580 return EFI_NOT_FOUND;
1581 }
1582
1583 if (!EnableAP) {
1584 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
1585 } else {
1586 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1587 }
1588
1589 if (HealthFlag != NULL) {
1590 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
1591 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
1592 }
1593
1594 return EFI_SUCCESS;
1595 }
1596
1597 /**
1598 This return the handle number for the calling processor. This service may be
1599 called from the BSP and APs.
1600
1601 @param[out] ProcessorNumber Pointer to the handle number of AP.
1602 The range is from 0 to the total number of
1603 logical processors minus 1. The total number of
1604 logical processors can be retrieved by
1605 MpInitLibGetNumberOfProcessors().
1606
1607 @retval EFI_SUCCESS The current processor handle number was returned
1608 in ProcessorNumber.
1609 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1610 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1611
1612 **/
1613 EFI_STATUS
1614 EFIAPI
1615 MpInitLibWhoAmI (
1616 OUT UINTN *ProcessorNumber
1617 )
1618 {
1619 CPU_MP_DATA *CpuMpData;
1620
1621 if (ProcessorNumber == NULL) {
1622 return EFI_INVALID_PARAMETER;
1623 }
1624
1625 CpuMpData = GetCpuMpData ();
1626
1627 return GetProcessorNumber (CpuMpData, ProcessorNumber);
1628 }
1629
1630 /**
1631 Retrieves the number of logical processor in the platform and the number of
1632 those logical processors that are enabled on this boot. This service may only
1633 be called from the BSP.
1634
1635 @param[out] NumberOfProcessors Pointer to the total number of logical
1636 processors in the system, including the BSP
1637 and disabled APs.
1638 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1639 processors that exist in system, including
1640 the BSP.
1641
1642 @retval EFI_SUCCESS The number of logical processors and enabled
1643 logical processors was retrieved.
1644 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1645 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1646 is NULL.
1647 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1648
1649 **/
1650 EFI_STATUS
1651 EFIAPI
1652 MpInitLibGetNumberOfProcessors (
1653 OUT UINTN *NumberOfProcessors, OPTIONAL
1654 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
1655 )
1656 {
1657 CPU_MP_DATA *CpuMpData;
1658 UINTN CallerNumber;
1659 UINTN ProcessorNumber;
1660 UINTN EnabledProcessorNumber;
1661 UINTN Index;
1662
1663 CpuMpData = GetCpuMpData ();
1664
1665 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
1666 return EFI_INVALID_PARAMETER;
1667 }
1668
1669 //
1670 // Check whether caller processor is BSP
1671 //
1672 MpInitLibWhoAmI (&CallerNumber);
1673 if (CallerNumber != CpuMpData->BspNumber) {
1674 return EFI_DEVICE_ERROR;
1675 }
1676
1677 ProcessorNumber = CpuMpData->CpuCount;
1678 EnabledProcessorNumber = 0;
1679 for (Index = 0; Index < ProcessorNumber; Index++) {
1680 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
1681 EnabledProcessorNumber ++;
1682 }
1683 }
1684
1685 if (NumberOfProcessors != NULL) {
1686 *NumberOfProcessors = ProcessorNumber;
1687 }
1688 if (NumberOfEnabledProcessors != NULL) {
1689 *NumberOfEnabledProcessors = EnabledProcessorNumber;
1690 }
1691
1692 return EFI_SUCCESS;
1693 }
1694
1695
1696 /**
1697 Worker function to execute a caller provided function on all enabled APs.
1698
1699 @param[in] Procedure A pointer to the function to be run on
1700 enabled APs of the system.
1701 @param[in] SingleThread If TRUE, then all the enabled APs execute
1702 the function specified by Procedure one by
1703 one, in ascending order of processor handle
1704 number. If FALSE, then all the enabled APs
1705 execute the function specified by Procedure
1706 simultaneously.
1707 @param[in] WaitEvent The event created by the caller with CreateEvent()
1708 service.
1709 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1710 APs to return from Procedure, either for
1711 blocking or non-blocking mode.
1712 @param[in] ProcedureArgument The parameter passed into Procedure for
1713 all APs.
1714 @param[out] FailedCpuList If all APs finish successfully, then its
1715 content is set to NULL. If not all APs
1716 finish before timeout expires, then its
1717 content is set to address of the buffer
1718 holding handle numbers of the failed APs.
1719
1720 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1721 the timeout expired.
1722 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1723 to all enabled APs.
1724 @retval others Failed to Startup all APs.
1725
1726 **/
1727 EFI_STATUS
1728 StartupAllAPsWorker (
1729 IN EFI_AP_PROCEDURE Procedure,
1730 IN BOOLEAN SingleThread,
1731 IN EFI_EVENT WaitEvent OPTIONAL,
1732 IN UINTN TimeoutInMicroseconds,
1733 IN VOID *ProcedureArgument OPTIONAL,
1734 OUT UINTN **FailedCpuList OPTIONAL
1735 )
1736 {
1737 EFI_STATUS Status;
1738 CPU_MP_DATA *CpuMpData;
1739 UINTN ProcessorCount;
1740 UINTN ProcessorNumber;
1741 UINTN CallerNumber;
1742 CPU_AP_DATA *CpuData;
1743 BOOLEAN HasEnabledAp;
1744 CPU_STATE ApState;
1745
1746 CpuMpData = GetCpuMpData ();
1747
1748 if (FailedCpuList != NULL) {
1749 *FailedCpuList = NULL;
1750 }
1751
1752 if (CpuMpData->CpuCount == 1) {
1753 return EFI_NOT_STARTED;
1754 }
1755
1756 if (Procedure == NULL) {
1757 return EFI_INVALID_PARAMETER;
1758 }
1759
1760 //
1761 // Check whether caller processor is BSP
1762 //
1763 MpInitLibWhoAmI (&CallerNumber);
1764 if (CallerNumber != CpuMpData->BspNumber) {
1765 return EFI_DEVICE_ERROR;
1766 }
1767
1768 //
1769 // Update AP state
1770 //
1771 CheckAndUpdateApsStatus ();
1772
1773 ProcessorCount = CpuMpData->CpuCount;
1774 HasEnabledAp = FALSE;
1775 //
1776 // Check whether all enabled APs are idle.
1777 // If any enabled AP is not idle, return EFI_NOT_READY.
1778 //
1779 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1780 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1781 if (ProcessorNumber != CpuMpData->BspNumber) {
1782 ApState = GetApState (CpuData);
1783 if (ApState != CpuStateDisabled) {
1784 HasEnabledAp = TRUE;
1785 if (ApState != CpuStateIdle) {
1786 //
1787 // If any enabled APs are busy, return EFI_NOT_READY.
1788 //
1789 return EFI_NOT_READY;
1790 }
1791 }
1792 }
1793 }
1794
1795 if (!HasEnabledAp) {
1796 //
1797 // If no enabled AP exists, return EFI_NOT_STARTED.
1798 //
1799 return EFI_NOT_STARTED;
1800 }
1801
1802 CpuMpData->StartCount = 0;
1803 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1804 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1805 CpuData->Waiting = FALSE;
1806 if (ProcessorNumber != CpuMpData->BspNumber) {
1807 if (CpuData->State == CpuStateIdle) {
1808 //
1809 // Mark this processor as responsible for current calling.
1810 //
1811 CpuData->Waiting = TRUE;
1812 CpuMpData->StartCount++;
1813 }
1814 }
1815 }
1816
1817 CpuMpData->Procedure = Procedure;
1818 CpuMpData->ProcArguments = ProcedureArgument;
1819 CpuMpData->SingleThread = SingleThread;
1820 CpuMpData->FinishedCount = 0;
1821 CpuMpData->RunningCount = 0;
1822 CpuMpData->FailedCpuList = FailedCpuList;
1823 CpuMpData->ExpectedTime = CalculateTimeout (
1824 TimeoutInMicroseconds,
1825 &CpuMpData->CurrentTime
1826 );
1827 CpuMpData->TotalTime = 0;
1828 CpuMpData->WaitEvent = WaitEvent;
1829
1830 if (!SingleThread) {
1831 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);
1832 } else {
1833 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1834 if (ProcessorNumber == CallerNumber) {
1835 continue;
1836 }
1837 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1838 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
1839 break;
1840 }
1841 }
1842 }
1843
1844 Status = EFI_SUCCESS;
1845 if (WaitEvent == NULL) {
1846 do {
1847 Status = CheckAllAPs ();
1848 } while (Status == EFI_NOT_READY);
1849 }
1850
1851 return Status;
1852 }
1853
1854 /**
1855 Worker function to let the caller get one enabled AP to execute a caller-provided
1856 function.
1857
1858 @param[in] Procedure A pointer to the function to be run on
1859 enabled APs of the system.
1860 @param[in] ProcessorNumber The handle number of the AP.
1861 @param[in] WaitEvent The event created by the caller with CreateEvent()
1862 service.
1863 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1864 APs to return from Procedure, either for
1865 blocking or non-blocking mode.
1866 @param[in] ProcedureArgument The parameter passed into Procedure for
1867 all APs.
1868 @param[out] Finished If AP returns from Procedure before the
1869 timeout expires, its content is set to TRUE.
1870 Otherwise, the value is set to FALSE.
1871
1872 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1873 the timeout expires.
1874 @retval others Failed to Startup AP.
1875
1876 **/
1877 EFI_STATUS
1878 StartupThisAPWorker (
1879 IN EFI_AP_PROCEDURE Procedure,
1880 IN UINTN ProcessorNumber,
1881 IN EFI_EVENT WaitEvent OPTIONAL,
1882 IN UINTN TimeoutInMicroseconds,
1883 IN VOID *ProcedureArgument OPTIONAL,
1884 OUT BOOLEAN *Finished OPTIONAL
1885 )
1886 {
1887 EFI_STATUS Status;
1888 CPU_MP_DATA *CpuMpData;
1889 CPU_AP_DATA *CpuData;
1890 UINTN CallerNumber;
1891
1892 CpuMpData = GetCpuMpData ();
1893
1894 if (Finished != NULL) {
1895 *Finished = FALSE;
1896 }
1897
1898 //
1899 // Check whether caller processor is BSP
1900 //
1901 MpInitLibWhoAmI (&CallerNumber);
1902 if (CallerNumber != CpuMpData->BspNumber) {
1903 return EFI_DEVICE_ERROR;
1904 }
1905
1906 //
1907 // Check whether processor with the handle specified by ProcessorNumber exists
1908 //
1909 if (ProcessorNumber >= CpuMpData->CpuCount) {
1910 return EFI_NOT_FOUND;
1911 }
1912
1913 //
1914 // Check whether specified processor is BSP
1915 //
1916 if (ProcessorNumber == CpuMpData->BspNumber) {
1917 return EFI_INVALID_PARAMETER;
1918 }
1919
1920 //
1921 // Check parameter Procedure
1922 //
1923 if (Procedure == NULL) {
1924 return EFI_INVALID_PARAMETER;
1925 }
1926
1927 //
1928 // Update AP state
1929 //
1930 CheckAndUpdateApsStatus ();
1931
1932 //
1933 // Check whether specified AP is disabled
1934 //
1935 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1936 return EFI_INVALID_PARAMETER;
1937 }
1938
1939 //
1940 // If WaitEvent is not NULL, execute in non-blocking mode.
1941 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1942 // CheckAPsStatus() will check completion and timeout periodically.
1943 //
1944 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1945 CpuData->WaitEvent = WaitEvent;
1946 CpuData->Finished = Finished;
1947 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
1948 CpuData->TotalTime = 0;
1949
1950 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
1951
1952 //
1953 // If WaitEvent is NULL, execute in blocking mode.
1954 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1955 //
1956 Status = EFI_SUCCESS;
1957 if (WaitEvent == NULL) {
1958 do {
1959 Status = CheckThisAP (ProcessorNumber);
1960 } while (Status == EFI_NOT_READY);
1961 }
1962
1963 return Status;
1964 }
1965
1966 /**
1967 Get pointer to CPU MP Data structure from GUIDed HOB.
1968
1969 @return The pointer to CPU MP Data structure.
1970 **/
1971 CPU_MP_DATA *
1972 GetCpuMpDataFromGuidedHob (
1973 VOID
1974 )
1975 {
1976 EFI_HOB_GUID_TYPE *GuidHob;
1977 VOID *DataInHob;
1978 CPU_MP_DATA *CpuMpData;
1979
1980 CpuMpData = NULL;
1981 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
1982 if (GuidHob != NULL) {
1983 DataInHob = GET_GUID_HOB_DATA (GuidHob);
1984 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);
1985 }
1986 return CpuMpData;
1987 }
1988
1989 /**
1990 Get available system memory below 1MB by specified size.
1991
1992 @param[in] CpuMpData The pointer to CPU MP Data structure.
1993 **/
1994 VOID
1995 BackupAndPrepareWakeupBuffer(
1996 IN CPU_MP_DATA *CpuMpData
1997 )
1998 {
1999 CopyMem (
2000 (VOID *) CpuMpData->BackupBuffer,
2001 (VOID *) CpuMpData->WakeupBuffer,
2002 CpuMpData->BackupBufferSize
2003 );
2004 CopyMem (
2005 (VOID *) CpuMpData->WakeupBuffer,
2006 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
2007 CpuMpData->AddressMap.RendezvousFunnelSize
2008 );
2009 }
2010
2011 /**
2012 Restore wakeup buffer data.
2013
2014 @param[in] CpuMpData The pointer to CPU MP Data structure.
2015 **/
2016 VOID
2017 RestoreWakeupBuffer(
2018 IN CPU_MP_DATA *CpuMpData
2019 )
2020 {
2021 CopyMem (
2022 (VOID *) CpuMpData->WakeupBuffer,
2023 (VOID *) CpuMpData->BackupBuffer,
2024 CpuMpData->BackupBufferSize
2025 );
2026 }