]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg: Move ProgramVirtualWireMode() to MpInitLib
[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 // Load microcode on AP
386 //
387 MicrocodeDetect (CpuMpData);
388 //
389 // Sync BSP's MTRR table to AP
390 //
391 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);
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 // Enable the local APIC for Virtual Wire Mode.
1369 //
1370 ProgramVirtualWireMode ();
1371
1372 if (OldCpuMpData == NULL) {
1373 if (MaxLogicalProcessorNumber > 1) {
1374 //
1375 // Wakeup all APs and calculate the processor count in system
1376 //
1377 CollectProcessorCount (CpuMpData);
1378 }
1379 } else {
1380 //
1381 // APs have been wakeup before, just get the CPU Information
1382 // from HOB
1383 //
1384 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1385 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1386 CpuMpData->InitFlag = ApInitReconfig;
1387 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1388 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1389 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1390 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);
1391 if (CpuInfoInHob[Index].InitialApicId >= 255) {
1392 CpuMpData->X2ApicEnable = TRUE;
1393 }
1394 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;
1395 CpuMpData->CpuData[Index].ApFunction = 0;
1396 CopyMem (
1397 &CpuMpData->CpuData[Index].VolatileRegisters,
1398 &CpuMpData->CpuData[0].VolatileRegisters,
1399 sizeof (CPU_VOLATILE_REGISTERS)
1400 );
1401 }
1402 if (MaxLogicalProcessorNumber > 1) {
1403 //
1404 // Wakeup APs to do some AP initialize sync
1405 //
1406 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);
1407 //
1408 // Wait for all APs finished initialization
1409 //
1410 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1411 CpuPause ();
1412 }
1413 CpuMpData->InitFlag = ApInitDone;
1414 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1415 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1416 }
1417 }
1418 }
1419
1420 //
1421 // Initialize global data for MP support
1422 //
1423 InitMpGlobalData (CpuMpData);
1424
1425 return EFI_SUCCESS;
1426 }
1427
1428 /**
1429 Gets detailed MP-related information on the requested processor at the
1430 instant this call is made. This service may only be called from the BSP.
1431
1432 @param[in] ProcessorNumber The handle number of processor.
1433 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1434 the requested processor is deposited.
1435 @param[out] HealthData Return processor health data.
1436
1437 @retval EFI_SUCCESS Processor information was returned.
1438 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1439 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1440 @retval EFI_NOT_FOUND The processor with the handle specified by
1441 ProcessorNumber does not exist in the platform.
1442 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1443
1444 **/
1445 EFI_STATUS
1446 EFIAPI
1447 MpInitLibGetProcessorInfo (
1448 IN UINTN ProcessorNumber,
1449 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
1450 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
1451 )
1452 {
1453 CPU_MP_DATA *CpuMpData;
1454 UINTN CallerNumber;
1455 CPU_INFO_IN_HOB *CpuInfoInHob;
1456
1457 CpuMpData = GetCpuMpData ();
1458 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1459
1460 //
1461 // Check whether caller processor is BSP
1462 //
1463 MpInitLibWhoAmI (&CallerNumber);
1464 if (CallerNumber != CpuMpData->BspNumber) {
1465 return EFI_DEVICE_ERROR;
1466 }
1467
1468 if (ProcessorInfoBuffer == NULL) {
1469 return EFI_INVALID_PARAMETER;
1470 }
1471
1472 if (ProcessorNumber >= CpuMpData->CpuCount) {
1473 return EFI_NOT_FOUND;
1474 }
1475
1476 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;
1477 ProcessorInfoBuffer->StatusFlag = 0;
1478 if (ProcessorNumber == CpuMpData->BspNumber) {
1479 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
1480 }
1481 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
1482 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
1483 }
1484 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1485 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
1486 } else {
1487 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
1488 }
1489
1490 //
1491 // Get processor location information
1492 //
1493 GetProcessorLocationByApicId (
1494 CpuInfoInHob[ProcessorNumber].ApicId,
1495 &ProcessorInfoBuffer->Location.Package,
1496 &ProcessorInfoBuffer->Location.Core,
1497 &ProcessorInfoBuffer->Location.Thread
1498 );
1499
1500 if (HealthData != NULL) {
1501 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
1502 }
1503
1504 return EFI_SUCCESS;
1505 }
1506
1507 /**
1508 Worker function to switch the requested AP to be the BSP from that point onward.
1509
1510 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1511 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1512 enabled AP. Otherwise, it will be disabled.
1513
1514 @retval EFI_SUCCESS BSP successfully switched.
1515 @retval others Failed to switch BSP.
1516
1517 **/
1518 EFI_STATUS
1519 SwitchBSPWorker (
1520 IN UINTN ProcessorNumber,
1521 IN BOOLEAN EnableOldBSP
1522 )
1523 {
1524 CPU_MP_DATA *CpuMpData;
1525 UINTN CallerNumber;
1526 CPU_STATE State;
1527 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
1528 BOOLEAN OldInterruptState;
1529 BOOLEAN OldTimerInterruptState;
1530
1531 //
1532 // Save and Disable Local APIC timer interrupt
1533 //
1534 OldTimerInterruptState = GetApicTimerInterruptState ();
1535 DisableApicTimerInterrupt ();
1536 //
1537 // Before send both BSP and AP to a procedure to exchange their roles,
1538 // interrupt must be disabled. This is because during the exchange role
1539 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1540 // be corrupted, since interrupt return address will be pushed to stack
1541 // by hardware.
1542 //
1543 OldInterruptState = SaveAndDisableInterrupts ();
1544
1545 //
1546 // Mask LINT0 & LINT1 for the old BSP
1547 //
1548 DisableLvtInterrupts ();
1549
1550 CpuMpData = GetCpuMpData ();
1551
1552 //
1553 // Check whether caller processor is BSP
1554 //
1555 MpInitLibWhoAmI (&CallerNumber);
1556 if (CallerNumber != CpuMpData->BspNumber) {
1557 return EFI_SUCCESS;
1558 }
1559
1560 if (ProcessorNumber >= CpuMpData->CpuCount) {
1561 return EFI_NOT_FOUND;
1562 }
1563
1564 //
1565 // Check whether specified AP is disabled
1566 //
1567 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
1568 if (State == CpuStateDisabled) {
1569 return EFI_INVALID_PARAMETER;
1570 }
1571
1572 //
1573 // Check whether ProcessorNumber specifies the current BSP
1574 //
1575 if (ProcessorNumber == CpuMpData->BspNumber) {
1576 return EFI_INVALID_PARAMETER;
1577 }
1578
1579 //
1580 // Check whether specified AP is busy
1581 //
1582 if (State == CpuStateBusy) {
1583 return EFI_NOT_READY;
1584 }
1585
1586 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
1587 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
1588 CpuMpData->SwitchBspFlag = TRUE;
1589 CpuMpData->NewBspNumber = ProcessorNumber;
1590
1591 //
1592 // Clear the BSP bit of MSR_IA32_APIC_BASE
1593 //
1594 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1595 ApicBaseMsr.Bits.BSP = 0;
1596 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1597
1598 //
1599 // Need to wakeUp AP (future BSP).
1600 //
1601 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);
1602
1603 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
1604
1605 //
1606 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1607 //
1608 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1609 ApicBaseMsr.Bits.BSP = 1;
1610 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1611
1612 //
1613 // Wait for old BSP finished AP task
1614 //
1615 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
1616 CpuPause ();
1617 }
1618
1619 CpuMpData->SwitchBspFlag = FALSE;
1620 //
1621 // Set old BSP enable state
1622 //
1623 if (!EnableOldBSP) {
1624 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
1625 } else {
1626 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
1627 }
1628 //
1629 // Save new BSP number
1630 //
1631 CpuMpData->BspNumber = (UINT32) ProcessorNumber;
1632
1633 //
1634 // Restore interrupt state.
1635 //
1636 SetInterruptState (OldInterruptState);
1637
1638 if (OldTimerInterruptState) {
1639 EnableApicTimerInterrupt ();
1640 }
1641
1642 return EFI_SUCCESS;
1643 }
1644
1645 /**
1646 Worker function to let the caller enable or disable an AP from this point onward.
1647 This service may only be called from the BSP.
1648
1649 @param[in] ProcessorNumber The handle number of AP.
1650 @param[in] EnableAP Specifies the new state for the processor for
1651 enabled, FALSE for disabled.
1652 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1653 the new health status of the AP.
1654
1655 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1656 @retval others Failed to Enable/Disable AP.
1657
1658 **/
1659 EFI_STATUS
1660 EnableDisableApWorker (
1661 IN UINTN ProcessorNumber,
1662 IN BOOLEAN EnableAP,
1663 IN UINT32 *HealthFlag OPTIONAL
1664 )
1665 {
1666 CPU_MP_DATA *CpuMpData;
1667 UINTN CallerNumber;
1668
1669 CpuMpData = GetCpuMpData ();
1670
1671 //
1672 // Check whether caller processor is BSP
1673 //
1674 MpInitLibWhoAmI (&CallerNumber);
1675 if (CallerNumber != CpuMpData->BspNumber) {
1676 return EFI_DEVICE_ERROR;
1677 }
1678
1679 if (ProcessorNumber == CpuMpData->BspNumber) {
1680 return EFI_INVALID_PARAMETER;
1681 }
1682
1683 if (ProcessorNumber >= CpuMpData->CpuCount) {
1684 return EFI_NOT_FOUND;
1685 }
1686
1687 if (!EnableAP) {
1688 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
1689 } else {
1690 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1691 }
1692
1693 if (HealthFlag != NULL) {
1694 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
1695 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
1696 }
1697
1698 return EFI_SUCCESS;
1699 }
1700
1701 /**
1702 This return the handle number for the calling processor. This service may be
1703 called from the BSP and APs.
1704
1705 @param[out] ProcessorNumber Pointer to the handle number of AP.
1706 The range is from 0 to the total number of
1707 logical processors minus 1. The total number of
1708 logical processors can be retrieved by
1709 MpInitLibGetNumberOfProcessors().
1710
1711 @retval EFI_SUCCESS The current processor handle number was returned
1712 in ProcessorNumber.
1713 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1714 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1715
1716 **/
1717 EFI_STATUS
1718 EFIAPI
1719 MpInitLibWhoAmI (
1720 OUT UINTN *ProcessorNumber
1721 )
1722 {
1723 CPU_MP_DATA *CpuMpData;
1724
1725 if (ProcessorNumber == NULL) {
1726 return EFI_INVALID_PARAMETER;
1727 }
1728
1729 CpuMpData = GetCpuMpData ();
1730
1731 return GetProcessorNumber (CpuMpData, ProcessorNumber);
1732 }
1733
1734 /**
1735 Retrieves the number of logical processor in the platform and the number of
1736 those logical processors that are enabled on this boot. This service may only
1737 be called from the BSP.
1738
1739 @param[out] NumberOfProcessors Pointer to the total number of logical
1740 processors in the system, including the BSP
1741 and disabled APs.
1742 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1743 processors that exist in system, including
1744 the BSP.
1745
1746 @retval EFI_SUCCESS The number of logical processors and enabled
1747 logical processors was retrieved.
1748 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1749 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1750 is NULL.
1751 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1752
1753 **/
1754 EFI_STATUS
1755 EFIAPI
1756 MpInitLibGetNumberOfProcessors (
1757 OUT UINTN *NumberOfProcessors, OPTIONAL
1758 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
1759 )
1760 {
1761 CPU_MP_DATA *CpuMpData;
1762 UINTN CallerNumber;
1763 UINTN ProcessorNumber;
1764 UINTN EnabledProcessorNumber;
1765 UINTN Index;
1766
1767 CpuMpData = GetCpuMpData ();
1768
1769 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
1770 return EFI_INVALID_PARAMETER;
1771 }
1772
1773 //
1774 // Check whether caller processor is BSP
1775 //
1776 MpInitLibWhoAmI (&CallerNumber);
1777 if (CallerNumber != CpuMpData->BspNumber) {
1778 return EFI_DEVICE_ERROR;
1779 }
1780
1781 ProcessorNumber = CpuMpData->CpuCount;
1782 EnabledProcessorNumber = 0;
1783 for (Index = 0; Index < ProcessorNumber; Index++) {
1784 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
1785 EnabledProcessorNumber ++;
1786 }
1787 }
1788
1789 if (NumberOfProcessors != NULL) {
1790 *NumberOfProcessors = ProcessorNumber;
1791 }
1792 if (NumberOfEnabledProcessors != NULL) {
1793 *NumberOfEnabledProcessors = EnabledProcessorNumber;
1794 }
1795
1796 return EFI_SUCCESS;
1797 }
1798
1799
1800 /**
1801 Worker function to execute a caller provided function on all enabled APs.
1802
1803 @param[in] Procedure A pointer to the function to be run on
1804 enabled APs of the system.
1805 @param[in] SingleThread If TRUE, then all the enabled APs execute
1806 the function specified by Procedure one by
1807 one, in ascending order of processor handle
1808 number. If FALSE, then all the enabled APs
1809 execute the function specified by Procedure
1810 simultaneously.
1811 @param[in] WaitEvent The event created by the caller with CreateEvent()
1812 service.
1813 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1814 APs to return from Procedure, either for
1815 blocking or non-blocking mode.
1816 @param[in] ProcedureArgument The parameter passed into Procedure for
1817 all APs.
1818 @param[out] FailedCpuList If all APs finish successfully, then its
1819 content is set to NULL. If not all APs
1820 finish before timeout expires, then its
1821 content is set to address of the buffer
1822 holding handle numbers of the failed APs.
1823
1824 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1825 the timeout expired.
1826 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1827 to all enabled APs.
1828 @retval others Failed to Startup all APs.
1829
1830 **/
1831 EFI_STATUS
1832 StartupAllAPsWorker (
1833 IN EFI_AP_PROCEDURE Procedure,
1834 IN BOOLEAN SingleThread,
1835 IN EFI_EVENT WaitEvent OPTIONAL,
1836 IN UINTN TimeoutInMicroseconds,
1837 IN VOID *ProcedureArgument OPTIONAL,
1838 OUT UINTN **FailedCpuList OPTIONAL
1839 )
1840 {
1841 EFI_STATUS Status;
1842 CPU_MP_DATA *CpuMpData;
1843 UINTN ProcessorCount;
1844 UINTN ProcessorNumber;
1845 UINTN CallerNumber;
1846 CPU_AP_DATA *CpuData;
1847 BOOLEAN HasEnabledAp;
1848 CPU_STATE ApState;
1849
1850 CpuMpData = GetCpuMpData ();
1851
1852 if (FailedCpuList != NULL) {
1853 *FailedCpuList = NULL;
1854 }
1855
1856 if (CpuMpData->CpuCount == 1) {
1857 return EFI_NOT_STARTED;
1858 }
1859
1860 if (Procedure == NULL) {
1861 return EFI_INVALID_PARAMETER;
1862 }
1863
1864 //
1865 // Check whether caller processor is BSP
1866 //
1867 MpInitLibWhoAmI (&CallerNumber);
1868 if (CallerNumber != CpuMpData->BspNumber) {
1869 return EFI_DEVICE_ERROR;
1870 }
1871
1872 //
1873 // Update AP state
1874 //
1875 CheckAndUpdateApsStatus ();
1876
1877 ProcessorCount = CpuMpData->CpuCount;
1878 HasEnabledAp = FALSE;
1879 //
1880 // Check whether all enabled APs are idle.
1881 // If any enabled AP is not idle, return EFI_NOT_READY.
1882 //
1883 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1884 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1885 if (ProcessorNumber != CpuMpData->BspNumber) {
1886 ApState = GetApState (CpuData);
1887 if (ApState != CpuStateDisabled) {
1888 HasEnabledAp = TRUE;
1889 if (ApState != CpuStateIdle) {
1890 //
1891 // If any enabled APs are busy, return EFI_NOT_READY.
1892 //
1893 return EFI_NOT_READY;
1894 }
1895 }
1896 }
1897 }
1898
1899 if (!HasEnabledAp) {
1900 //
1901 // If no enabled AP exists, return EFI_NOT_STARTED.
1902 //
1903 return EFI_NOT_STARTED;
1904 }
1905
1906 CpuMpData->StartCount = 0;
1907 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1908 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1909 CpuData->Waiting = FALSE;
1910 if (ProcessorNumber != CpuMpData->BspNumber) {
1911 if (CpuData->State == CpuStateIdle) {
1912 //
1913 // Mark this processor as responsible for current calling.
1914 //
1915 CpuData->Waiting = TRUE;
1916 CpuMpData->StartCount++;
1917 }
1918 }
1919 }
1920
1921 CpuMpData->Procedure = Procedure;
1922 CpuMpData->ProcArguments = ProcedureArgument;
1923 CpuMpData->SingleThread = SingleThread;
1924 CpuMpData->FinishedCount = 0;
1925 CpuMpData->RunningCount = 0;
1926 CpuMpData->FailedCpuList = FailedCpuList;
1927 CpuMpData->ExpectedTime = CalculateTimeout (
1928 TimeoutInMicroseconds,
1929 &CpuMpData->CurrentTime
1930 );
1931 CpuMpData->TotalTime = 0;
1932 CpuMpData->WaitEvent = WaitEvent;
1933
1934 if (!SingleThread) {
1935 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);
1936 } else {
1937 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
1938 if (ProcessorNumber == CallerNumber) {
1939 continue;
1940 }
1941 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1942 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
1943 break;
1944 }
1945 }
1946 }
1947
1948 Status = EFI_SUCCESS;
1949 if (WaitEvent == NULL) {
1950 do {
1951 Status = CheckAllAPs ();
1952 } while (Status == EFI_NOT_READY);
1953 }
1954
1955 return Status;
1956 }
1957
1958 /**
1959 Worker function to let the caller get one enabled AP to execute a caller-provided
1960 function.
1961
1962 @param[in] Procedure A pointer to the function to be run on
1963 enabled APs of the system.
1964 @param[in] ProcessorNumber The handle number of the AP.
1965 @param[in] WaitEvent The event created by the caller with CreateEvent()
1966 service.
1967 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1968 APs to return from Procedure, either for
1969 blocking or non-blocking mode.
1970 @param[in] ProcedureArgument The parameter passed into Procedure for
1971 all APs.
1972 @param[out] Finished If AP returns from Procedure before the
1973 timeout expires, its content is set to TRUE.
1974 Otherwise, the value is set to FALSE.
1975
1976 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1977 the timeout expires.
1978 @retval others Failed to Startup AP.
1979
1980 **/
1981 EFI_STATUS
1982 StartupThisAPWorker (
1983 IN EFI_AP_PROCEDURE Procedure,
1984 IN UINTN ProcessorNumber,
1985 IN EFI_EVENT WaitEvent OPTIONAL,
1986 IN UINTN TimeoutInMicroseconds,
1987 IN VOID *ProcedureArgument OPTIONAL,
1988 OUT BOOLEAN *Finished OPTIONAL
1989 )
1990 {
1991 EFI_STATUS Status;
1992 CPU_MP_DATA *CpuMpData;
1993 CPU_AP_DATA *CpuData;
1994 UINTN CallerNumber;
1995
1996 CpuMpData = GetCpuMpData ();
1997
1998 if (Finished != NULL) {
1999 *Finished = FALSE;
2000 }
2001
2002 //
2003 // Check whether caller processor is BSP
2004 //
2005 MpInitLibWhoAmI (&CallerNumber);
2006 if (CallerNumber != CpuMpData->BspNumber) {
2007 return EFI_DEVICE_ERROR;
2008 }
2009
2010 //
2011 // Check whether processor with the handle specified by ProcessorNumber exists
2012 //
2013 if (ProcessorNumber >= CpuMpData->CpuCount) {
2014 return EFI_NOT_FOUND;
2015 }
2016
2017 //
2018 // Check whether specified processor is BSP
2019 //
2020 if (ProcessorNumber == CpuMpData->BspNumber) {
2021 return EFI_INVALID_PARAMETER;
2022 }
2023
2024 //
2025 // Check parameter Procedure
2026 //
2027 if (Procedure == NULL) {
2028 return EFI_INVALID_PARAMETER;
2029 }
2030
2031 //
2032 // Update AP state
2033 //
2034 CheckAndUpdateApsStatus ();
2035
2036 //
2037 // Check whether specified AP is disabled
2038 //
2039 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2040 return EFI_INVALID_PARAMETER;
2041 }
2042
2043 //
2044 // If WaitEvent is not NULL, execute in non-blocking mode.
2045 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2046 // CheckAPsStatus() will check completion and timeout periodically.
2047 //
2048 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2049 CpuData->WaitEvent = WaitEvent;
2050 CpuData->Finished = Finished;
2051 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
2052 CpuData->TotalTime = 0;
2053
2054 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
2055
2056 //
2057 // If WaitEvent is NULL, execute in blocking mode.
2058 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2059 //
2060 Status = EFI_SUCCESS;
2061 if (WaitEvent == NULL) {
2062 do {
2063 Status = CheckThisAP (ProcessorNumber);
2064 } while (Status == EFI_NOT_READY);
2065 }
2066
2067 return Status;
2068 }
2069
2070 /**
2071 Get pointer to CPU MP Data structure from GUIDed HOB.
2072
2073 @return The pointer to CPU MP Data structure.
2074 **/
2075 CPU_MP_DATA *
2076 GetCpuMpDataFromGuidedHob (
2077 VOID
2078 )
2079 {
2080 EFI_HOB_GUID_TYPE *GuidHob;
2081 VOID *DataInHob;
2082 CPU_MP_DATA *CpuMpData;
2083
2084 CpuMpData = NULL;
2085 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
2086 if (GuidHob != NULL) {
2087 DataInHob = GET_GUID_HOB_DATA (GuidHob);
2088 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);
2089 }
2090 return CpuMpData;
2091 }
2092
2093 /**
2094 Get available system memory below 1MB by specified size.
2095
2096 @param[in] CpuMpData The pointer to CPU MP Data structure.
2097 **/
2098 VOID
2099 BackupAndPrepareWakeupBuffer(
2100 IN CPU_MP_DATA *CpuMpData
2101 )
2102 {
2103 CopyMem (
2104 (VOID *) CpuMpData->BackupBuffer,
2105 (VOID *) CpuMpData->WakeupBuffer,
2106 CpuMpData->BackupBufferSize
2107 );
2108 CopyMem (
2109 (VOID *) CpuMpData->WakeupBuffer,
2110 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
2111 CpuMpData->AddressMap.RendezvousFunnelSize
2112 );
2113 }
2114
2115 /**
2116 Restore wakeup buffer data.
2117
2118 @param[in] CpuMpData The pointer to CPU MP Data structure.
2119 **/
2120 VOID
2121 RestoreWakeupBuffer(
2122 IN CPU_MP_DATA *CpuMpData
2123 )
2124 {
2125 CopyMem (
2126 (VOID *) CpuMpData->WakeupBuffer,
2127 (VOID *) CpuMpData->BackupBuffer,
2128 CpuMpData->BackupBufferSize
2129 );
2130 }