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