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