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