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