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 Initialize the CPU registers from a register table.
165 @param[in] RegisterTable The register table for this AP.
166 @param[in] ApLocation AP location info for this ap.
167 @param[in] CpuStatus CPU status info for this CPU.
168 @param[in] CpuFlags Flags data structure used when program the register.
170 @note This service could be called by BSP/APs.
173 ProgramProcessorRegister (
174 IN CPU_REGISTER_TABLE
*RegisterTable
,
175 IN EFI_CPU_PHYSICAL_LOCATION
*ApLocation
,
176 IN CPU_STATUS_INFORMATION
*CpuStatus
,
177 IN PROGRAM_CPU_REGISTER_FLAGS
*CpuFlags
180 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
183 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntryHead
;
184 volatile UINT32
*SemaphorePtr
;
186 UINT32 PackageThreadsCount
;
187 UINT32 CurrentThread
;
188 UINTN ProcessorIndex
;
189 UINTN ValidThreadCount
;
190 UINT32
*ValidCoreCountPerPackage
;
193 // Traverse Register Table of this logical processor
195 RegisterTableEntryHead
= (CPU_REGISTER_TABLE_ENTRY
*) (UINTN
) RegisterTable
->RegisterTableEntry
;
197 for (Index
= 0; Index
< RegisterTable
->TableLength
; Index
++) {
199 RegisterTableEntry
= &RegisterTableEntryHead
[Index
];
202 // Check the type of specified register
204 switch (RegisterTableEntry
->RegisterType
) {
206 // The specified register is Control Register
208 case ControlRegister
:
209 switch (RegisterTableEntry
->Index
) {
211 Value
= AsmReadCr0 ();
212 Value
= (UINTN
) BitFieldWrite64 (
214 RegisterTableEntry
->ValidBitStart
,
215 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
216 (UINTN
) RegisterTableEntry
->Value
221 Value
= AsmReadCr2 ();
222 Value
= (UINTN
) BitFieldWrite64 (
224 RegisterTableEntry
->ValidBitStart
,
225 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
226 (UINTN
) RegisterTableEntry
->Value
231 Value
= AsmReadCr3 ();
232 Value
= (UINTN
) BitFieldWrite64 (
234 RegisterTableEntry
->ValidBitStart
,
235 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
236 (UINTN
) RegisterTableEntry
->Value
241 Value
= AsmReadCr4 ();
242 Value
= (UINTN
) BitFieldWrite64 (
244 RegisterTableEntry
->ValidBitStart
,
245 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
246 (UINTN
) RegisterTableEntry
->Value
255 // The specified register is Model Specific Register
259 // If this function is called to restore register setting after INIT signal,
260 // there is no need to restore MSRs in register table.
262 if (RegisterTableEntry
->ValidBitLength
>= 64) {
264 // If length is not less than 64 bits, then directly write without reading
267 RegisterTableEntry
->Index
,
268 RegisterTableEntry
->Value
272 // Set the bit section according to bit start and length
274 AsmMsrBitFieldWrite64 (
275 RegisterTableEntry
->Index
,
276 RegisterTableEntry
->ValidBitStart
,
277 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
278 RegisterTableEntry
->Value
283 // MemoryMapped operations
286 AcquireSpinLock (&CpuFlags
->MemoryMappedLock
);
287 MmioBitFieldWrite32 (
288 (UINTN
)(RegisterTableEntry
->Index
| LShiftU64 (RegisterTableEntry
->HighIndex
, 32)),
289 RegisterTableEntry
->ValidBitStart
,
290 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
291 (UINT32
)RegisterTableEntry
->Value
293 ReleaseSpinLock (&CpuFlags
->MemoryMappedLock
);
296 // Enable or disable cache
300 // If value of the entry is 0, then disable cache. Otherwise, enable cache.
302 if (RegisterTableEntry
->Value
== 0) {
310 // Semaphore works logic like below:
312 // V(x) = LibReleaseSemaphore (Semaphore[FirstThread + x]);
313 // P(x) = LibWaitForSemaphore (Semaphore[FirstThread + x]);
315 // All threads (T0...Tn) waits in P() line and continues running
321 // V(0...n) V(0...n) ... V(0...n)
322 // n * P(0) n * P(1) ... n * P(n)
325 (ApLocation
!= NULL
) &&
326 (CpuStatus
->ValidCoreCountPerPackage
!= 0) &&
327 (CpuFlags
->CoreSemaphoreCount
!= NULL
) &&
328 (CpuFlags
->PackageSemaphoreCount
!= NULL
)
330 switch (RegisterTableEntry
->Value
) {
332 SemaphorePtr
= CpuFlags
->CoreSemaphoreCount
;
334 // Get Offset info for the first thread in the core which current thread belongs to.
336 FirstThread
= (ApLocation
->Package
* CpuStatus
->MaxCoreCount
+ ApLocation
->Core
) * CpuStatus
->MaxThreadCount
;
337 CurrentThread
= FirstThread
+ ApLocation
->Thread
;
339 // First Notify all threads in current Core that this thread has ready.
341 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
342 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
345 // Second, check whether all valid threads in current core have ready.
347 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
348 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
353 SemaphorePtr
= CpuFlags
->PackageSemaphoreCount
;
354 ValidCoreCountPerPackage
= (UINT32
*)(UINTN
)CpuStatus
->ValidCoreCountPerPackage
;
356 // Get Offset info for the first thread in the package which current thread belongs to.
358 FirstThread
= ApLocation
->Package
* CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
;
360 // Get the possible threads count for current package.
362 PackageThreadsCount
= CpuStatus
->MaxThreadCount
* CpuStatus
->MaxCoreCount
;
363 CurrentThread
= FirstThread
+ CpuStatus
->MaxThreadCount
* ApLocation
->Core
+ ApLocation
->Thread
;
365 // Get the valid thread count for current package.
367 ValidThreadCount
= CpuStatus
->MaxThreadCount
* ValidCoreCountPerPackage
[ApLocation
->Package
];
370 // Different packages may have different valid cores in them. If driver maintail clearly
371 // cores number in different packages, the logic will be much complicated.
372 // Here driver just simply records the max core number in all packages and use it as expect
373 // core number for all packages.
374 // In below two steps logic, first current thread will Release semaphore for each thread
375 // in current package. Maybe some threads are not valid in this package, but driver don't
376 // care. Second, driver will let current thread wait semaphore for all valid threads in
377 // current package. Because only the valid threads will do release semaphore for this
378 // thread, driver here only need to wait the valid thread count.
382 // First Notify all threads in current package that this thread has ready.
384 for (ProcessorIndex
= 0; ProcessorIndex
< PackageThreadsCount
; ProcessorIndex
++) {
385 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
388 // Second, check whether all valid threads in current package have ready.
390 for (ProcessorIndex
= 0; ProcessorIndex
< ValidThreadCount
; ProcessorIndex
++) {
391 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
408 Set Processor register for one AP.
410 @param PreSmmRegisterTable Use pre Smm register table or register table.
415 IN BOOLEAN PreSmmRegisterTable
418 CPU_REGISTER_TABLE
*RegisterTable
;
419 CPU_REGISTER_TABLE
*RegisterTables
;
424 if (PreSmmRegisterTable
) {
425 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
;
427 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
;
430 InitApicId
= GetInitialApicId ();
431 RegisterTable
= NULL
;
432 ProcIndex
= (UINTN
)-1;
433 for (Index
= 0; Index
< mAcpiCpuData
.NumberOfCpus
; Index
++) {
434 if (RegisterTables
[Index
].InitialApicId
== InitApicId
) {
435 RegisterTable
= &RegisterTables
[Index
];
440 ASSERT (RegisterTable
!= NULL
);
442 if (mAcpiCpuData
.ApLocation
!= 0) {
443 ProgramProcessorRegister (
445 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)mAcpiCpuData
.ApLocation
+ ProcIndex
,
446 &mAcpiCpuData
.CpuStatus
,
450 ProgramProcessorRegister (
453 &mAcpiCpuData
.CpuStatus
,
460 AP initialization before then after SMBASE relocation in the S3 boot path.
470 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
475 // Count down the number with lock mechanism.
477 InterlockedDecrement (&mNumberToFinish
);
480 // Wait for BSP to signal SMM Base relocation done.
482 while (!mInitApsAfterSmmBaseReloc
) {
486 ProgramVirtualWireMode ();
487 DisableLvtInterrupts ();
492 // Place AP into the safe code, count down the number with lock mechanism in the safe code.
494 TopOfStack
= (UINTN
) Stack
+ sizeof (Stack
);
495 TopOfStack
&= ~(UINTN
) (CPU_STACK_ALIGNMENT
- 1);
496 CopyMem ((VOID
*) (UINTN
) mApHltLoopCode
, mApHltLoopCodeTemplate
, sizeof (mApHltLoopCodeTemplate
));
497 TransferApToSafeState ((UINTN
)mApHltLoopCode
, TopOfStack
, (UINTN
)&mNumberToFinish
);
501 Prepares startup vector for APs.
503 This function prepares startup vector for APs.
505 @param WorkingBuffer The address of the work buffer.
508 PrepareApStartupVector (
509 EFI_PHYSICAL_ADDRESS WorkingBuffer
512 EFI_PHYSICAL_ADDRESS StartupVector
;
513 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
516 // Get the address map of startup code for AP,
517 // including code size, and offset of long jump instructions to redirect.
519 ZeroMem (&AddressMap
, sizeof (AddressMap
));
520 AsmGetAddressMap (&AddressMap
);
522 StartupVector
= WorkingBuffer
;
525 // Copy AP startup code to startup vector, and then redirect the long jump
526 // instructions for mode switching.
528 CopyMem ((VOID
*) (UINTN
) StartupVector
, AddressMap
.RendezvousFunnelAddress
, AddressMap
.Size
);
529 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.FlatJumpOffset
+ 3) = (UINT32
) (StartupVector
+ AddressMap
.PModeEntryOffset
);
530 if (AddressMap
.LongJumpOffset
!= 0) {
531 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.LongJumpOffset
+ 2) = (UINT32
) (StartupVector
+ AddressMap
.LModeEntryOffset
);
535 // Get the start address of exchange data between BSP and AP.
537 mExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (StartupVector
+ AddressMap
.Size
);
538 ZeroMem ((VOID
*) mExchangeInfo
, sizeof (MP_CPU_EXCHANGE_INFO
));
540 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->GdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
541 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->IdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
543 mExchangeInfo
->StackStart
= (VOID
*) (UINTN
) mAcpiCpuData
.StackAddress
;
544 mExchangeInfo
->StackSize
= mAcpiCpuData
.StackSize
;
545 mExchangeInfo
->BufferStart
= (UINT32
) StartupVector
;
546 mExchangeInfo
->Cr3
= (UINT32
) (AsmReadCr3 ());
547 mExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
551 The function is invoked before SMBASE relocation in S3 path to restores CPU status.
553 The function is invoked before SMBASE relocation in S3 path. It does first time microcode load
554 and restores MTRRs for both BSP and APs.
558 InitializeCpuBeforeRebase (
562 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
566 ProgramVirtualWireMode ();
568 PrepareApStartupVector (mAcpiCpuData
.StartupVector
);
570 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
571 mExchangeInfo
->ApFunction
= (VOID
*) (UINTN
) InitializeAp
;
574 // Execute code for before SmmBaseReloc. Note: This flag is maintained across S3 boots.
576 mInitApsAfterSmmBaseReloc
= FALSE
;
579 // Send INIT IPI - SIPI to all APs
581 SendInitSipiSipiAllExcludingSelf ((UINT32
)mAcpiCpuData
.StartupVector
);
583 while (mNumberToFinish
> 0) {
589 The function is invoked after SMBASE relocation in S3 path to restores CPU status.
591 The function is invoked after SMBASE relocation in S3 path. It restores configuration according to
592 data saved by normal boot path for both BSP and APs.
596 InitializeCpuAfterRebase (
600 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
603 // Signal that SMM base relocation is complete and to continue initialization for all APs.
605 mInitApsAfterSmmBaseReloc
= TRUE
;
608 // Must begin set register after all APs have continue their initialization.
609 // This is a requirement to support semaphore mechanism in register table.
610 // Because if semaphore's dependence type is package type, semaphore will wait
611 // for all Aps in one package finishing their tasks before set next register
612 // for all APs. If the Aps not begin its task during BSP doing its task, the
613 // BSP thread will hang because it is waiting for other Aps in the same
614 // package finishing their task.
618 while (mNumberToFinish
> 0) {
624 Restore SMM Configuration in S3 boot path.
628 RestoreSmmConfigurationInS3 (
632 if (!mAcpiS3Enable
) {
637 // Restore SMM Configuration in S3 boot path.
639 if (mRestoreSmmConfigurationInS3
) {
641 // Need make sure gSmst is correct because below function may use them.
643 gSmst
->SmmStartupThisAp
= gSmmCpuPrivate
->SmmCoreEntryContext
.SmmStartupThisAp
;
644 gSmst
->CurrentlyExecutingCpu
= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
;
645 gSmst
->NumberOfCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
646 gSmst
->CpuSaveStateSize
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveStateSize
;
647 gSmst
->CpuSaveState
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveState
;
650 // Configure SMM Code Access Check feature if available.
652 ConfigSmmCodeAccessCheck ();
654 SmmCpuFeaturesCompleteSmmReadyToLock ();
656 mRestoreSmmConfigurationInS3
= FALSE
;
661 Perform SMM initialization for all processors in the S3 boot path.
663 For a native platform, MP initialization in the S3 boot path is also performed in this function.
671 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
672 IA32_DESCRIPTOR Ia32Idtr
;
673 IA32_DESCRIPTOR X64Idtr
;
674 IA32_IDT_GATE_DESCRIPTOR IdtEntryTable
[EXCEPTION_VECTOR_NUMBER
];
677 DEBUG ((EFI_D_INFO
, "SmmRestoreCpu()\n"));
682 // See if there is enough context to resume PEI Phase
684 if (mSmmS3ResumeState
== NULL
) {
685 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
689 SmmS3ResumeState
= mSmmS3ResumeState
;
690 ASSERT (SmmS3ResumeState
!= NULL
);
692 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
694 // Save the IA32 IDT Descriptor
696 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
699 // Setup X64 IDT table
701 ZeroMem (IdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32);
702 X64Idtr
.Base
= (UINTN
) IdtEntryTable
;
703 X64Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32 - 1);
704 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &X64Idtr
);
707 // Setup the default exception handler
709 Status
= InitializeCpuExceptionHandlers (NULL
);
710 ASSERT_EFI_ERROR (Status
);
713 // Initialize Debug Agent to support source level debug
715 InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
, (VOID
*)&Ia32Idtr
, NULL
);
719 // Skip initialization if mAcpiCpuData is not valid
721 if (mAcpiCpuData
.NumberOfCpus
> 0) {
723 // First time microcode load and restore MTRRs
725 InitializeCpuBeforeRebase ();
729 // Restore SMBASE for BSP and all APs
734 // Skip initialization if mAcpiCpuData is not valid
736 if (mAcpiCpuData
.NumberOfCpus
> 0) {
738 // Restore MSRs for BSP and all APs
740 InitializeCpuAfterRebase ();
744 // Set a flag to restore SMM configuration in S3 path.
746 mRestoreSmmConfigurationInS3
= TRUE
;
748 DEBUG (( EFI_D_INFO
, "SMM S3 Return CS = %x\n", SmmS3ResumeState
->ReturnCs
));
749 DEBUG (( EFI_D_INFO
, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState
->ReturnEntryPoint
));
750 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState
->ReturnContext1
));
751 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState
->ReturnContext2
));
752 DEBUG (( EFI_D_INFO
, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState
->ReturnStackPointer
));
755 // If SMM is in 32-bit mode, then use SwitchStack() to resume PEI Phase
757 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_32
) {
758 DEBUG ((EFI_D_INFO
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
761 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)SmmS3ResumeState
->ReturnEntryPoint
,
762 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext1
,
763 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext2
,
764 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnStackPointer
769 // If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase
771 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
772 DEBUG ((EFI_D_INFO
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
774 // Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.
776 SaveAndSetDebugTimerInterrupt (FALSE
);
778 // Restore IA32 IDT table
780 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
782 SmmS3ResumeState
->ReturnCs
,
783 (UINT32
)SmmS3ResumeState
->ReturnEntryPoint
,
784 (UINT32
)SmmS3ResumeState
->ReturnContext1
,
785 (UINT32
)SmmS3ResumeState
->ReturnContext2
,
786 (UINT32
)SmmS3ResumeState
->ReturnStackPointer
791 // Can not resume PEI Phase
793 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
798 Initialize SMM S3 resume state structure used during S3 Resume.
800 @param[in] Cr3 The base address of the page tables to use in SMM.
804 InitSmmS3ResumeState (
809 EFI_SMRAM_DESCRIPTOR
*SmramDescriptor
;
810 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
811 EFI_PHYSICAL_ADDRESS Address
;
814 if (!mAcpiS3Enable
) {
818 GuidHob
= GetFirstGuidHob (&gEfiAcpiVariableGuid
);
819 if (GuidHob
== NULL
) {
822 "ERROR:%a(): HOB(gEfiAcpiVariableGuid=%g) needed by S3 resume doesn't exist!\n",
824 &gEfiAcpiVariableGuid
828 SmramDescriptor
= (EFI_SMRAM_DESCRIPTOR
*) GET_GUID_HOB_DATA (GuidHob
);
830 DEBUG ((EFI_D_INFO
, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor
));
831 DEBUG ((EFI_D_INFO
, "SMM S3 Structure = %x\n", SmramDescriptor
->CpuStart
));
833 SmmS3ResumeState
= (SMM_S3_RESUME_STATE
*)(UINTN
)SmramDescriptor
->CpuStart
;
834 ZeroMem (SmmS3ResumeState
, sizeof (SMM_S3_RESUME_STATE
));
836 mSmmS3ResumeState
= SmmS3ResumeState
;
837 SmmS3ResumeState
->Smst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gSmst
;
839 SmmS3ResumeState
->SmmS3ResumeEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmRestoreCpu
;
841 SmmS3ResumeState
->SmmS3StackSize
= SIZE_32KB
;
842 SmmS3ResumeState
->SmmS3StackBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN
)SmmS3ResumeState
->SmmS3StackSize
));
843 if (SmmS3ResumeState
->SmmS3StackBase
== 0) {
844 SmmS3ResumeState
->SmmS3StackSize
= 0;
847 SmmS3ResumeState
->SmmS3Cr0
= mSmmCr0
;
848 SmmS3ResumeState
->SmmS3Cr3
= Cr3
;
849 SmmS3ResumeState
->SmmS3Cr4
= mSmmCr4
;
851 if (sizeof (UINTN
) == sizeof (UINT64
)) {
852 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_64
;
854 if (sizeof (UINTN
) == sizeof (UINT32
)) {
855 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_32
;
859 // Patch SmmS3ResumeState->SmmS3Cr3
865 // Allocate safe memory in ACPI NVS for AP to execute hlt loop in
866 // protected mode on S3 path
868 Address
= BASE_4GB
- 1;
869 Status
= gBS
->AllocatePages (
872 EFI_SIZE_TO_PAGES (sizeof (mApHltLoopCodeTemplate
)),
875 ASSERT_EFI_ERROR (Status
);
876 mApHltLoopCode
= (UINT8
*) (UINTN
) Address
;
880 Copy register table from ACPI NVS memory into SMRAM.
882 @param[in] DestinationRegisterTableList Points to destination register table.
883 @param[in] SourceRegisterTableList Points to source register table.
884 @param[in] NumberOfCpus Number of CPUs.
889 IN CPU_REGISTER_TABLE
*DestinationRegisterTableList
,
890 IN CPU_REGISTER_TABLE
*SourceRegisterTableList
,
891 IN UINT32 NumberOfCpus
895 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
897 CopyMem (DestinationRegisterTableList
, SourceRegisterTableList
, NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
898 for (Index
= 0; Index
< NumberOfCpus
; Index
++) {
899 if (DestinationRegisterTableList
[Index
].AllocatedSize
!= 0) {
900 RegisterTableEntry
= AllocateCopyPool (
901 DestinationRegisterTableList
[Index
].AllocatedSize
,
902 (VOID
*)(UINTN
)SourceRegisterTableList
[Index
].RegisterTableEntry
904 ASSERT (RegisterTableEntry
!= NULL
);
905 DestinationRegisterTableList
[Index
].RegisterTableEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RegisterTableEntry
;
919 ACPI_CPU_DATA
*AcpiCpuData
;
920 IA32_DESCRIPTOR
*Gdtr
;
921 IA32_DESCRIPTOR
*Idtr
;
924 VOID
*MachineCheckHandlerForAp
;
925 CPU_STATUS_INFORMATION
*CpuStatus
;
927 if (!mAcpiS3Enable
) {
932 // Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0
934 mAcpiCpuData
.NumberOfCpus
= 0;
937 // If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM
939 AcpiCpuData
= (ACPI_CPU_DATA
*)(UINTN
)PcdGet64 (PcdCpuS3DataAddress
);
940 if (AcpiCpuData
== 0) {
945 // For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.
947 CopyMem (&mAcpiCpuData
, AcpiCpuData
, sizeof (mAcpiCpuData
));
949 mAcpiCpuData
.MtrrTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (MTRR_SETTINGS
));
950 ASSERT (mAcpiCpuData
.MtrrTable
!= 0);
952 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.MtrrTable
, (VOID
*)(UINTN
)AcpiCpuData
->MtrrTable
, sizeof (MTRR_SETTINGS
));
954 mAcpiCpuData
.GdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
955 ASSERT (mAcpiCpuData
.GdtrProfile
!= 0);
957 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.GdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
959 mAcpiCpuData
.IdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
960 ASSERT (mAcpiCpuData
.IdtrProfile
!= 0);
962 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.IdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
964 mAcpiCpuData
.PreSmmInitRegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
965 ASSERT (mAcpiCpuData
.PreSmmInitRegisterTable
!= 0);
968 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
,
969 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->PreSmmInitRegisterTable
,
970 mAcpiCpuData
.NumberOfCpus
973 mAcpiCpuData
.RegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
974 ASSERT (mAcpiCpuData
.RegisterTable
!= 0);
977 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
,
978 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->RegisterTable
,
979 mAcpiCpuData
.NumberOfCpus
983 // Copy AP's GDT, IDT and Machine Check handler into SMRAM.
985 Gdtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.GdtrProfile
;
986 Idtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.IdtrProfile
;
988 GdtForAp
= AllocatePool ((Gdtr
->Limit
+ 1) + (Idtr
->Limit
+ 1) + mAcpiCpuData
.ApMachineCheckHandlerSize
);
989 ASSERT (GdtForAp
!= NULL
);
990 IdtForAp
= (VOID
*) ((UINTN
)GdtForAp
+ (Gdtr
->Limit
+ 1));
991 MachineCheckHandlerForAp
= (VOID
*) ((UINTN
)IdtForAp
+ (Idtr
->Limit
+ 1));
993 CopyMem (GdtForAp
, (VOID
*)Gdtr
->Base
, Gdtr
->Limit
+ 1);
994 CopyMem (IdtForAp
, (VOID
*)Idtr
->Base
, Idtr
->Limit
+ 1);
995 CopyMem (MachineCheckHandlerForAp
, (VOID
*)(UINTN
)mAcpiCpuData
.ApMachineCheckHandlerBase
, mAcpiCpuData
.ApMachineCheckHandlerSize
);
997 Gdtr
->Base
= (UINTN
)GdtForAp
;
998 Idtr
->Base
= (UINTN
)IdtForAp
;
999 mAcpiCpuData
.ApMachineCheckHandlerBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MachineCheckHandlerForAp
;
1001 CpuStatus
= &mAcpiCpuData
.CpuStatus
;
1002 CopyMem (CpuStatus
, &AcpiCpuData
->CpuStatus
, sizeof (CPU_STATUS_INFORMATION
));
1003 if (AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
!= 0) {
1004 CpuStatus
->ValidCoreCountPerPackage
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1005 sizeof (UINT32
) * CpuStatus
->PackageCount
,
1006 (UINT32
*)(UINTN
)AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
1008 ASSERT (CpuStatus
->ValidCoreCountPerPackage
!= 0);
1010 if (AcpiCpuData
->ApLocation
!= 0) {
1011 mAcpiCpuData
.ApLocation
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1012 mAcpiCpuData
.NumberOfCpus
* sizeof (EFI_CPU_PHYSICAL_LOCATION
),
1013 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)AcpiCpuData
->ApLocation
1015 ASSERT (mAcpiCpuData
.ApLocation
!= 0);
1017 if (CpuStatus
->PackageCount
!= 0) {
1018 mCpuFlags
.CoreSemaphoreCount
= AllocateZeroPool (
1019 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1020 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1022 ASSERT (mCpuFlags
.CoreSemaphoreCount
!= NULL
);
1023 mCpuFlags
.PackageSemaphoreCount
= AllocateZeroPool (
1024 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1025 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1027 ASSERT (mCpuFlags
.PackageSemaphoreCount
!= NULL
);
1029 InitializeSpinLock((SPIN_LOCK
*) &mCpuFlags
.MemoryMappedLock
);
1033 Get ACPI S3 enable flag.
1037 GetAcpiS3EnableFlag (
1041 mAcpiS3Enable
= PcdGetBool (PcdAcpiS3Enable
);