2 Code for Processor S3 restoration
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "PiSmmCpuDxeSmm.h"
17 IA32_DESCRIPTOR GdtrProfile
;
18 IA32_DESCRIPTOR IdtrProfile
;
21 UINTN InitializeFloatingPointUnitsAddress
;
22 } MP_CPU_EXCHANGE_INFO
;
26 UINT8
*RendezvousFunnelAddress
;
27 UINTN PModeEntryOffset
;
30 UINTN LModeEntryOffset
;
32 } MP_ASSEMBLY_ADDRESS_MAP
;
35 // Flags used when program the register.
38 volatile UINTN MemoryMappedLock
; // Spinlock used to program mmio
39 volatile UINT32
*CoreSemaphoreCount
; // Semaphore container used to program
40 // core level semaphore.
41 volatile UINT32
*PackageSemaphoreCount
; // Semaphore container used to program
42 // package level semaphore.
43 } PROGRAM_CPU_REGISTER_FLAGS
;
46 // Signal that SMM BASE relocation is complete.
48 volatile BOOLEAN mInitApsAfterSmmBaseReloc
;
51 Get starting address and size of the rendezvous entry for APs.
52 Information for fixing a jump instruction in the code is also returned.
54 @param AddressMap Output buffer for address map information.
59 MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
62 #define LEGACY_REGION_SIZE (2 * 0x1000)
63 #define LEGACY_REGION_BASE (0xA0000 - LEGACY_REGION_SIZE)
65 PROGRAM_CPU_REGISTER_FLAGS mCpuFlags
;
66 ACPI_CPU_DATA mAcpiCpuData
;
67 volatile UINT32 mNumberToFinish
;
68 MP_CPU_EXCHANGE_INFO
*mExchangeInfo
;
69 BOOLEAN mRestoreSmmConfigurationInS3
= FALSE
;
74 BOOLEAN mSmmS3Flag
= FALSE
;
77 // Pointer to structure used during S3 Resume
79 SMM_S3_RESUME_STATE
*mSmmS3ResumeState
= NULL
;
81 BOOLEAN mAcpiS3Enable
= TRUE
;
83 UINT8
*mApHltLoopCode
= NULL
;
84 UINT8 mApHltLoopCodeTemplate
[] = {
85 0x8B, 0x44, 0x24, 0x04, // mov eax, dword ptr [esp+4]
86 0xF0, 0xFF, 0x08, // lock dec dword ptr [eax]
93 Sync up the MTRR values for all processors.
95 @param MtrrTable Table holding fixed/variable MTRR values to be loaded.
100 EFI_PHYSICAL_ADDRESS MtrrTable
106 Sync up the MTRR values for all processors.
115 MTRR_SETTINGS
*MtrrSettings
;
117 MtrrSettings
= (MTRR_SETTINGS
*) (UINTN
) MtrrTable
;
118 MtrrSetAllMtrrs (MtrrSettings
);
122 Increment semaphore by 1.
124 @param Sem IN: 32-bit unsigned integer
129 IN OUT
volatile UINT32
*Sem
132 InterlockedIncrement (Sem
);
136 Decrement the semaphore by 1 if it is not zero.
138 Performs an atomic decrement operation for semaphore.
139 The compare exchange operation must be performed using
142 @param Sem IN: 32-bit unsigned integer
147 IN OUT
volatile UINT32
*Sem
154 } while (Value
== 0 ||
155 InterlockedCompareExchange32 (
163 Read / write CR value.
165 @param[in] CrIndex The CR index which need to read/write.
166 @param[in] Read Read or write. TRUE is read.
167 @param[in,out] CrValue CR value.
169 @retval EFI_SUCCESS means read/write success, else return EFI_UNSUPPORTED.
175 IN OUT UINTN
*CrValue
181 *CrValue
= AsmReadCr0 ();
183 AsmWriteCr0 (*CrValue
);
188 *CrValue
= AsmReadCr2 ();
190 AsmWriteCr2 (*CrValue
);
195 *CrValue
= AsmReadCr3 ();
197 AsmWriteCr3 (*CrValue
);
202 *CrValue
= AsmReadCr4 ();
204 AsmWriteCr4 (*CrValue
);
208 return EFI_UNSUPPORTED
;;
215 Initialize the CPU registers from a register table.
217 @param[in] RegisterTable The register table for this AP.
218 @param[in] ApLocation AP location info for this ap.
219 @param[in] CpuStatus CPU status info for this CPU.
220 @param[in] CpuFlags Flags data structure used when program the register.
222 @note This service could be called by BSP/APs.
225 ProgramProcessorRegister (
226 IN CPU_REGISTER_TABLE
*RegisterTable
,
227 IN EFI_CPU_PHYSICAL_LOCATION
*ApLocation
,
228 IN CPU_STATUS_INFORMATION
*CpuStatus
,
229 IN PROGRAM_CPU_REGISTER_FLAGS
*CpuFlags
232 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
235 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntryHead
;
236 volatile UINT32
*SemaphorePtr
;
238 UINT32 PackageThreadsCount
;
239 UINT32 CurrentThread
;
240 UINTN ProcessorIndex
;
241 UINTN ValidThreadCount
;
242 UINT32
*ValidCoreCountPerPackage
;
246 // Traverse Register Table of this logical processor
248 RegisterTableEntryHead
= (CPU_REGISTER_TABLE_ENTRY
*) (UINTN
) RegisterTable
->RegisterTableEntry
;
250 for (Index
= 0; Index
< RegisterTable
->TableLength
; Index
++) {
252 RegisterTableEntry
= &RegisterTableEntryHead
[Index
];
255 // Check the type of specified register
257 switch (RegisterTableEntry
->RegisterType
) {
259 // The specified register is Control Register
261 case ControlRegister
:
262 Status
= ReadWriteCr (RegisterTableEntry
->Index
, TRUE
, &Value
);
263 if (EFI_ERROR (Status
)) {
266 Value
= (UINTN
) BitFieldWrite64 (
268 RegisterTableEntry
->ValidBitStart
,
269 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
270 RegisterTableEntry
->Value
272 ReadWriteCr (RegisterTableEntry
->Index
, FALSE
, &Value
);
275 // The specified register is Model Specific Register
279 // If this function is called to restore register setting after INIT signal,
280 // there is no need to restore MSRs in register table.
282 if (RegisterTableEntry
->ValidBitLength
>= 64) {
284 // If length is not less than 64 bits, then directly write without reading
287 RegisterTableEntry
->Index
,
288 RegisterTableEntry
->Value
292 // Set the bit section according to bit start and length
294 AsmMsrBitFieldWrite64 (
295 RegisterTableEntry
->Index
,
296 RegisterTableEntry
->ValidBitStart
,
297 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
298 RegisterTableEntry
->Value
303 // MemoryMapped operations
306 AcquireSpinLock (&CpuFlags
->MemoryMappedLock
);
307 MmioBitFieldWrite32 (
308 (UINTN
)(RegisterTableEntry
->Index
| LShiftU64 (RegisterTableEntry
->HighIndex
, 32)),
309 RegisterTableEntry
->ValidBitStart
,
310 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
311 (UINT32
)RegisterTableEntry
->Value
313 ReleaseSpinLock (&CpuFlags
->MemoryMappedLock
);
316 // Enable or disable cache
320 // If value of the entry is 0, then disable cache. Otherwise, enable cache.
322 if (RegisterTableEntry
->Value
== 0) {
330 // Semaphore works logic like below:
332 // V(x) = LibReleaseSemaphore (Semaphore[FirstThread + x]);
333 // P(x) = LibWaitForSemaphore (Semaphore[FirstThread + x]);
335 // All threads (T0...Tn) waits in P() line and continues running
341 // V(0...n) V(0...n) ... V(0...n)
342 // n * P(0) n * P(1) ... n * P(n)
345 (ApLocation
!= NULL
) &&
346 (CpuStatus
->ValidCoreCountPerPackage
!= 0) &&
347 (CpuFlags
->CoreSemaphoreCount
!= NULL
) &&
348 (CpuFlags
->PackageSemaphoreCount
!= NULL
)
350 switch (RegisterTableEntry
->Value
) {
352 SemaphorePtr
= CpuFlags
->CoreSemaphoreCount
;
354 // Get Offset info for the first thread in the core which current thread belongs to.
356 FirstThread
= (ApLocation
->Package
* CpuStatus
->MaxCoreCount
+ ApLocation
->Core
) * CpuStatus
->MaxThreadCount
;
357 CurrentThread
= FirstThread
+ ApLocation
->Thread
;
359 // First Notify all threads in current Core that this thread has ready.
361 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
362 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
365 // Second, check whether all valid threads in current core have ready.
367 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
368 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
373 SemaphorePtr
= CpuFlags
->PackageSemaphoreCount
;
374 ValidCoreCountPerPackage
= (UINT32
*)(UINTN
)CpuStatus
->ValidCoreCountPerPackage
;
376 // Get Offset info for the first thread in the package which current thread belongs to.
378 FirstThread
= ApLocation
->Package
* CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
;
380 // Get the possible threads count for current package.
382 PackageThreadsCount
= CpuStatus
->MaxThreadCount
* CpuStatus
->MaxCoreCount
;
383 CurrentThread
= FirstThread
+ CpuStatus
->MaxThreadCount
* ApLocation
->Core
+ ApLocation
->Thread
;
385 // Get the valid thread count for current package.
387 ValidThreadCount
= CpuStatus
->MaxThreadCount
* ValidCoreCountPerPackage
[ApLocation
->Package
];
390 // Different packages may have different valid cores in them. If driver maintail clearly
391 // cores number in different packages, the logic will be much complicated.
392 // Here driver just simply records the max core number in all packages and use it as expect
393 // core number for all packages.
394 // In below two steps logic, first current thread will Release semaphore for each thread
395 // in current package. Maybe some threads are not valid in this package, but driver don't
396 // care. Second, driver will let current thread wait semaphore for all valid threads in
397 // current package. Because only the valid threads will do release semaphore for this
398 // thread, driver here only need to wait the valid thread count.
402 // First Notify all threads in current package that this thread has ready.
404 for (ProcessorIndex
= 0; ProcessorIndex
< PackageThreadsCount
; ProcessorIndex
++) {
405 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
408 // Second, check whether all valid threads in current package have ready.
410 for (ProcessorIndex
= 0; ProcessorIndex
< ValidThreadCount
; ProcessorIndex
++) {
411 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
428 Set Processor register for one AP.
430 @param PreSmmRegisterTable Use pre Smm register table or register table.
435 IN BOOLEAN PreSmmRegisterTable
438 CPU_REGISTER_TABLE
*RegisterTable
;
439 CPU_REGISTER_TABLE
*RegisterTables
;
444 if (PreSmmRegisterTable
) {
445 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
;
447 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
;
450 InitApicId
= GetInitialApicId ();
451 RegisterTable
= NULL
;
452 ProcIndex
= (UINTN
)-1;
453 for (Index
= 0; Index
< mAcpiCpuData
.NumberOfCpus
; Index
++) {
454 if (RegisterTables
[Index
].InitialApicId
== InitApicId
) {
455 RegisterTable
= &RegisterTables
[Index
];
460 ASSERT (RegisterTable
!= NULL
);
462 if (mAcpiCpuData
.ApLocation
!= 0) {
463 ProgramProcessorRegister (
465 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)mAcpiCpuData
.ApLocation
+ ProcIndex
,
466 &mAcpiCpuData
.CpuStatus
,
470 ProgramProcessorRegister (
473 &mAcpiCpuData
.CpuStatus
,
480 AP initialization before then after SMBASE relocation in the S3 boot path.
490 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
495 // Count down the number with lock mechanism.
497 InterlockedDecrement (&mNumberToFinish
);
500 // Wait for BSP to signal SMM Base relocation done.
502 while (!mInitApsAfterSmmBaseReloc
) {
506 ProgramVirtualWireMode ();
507 DisableLvtInterrupts ();
512 // Place AP into the safe code, count down the number with lock mechanism in the safe code.
514 TopOfStack
= (UINTN
) Stack
+ sizeof (Stack
);
515 TopOfStack
&= ~(UINTN
) (CPU_STACK_ALIGNMENT
- 1);
516 CopyMem ((VOID
*) (UINTN
) mApHltLoopCode
, mApHltLoopCodeTemplate
, sizeof (mApHltLoopCodeTemplate
));
517 TransferApToSafeState ((UINTN
)mApHltLoopCode
, TopOfStack
, (UINTN
)&mNumberToFinish
);
521 Prepares startup vector for APs.
523 This function prepares startup vector for APs.
525 @param WorkingBuffer The address of the work buffer.
528 PrepareApStartupVector (
529 EFI_PHYSICAL_ADDRESS WorkingBuffer
532 EFI_PHYSICAL_ADDRESS StartupVector
;
533 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
536 // Get the address map of startup code for AP,
537 // including code size, and offset of long jump instructions to redirect.
539 ZeroMem (&AddressMap
, sizeof (AddressMap
));
540 AsmGetAddressMap (&AddressMap
);
542 StartupVector
= WorkingBuffer
;
545 // Copy AP startup code to startup vector, and then redirect the long jump
546 // instructions for mode switching.
548 CopyMem ((VOID
*) (UINTN
) StartupVector
, AddressMap
.RendezvousFunnelAddress
, AddressMap
.Size
);
549 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.FlatJumpOffset
+ 3) = (UINT32
) (StartupVector
+ AddressMap
.PModeEntryOffset
);
550 if (AddressMap
.LongJumpOffset
!= 0) {
551 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.LongJumpOffset
+ 2) = (UINT32
) (StartupVector
+ AddressMap
.LModeEntryOffset
);
555 // Get the start address of exchange data between BSP and AP.
557 mExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (StartupVector
+ AddressMap
.Size
);
558 ZeroMem ((VOID
*) mExchangeInfo
, sizeof (MP_CPU_EXCHANGE_INFO
));
560 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->GdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
561 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->IdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
563 mExchangeInfo
->StackStart
= (VOID
*) (UINTN
) mAcpiCpuData
.StackAddress
;
564 mExchangeInfo
->StackSize
= mAcpiCpuData
.StackSize
;
565 mExchangeInfo
->BufferStart
= (UINT32
) StartupVector
;
566 mExchangeInfo
->Cr3
= (UINT32
) (AsmReadCr3 ());
567 mExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
571 The function is invoked before SMBASE relocation in S3 path to restores CPU status.
573 The function is invoked before SMBASE relocation in S3 path. It does first time microcode load
574 and restores MTRRs for both BSP and APs.
578 InitializeCpuBeforeRebase (
582 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
586 ProgramVirtualWireMode ();
588 PrepareApStartupVector (mAcpiCpuData
.StartupVector
);
590 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
591 mExchangeInfo
->ApFunction
= (VOID
*) (UINTN
) InitializeAp
;
594 // Execute code for before SmmBaseReloc. Note: This flag is maintained across S3 boots.
596 mInitApsAfterSmmBaseReloc
= FALSE
;
599 // Send INIT IPI - SIPI to all APs
601 SendInitSipiSipiAllExcludingSelf ((UINT32
)mAcpiCpuData
.StartupVector
);
603 while (mNumberToFinish
> 0) {
609 The function is invoked after SMBASE relocation in S3 path to restores CPU status.
611 The function is invoked after SMBASE relocation in S3 path. It restores configuration according to
612 data saved by normal boot path for both BSP and APs.
616 InitializeCpuAfterRebase (
620 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
623 // Signal that SMM base relocation is complete and to continue initialization for all APs.
625 mInitApsAfterSmmBaseReloc
= TRUE
;
628 // Must begin set register after all APs have continue their initialization.
629 // This is a requirement to support semaphore mechanism in register table.
630 // Because if semaphore's dependence type is package type, semaphore will wait
631 // for all Aps in one package finishing their tasks before set next register
632 // for all APs. If the Aps not begin its task during BSP doing its task, the
633 // BSP thread will hang because it is waiting for other Aps in the same
634 // package finishing their task.
638 while (mNumberToFinish
> 0) {
644 Restore SMM Configuration in S3 boot path.
648 RestoreSmmConfigurationInS3 (
652 if (!mAcpiS3Enable
) {
657 // Restore SMM Configuration in S3 boot path.
659 if (mRestoreSmmConfigurationInS3
) {
661 // Need make sure gSmst is correct because below function may use them.
663 gSmst
->SmmStartupThisAp
= gSmmCpuPrivate
->SmmCoreEntryContext
.SmmStartupThisAp
;
664 gSmst
->CurrentlyExecutingCpu
= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
;
665 gSmst
->NumberOfCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
666 gSmst
->CpuSaveStateSize
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveStateSize
;
667 gSmst
->CpuSaveState
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveState
;
670 // Configure SMM Code Access Check feature if available.
672 ConfigSmmCodeAccessCheck ();
674 SmmCpuFeaturesCompleteSmmReadyToLock ();
676 mRestoreSmmConfigurationInS3
= FALSE
;
681 Perform SMM initialization for all processors in the S3 boot path.
683 For a native platform, MP initialization in the S3 boot path is also performed in this function.
691 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
692 IA32_DESCRIPTOR Ia32Idtr
;
693 IA32_DESCRIPTOR X64Idtr
;
694 IA32_IDT_GATE_DESCRIPTOR IdtEntryTable
[EXCEPTION_VECTOR_NUMBER
];
697 DEBUG ((EFI_D_INFO
, "SmmRestoreCpu()\n"));
702 // See if there is enough context to resume PEI Phase
704 if (mSmmS3ResumeState
== NULL
) {
705 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
709 SmmS3ResumeState
= mSmmS3ResumeState
;
710 ASSERT (SmmS3ResumeState
!= NULL
);
712 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
714 // Save the IA32 IDT Descriptor
716 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
719 // Setup X64 IDT table
721 ZeroMem (IdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32);
722 X64Idtr
.Base
= (UINTN
) IdtEntryTable
;
723 X64Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32 - 1);
724 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &X64Idtr
);
727 // Setup the default exception handler
729 Status
= InitializeCpuExceptionHandlers (NULL
);
730 ASSERT_EFI_ERROR (Status
);
733 // Initialize Debug Agent to support source level debug
735 InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
, (VOID
*)&Ia32Idtr
, NULL
);
739 // Skip initialization if mAcpiCpuData is not valid
741 if (mAcpiCpuData
.NumberOfCpus
> 0) {
743 // First time microcode load and restore MTRRs
745 InitializeCpuBeforeRebase ();
749 // Restore SMBASE for BSP and all APs
754 // Skip initialization if mAcpiCpuData is not valid
756 if (mAcpiCpuData
.NumberOfCpus
> 0) {
758 // Restore MSRs for BSP and all APs
760 InitializeCpuAfterRebase ();
764 // Set a flag to restore SMM configuration in S3 path.
766 mRestoreSmmConfigurationInS3
= TRUE
;
768 DEBUG (( EFI_D_INFO
, "SMM S3 Return CS = %x\n", SmmS3ResumeState
->ReturnCs
));
769 DEBUG (( EFI_D_INFO
, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState
->ReturnEntryPoint
));
770 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState
->ReturnContext1
));
771 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState
->ReturnContext2
));
772 DEBUG (( EFI_D_INFO
, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState
->ReturnStackPointer
));
775 // If SMM is in 32-bit mode, then use SwitchStack() to resume PEI Phase
777 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_32
) {
778 DEBUG ((EFI_D_INFO
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
781 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)SmmS3ResumeState
->ReturnEntryPoint
,
782 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext1
,
783 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext2
,
784 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnStackPointer
789 // If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase
791 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
792 DEBUG ((EFI_D_INFO
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
794 // Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.
796 SaveAndSetDebugTimerInterrupt (FALSE
);
798 // Restore IA32 IDT table
800 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
802 SmmS3ResumeState
->ReturnCs
,
803 (UINT32
)SmmS3ResumeState
->ReturnEntryPoint
,
804 (UINT32
)SmmS3ResumeState
->ReturnContext1
,
805 (UINT32
)SmmS3ResumeState
->ReturnContext2
,
806 (UINT32
)SmmS3ResumeState
->ReturnStackPointer
811 // Can not resume PEI Phase
813 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
818 Initialize SMM S3 resume state structure used during S3 Resume.
820 @param[in] Cr3 The base address of the page tables to use in SMM.
824 InitSmmS3ResumeState (
829 EFI_SMRAM_DESCRIPTOR
*SmramDescriptor
;
830 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
831 EFI_PHYSICAL_ADDRESS Address
;
834 if (!mAcpiS3Enable
) {
838 GuidHob
= GetFirstGuidHob (&gEfiAcpiVariableGuid
);
839 if (GuidHob
== NULL
) {
842 "ERROR:%a(): HOB(gEfiAcpiVariableGuid=%g) needed by S3 resume doesn't exist!\n",
844 &gEfiAcpiVariableGuid
848 SmramDescriptor
= (EFI_SMRAM_DESCRIPTOR
*) GET_GUID_HOB_DATA (GuidHob
);
850 DEBUG ((EFI_D_INFO
, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor
));
851 DEBUG ((EFI_D_INFO
, "SMM S3 Structure = %x\n", SmramDescriptor
->CpuStart
));
853 SmmS3ResumeState
= (SMM_S3_RESUME_STATE
*)(UINTN
)SmramDescriptor
->CpuStart
;
854 ZeroMem (SmmS3ResumeState
, sizeof (SMM_S3_RESUME_STATE
));
856 mSmmS3ResumeState
= SmmS3ResumeState
;
857 SmmS3ResumeState
->Smst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gSmst
;
859 SmmS3ResumeState
->SmmS3ResumeEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmRestoreCpu
;
861 SmmS3ResumeState
->SmmS3StackSize
= SIZE_32KB
;
862 SmmS3ResumeState
->SmmS3StackBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN
)SmmS3ResumeState
->SmmS3StackSize
));
863 if (SmmS3ResumeState
->SmmS3StackBase
== 0) {
864 SmmS3ResumeState
->SmmS3StackSize
= 0;
867 SmmS3ResumeState
->SmmS3Cr0
= mSmmCr0
;
868 SmmS3ResumeState
->SmmS3Cr3
= Cr3
;
869 SmmS3ResumeState
->SmmS3Cr4
= mSmmCr4
;
871 if (sizeof (UINTN
) == sizeof (UINT64
)) {
872 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_64
;
874 if (sizeof (UINTN
) == sizeof (UINT32
)) {
875 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_32
;
879 // Patch SmmS3ResumeState->SmmS3Cr3
885 // Allocate safe memory in ACPI NVS for AP to execute hlt loop in
886 // protected mode on S3 path
888 Address
= BASE_4GB
- 1;
889 Status
= gBS
->AllocatePages (
892 EFI_SIZE_TO_PAGES (sizeof (mApHltLoopCodeTemplate
)),
895 ASSERT_EFI_ERROR (Status
);
896 mApHltLoopCode
= (UINT8
*) (UINTN
) Address
;
900 Copy register table from ACPI NVS memory into SMRAM.
902 @param[in] DestinationRegisterTableList Points to destination register table.
903 @param[in] SourceRegisterTableList Points to source register table.
904 @param[in] NumberOfCpus Number of CPUs.
909 IN CPU_REGISTER_TABLE
*DestinationRegisterTableList
,
910 IN CPU_REGISTER_TABLE
*SourceRegisterTableList
,
911 IN UINT32 NumberOfCpus
915 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
917 CopyMem (DestinationRegisterTableList
, SourceRegisterTableList
, NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
918 for (Index
= 0; Index
< NumberOfCpus
; Index
++) {
919 if (DestinationRegisterTableList
[Index
].AllocatedSize
!= 0) {
920 RegisterTableEntry
= AllocateCopyPool (
921 DestinationRegisterTableList
[Index
].AllocatedSize
,
922 (VOID
*)(UINTN
)SourceRegisterTableList
[Index
].RegisterTableEntry
924 ASSERT (RegisterTableEntry
!= NULL
);
925 DestinationRegisterTableList
[Index
].RegisterTableEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RegisterTableEntry
;
939 ACPI_CPU_DATA
*AcpiCpuData
;
940 IA32_DESCRIPTOR
*Gdtr
;
941 IA32_DESCRIPTOR
*Idtr
;
944 VOID
*MachineCheckHandlerForAp
;
945 CPU_STATUS_INFORMATION
*CpuStatus
;
947 if (!mAcpiS3Enable
) {
952 // Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0
954 mAcpiCpuData
.NumberOfCpus
= 0;
957 // If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM
959 AcpiCpuData
= (ACPI_CPU_DATA
*)(UINTN
)PcdGet64 (PcdCpuS3DataAddress
);
960 if (AcpiCpuData
== 0) {
965 // For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.
967 CopyMem (&mAcpiCpuData
, AcpiCpuData
, sizeof (mAcpiCpuData
));
969 mAcpiCpuData
.MtrrTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (MTRR_SETTINGS
));
970 ASSERT (mAcpiCpuData
.MtrrTable
!= 0);
972 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.MtrrTable
, (VOID
*)(UINTN
)AcpiCpuData
->MtrrTable
, sizeof (MTRR_SETTINGS
));
974 mAcpiCpuData
.GdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
975 ASSERT (mAcpiCpuData
.GdtrProfile
!= 0);
977 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.GdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
979 mAcpiCpuData
.IdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
980 ASSERT (mAcpiCpuData
.IdtrProfile
!= 0);
982 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.IdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
984 mAcpiCpuData
.PreSmmInitRegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
985 ASSERT (mAcpiCpuData
.PreSmmInitRegisterTable
!= 0);
988 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
,
989 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->PreSmmInitRegisterTable
,
990 mAcpiCpuData
.NumberOfCpus
993 mAcpiCpuData
.RegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
994 ASSERT (mAcpiCpuData
.RegisterTable
!= 0);
997 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
,
998 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->RegisterTable
,
999 mAcpiCpuData
.NumberOfCpus
1003 // Copy AP's GDT, IDT and Machine Check handler into SMRAM.
1005 Gdtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.GdtrProfile
;
1006 Idtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.IdtrProfile
;
1008 GdtForAp
= AllocatePool ((Gdtr
->Limit
+ 1) + (Idtr
->Limit
+ 1) + mAcpiCpuData
.ApMachineCheckHandlerSize
);
1009 ASSERT (GdtForAp
!= NULL
);
1010 IdtForAp
= (VOID
*) ((UINTN
)GdtForAp
+ (Gdtr
->Limit
+ 1));
1011 MachineCheckHandlerForAp
= (VOID
*) ((UINTN
)IdtForAp
+ (Idtr
->Limit
+ 1));
1013 CopyMem (GdtForAp
, (VOID
*)Gdtr
->Base
, Gdtr
->Limit
+ 1);
1014 CopyMem (IdtForAp
, (VOID
*)Idtr
->Base
, Idtr
->Limit
+ 1);
1015 CopyMem (MachineCheckHandlerForAp
, (VOID
*)(UINTN
)mAcpiCpuData
.ApMachineCheckHandlerBase
, mAcpiCpuData
.ApMachineCheckHandlerSize
);
1017 Gdtr
->Base
= (UINTN
)GdtForAp
;
1018 Idtr
->Base
= (UINTN
)IdtForAp
;
1019 mAcpiCpuData
.ApMachineCheckHandlerBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MachineCheckHandlerForAp
;
1021 CpuStatus
= &mAcpiCpuData
.CpuStatus
;
1022 CopyMem (CpuStatus
, &AcpiCpuData
->CpuStatus
, sizeof (CPU_STATUS_INFORMATION
));
1023 if (AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
!= 0) {
1024 CpuStatus
->ValidCoreCountPerPackage
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1025 sizeof (UINT32
) * CpuStatus
->PackageCount
,
1026 (UINT32
*)(UINTN
)AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
1028 ASSERT (CpuStatus
->ValidCoreCountPerPackage
!= 0);
1030 if (AcpiCpuData
->ApLocation
!= 0) {
1031 mAcpiCpuData
.ApLocation
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1032 mAcpiCpuData
.NumberOfCpus
* sizeof (EFI_CPU_PHYSICAL_LOCATION
),
1033 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)AcpiCpuData
->ApLocation
1035 ASSERT (mAcpiCpuData
.ApLocation
!= 0);
1037 if (CpuStatus
->PackageCount
!= 0) {
1038 mCpuFlags
.CoreSemaphoreCount
= AllocateZeroPool (
1039 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1040 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1042 ASSERT (mCpuFlags
.CoreSemaphoreCount
!= NULL
);
1043 mCpuFlags
.PackageSemaphoreCount
= AllocateZeroPool (
1044 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1045 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1047 ASSERT (mCpuFlags
.PackageSemaphoreCount
!= NULL
);
1049 InitializeSpinLock((SPIN_LOCK
*) &mCpuFlags
.MemoryMappedLock
);
1053 Get ACPI S3 enable flag.
1057 GetAcpiS3EnableFlag (
1061 mAcpiS3Enable
= PcdGetBool (PcdAcpiS3Enable
);