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