]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.c
34555c069331c4e2e4a2897db43a0b358ac05909
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
1 /** @file
2 CPU MP Initialize Library common functions.
3
4 Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "MpLib.h"
12 #include <Library/VmgExitLib.h>
13 #include <Register/Amd/Fam17Msr.h>
14 #include <Register/Amd/Ghcb.h>
15
16 EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
17
18 /**
19 The function will check if BSP Execute Disable is enabled.
20
21 DxeIpl may have enabled Execute Disable for BSP, APs need to
22 get the status and sync up the settings.
23 If BSP's CR0.Paging is not set, BSP execute Disble feature is
24 not working actually.
25
26 @retval TRUE BSP Execute Disable is enabled.
27 @retval FALSE BSP Execute Disable is not enabled.
28 **/
29 BOOLEAN
30 IsBspExecuteDisableEnabled (
31 VOID
32 )
33 {
34 UINT32 Eax;
35 CPUID_EXTENDED_CPU_SIG_EDX Edx;
36 MSR_IA32_EFER_REGISTER EferMsr;
37 BOOLEAN Enabled;
38 IA32_CR0 Cr0;
39
40 Enabled = FALSE;
41 Cr0.UintN = AsmReadCr0 ();
42 if (Cr0.Bits.PG != 0) {
43 //
44 // If CR0 Paging bit is set
45 //
46 AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);
47 if (Eax >= CPUID_EXTENDED_CPU_SIG) {
48 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &Edx.Uint32);
49 //
50 // CPUID 0x80000001
51 // Bit 20: Execute Disable Bit available.
52 //
53 if (Edx.Bits.NX != 0) {
54 EferMsr.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);
55 //
56 // MSR 0xC0000080
57 // Bit 11: Execute Disable Bit enable.
58 //
59 if (EferMsr.Bits.NXE != 0) {
60 Enabled = TRUE;
61 }
62 }
63 }
64 }
65
66 return Enabled;
67 }
68
69 /**
70 Worker function for SwitchBSP().
71
72 Worker function for SwitchBSP(), assigned to the AP which is intended
73 to become BSP.
74
75 @param[in] Buffer Pointer to CPU MP Data
76 **/
77 VOID
78 EFIAPI
79 FutureBSPProc (
80 IN VOID *Buffer
81 )
82 {
83 CPU_MP_DATA *DataInHob;
84
85 DataInHob = (CPU_MP_DATA *)Buffer;
86 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);
87 }
88
89 /**
90 Get the Application Processors state.
91
92 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
93
94 @return The AP status
95 **/
96 CPU_STATE
97 GetApState (
98 IN CPU_AP_DATA *CpuData
99 )
100 {
101 return CpuData->State;
102 }
103
104 /**
105 Set the Application Processors state.
106
107 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
108 @param[in] State The AP status
109 **/
110 VOID
111 SetApState (
112 IN CPU_AP_DATA *CpuData,
113 IN CPU_STATE State
114 )
115 {
116 AcquireSpinLock (&CpuData->ApLock);
117 CpuData->State = State;
118 ReleaseSpinLock (&CpuData->ApLock);
119 }
120
121 /**
122 Save BSP's local APIC timer setting.
123
124 @param[in] CpuMpData Pointer to CPU MP Data
125 **/
126 VOID
127 SaveLocalApicTimerSetting (
128 IN CPU_MP_DATA *CpuMpData
129 )
130 {
131 //
132 // Record the current local APIC timer setting of BSP
133 //
134 GetApicTimerState (
135 &CpuMpData->DivideValue,
136 &CpuMpData->PeriodicMode,
137 &CpuMpData->Vector
138 );
139 CpuMpData->CurrentTimerCount = GetApicTimerCurrentCount ();
140 CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();
141 }
142
143 /**
144 Sync local APIC timer setting from BSP to AP.
145
146 @param[in] CpuMpData Pointer to CPU MP Data
147 **/
148 VOID
149 SyncLocalApicTimerSetting (
150 IN CPU_MP_DATA *CpuMpData
151 )
152 {
153 //
154 // Sync local APIC timer setting from BSP to AP
155 //
156 InitializeApicTimer (
157 CpuMpData->DivideValue,
158 CpuMpData->CurrentTimerCount,
159 CpuMpData->PeriodicMode,
160 CpuMpData->Vector
161 );
162 //
163 // Disable AP's local APIC timer interrupt
164 //
165 DisableApicTimerInterrupt ();
166 }
167
168 /**
169 Save the volatile registers required to be restored following INIT IPI.
170
171 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
172 **/
173 VOID
174 SaveVolatileRegisters (
175 OUT CPU_VOLATILE_REGISTERS *VolatileRegisters
176 )
177 {
178 CPUID_VERSION_INFO_EDX VersionInfoEdx;
179
180 VolatileRegisters->Cr0 = AsmReadCr0 ();
181 VolatileRegisters->Cr3 = AsmReadCr3 ();
182 VolatileRegisters->Cr4 = AsmReadCr4 ();
183
184 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
185 if (VersionInfoEdx.Bits.DE != 0) {
186 //
187 // If processor supports Debugging Extensions feature
188 // by CPUID.[EAX=01H]:EDX.BIT2
189 //
190 VolatileRegisters->Dr0 = AsmReadDr0 ();
191 VolatileRegisters->Dr1 = AsmReadDr1 ();
192 VolatileRegisters->Dr2 = AsmReadDr2 ();
193 VolatileRegisters->Dr3 = AsmReadDr3 ();
194 VolatileRegisters->Dr6 = AsmReadDr6 ();
195 VolatileRegisters->Dr7 = AsmReadDr7 ();
196 }
197
198 AsmReadGdtr (&VolatileRegisters->Gdtr);
199 AsmReadIdtr (&VolatileRegisters->Idtr);
200 VolatileRegisters->Tr = AsmReadTr ();
201 }
202
203 /**
204 Restore the volatile registers following INIT IPI.
205
206 @param[in] VolatileRegisters Pointer to volatile resisters
207 @param[in] IsRestoreDr TRUE: Restore DRx if supported
208 FALSE: Do not restore DRx
209 **/
210 VOID
211 RestoreVolatileRegisters (
212 IN CPU_VOLATILE_REGISTERS *VolatileRegisters,
213 IN BOOLEAN IsRestoreDr
214 )
215 {
216 CPUID_VERSION_INFO_EDX VersionInfoEdx;
217 IA32_TSS_DESCRIPTOR *Tss;
218
219 AsmWriteCr3 (VolatileRegisters->Cr3);
220 AsmWriteCr4 (VolatileRegisters->Cr4);
221 AsmWriteCr0 (VolatileRegisters->Cr0);
222
223 if (IsRestoreDr) {
224 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
225 if (VersionInfoEdx.Bits.DE != 0) {
226 //
227 // If processor supports Debugging Extensions feature
228 // by CPUID.[EAX=01H]:EDX.BIT2
229 //
230 AsmWriteDr0 (VolatileRegisters->Dr0);
231 AsmWriteDr1 (VolatileRegisters->Dr1);
232 AsmWriteDr2 (VolatileRegisters->Dr2);
233 AsmWriteDr3 (VolatileRegisters->Dr3);
234 AsmWriteDr6 (VolatileRegisters->Dr6);
235 AsmWriteDr7 (VolatileRegisters->Dr7);
236 }
237 }
238
239 AsmWriteGdtr (&VolatileRegisters->Gdtr);
240 AsmWriteIdtr (&VolatileRegisters->Idtr);
241 if ((VolatileRegisters->Tr != 0) &&
242 (VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit))
243 {
244 Tss = (IA32_TSS_DESCRIPTOR *)(VolatileRegisters->Gdtr.Base +
245 VolatileRegisters->Tr);
246 if (Tss->Bits.P == 1) {
247 Tss->Bits.Type &= 0xD; // 1101 - Clear busy bit just in case
248 AsmWriteTr (VolatileRegisters->Tr);
249 }
250 }
251 }
252
253 /**
254 Detect whether Mwait-monitor feature is supported.
255
256 @retval TRUE Mwait-monitor feature is supported.
257 @retval FALSE Mwait-monitor feature is not supported.
258 **/
259 BOOLEAN
260 IsMwaitSupport (
261 VOID
262 )
263 {
264 CPUID_VERSION_INFO_ECX VersionInfoEcx;
265
266 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);
267 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;
268 }
269
270 /**
271 Get AP loop mode.
272
273 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
274
275 @return The AP loop mode.
276 **/
277 UINT8
278 GetApLoopMode (
279 OUT UINT32 *MonitorFilterSize
280 )
281 {
282 UINT8 ApLoopMode;
283 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;
284
285 ASSERT (MonitorFilterSize != NULL);
286
287 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
288 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);
289 if (ApLoopMode == ApInMwaitLoop) {
290 if (!IsMwaitSupport ()) {
291 //
292 // If processor does not support MONITOR/MWAIT feature,
293 // force AP in Hlt-loop mode
294 //
295 ApLoopMode = ApInHltLoop;
296 }
297
298 if (PcdGetBool (PcdSevEsIsEnabled)) {
299 //
300 // For SEV-ES, force AP in Hlt-loop mode in order to use the GHCB
301 // protocol for starting APs
302 //
303 ApLoopMode = ApInHltLoop;
304 }
305 }
306
307 if (ApLoopMode != ApInMwaitLoop) {
308 *MonitorFilterSize = sizeof (UINT32);
309 } else {
310 //
311 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
312 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
313 //
314 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);
315 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;
316 }
317
318 return ApLoopMode;
319 }
320
321 /**
322 Sort the APIC ID of all processors.
323
324 This function sorts the APIC ID of all processors so that processor number is
325 assigned in the ascending order of APIC ID which eases MP debugging.
326
327 @param[in] CpuMpData Pointer to PEI CPU MP Data
328 **/
329 VOID
330 SortApicId (
331 IN CPU_MP_DATA *CpuMpData
332 )
333 {
334 UINTN Index1;
335 UINTN Index2;
336 UINTN Index3;
337 UINT32 ApicId;
338 CPU_INFO_IN_HOB CpuInfo;
339 UINT32 ApCount;
340 CPU_INFO_IN_HOB *CpuInfoInHob;
341 volatile UINT32 *StartupApSignal;
342
343 ApCount = CpuMpData->CpuCount - 1;
344 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
345 if (ApCount != 0) {
346 for (Index1 = 0; Index1 < ApCount; Index1++) {
347 Index3 = Index1;
348 //
349 // Sort key is the hardware default APIC ID
350 //
351 ApicId = CpuInfoInHob[Index1].ApicId;
352 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {
353 if (ApicId > CpuInfoInHob[Index2].ApicId) {
354 Index3 = Index2;
355 ApicId = CpuInfoInHob[Index2].ApicId;
356 }
357 }
358
359 if (Index3 != Index1) {
360 CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));
361 CopyMem (
362 &CpuInfoInHob[Index3],
363 &CpuInfoInHob[Index1],
364 sizeof (CPU_INFO_IN_HOB)
365 );
366 CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));
367
368 //
369 // Also exchange the StartupApSignal.
370 //
371 StartupApSignal = CpuMpData->CpuData[Index3].StartupApSignal;
372 CpuMpData->CpuData[Index3].StartupApSignal =
373 CpuMpData->CpuData[Index1].StartupApSignal;
374 CpuMpData->CpuData[Index1].StartupApSignal = StartupApSignal;
375 }
376 }
377
378 //
379 // Get the processor number for the BSP
380 //
381 ApicId = GetInitialApicId ();
382 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {
383 if (CpuInfoInHob[Index1].ApicId == ApicId) {
384 CpuMpData->BspNumber = (UINT32)Index1;
385 break;
386 }
387 }
388 }
389 }
390
391 /**
392 Enable x2APIC mode on APs.
393
394 @param[in, out] Buffer Pointer to private data buffer.
395 **/
396 VOID
397 EFIAPI
398 ApFuncEnableX2Apic (
399 IN OUT VOID *Buffer
400 )
401 {
402 SetApicMode (LOCAL_APIC_MODE_X2APIC);
403 }
404
405 /**
406 Do sync on APs.
407
408 @param[in, out] Buffer Pointer to private data buffer.
409 **/
410 VOID
411 EFIAPI
412 ApInitializeSync (
413 IN OUT VOID *Buffer
414 )
415 {
416 CPU_MP_DATA *CpuMpData;
417 UINTN ProcessorNumber;
418 EFI_STATUS Status;
419
420 CpuMpData = (CPU_MP_DATA *)Buffer;
421 Status = GetProcessorNumber (CpuMpData, &ProcessorNumber);
422 ASSERT_EFI_ERROR (Status);
423 //
424 // Load microcode on AP
425 //
426 MicrocodeDetect (CpuMpData, ProcessorNumber);
427 //
428 // Sync BSP's MTRR table to AP
429 //
430 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);
431 }
432
433 /**
434 Find the current Processor number by APIC ID.
435
436 @param[in] CpuMpData Pointer to PEI CPU MP Data
437 @param[out] ProcessorNumber Return the pocessor number found
438
439 @retval EFI_SUCCESS ProcessorNumber is found and returned.
440 @retval EFI_NOT_FOUND ProcessorNumber is not found.
441 **/
442 EFI_STATUS
443 GetProcessorNumber (
444 IN CPU_MP_DATA *CpuMpData,
445 OUT UINTN *ProcessorNumber
446 )
447 {
448 UINTN TotalProcessorNumber;
449 UINTN Index;
450 CPU_INFO_IN_HOB *CpuInfoInHob;
451 UINT32 CurrentApicId;
452
453 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
454
455 TotalProcessorNumber = CpuMpData->CpuCount;
456 CurrentApicId = GetApicId ();
457 for (Index = 0; Index < TotalProcessorNumber; Index++) {
458 if (CpuInfoInHob[Index].ApicId == CurrentApicId) {
459 *ProcessorNumber = Index;
460 return EFI_SUCCESS;
461 }
462 }
463
464 return EFI_NOT_FOUND;
465 }
466
467 /**
468 This function will get CPU count in the system.
469
470 @param[in] CpuMpData Pointer to PEI CPU MP Data
471
472 @return CPU count detected
473 **/
474 UINTN
475 CollectProcessorCount (
476 IN CPU_MP_DATA *CpuMpData
477 )
478 {
479 UINTN Index;
480 CPU_INFO_IN_HOB *CpuInfoInHob;
481 BOOLEAN X2Apic;
482
483 //
484 // Send 1st broadcast IPI to APs to wakeup APs
485 //
486 CpuMpData->InitFlag = ApInitConfig;
487 WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);
488 CpuMpData->InitFlag = ApInitDone;
489 //
490 // When InitFlag == ApInitConfig, WakeUpAP () guarantees all APs are checked in.
491 // FinishedCount is the number of check-in APs.
492 //
493 CpuMpData->CpuCount = CpuMpData->FinishedCount + 1;
494 ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
495
496 //
497 // Enable x2APIC mode if
498 // 1. Number of CPU is greater than 255; or
499 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
500 //
501 X2Apic = FALSE;
502 if (CpuMpData->CpuCount > 255) {
503 //
504 // If there are more than 255 processor found, force to enable X2APIC
505 //
506 X2Apic = TRUE;
507 } else {
508 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
509 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
510 if (CpuInfoInHob[Index].InitialApicId >= 0xFF) {
511 X2Apic = TRUE;
512 break;
513 }
514 }
515 }
516
517 if (X2Apic) {
518 DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));
519 //
520 // Wakeup all APs to enable x2APIC mode
521 //
522 WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL, TRUE);
523 //
524 // Wait for all known APs finished
525 //
526 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
527 CpuPause ();
528 }
529
530 //
531 // Enable x2APIC on BSP
532 //
533 SetApicMode (LOCAL_APIC_MODE_X2APIC);
534 //
535 // Set BSP/Aps state to IDLE
536 //
537 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
538 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
539 }
540 }
541
542 DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));
543 //
544 // Sort BSP/Aps by CPU APIC ID in ascending order
545 //
546 SortApicId (CpuMpData);
547
548 DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));
549
550 return CpuMpData->CpuCount;
551 }
552
553 /**
554 Initialize CPU AP Data when AP is wakeup at the first time.
555
556 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
557 @param[in] ProcessorNumber The handle number of processor
558 @param[in] BistData Processor BIST data
559 @param[in] ApTopOfStack Top of AP stack
560
561 **/
562 VOID
563 InitializeApData (
564 IN OUT CPU_MP_DATA *CpuMpData,
565 IN UINTN ProcessorNumber,
566 IN UINT32 BistData,
567 IN UINT64 ApTopOfStack
568 )
569 {
570 CPU_INFO_IN_HOB *CpuInfoInHob;
571 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr;
572
573 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
574 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
575 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
576 CpuInfoInHob[ProcessorNumber].Health = BistData;
577 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;
578
579 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
580 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;
581
582 //
583 // NOTE: PlatformId is not relevant on AMD platforms.
584 //
585 if (!StandardSignatureIsAuthenticAMD ()) {
586 PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID);
587 CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8)PlatformIdMsr.Bits.PlatformId;
588 }
589
590 AsmCpuid (
591 CPUID_VERSION_INFO,
592 &CpuMpData->CpuData[ProcessorNumber].ProcessorSignature,
593 NULL,
594 NULL,
595 NULL
596 );
597
598 InitializeSpinLock (&CpuMpData->CpuData[ProcessorNumber].ApLock);
599 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
600 }
601
602 /**
603 This function will be called from AP reset code if BSP uses WakeUpAP.
604
605 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
606 @param[in] ApIndex Number of current executing AP
607 **/
608 VOID
609 EFIAPI
610 ApWakeupFunction (
611 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,
612 IN UINTN ApIndex
613 )
614 {
615 CPU_MP_DATA *CpuMpData;
616 UINTN ProcessorNumber;
617 EFI_AP_PROCEDURE Procedure;
618 VOID *Parameter;
619 UINT32 BistData;
620 volatile UINT32 *ApStartupSignalBuffer;
621 CPU_INFO_IN_HOB *CpuInfoInHob;
622 UINT64 ApTopOfStack;
623 UINTN CurrentApicMode;
624
625 //
626 // AP finished assembly code and begin to execute C code
627 //
628 CpuMpData = ExchangeInfo->CpuMpData;
629
630 //
631 // AP's local APIC settings will be lost after received INIT IPI
632 // We need to re-initialize them at here
633 //
634 ProgramVirtualWireMode ();
635 //
636 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
637 //
638 DisableLvtInterrupts ();
639 SyncLocalApicTimerSetting (CpuMpData);
640
641 CurrentApicMode = GetApicMode ();
642 while (TRUE) {
643 if (CpuMpData->InitFlag == ApInitConfig) {
644 ProcessorNumber = ApIndex;
645 //
646 // This is first time AP wakeup, get BIST information from AP stack
647 //
648 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;
649 BistData = *(UINT32 *)((UINTN)ApTopOfStack - sizeof (UINTN));
650 //
651 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
652 // to initialize AP in InitConfig path.
653 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
654 //
655 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
656 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);
657 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
658 } else {
659 //
660 // Execute AP function if AP is ready
661 //
662 GetProcessorNumber (CpuMpData, &ProcessorNumber);
663 //
664 // Clear AP start-up signal when AP waken up
665 //
666 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
667 InterlockedCompareExchange32 (
668 (UINT32 *)ApStartupSignalBuffer,
669 WAKEUP_AP_SIGNAL,
670 0
671 );
672
673 if (CpuMpData->InitFlag == ApInitReconfig) {
674 //
675 // ApInitReconfig happens when:
676 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
677 // 2. AP is initialized in DXE phase.
678 // In either case, use the volatile registers value derived from BSP.
679 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
680 // different IDT shared by all APs.
681 //
682 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
683 } else {
684 if (CpuMpData->ApLoopMode == ApInHltLoop) {
685 //
686 // Restore AP's volatile registers saved before AP is halted
687 //
688 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);
689 } else {
690 //
691 // The CPU driver might not flush TLB for APs on spot after updating
692 // page attributes. AP in mwait loop mode needs to take care of it when
693 // woken up.
694 //
695 CpuFlushTlb ();
696 }
697 }
698
699 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {
700 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;
701 Parameter = (VOID *)CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;
702 if (Procedure != NULL) {
703 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);
704 //
705 // Enable source debugging on AP function
706 //
707 EnableDebugAgent ();
708 //
709 // Invoke AP function here
710 //
711 Procedure (Parameter);
712 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
713 if (CpuMpData->SwitchBspFlag) {
714 //
715 // Re-get the processor number due to BSP/AP maybe exchange in AP function
716 //
717 GetProcessorNumber (CpuMpData, &ProcessorNumber);
718 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;
719 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;
720 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
721 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;
722 } else {
723 if ((CpuInfoInHob[ProcessorNumber].ApicId != GetApicId ()) ||
724 (CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ()))
725 {
726 if (CurrentApicMode != GetApicMode ()) {
727 //
728 // If APIC mode change happened during AP function execution,
729 // we do not support APIC ID value changed.
730 //
731 ASSERT (FALSE);
732 CpuDeadLoop ();
733 } else {
734 //
735 // Re-get the CPU APICID and Initial APICID if they are changed
736 //
737 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
738 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
739 }
740 }
741 }
742 }
743
744 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);
745 }
746 }
747
748 if (CpuMpData->ApLoopMode == ApInHltLoop) {
749 //
750 // Save AP volatile registers
751 //
752 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);
753 }
754
755 //
756 // AP finished executing C code
757 //
758 InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCount);
759
760 if (CpuMpData->InitFlag == ApInitConfig) {
761 //
762 // Delay decrementing the APs executing count when SEV-ES is enabled
763 // to allow the APs to issue an AP_RESET_HOLD before the BSP possibly
764 // performs another INIT-SIPI-SIPI sequence.
765 //
766 if (!CpuMpData->SevEsIsEnabled) {
767 InterlockedDecrement ((UINT32 *)&CpuMpData->MpCpuExchangeInfo->NumApsExecuting);
768 }
769 }
770
771 //
772 // Place AP is specified loop mode
773 //
774 if (CpuMpData->ApLoopMode == ApInHltLoop) {
775 //
776 // Place AP in HLT-loop
777 //
778 while (TRUE) {
779 DisableInterrupts ();
780 if (CpuMpData->SevEsIsEnabled) {
781 SevEsPlaceApHlt (CpuMpData);
782 } else {
783 CpuSleep ();
784 }
785
786 CpuPause ();
787 }
788 }
789
790 while (TRUE) {
791 DisableInterrupts ();
792 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
793 //
794 // Place AP in MWAIT-loop
795 //
796 AsmMonitor ((UINTN)ApStartupSignalBuffer, 0, 0);
797 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {
798 //
799 // Check AP start-up signal again.
800 // If AP start-up signal is not set, place AP into
801 // the specified C-state
802 //
803 AsmMwait (CpuMpData->ApTargetCState << 4, 0);
804 }
805 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {
806 //
807 // Place AP in Run-loop
808 //
809 CpuPause ();
810 } else {
811 ASSERT (FALSE);
812 }
813
814 //
815 // If AP start-up signal is written, AP is waken up
816 // otherwise place AP in loop again
817 //
818 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {
819 break;
820 }
821 }
822 }
823 }
824
825 /**
826 Wait for AP wakeup and write AP start-up signal till AP is waken up.
827
828 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
829 **/
830 VOID
831 WaitApWakeup (
832 IN volatile UINT32 *ApStartupSignalBuffer
833 )
834 {
835 //
836 // If AP is waken up, StartupApSignal should be cleared.
837 // Otherwise, write StartupApSignal again till AP waken up.
838 //
839 while (InterlockedCompareExchange32 (
840 (UINT32 *)ApStartupSignalBuffer,
841 WAKEUP_AP_SIGNAL,
842 WAKEUP_AP_SIGNAL
843 ) != 0)
844 {
845 CpuPause ();
846 }
847 }
848
849 /**
850 This function will fill the exchange info structure.
851
852 @param[in] CpuMpData Pointer to CPU MP Data
853
854 **/
855 VOID
856 FillExchangeInfoData (
857 IN CPU_MP_DATA *CpuMpData
858 )
859 {
860 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
861 UINTN Size;
862 IA32_SEGMENT_DESCRIPTOR *Selector;
863 IA32_CR4 Cr4;
864
865 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
866 ExchangeInfo->StackStart = CpuMpData->Buffer;
867 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;
868 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;
869 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;
870
871 ExchangeInfo->CodeSegment = AsmReadCs ();
872 ExchangeInfo->DataSegment = AsmReadDs ();
873
874 ExchangeInfo->Cr3 = AsmReadCr3 ();
875
876 ExchangeInfo->CFunction = (UINTN)ApWakeupFunction;
877 ExchangeInfo->ApIndex = 0;
878 ExchangeInfo->NumApsExecuting = 0;
879 ExchangeInfo->InitFlag = (UINTN)CpuMpData->InitFlag;
880 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
881 ExchangeInfo->CpuMpData = CpuMpData;
882
883 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();
884
885 ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;
886
887 //
888 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
889 // to determin whether 5-Level Paging is enabled.
890 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
891 // current system setting.
892 // Using latter way is simpler because it also eliminates the needs to
893 // check whether platform wants to enable it.
894 //
895 Cr4.UintN = AsmReadCr4 ();
896 ExchangeInfo->Enable5LevelPaging = (BOOLEAN)(Cr4.Bits.LA57 == 1);
897 DEBUG ((DEBUG_INFO, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName, ExchangeInfo->Enable5LevelPaging));
898
899 ExchangeInfo->SevEsIsEnabled = CpuMpData->SevEsIsEnabled;
900 ExchangeInfo->GhcbBase = (UINTN)CpuMpData->GhcbBase;
901
902 //
903 // Get the BSP's data of GDT and IDT
904 //
905 AsmReadGdtr ((IA32_DESCRIPTOR *)&ExchangeInfo->GdtrProfile);
906 AsmReadIdtr ((IA32_DESCRIPTOR *)&ExchangeInfo->IdtrProfile);
907
908 //
909 // Find a 32-bit code segment
910 //
911 Selector = (IA32_SEGMENT_DESCRIPTOR *)ExchangeInfo->GdtrProfile.Base;
912 Size = ExchangeInfo->GdtrProfile.Limit + 1;
913 while (Size > 0) {
914 if ((Selector->Bits.L == 0) && (Selector->Bits.Type >= 8)) {
915 ExchangeInfo->ModeTransitionSegment =
916 (UINT16)((UINTN)Selector - ExchangeInfo->GdtrProfile.Base);
917 break;
918 }
919
920 Selector += 1;
921 Size -= sizeof (IA32_SEGMENT_DESCRIPTOR);
922 }
923
924 //
925 // Copy all 32-bit code and 64-bit code into memory with type of
926 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
927 //
928 if (CpuMpData->WakeupBufferHigh != 0) {
929 Size = CpuMpData->AddressMap.RendezvousFunnelSize +
930 CpuMpData->AddressMap.SwitchToRealSize -
931 CpuMpData->AddressMap.ModeTransitionOffset;
932 CopyMem (
933 (VOID *)CpuMpData->WakeupBufferHigh,
934 CpuMpData->AddressMap.RendezvousFunnelAddress +
935 CpuMpData->AddressMap.ModeTransitionOffset,
936 Size
937 );
938
939 ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh;
940 } else {
941 ExchangeInfo->ModeTransitionMemory = (UINT32)
942 (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset);
943 }
944
945 ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory +
946 (UINT32)ExchangeInfo->ModeOffset -
947 (UINT32)CpuMpData->AddressMap.ModeTransitionOffset;
948 ExchangeInfo->ModeHighSegment = (UINT16)ExchangeInfo->CodeSegment;
949 }
950
951 /**
952 Helper function that waits until the finished AP count reaches the specified
953 limit, or the specified timeout elapses (whichever comes first).
954
955 @param[in] CpuMpData Pointer to CPU MP Data.
956 @param[in] FinishedApLimit The number of finished APs to wait for.
957 @param[in] TimeLimit The number of microseconds to wait for.
958 **/
959 VOID
960 TimedWaitForApFinish (
961 IN CPU_MP_DATA *CpuMpData,
962 IN UINT32 FinishedApLimit,
963 IN UINT32 TimeLimit
964 );
965
966 /**
967 Get available system memory below 1MB by specified size.
968
969 @param[in] CpuMpData The pointer to CPU MP Data structure.
970 **/
971 VOID
972 BackupAndPrepareWakeupBuffer (
973 IN CPU_MP_DATA *CpuMpData
974 )
975 {
976 CopyMem (
977 (VOID *)CpuMpData->BackupBuffer,
978 (VOID *)CpuMpData->WakeupBuffer,
979 CpuMpData->BackupBufferSize
980 );
981 CopyMem (
982 (VOID *)CpuMpData->WakeupBuffer,
983 (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress,
984 CpuMpData->AddressMap.RendezvousFunnelSize +
985 CpuMpData->AddressMap.SwitchToRealSize
986 );
987 }
988
989 /**
990 Restore wakeup buffer data.
991
992 @param[in] CpuMpData The pointer to CPU MP Data structure.
993 **/
994 VOID
995 RestoreWakeupBuffer (
996 IN CPU_MP_DATA *CpuMpData
997 )
998 {
999 CopyMem (
1000 (VOID *)CpuMpData->WakeupBuffer,
1001 (VOID *)CpuMpData->BackupBuffer,
1002 CpuMpData->BackupBufferSize
1003 );
1004 }
1005
1006 /**
1007 Calculate the size of the reset vector.
1008
1009 @param[in] AddressMap The pointer to Address Map structure.
1010
1011 @return Total amount of memory required for the AP reset area
1012 **/
1013 STATIC
1014 UINTN
1015 GetApResetVectorSize (
1016 IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap
1017 )
1018 {
1019 UINTN Size;
1020
1021 Size = AddressMap->RendezvousFunnelSize +
1022 AddressMap->SwitchToRealSize +
1023 sizeof (MP_CPU_EXCHANGE_INFO);
1024
1025 return Size;
1026 }
1027
1028 /**
1029 Allocate reset vector buffer.
1030
1031 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1032 **/
1033 VOID
1034 AllocateResetVector (
1035 IN OUT CPU_MP_DATA *CpuMpData
1036 )
1037 {
1038 UINTN ApResetVectorSize;
1039 UINTN ApResetStackSize;
1040
1041 if (CpuMpData->WakeupBuffer == (UINTN)-1) {
1042 ApResetVectorSize = GetApResetVectorSize (&CpuMpData->AddressMap);
1043
1044 CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize);
1045 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN)
1046 (CpuMpData->WakeupBuffer +
1047 CpuMpData->AddressMap.RendezvousFunnelSize +
1048 CpuMpData->AddressMap.SwitchToRealSize);
1049 CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer (
1050 CpuMpData->AddressMap.RendezvousFunnelSize +
1051 CpuMpData->AddressMap.SwitchToRealSize -
1052 CpuMpData->AddressMap.ModeTransitionOffset
1053 );
1054 //
1055 // The AP reset stack is only used by SEV-ES guests. Do not allocate it
1056 // if SEV-ES is not enabled.
1057 //
1058 if (PcdGetBool (PcdSevEsIsEnabled)) {
1059 //
1060 // Stack location is based on ProcessorNumber, so use the total number
1061 // of processors for calculating the total stack area.
1062 //
1063 ApResetStackSize = (AP_RESET_STACK_SIZE *
1064 PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
1065
1066 //
1067 // Invoke GetWakeupBuffer a second time to allocate the stack area
1068 // below 1MB. The returned buffer will be page aligned and sized and
1069 // below the previously allocated buffer.
1070 //
1071 CpuMpData->SevEsAPResetStackStart = GetWakeupBuffer (ApResetStackSize);
1072
1073 //
1074 // Check to be sure that the "allocate below" behavior hasn't changed.
1075 // This will also catch a failed allocation, as "-1" is returned on
1076 // failure.
1077 //
1078 if (CpuMpData->SevEsAPResetStackStart >= CpuMpData->WakeupBuffer) {
1079 DEBUG ((
1080 DEBUG_ERROR,
1081 "SEV-ES AP reset stack is not below wakeup buffer\n"
1082 ));
1083
1084 ASSERT (FALSE);
1085 CpuDeadLoop ();
1086 }
1087 }
1088 }
1089
1090 BackupAndPrepareWakeupBuffer (CpuMpData);
1091 }
1092
1093 /**
1094 Free AP reset vector buffer.
1095
1096 @param[in] CpuMpData The pointer to CPU MP Data structure.
1097 **/
1098 VOID
1099 FreeResetVector (
1100 IN CPU_MP_DATA *CpuMpData
1101 )
1102 {
1103 //
1104 // If SEV-ES is enabled, the reset area is needed for AP parking and
1105 // and AP startup in the OS, so the reset area is reserved. Do not
1106 // perform the restore as this will overwrite memory which has data
1107 // needed by SEV-ES.
1108 //
1109 if (!CpuMpData->SevEsIsEnabled) {
1110 RestoreWakeupBuffer (CpuMpData);
1111 }
1112 }
1113
1114 /**
1115 This function will be called by BSP to wakeup AP.
1116
1117 @param[in] CpuMpData Pointer to CPU MP Data
1118 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1119 FALSE: Send IPI to AP by ApicId
1120 @param[in] ProcessorNumber The handle number of specified processor
1121 @param[in] Procedure The function to be invoked by AP
1122 @param[in] ProcedureArgument The argument to be passed into AP function
1123 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1124 **/
1125 VOID
1126 WakeUpAP (
1127 IN CPU_MP_DATA *CpuMpData,
1128 IN BOOLEAN Broadcast,
1129 IN UINTN ProcessorNumber,
1130 IN EFI_AP_PROCEDURE Procedure OPTIONAL,
1131 IN VOID *ProcedureArgument OPTIONAL,
1132 IN BOOLEAN WakeUpDisabledAps
1133 )
1134 {
1135 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
1136 UINTN Index;
1137 CPU_AP_DATA *CpuData;
1138 BOOLEAN ResetVectorRequired;
1139 CPU_INFO_IN_HOB *CpuInfoInHob;
1140
1141 CpuMpData->FinishedCount = 0;
1142 ResetVectorRequired = FALSE;
1143
1144 if (CpuMpData->WakeUpByInitSipiSipi ||
1145 (CpuMpData->InitFlag != ApInitDone))
1146 {
1147 ResetVectorRequired = TRUE;
1148 AllocateResetVector (CpuMpData);
1149 AllocateSevEsAPMemory (CpuMpData);
1150 FillExchangeInfoData (CpuMpData);
1151 SaveLocalApicTimerSetting (CpuMpData);
1152 }
1153
1154 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
1155 //
1156 // Get AP target C-state each time when waking up AP,
1157 // for it maybe updated by platform again
1158 //
1159 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);
1160 }
1161
1162 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
1163
1164 if (Broadcast) {
1165 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1166 if (Index != CpuMpData->BspNumber) {
1167 CpuData = &CpuMpData->CpuData[Index];
1168 //
1169 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1170 // the AP procedure will be skipped for disabled AP because AP state
1171 // is not CpuStateReady.
1172 //
1173 if ((GetApState (CpuData) == CpuStateDisabled) && !WakeUpDisabledAps) {
1174 continue;
1175 }
1176
1177 CpuData->ApFunction = (UINTN)Procedure;
1178 CpuData->ApFunctionArgument = (UINTN)ProcedureArgument;
1179 SetApState (CpuData, CpuStateReady);
1180 if (CpuMpData->InitFlag != ApInitConfig) {
1181 *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
1182 }
1183 }
1184 }
1185
1186 if (ResetVectorRequired) {
1187 //
1188 // For SEV-ES, the initial AP boot address will be defined by
1189 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1190 // from the original INIT-SIPI-SIPI.
1191 //
1192 if (CpuMpData->SevEsIsEnabled) {
1193 SetSevEsJumpTable (ExchangeInfo->BufferStart);
1194 }
1195
1196 //
1197 // Wakeup all APs
1198 //
1199 SendInitSipiSipiAllExcludingSelf ((UINT32)ExchangeInfo->BufferStart);
1200 }
1201
1202 if (CpuMpData->InitFlag == ApInitConfig) {
1203 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) {
1204 //
1205 // The AP enumeration algorithm below is suitable only when the
1206 // platform can tell us the *exact* boot CPU count in advance.
1207 //
1208 // The wait below finishes only when the detected AP count reaches
1209 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1210 // takes. If at least one AP fails to check in (meaning a platform
1211 // hardware bug), the detection hangs forever, by design. If the actual
1212 // boot CPU count in the system is higher than
1213 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1214 // misconfiguration), then some APs may complete initialization after
1215 // the wait finishes, and cause undefined behavior.
1216 //
1217 TimedWaitForApFinish (
1218 CpuMpData,
1219 PcdGet32 (PcdCpuBootLogicalProcessorNumber) - 1,
1220 MAX_UINT32 // approx. 71 minutes
1221 );
1222 } else {
1223 //
1224 // The AP enumeration algorithm below is suitable for two use cases.
1225 //
1226 // (1) The check-in time for an individual AP is bounded, and APs run
1227 // through their initialization routines strongly concurrently. In
1228 // particular, the number of concurrently running APs
1229 // ("NumApsExecuting") is never expected to fall to zero
1230 // *temporarily* -- it is expected to fall to zero only when all
1231 // APs have checked-in.
1232 //
1233 // In this case, the platform is supposed to set
1234 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1235 // enough for one AP to start initialization). The timeout will be
1236 // reached soon, and remaining APs are collected by watching
1237 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1238 // mid-process, while some APs have not completed initialization,
1239 // the behavior is undefined.
1240 //
1241 // (2) The check-in time for an individual AP is unbounded, and/or APs
1242 // may complete their initializations widely spread out. In
1243 // particular, some APs may finish initialization before some APs
1244 // even start.
1245 //
1246 // In this case, the platform is supposed to set
1247 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1248 // enumeration will always take that long (except when the boot CPU
1249 // count happens to be maximal, that is,
1250 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1251 // check-in before the timeout, and NumApsExecuting is assumed zero
1252 // at timeout. APs that miss the time-out may cause undefined
1253 // behavior.
1254 //
1255 TimedWaitForApFinish (
1256 CpuMpData,
1257 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,
1258 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)
1259 );
1260
1261 while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {
1262 CpuPause ();
1263 }
1264 }
1265 } else {
1266 //
1267 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1268 //
1269 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1270 CpuData = &CpuMpData->CpuData[Index];
1271 if (Index != CpuMpData->BspNumber) {
1272 WaitApWakeup (CpuData->StartupApSignal);
1273 }
1274 }
1275 }
1276 } else {
1277 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1278 CpuData->ApFunction = (UINTN)Procedure;
1279 CpuData->ApFunctionArgument = (UINTN)ProcedureArgument;
1280 SetApState (CpuData, CpuStateReady);
1281 //
1282 // Wakeup specified AP
1283 //
1284 ASSERT (CpuMpData->InitFlag != ApInitConfig);
1285 *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
1286 if (ResetVectorRequired) {
1287 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
1288
1289 //
1290 // For SEV-ES, the initial AP boot address will be defined by
1291 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1292 // from the original INIT-SIPI-SIPI.
1293 //
1294 if (CpuMpData->SevEsIsEnabled) {
1295 SetSevEsJumpTable (ExchangeInfo->BufferStart);
1296 }
1297
1298 SendInitSipiSipi (
1299 CpuInfoInHob[ProcessorNumber].ApicId,
1300 (UINT32)ExchangeInfo->BufferStart
1301 );
1302 }
1303
1304 //
1305 // Wait specified AP waken up
1306 //
1307 WaitApWakeup (CpuData->StartupApSignal);
1308 }
1309
1310 if (ResetVectorRequired) {
1311 FreeResetVector (CpuMpData);
1312 }
1313
1314 //
1315 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1316 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1317 // S3SmmInitDone Ppi.
1318 //
1319 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);
1320 }
1321
1322 /**
1323 Calculate timeout value and return the current performance counter value.
1324
1325 Calculate the number of performance counter ticks required for a timeout.
1326 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1327 as infinity.
1328
1329 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1330 @param[out] CurrentTime Returns the current value of the performance counter.
1331
1332 @return Expected time stamp counter for timeout.
1333 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1334 as infinity.
1335
1336 **/
1337 UINT64
1338 CalculateTimeout (
1339 IN UINTN TimeoutInMicroseconds,
1340 OUT UINT64 *CurrentTime
1341 )
1342 {
1343 UINT64 TimeoutInSeconds;
1344 UINT64 TimestampCounterFreq;
1345
1346 //
1347 // Read the current value of the performance counter
1348 //
1349 *CurrentTime = GetPerformanceCounter ();
1350
1351 //
1352 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1353 // as infinity.
1354 //
1355 if (TimeoutInMicroseconds == 0) {
1356 return 0;
1357 }
1358
1359 //
1360 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1361 // in Hz.
1362 //
1363 TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);
1364
1365 //
1366 // Check the potential overflow before calculate the number of ticks for the timeout value.
1367 //
1368 if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {
1369 //
1370 // Convert microseconds into seconds if direct multiplication overflows
1371 //
1372 TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);
1373 //
1374 // Assertion if the final tick count exceeds MAX_UINT64
1375 //
1376 ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);
1377 return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);
1378 } else {
1379 //
1380 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1381 // it by 1,000,000, to get the number of ticks for the timeout value.
1382 //
1383 return DivU64x32 (
1384 MultU64x64 (
1385 TimestampCounterFreq,
1386 TimeoutInMicroseconds
1387 ),
1388 1000000
1389 );
1390 }
1391 }
1392
1393 /**
1394 Checks whether timeout expires.
1395
1396 Check whether the number of elapsed performance counter ticks required for
1397 a timeout condition has been reached.
1398 If Timeout is zero, which means infinity, return value is always FALSE.
1399
1400 @param[in, out] PreviousTime On input, the value of the performance counter
1401 when it was last read.
1402 On output, the current value of the performance
1403 counter
1404 @param[in] TotalTime The total amount of elapsed time in performance
1405 counter ticks.
1406 @param[in] Timeout The number of performance counter ticks required
1407 to reach a timeout condition.
1408
1409 @retval TRUE A timeout condition has been reached.
1410 @retval FALSE A timeout condition has not been reached.
1411
1412 **/
1413 BOOLEAN
1414 CheckTimeout (
1415 IN OUT UINT64 *PreviousTime,
1416 IN UINT64 *TotalTime,
1417 IN UINT64 Timeout
1418 )
1419 {
1420 UINT64 Start;
1421 UINT64 End;
1422 UINT64 CurrentTime;
1423 INT64 Delta;
1424 INT64 Cycle;
1425
1426 if (Timeout == 0) {
1427 return FALSE;
1428 }
1429
1430 GetPerformanceCounterProperties (&Start, &End);
1431 Cycle = End - Start;
1432 if (Cycle < 0) {
1433 Cycle = -Cycle;
1434 }
1435
1436 Cycle++;
1437 CurrentTime = GetPerformanceCounter ();
1438 Delta = (INT64)(CurrentTime - *PreviousTime);
1439 if (Start > End) {
1440 Delta = -Delta;
1441 }
1442
1443 if (Delta < 0) {
1444 Delta += Cycle;
1445 }
1446
1447 *TotalTime += Delta;
1448 *PreviousTime = CurrentTime;
1449 if (*TotalTime > Timeout) {
1450 return TRUE;
1451 }
1452
1453 return FALSE;
1454 }
1455
1456 /**
1457 Helper function that waits until the finished AP count reaches the specified
1458 limit, or the specified timeout elapses (whichever comes first).
1459
1460 @param[in] CpuMpData Pointer to CPU MP Data.
1461 @param[in] FinishedApLimit The number of finished APs to wait for.
1462 @param[in] TimeLimit The number of microseconds to wait for.
1463 **/
1464 VOID
1465 TimedWaitForApFinish (
1466 IN CPU_MP_DATA *CpuMpData,
1467 IN UINT32 FinishedApLimit,
1468 IN UINT32 TimeLimit
1469 )
1470 {
1471 //
1472 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1473 // "infinity", so check for (TimeLimit == 0) explicitly.
1474 //
1475 if (TimeLimit == 0) {
1476 return;
1477 }
1478
1479 CpuMpData->TotalTime = 0;
1480 CpuMpData->ExpectedTime = CalculateTimeout (
1481 TimeLimit,
1482 &CpuMpData->CurrentTime
1483 );
1484 while (CpuMpData->FinishedCount < FinishedApLimit &&
1485 !CheckTimeout (
1486 &CpuMpData->CurrentTime,
1487 &CpuMpData->TotalTime,
1488 CpuMpData->ExpectedTime
1489 ))
1490 {
1491 CpuPause ();
1492 }
1493
1494 if (CpuMpData->FinishedCount >= FinishedApLimit) {
1495 DEBUG ((
1496 DEBUG_VERBOSE,
1497 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1498 __FUNCTION__,
1499 FinishedApLimit,
1500 DivU64x64Remainder (
1501 MultU64x32 (CpuMpData->TotalTime, 1000000),
1502 GetPerformanceCounterProperties (NULL, NULL),
1503 NULL
1504 )
1505 ));
1506 }
1507 }
1508
1509 /**
1510 Reset an AP to Idle state.
1511
1512 Any task being executed by the AP will be aborted and the AP
1513 will be waiting for a new task in Wait-For-SIPI state.
1514
1515 @param[in] ProcessorNumber The handle number of processor.
1516 **/
1517 VOID
1518 ResetProcessorToIdleState (
1519 IN UINTN ProcessorNumber
1520 )
1521 {
1522 CPU_MP_DATA *CpuMpData;
1523
1524 CpuMpData = GetCpuMpData ();
1525
1526 CpuMpData->InitFlag = ApInitReconfig;
1527 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL, TRUE);
1528 while (CpuMpData->FinishedCount < 1) {
1529 CpuPause ();
1530 }
1531
1532 CpuMpData->InitFlag = ApInitDone;
1533
1534 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1535 }
1536
1537 /**
1538 Searches for the next waiting AP.
1539
1540 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1541
1542 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1543
1544 @retval EFI_SUCCESS The next waiting AP has been found.
1545 @retval EFI_NOT_FOUND No waiting AP exists.
1546
1547 **/
1548 EFI_STATUS
1549 GetNextWaitingProcessorNumber (
1550 OUT UINTN *NextProcessorNumber
1551 )
1552 {
1553 UINTN ProcessorNumber;
1554 CPU_MP_DATA *CpuMpData;
1555
1556 CpuMpData = GetCpuMpData ();
1557
1558 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1559 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1560 *NextProcessorNumber = ProcessorNumber;
1561 return EFI_SUCCESS;
1562 }
1563 }
1564
1565 return EFI_NOT_FOUND;
1566 }
1567
1568 /** Checks status of specified AP.
1569
1570 This function checks whether the specified AP has finished the task assigned
1571 by StartupThisAP(), and whether timeout expires.
1572
1573 @param[in] ProcessorNumber The handle number of processor.
1574
1575 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1576 @retval EFI_TIMEOUT The timeout expires.
1577 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1578 **/
1579 EFI_STATUS
1580 CheckThisAP (
1581 IN UINTN ProcessorNumber
1582 )
1583 {
1584 CPU_MP_DATA *CpuMpData;
1585 CPU_AP_DATA *CpuData;
1586
1587 CpuMpData = GetCpuMpData ();
1588 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1589
1590 //
1591 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1592 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1593 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1594 //
1595 //
1596 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1597 //
1598 if (GetApState (CpuData) == CpuStateFinished) {
1599 if (CpuData->Finished != NULL) {
1600 *(CpuData->Finished) = TRUE;
1601 }
1602
1603 SetApState (CpuData, CpuStateIdle);
1604 return EFI_SUCCESS;
1605 } else {
1606 //
1607 // If timeout expires for StartupThisAP(), report timeout.
1608 //
1609 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
1610 if (CpuData->Finished != NULL) {
1611 *(CpuData->Finished) = FALSE;
1612 }
1613
1614 //
1615 // Reset failed AP to idle state
1616 //
1617 ResetProcessorToIdleState (ProcessorNumber);
1618
1619 return EFI_TIMEOUT;
1620 }
1621 }
1622
1623 return EFI_NOT_READY;
1624 }
1625
1626 /**
1627 Checks status of all APs.
1628
1629 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1630 and whether timeout expires.
1631
1632 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1633 @retval EFI_TIMEOUT The timeout expires.
1634 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1635 **/
1636 EFI_STATUS
1637 CheckAllAPs (
1638 VOID
1639 )
1640 {
1641 UINTN ProcessorNumber;
1642 UINTN NextProcessorNumber;
1643 UINTN ListIndex;
1644 EFI_STATUS Status;
1645 CPU_MP_DATA *CpuMpData;
1646 CPU_AP_DATA *CpuData;
1647
1648 CpuMpData = GetCpuMpData ();
1649
1650 NextProcessorNumber = 0;
1651
1652 //
1653 // Go through all APs that are responsible for the StartupAllAPs().
1654 //
1655 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1656 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {
1657 continue;
1658 }
1659
1660 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1661 //
1662 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1663 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1664 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1665 //
1666 if (GetApState (CpuData) == CpuStateFinished) {
1667 CpuMpData->RunningCount--;
1668 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1669 SetApState (CpuData, CpuStateIdle);
1670
1671 //
1672 // If in Single Thread mode, then search for the next waiting AP for execution.
1673 //
1674 if (CpuMpData->SingleThread) {
1675 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
1676
1677 if (!EFI_ERROR (Status)) {
1678 WakeUpAP (
1679 CpuMpData,
1680 FALSE,
1681 (UINT32)NextProcessorNumber,
1682 CpuMpData->Procedure,
1683 CpuMpData->ProcArguments,
1684 TRUE
1685 );
1686 }
1687 }
1688 }
1689 }
1690
1691 //
1692 // If all APs finish, return EFI_SUCCESS.
1693 //
1694 if (CpuMpData->RunningCount == 0) {
1695 return EFI_SUCCESS;
1696 }
1697
1698 //
1699 // If timeout expires, report timeout.
1700 //
1701 if (CheckTimeout (
1702 &CpuMpData->CurrentTime,
1703 &CpuMpData->TotalTime,
1704 CpuMpData->ExpectedTime
1705 )
1706 )
1707 {
1708 //
1709 // If FailedCpuList is not NULL, record all failed APs in it.
1710 //
1711 if (CpuMpData->FailedCpuList != NULL) {
1712 *CpuMpData->FailedCpuList =
1713 AllocatePool ((CpuMpData->RunningCount + 1) * sizeof (UINTN));
1714 ASSERT (*CpuMpData->FailedCpuList != NULL);
1715 }
1716
1717 ListIndex = 0;
1718
1719 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1720 //
1721 // Check whether this processor is responsible for StartupAllAPs().
1722 //
1723 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1724 //
1725 // Reset failed APs to idle state
1726 //
1727 ResetProcessorToIdleState (ProcessorNumber);
1728 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1729 if (CpuMpData->FailedCpuList != NULL) {
1730 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;
1731 }
1732 }
1733 }
1734
1735 if (CpuMpData->FailedCpuList != NULL) {
1736 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
1737 }
1738
1739 return EFI_TIMEOUT;
1740 }
1741
1742 return EFI_NOT_READY;
1743 }
1744
1745 /**
1746 MP Initialize Library initialization.
1747
1748 This service will allocate AP reset vector and wakeup all APs to do APs
1749 initialization.
1750
1751 This service must be invoked before all other MP Initialize Library
1752 service are invoked.
1753
1754 @retval EFI_SUCCESS MP initialization succeeds.
1755 @retval Others MP initialization fails.
1756
1757 **/
1758 EFI_STATUS
1759 EFIAPI
1760 MpInitLibInitialize (
1761 VOID
1762 )
1763 {
1764 CPU_MP_DATA *OldCpuMpData;
1765 CPU_INFO_IN_HOB *CpuInfoInHob;
1766 UINT32 MaxLogicalProcessorNumber;
1767 UINT32 ApStackSize;
1768 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1769 CPU_VOLATILE_REGISTERS VolatileRegisters;
1770 UINTN BufferSize;
1771 UINT32 MonitorFilterSize;
1772 VOID *MpBuffer;
1773 UINTN Buffer;
1774 CPU_MP_DATA *CpuMpData;
1775 UINT8 ApLoopMode;
1776 UINT8 *MonitorBuffer;
1777 UINTN Index;
1778 UINTN ApResetVectorSize;
1779 UINTN BackupBufferAddr;
1780 UINTN ApIdtBase;
1781
1782 OldCpuMpData = GetCpuMpDataFromGuidedHob ();
1783 if (OldCpuMpData == NULL) {
1784 MaxLogicalProcessorNumber = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
1785 } else {
1786 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;
1787 }
1788
1789 ASSERT (MaxLogicalProcessorNumber != 0);
1790
1791 AsmGetAddressMap (&AddressMap);
1792 ApResetVectorSize = GetApResetVectorSize (&AddressMap);
1793 ApStackSize = PcdGet32 (PcdCpuApStackSize);
1794 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
1795
1796 //
1797 // Save BSP's Control registers for APs.
1798 //
1799 SaveVolatileRegisters (&VolatileRegisters);
1800
1801 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
1802 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
1803 BufferSize += ApResetVectorSize;
1804 BufferSize = ALIGN_VALUE (BufferSize, 8);
1805 BufferSize += VolatileRegisters.Idtr.Limit + 1;
1806 BufferSize += sizeof (CPU_MP_DATA);
1807 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
1808 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
1809 ASSERT (MpBuffer != NULL);
1810 ZeroMem (MpBuffer, BufferSize);
1811 Buffer = (UINTN)MpBuffer;
1812
1813 //
1814 // The layout of the Buffer is as below:
1815 //
1816 // +--------------------+ <-- Buffer
1817 // AP Stacks (N)
1818 // +--------------------+ <-- MonitorBuffer
1819 // AP Monitor Filters (N)
1820 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1821 // Backup Buffer
1822 // +--------------------+
1823 // Padding
1824 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1825 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1826 // +--------------------+ <-- CpuMpData
1827 // CPU_MP_DATA
1828 // +--------------------+ <-- CpuMpData->CpuData
1829 // CPU_AP_DATA (N)
1830 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1831 // CPU_INFO_IN_HOB (N)
1832 // +--------------------+
1833 //
1834 MonitorBuffer = (UINT8 *)(Buffer + ApStackSize * MaxLogicalProcessorNumber);
1835 BackupBufferAddr = (UINTN)MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
1836 ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8);
1837 CpuMpData = (CPU_MP_DATA *)(ApIdtBase + VolatileRegisters.Idtr.Limit + 1);
1838 CpuMpData->Buffer = Buffer;
1839 CpuMpData->CpuApStackSize = ApStackSize;
1840 CpuMpData->BackupBuffer = BackupBufferAddr;
1841 CpuMpData->BackupBufferSize = ApResetVectorSize;
1842 CpuMpData->WakeupBuffer = (UINTN)-1;
1843 CpuMpData->CpuCount = 1;
1844 CpuMpData->BspNumber = 0;
1845 CpuMpData->WaitEvent = NULL;
1846 CpuMpData->SwitchBspFlag = FALSE;
1847 CpuMpData->CpuData = (CPU_AP_DATA *)(CpuMpData + 1);
1848 CpuMpData->CpuInfoInHob = (UINT64)(UINTN)(CpuMpData->CpuData + MaxLogicalProcessorNumber);
1849 InitializeSpinLock (&CpuMpData->MpLock);
1850 CpuMpData->SevEsIsEnabled = PcdGetBool (PcdSevEsIsEnabled);
1851 CpuMpData->SevEsAPBuffer = (UINTN)-1;
1852 CpuMpData->GhcbBase = PcdGet64 (PcdGhcbBase);
1853
1854 //
1855 // Make sure no memory usage outside of the allocated buffer.
1856 //
1857 ASSERT (
1858 (CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) ==
1859 Buffer + BufferSize
1860 );
1861
1862 //
1863 // Duplicate BSP's IDT to APs.
1864 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1865 //
1866 CopyMem ((VOID *)ApIdtBase, (VOID *)VolatileRegisters.Idtr.Base, VolatileRegisters.Idtr.Limit + 1);
1867 VolatileRegisters.Idtr.Base = ApIdtBase;
1868 //
1869 // Don't pass BSP's TR to APs to avoid AP init failure.
1870 //
1871 VolatileRegisters.Tr = 0;
1872 CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &VolatileRegisters, sizeof (VolatileRegisters));
1873 //
1874 // Set BSP basic information
1875 //
1876 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSize);
1877 //
1878 // Save assembly code information
1879 //
1880 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
1881 //
1882 // Finally set AP loop mode
1883 //
1884 CpuMpData->ApLoopMode = ApLoopMode;
1885 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
1886
1887 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);
1888
1889 //
1890 // Set up APs wakeup signal buffer
1891 //
1892 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {
1893 CpuMpData->CpuData[Index].StartupApSignal =
1894 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
1895 }
1896
1897 //
1898 // Enable the local APIC for Virtual Wire Mode.
1899 //
1900 ProgramVirtualWireMode ();
1901
1902 if (OldCpuMpData == NULL) {
1903 if (MaxLogicalProcessorNumber > 1) {
1904 //
1905 // Wakeup all APs and calculate the processor count in system
1906 //
1907 CollectProcessorCount (CpuMpData);
1908 }
1909 } else {
1910 //
1911 // APs have been wakeup before, just get the CPU Information
1912 // from HOB
1913 //
1914 OldCpuMpData->NewCpuMpData = CpuMpData;
1915 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1916 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1917 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1918 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
1919 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1920 InitializeSpinLock (&CpuMpData->CpuData[Index].ApLock);
1921 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0) ? TRUE : FALSE;
1922 CpuMpData->CpuData[Index].ApFunction = 0;
1923 }
1924 }
1925
1926 if (!GetMicrocodePatchInfoFromHob (
1927 &CpuMpData->MicrocodePatchAddress,
1928 &CpuMpData->MicrocodePatchRegionSize
1929 ))
1930 {
1931 //
1932 // The microcode patch information cache HOB does not exist, which means
1933 // the microcode patches data has not been loaded into memory yet
1934 //
1935 ShadowMicrocodeUpdatePatch (CpuMpData);
1936 }
1937
1938 //
1939 // Detect and apply Microcode on BSP
1940 //
1941 MicrocodeDetect (CpuMpData, CpuMpData->BspNumber);
1942 //
1943 // Store BSP's MTRR setting
1944 //
1945 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
1946
1947 //
1948 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1949 //
1950 if (CpuMpData->CpuCount > 1) {
1951 if (OldCpuMpData != NULL) {
1952 //
1953 // Only needs to use this flag for DXE phase to update the wake up
1954 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1955 // in DXE.
1956 //
1957 CpuMpData->InitFlag = ApInitReconfig;
1958 }
1959
1960 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE);
1961 //
1962 // Wait for all APs finished initialization
1963 //
1964 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1965 CpuPause ();
1966 }
1967
1968 if (OldCpuMpData != NULL) {
1969 CpuMpData->InitFlag = ApInitDone;
1970 }
1971
1972 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1973 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1974 }
1975 }
1976
1977 //
1978 // Dump the microcode revision for each core.
1979 //
1980 DEBUG_CODE_BEGIN ();
1981 UINT32 ThreadId;
1982 UINT32 ExpectedMicrocodeRevision;
1983
1984 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
1985 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1986 GetProcessorLocationByApicId (CpuInfoInHob[Index].InitialApicId, NULL, NULL, &ThreadId);
1987 if (ThreadId == 0) {
1988 //
1989 // MicrocodeDetect() loads microcode in first thread of each core, so,
1990 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.
1991 //
1992 ExpectedMicrocodeRevision = 0;
1993 if (CpuMpData->CpuData[Index].MicrocodeEntryAddr != 0) {
1994 ExpectedMicrocodeRevision = ((CPU_MICROCODE_HEADER *)(UINTN)CpuMpData->CpuData[Index].MicrocodeEntryAddr)->UpdateRevision;
1995 }
1996
1997 DEBUG ((
1998 DEBUG_INFO,
1999 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",
2000 Index,
2001 CpuMpData->CpuData[Index].MicrocodeRevision,
2002 ExpectedMicrocodeRevision
2003 ));
2004 }
2005 }
2006
2007 DEBUG_CODE_END ();
2008 //
2009 // Initialize global data for MP support
2010 //
2011 InitMpGlobalData (CpuMpData);
2012
2013 return EFI_SUCCESS;
2014 }
2015
2016 /**
2017 Gets detailed MP-related information on the requested processor at the
2018 instant this call is made. This service may only be called from the BSP.
2019
2020 @param[in] ProcessorNumber The handle number of processor.
2021 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
2022 the requested processor is deposited.
2023 @param[out] HealthData Return processor health data.
2024
2025 @retval EFI_SUCCESS Processor information was returned.
2026 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2027 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
2028 @retval EFI_NOT_FOUND The processor with the handle specified by
2029 ProcessorNumber does not exist in the platform.
2030 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2031
2032 **/
2033 EFI_STATUS
2034 EFIAPI
2035 MpInitLibGetProcessorInfo (
2036 IN UINTN ProcessorNumber,
2037 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
2038 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
2039 )
2040 {
2041 CPU_MP_DATA *CpuMpData;
2042 UINTN CallerNumber;
2043 CPU_INFO_IN_HOB *CpuInfoInHob;
2044 UINTN OriginalProcessorNumber;
2045
2046 CpuMpData = GetCpuMpData ();
2047 CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
2048
2049 //
2050 // Lower 24 bits contains the actual processor number.
2051 //
2052 OriginalProcessorNumber = ProcessorNumber;
2053 ProcessorNumber &= BIT24 - 1;
2054
2055 //
2056 // Check whether caller processor is BSP
2057 //
2058 MpInitLibWhoAmI (&CallerNumber);
2059 if (CallerNumber != CpuMpData->BspNumber) {
2060 return EFI_DEVICE_ERROR;
2061 }
2062
2063 if (ProcessorInfoBuffer == NULL) {
2064 return EFI_INVALID_PARAMETER;
2065 }
2066
2067 if (ProcessorNumber >= CpuMpData->CpuCount) {
2068 return EFI_NOT_FOUND;
2069 }
2070
2071 ProcessorInfoBuffer->ProcessorId = (UINT64)CpuInfoInHob[ProcessorNumber].ApicId;
2072 ProcessorInfoBuffer->StatusFlag = 0;
2073 if (ProcessorNumber == CpuMpData->BspNumber) {
2074 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
2075 }
2076
2077 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
2078 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
2079 }
2080
2081 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2082 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
2083 } else {
2084 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
2085 }
2086
2087 //
2088 // Get processor location information
2089 //
2090 GetProcessorLocationByApicId (
2091 CpuInfoInHob[ProcessorNumber].ApicId,
2092 &ProcessorInfoBuffer->Location.Package,
2093 &ProcessorInfoBuffer->Location.Core,
2094 &ProcessorInfoBuffer->Location.Thread
2095 );
2096
2097 if ((OriginalProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) {
2098 GetProcessorLocation2ByApicId (
2099 CpuInfoInHob[ProcessorNumber].ApicId,
2100 &ProcessorInfoBuffer->ExtendedInformation.Location2.Package,
2101 &ProcessorInfoBuffer->ExtendedInformation.Location2.Die,
2102 &ProcessorInfoBuffer->ExtendedInformation.Location2.Tile,
2103 &ProcessorInfoBuffer->ExtendedInformation.Location2.Module,
2104 &ProcessorInfoBuffer->ExtendedInformation.Location2.Core,
2105 &ProcessorInfoBuffer->ExtendedInformation.Location2.Thread
2106 );
2107 }
2108
2109 if (HealthData != NULL) {
2110 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
2111 }
2112
2113 return EFI_SUCCESS;
2114 }
2115
2116 /**
2117 Worker function to switch the requested AP to be the BSP from that point onward.
2118
2119 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
2120 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
2121 enabled AP. Otherwise, it will be disabled.
2122
2123 @retval EFI_SUCCESS BSP successfully switched.
2124 @retval others Failed to switch BSP.
2125
2126 **/
2127 EFI_STATUS
2128 SwitchBSPWorker (
2129 IN UINTN ProcessorNumber,
2130 IN BOOLEAN EnableOldBSP
2131 )
2132 {
2133 CPU_MP_DATA *CpuMpData;
2134 UINTN CallerNumber;
2135 CPU_STATE State;
2136 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
2137 BOOLEAN OldInterruptState;
2138 BOOLEAN OldTimerInterruptState;
2139
2140 //
2141 // Save and Disable Local APIC timer interrupt
2142 //
2143 OldTimerInterruptState = GetApicTimerInterruptState ();
2144 DisableApicTimerInterrupt ();
2145 //
2146 // Before send both BSP and AP to a procedure to exchange their roles,
2147 // interrupt must be disabled. This is because during the exchange role
2148 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
2149 // be corrupted, since interrupt return address will be pushed to stack
2150 // by hardware.
2151 //
2152 OldInterruptState = SaveAndDisableInterrupts ();
2153
2154 //
2155 // Mask LINT0 & LINT1 for the old BSP
2156 //
2157 DisableLvtInterrupts ();
2158
2159 CpuMpData = GetCpuMpData ();
2160
2161 //
2162 // Check whether caller processor is BSP
2163 //
2164 MpInitLibWhoAmI (&CallerNumber);
2165 if (CallerNumber != CpuMpData->BspNumber) {
2166 return EFI_DEVICE_ERROR;
2167 }
2168
2169 if (ProcessorNumber >= CpuMpData->CpuCount) {
2170 return EFI_NOT_FOUND;
2171 }
2172
2173 //
2174 // Check whether specified AP is disabled
2175 //
2176 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
2177 if (State == CpuStateDisabled) {
2178 return EFI_INVALID_PARAMETER;
2179 }
2180
2181 //
2182 // Check whether ProcessorNumber specifies the current BSP
2183 //
2184 if (ProcessorNumber == CpuMpData->BspNumber) {
2185 return EFI_INVALID_PARAMETER;
2186 }
2187
2188 //
2189 // Check whether specified AP is busy
2190 //
2191 if (State == CpuStateBusy) {
2192 return EFI_NOT_READY;
2193 }
2194
2195 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
2196 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
2197 CpuMpData->SwitchBspFlag = TRUE;
2198 CpuMpData->NewBspNumber = ProcessorNumber;
2199
2200 //
2201 // Clear the BSP bit of MSR_IA32_APIC_BASE
2202 //
2203 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
2204 ApicBaseMsr.Bits.BSP = 0;
2205 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
2206
2207 //
2208 // Need to wakeUp AP (future BSP).
2209 //
2210 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData, TRUE);
2211
2212 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
2213
2214 //
2215 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2216 //
2217 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
2218 ApicBaseMsr.Bits.BSP = 1;
2219 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
2220 ProgramVirtualWireMode ();
2221
2222 //
2223 // Wait for old BSP finished AP task
2224 //
2225 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
2226 CpuPause ();
2227 }
2228
2229 CpuMpData->SwitchBspFlag = FALSE;
2230 //
2231 // Set old BSP enable state
2232 //
2233 if (!EnableOldBSP) {
2234 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
2235 } else {
2236 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
2237 }
2238
2239 //
2240 // Save new BSP number
2241 //
2242 CpuMpData->BspNumber = (UINT32)ProcessorNumber;
2243
2244 //
2245 // Restore interrupt state.
2246 //
2247 SetInterruptState (OldInterruptState);
2248
2249 if (OldTimerInterruptState) {
2250 EnableApicTimerInterrupt ();
2251 }
2252
2253 return EFI_SUCCESS;
2254 }
2255
2256 /**
2257 Worker function to let the caller enable or disable an AP from this point onward.
2258 This service may only be called from the BSP.
2259
2260 @param[in] ProcessorNumber The handle number of AP.
2261 @param[in] EnableAP Specifies the new state for the processor for
2262 enabled, FALSE for disabled.
2263 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2264 the new health status of the AP.
2265
2266 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2267 @retval others Failed to Enable/Disable AP.
2268
2269 **/
2270 EFI_STATUS
2271 EnableDisableApWorker (
2272 IN UINTN ProcessorNumber,
2273 IN BOOLEAN EnableAP,
2274 IN UINT32 *HealthFlag OPTIONAL
2275 )
2276 {
2277 CPU_MP_DATA *CpuMpData;
2278 UINTN CallerNumber;
2279
2280 CpuMpData = GetCpuMpData ();
2281
2282 //
2283 // Check whether caller processor is BSP
2284 //
2285 MpInitLibWhoAmI (&CallerNumber);
2286 if (CallerNumber != CpuMpData->BspNumber) {
2287 return EFI_DEVICE_ERROR;
2288 }
2289
2290 if (ProcessorNumber == CpuMpData->BspNumber) {
2291 return EFI_INVALID_PARAMETER;
2292 }
2293
2294 if (ProcessorNumber >= CpuMpData->CpuCount) {
2295 return EFI_NOT_FOUND;
2296 }
2297
2298 if (!EnableAP) {
2299 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
2300 } else {
2301 ResetProcessorToIdleState (ProcessorNumber);
2302 }
2303
2304 if (HealthFlag != NULL) {
2305 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
2306 (BOOLEAN)((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
2307 }
2308
2309 return EFI_SUCCESS;
2310 }
2311
2312 /**
2313 This return the handle number for the calling processor. This service may be
2314 called from the BSP and APs.
2315
2316 @param[out] ProcessorNumber Pointer to the handle number of AP.
2317 The range is from 0 to the total number of
2318 logical processors minus 1. The total number of
2319 logical processors can be retrieved by
2320 MpInitLibGetNumberOfProcessors().
2321
2322 @retval EFI_SUCCESS The current processor handle number was returned
2323 in ProcessorNumber.
2324 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2325 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2326
2327 **/
2328 EFI_STATUS
2329 EFIAPI
2330 MpInitLibWhoAmI (
2331 OUT UINTN *ProcessorNumber
2332 )
2333 {
2334 CPU_MP_DATA *CpuMpData;
2335
2336 if (ProcessorNumber == NULL) {
2337 return EFI_INVALID_PARAMETER;
2338 }
2339
2340 CpuMpData = GetCpuMpData ();
2341
2342 return GetProcessorNumber (CpuMpData, ProcessorNumber);
2343 }
2344
2345 /**
2346 Retrieves the number of logical processor in the platform and the number of
2347 those logical processors that are enabled on this boot. This service may only
2348 be called from the BSP.
2349
2350 @param[out] NumberOfProcessors Pointer to the total number of logical
2351 processors in the system, including the BSP
2352 and disabled APs.
2353 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2354 processors that exist in system, including
2355 the BSP.
2356
2357 @retval EFI_SUCCESS The number of logical processors and enabled
2358 logical processors was retrieved.
2359 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2360 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2361 is NULL.
2362 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2363
2364 **/
2365 EFI_STATUS
2366 EFIAPI
2367 MpInitLibGetNumberOfProcessors (
2368 OUT UINTN *NumberOfProcessors OPTIONAL,
2369 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
2370 )
2371 {
2372 CPU_MP_DATA *CpuMpData;
2373 UINTN CallerNumber;
2374 UINTN ProcessorNumber;
2375 UINTN EnabledProcessorNumber;
2376 UINTN Index;
2377
2378 CpuMpData = GetCpuMpData ();
2379
2380 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
2381 return EFI_INVALID_PARAMETER;
2382 }
2383
2384 //
2385 // Check whether caller processor is BSP
2386 //
2387 MpInitLibWhoAmI (&CallerNumber);
2388 if (CallerNumber != CpuMpData->BspNumber) {
2389 return EFI_DEVICE_ERROR;
2390 }
2391
2392 ProcessorNumber = CpuMpData->CpuCount;
2393 EnabledProcessorNumber = 0;
2394 for (Index = 0; Index < ProcessorNumber; Index++) {
2395 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
2396 EnabledProcessorNumber++;
2397 }
2398 }
2399
2400 if (NumberOfProcessors != NULL) {
2401 *NumberOfProcessors = ProcessorNumber;
2402 }
2403
2404 if (NumberOfEnabledProcessors != NULL) {
2405 *NumberOfEnabledProcessors = EnabledProcessorNumber;
2406 }
2407
2408 return EFI_SUCCESS;
2409 }
2410
2411 /**
2412 Worker function to execute a caller provided function on all enabled APs.
2413
2414 @param[in] Procedure A pointer to the function to be run on
2415 enabled APs of the system.
2416 @param[in] SingleThread If TRUE, then all the enabled APs execute
2417 the function specified by Procedure one by
2418 one, in ascending order of processor handle
2419 number. If FALSE, then all the enabled APs
2420 execute the function specified by Procedure
2421 simultaneously.
2422 @param[in] ExcludeBsp Whether let BSP also trig this task.
2423 @param[in] WaitEvent The event created by the caller with CreateEvent()
2424 service.
2425 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2426 APs to return from Procedure, either for
2427 blocking or non-blocking mode.
2428 @param[in] ProcedureArgument The parameter passed into Procedure for
2429 all APs.
2430 @param[out] FailedCpuList If all APs finish successfully, then its
2431 content is set to NULL. If not all APs
2432 finish before timeout expires, then its
2433 content is set to address of the buffer
2434 holding handle numbers of the failed APs.
2435
2436 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2437 the timeout expired.
2438 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2439 to all enabled APs.
2440 @retval others Failed to Startup all APs.
2441
2442 **/
2443 EFI_STATUS
2444 StartupAllCPUsWorker (
2445 IN EFI_AP_PROCEDURE Procedure,
2446 IN BOOLEAN SingleThread,
2447 IN BOOLEAN ExcludeBsp,
2448 IN EFI_EVENT WaitEvent OPTIONAL,
2449 IN UINTN TimeoutInMicroseconds,
2450 IN VOID *ProcedureArgument OPTIONAL,
2451 OUT UINTN **FailedCpuList OPTIONAL
2452 )
2453 {
2454 EFI_STATUS Status;
2455 CPU_MP_DATA *CpuMpData;
2456 UINTN ProcessorCount;
2457 UINTN ProcessorNumber;
2458 UINTN CallerNumber;
2459 CPU_AP_DATA *CpuData;
2460 BOOLEAN HasEnabledAp;
2461 CPU_STATE ApState;
2462
2463 CpuMpData = GetCpuMpData ();
2464
2465 if (FailedCpuList != NULL) {
2466 *FailedCpuList = NULL;
2467 }
2468
2469 if ((CpuMpData->CpuCount == 1) && ExcludeBsp) {
2470 return EFI_NOT_STARTED;
2471 }
2472
2473 if (Procedure == NULL) {
2474 return EFI_INVALID_PARAMETER;
2475 }
2476
2477 //
2478 // Check whether caller processor is BSP
2479 //
2480 MpInitLibWhoAmI (&CallerNumber);
2481 if (CallerNumber != CpuMpData->BspNumber) {
2482 return EFI_DEVICE_ERROR;
2483 }
2484
2485 //
2486 // Update AP state
2487 //
2488 CheckAndUpdateApsStatus ();
2489
2490 ProcessorCount = CpuMpData->CpuCount;
2491 HasEnabledAp = FALSE;
2492 //
2493 // Check whether all enabled APs are idle.
2494 // If any enabled AP is not idle, return EFI_NOT_READY.
2495 //
2496 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2497 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2498 if (ProcessorNumber != CpuMpData->BspNumber) {
2499 ApState = GetApState (CpuData);
2500 if (ApState != CpuStateDisabled) {
2501 HasEnabledAp = TRUE;
2502 if (ApState != CpuStateIdle) {
2503 //
2504 // If any enabled APs are busy, return EFI_NOT_READY.
2505 //
2506 return EFI_NOT_READY;
2507 }
2508 }
2509 }
2510 }
2511
2512 if (!HasEnabledAp && ExcludeBsp) {
2513 //
2514 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2515 //
2516 return EFI_NOT_STARTED;
2517 }
2518
2519 CpuMpData->RunningCount = 0;
2520 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2521 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2522 CpuData->Waiting = FALSE;
2523 if (ProcessorNumber != CpuMpData->BspNumber) {
2524 if (CpuData->State == CpuStateIdle) {
2525 //
2526 // Mark this processor as responsible for current calling.
2527 //
2528 CpuData->Waiting = TRUE;
2529 CpuMpData->RunningCount++;
2530 }
2531 }
2532 }
2533
2534 CpuMpData->Procedure = Procedure;
2535 CpuMpData->ProcArguments = ProcedureArgument;
2536 CpuMpData->SingleThread = SingleThread;
2537 CpuMpData->FinishedCount = 0;
2538 CpuMpData->FailedCpuList = FailedCpuList;
2539 CpuMpData->ExpectedTime = CalculateTimeout (
2540 TimeoutInMicroseconds,
2541 &CpuMpData->CurrentTime
2542 );
2543 CpuMpData->TotalTime = 0;
2544 CpuMpData->WaitEvent = WaitEvent;
2545
2546 if (!SingleThread) {
2547 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument, FALSE);
2548 } else {
2549 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2550 if (ProcessorNumber == CallerNumber) {
2551 continue;
2552 }
2553
2554 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
2555 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
2556 break;
2557 }
2558 }
2559 }
2560
2561 if (!ExcludeBsp) {
2562 //
2563 // Start BSP.
2564 //
2565 Procedure (ProcedureArgument);
2566 }
2567
2568 Status = EFI_SUCCESS;
2569 if (WaitEvent == NULL) {
2570 do {
2571 Status = CheckAllAPs ();
2572 } while (Status == EFI_NOT_READY);
2573 }
2574
2575 return Status;
2576 }
2577
2578 /**
2579 Worker function to let the caller get one enabled AP to execute a caller-provided
2580 function.
2581
2582 @param[in] Procedure A pointer to the function to be run on
2583 enabled APs of the system.
2584 @param[in] ProcessorNumber The handle number of the AP.
2585 @param[in] WaitEvent The event created by the caller with CreateEvent()
2586 service.
2587 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2588 APs to return from Procedure, either for
2589 blocking or non-blocking mode.
2590 @param[in] ProcedureArgument The parameter passed into Procedure for
2591 all APs.
2592 @param[out] Finished If AP returns from Procedure before the
2593 timeout expires, its content is set to TRUE.
2594 Otherwise, the value is set to FALSE.
2595
2596 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2597 the timeout expires.
2598 @retval others Failed to Startup AP.
2599
2600 **/
2601 EFI_STATUS
2602 StartupThisAPWorker (
2603 IN EFI_AP_PROCEDURE Procedure,
2604 IN UINTN ProcessorNumber,
2605 IN EFI_EVENT WaitEvent OPTIONAL,
2606 IN UINTN TimeoutInMicroseconds,
2607 IN VOID *ProcedureArgument OPTIONAL,
2608 OUT BOOLEAN *Finished OPTIONAL
2609 )
2610 {
2611 EFI_STATUS Status;
2612 CPU_MP_DATA *CpuMpData;
2613 CPU_AP_DATA *CpuData;
2614 UINTN CallerNumber;
2615
2616 CpuMpData = GetCpuMpData ();
2617
2618 if (Finished != NULL) {
2619 *Finished = FALSE;
2620 }
2621
2622 //
2623 // Check whether caller processor is BSP
2624 //
2625 MpInitLibWhoAmI (&CallerNumber);
2626 if (CallerNumber != CpuMpData->BspNumber) {
2627 return EFI_DEVICE_ERROR;
2628 }
2629
2630 //
2631 // Check whether processor with the handle specified by ProcessorNumber exists
2632 //
2633 if (ProcessorNumber >= CpuMpData->CpuCount) {
2634 return EFI_NOT_FOUND;
2635 }
2636
2637 //
2638 // Check whether specified processor is BSP
2639 //
2640 if (ProcessorNumber == CpuMpData->BspNumber) {
2641 return EFI_INVALID_PARAMETER;
2642 }
2643
2644 //
2645 // Check parameter Procedure
2646 //
2647 if (Procedure == NULL) {
2648 return EFI_INVALID_PARAMETER;
2649 }
2650
2651 //
2652 // Update AP state
2653 //
2654 CheckAndUpdateApsStatus ();
2655
2656 //
2657 // Check whether specified AP is disabled
2658 //
2659 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2660 return EFI_INVALID_PARAMETER;
2661 }
2662
2663 //
2664 // If WaitEvent is not NULL, execute in non-blocking mode.
2665 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2666 // CheckAPsStatus() will check completion and timeout periodically.
2667 //
2668 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2669 CpuData->WaitEvent = WaitEvent;
2670 CpuData->Finished = Finished;
2671 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
2672 CpuData->TotalTime = 0;
2673
2674 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
2675
2676 //
2677 // If WaitEvent is NULL, execute in blocking mode.
2678 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2679 //
2680 Status = EFI_SUCCESS;
2681 if (WaitEvent == NULL) {
2682 do {
2683 Status = CheckThisAP (ProcessorNumber);
2684 } while (Status == EFI_NOT_READY);
2685 }
2686
2687 return Status;
2688 }
2689
2690 /**
2691 Get pointer to CPU MP Data structure from GUIDed HOB.
2692
2693 @return The pointer to CPU MP Data structure.
2694 **/
2695 CPU_MP_DATA *
2696 GetCpuMpDataFromGuidedHob (
2697 VOID
2698 )
2699 {
2700 EFI_HOB_GUID_TYPE *GuidHob;
2701 VOID *DataInHob;
2702 CPU_MP_DATA *CpuMpData;
2703
2704 CpuMpData = NULL;
2705 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
2706 if (GuidHob != NULL) {
2707 DataInHob = GET_GUID_HOB_DATA (GuidHob);
2708 CpuMpData = (CPU_MP_DATA *)(*(UINTN *)DataInHob);
2709 }
2710
2711 return CpuMpData;
2712 }
2713
2714 /**
2715 This service executes a caller provided function on all enabled CPUs.
2716
2717 @param[in] Procedure A pointer to the function to be run on
2718 enabled APs of the system. See type
2719 EFI_AP_PROCEDURE.
2720 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2721 APs to return from Procedure, either for
2722 blocking or non-blocking mode. Zero means
2723 infinity. TimeoutInMicroseconds is ignored
2724 for BSP.
2725 @param[in] ProcedureArgument The parameter passed into Procedure for
2726 all APs.
2727
2728 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2729 the timeout expired.
2730 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2731 to all enabled CPUs.
2732 @retval EFI_DEVICE_ERROR Caller processor is AP.
2733 @retval EFI_NOT_READY Any enabled APs are busy.
2734 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2735 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2736 all enabled APs have finished.
2737 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2738
2739 **/
2740 EFI_STATUS
2741 EFIAPI
2742 MpInitLibStartupAllCPUs (
2743 IN EFI_AP_PROCEDURE Procedure,
2744 IN UINTN TimeoutInMicroseconds,
2745 IN VOID *ProcedureArgument OPTIONAL
2746 )
2747 {
2748 return StartupAllCPUsWorker (
2749 Procedure,
2750 FALSE,
2751 FALSE,
2752 NULL,
2753 TimeoutInMicroseconds,
2754 ProcedureArgument,
2755 NULL
2756 );
2757 }