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 ConsoleLogLock
; // Spinlock used to control console.
39 volatile UINTN MemoryMappedLock
; // Spinlock used to program mmio
40 volatile UINT32
*CoreSemaphoreCount
; // Semaphore container used to program
41 // core level semaphore.
42 volatile UINT32
*PackageSemaphoreCount
; // Semaphore container used to program
43 // package level semaphore.
44 } PROGRAM_CPU_REGISTER_FLAGS
;
47 // Signal that SMM BASE relocation is complete.
49 volatile BOOLEAN mInitApsAfterSmmBaseReloc
;
52 Get starting address and size of the rendezvous entry for APs.
53 Information for fixing a jump instruction in the code is also returned.
55 @param AddressMap Output buffer for address map information.
60 MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
63 #define LEGACY_REGION_SIZE (2 * 0x1000)
64 #define LEGACY_REGION_BASE (0xA0000 - LEGACY_REGION_SIZE)
66 PROGRAM_CPU_REGISTER_FLAGS mCpuFlags
;
67 ACPI_CPU_DATA mAcpiCpuData
;
68 volatile UINT32 mNumberToFinish
;
69 MP_CPU_EXCHANGE_INFO
*mExchangeInfo
;
70 BOOLEAN mRestoreSmmConfigurationInS3
= FALSE
;
75 BOOLEAN mSmmS3Flag
= FALSE
;
78 // Pointer to structure used during S3 Resume
80 SMM_S3_RESUME_STATE
*mSmmS3ResumeState
= NULL
;
82 BOOLEAN mAcpiS3Enable
= TRUE
;
84 UINT8
*mApHltLoopCode
= NULL
;
85 UINT8 mApHltLoopCodeTemplate
[] = {
86 0x8B, 0x44, 0x24, 0x04, // mov eax, dword ptr [esp+4]
87 0xF0, 0xFF, 0x08, // lock dec dword ptr [eax]
94 Sync up the MTRR values for all processors.
96 @param MtrrTable Table holding fixed/variable MTRR values to be loaded.
101 EFI_PHYSICAL_ADDRESS MtrrTable
107 Sync up the MTRR values for all processors.
116 MTRR_SETTINGS
*MtrrSettings
;
118 MtrrSettings
= (MTRR_SETTINGS
*) (UINTN
) MtrrTable
;
119 MtrrSetAllMtrrs (MtrrSettings
);
123 Increment semaphore by 1.
125 @param Sem IN: 32-bit unsigned integer
130 IN OUT
volatile UINT32
*Sem
133 InterlockedIncrement (Sem
);
137 Decrement the semaphore by 1 if it is not zero.
139 Performs an atomic decrement operation for semaphore.
140 The compare exchange operation must be performed using
143 @param Sem IN: 32-bit unsigned integer
148 IN OUT
volatile UINT32
*Sem
155 } while (Value
== 0 ||
156 InterlockedCompareExchange32 (
164 Initialize the CPU registers from a register table.
166 @param[in] RegisterTable The register table for this AP.
167 @param[in] ApLocation AP location info for this ap.
168 @param[in] CpuStatus CPU status info for this CPU.
169 @param[in] CpuFlags Flags data structure used when program the register.
171 @note This service could be called by BSP/APs.
174 ProgramProcessorRegister (
175 IN CPU_REGISTER_TABLE
*RegisterTable
,
176 IN EFI_CPU_PHYSICAL_LOCATION
*ApLocation
,
177 IN CPU_STATUS_INFORMATION
*CpuStatus
,
178 IN PROGRAM_CPU_REGISTER_FLAGS
*CpuFlags
181 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
184 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntryHead
;
185 volatile UINT32
*SemaphorePtr
;
187 UINT32 PackageThreadsCount
;
188 UINT32 CurrentThread
;
189 UINTN ProcessorIndex
;
190 UINTN ValidThreadCount
;
191 UINT32
*ValidCoreCountPerPackage
;
194 // Traverse Register Table of this logical processor
196 RegisterTableEntryHead
= (CPU_REGISTER_TABLE_ENTRY
*) (UINTN
) RegisterTable
->RegisterTableEntry
;
198 for (Index
= 0; Index
< RegisterTable
->TableLength
; Index
++) {
200 RegisterTableEntry
= &RegisterTableEntryHead
[Index
];
203 // Check the type of specified register
205 switch (RegisterTableEntry
->RegisterType
) {
207 // The specified register is Control Register
209 case ControlRegister
:
210 switch (RegisterTableEntry
->Index
) {
212 Value
= AsmReadCr0 ();
213 Value
= (UINTN
) BitFieldWrite64 (
215 RegisterTableEntry
->ValidBitStart
,
216 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
217 (UINTN
) RegisterTableEntry
->Value
222 Value
= AsmReadCr2 ();
223 Value
= (UINTN
) BitFieldWrite64 (
225 RegisterTableEntry
->ValidBitStart
,
226 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
227 (UINTN
) RegisterTableEntry
->Value
232 Value
= AsmReadCr3 ();
233 Value
= (UINTN
) BitFieldWrite64 (
235 RegisterTableEntry
->ValidBitStart
,
236 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
237 (UINTN
) RegisterTableEntry
->Value
242 Value
= AsmReadCr4 ();
243 Value
= (UINTN
) BitFieldWrite64 (
245 RegisterTableEntry
->ValidBitStart
,
246 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
247 (UINTN
) RegisterTableEntry
->Value
256 // The specified register is Model Specific Register
260 // If this function is called to restore register setting after INIT signal,
261 // there is no need to restore MSRs in register table.
263 if (RegisterTableEntry
->ValidBitLength
>= 64) {
265 // If length is not less than 64 bits, then directly write without reading
268 RegisterTableEntry
->Index
,
269 RegisterTableEntry
->Value
273 // Set the bit section according to bit start and length
275 AsmMsrBitFieldWrite64 (
276 RegisterTableEntry
->Index
,
277 RegisterTableEntry
->ValidBitStart
,
278 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
279 RegisterTableEntry
->Value
284 // MemoryMapped operations
287 AcquireSpinLock (&CpuFlags
->MemoryMappedLock
);
288 MmioBitFieldWrite32 (
289 (UINTN
)(RegisterTableEntry
->Index
| LShiftU64 (RegisterTableEntry
->HighIndex
, 32)),
290 RegisterTableEntry
->ValidBitStart
,
291 RegisterTableEntry
->ValidBitStart
+ RegisterTableEntry
->ValidBitLength
- 1,
292 (UINT32
)RegisterTableEntry
->Value
294 ReleaseSpinLock (&CpuFlags
->MemoryMappedLock
);
297 // Enable or disable cache
301 // If value of the entry is 0, then disable cache. Otherwise, enable cache.
303 if (RegisterTableEntry
->Value
== 0) {
311 // Semaphore works logic like below:
313 // V(x) = LibReleaseSemaphore (Semaphore[FirstThread + x]);
314 // P(x) = LibWaitForSemaphore (Semaphore[FirstThread + x]);
316 // All threads (T0...Tn) waits in P() line and continues running
322 // V(0...n) V(0...n) ... V(0...n)
323 // n * P(0) n * P(1) ... n * P(n)
326 (ApLocation
!= NULL
) &&
327 (CpuStatus
->ValidCoreCountPerPackage
!= 0) &&
328 (CpuFlags
->CoreSemaphoreCount
!= NULL
) &&
329 (CpuFlags
->PackageSemaphoreCount
!= NULL
)
331 switch (RegisterTableEntry
->Value
) {
333 SemaphorePtr
= CpuFlags
->CoreSemaphoreCount
;
335 // Get Offset info for the first thread in the core which current thread belongs to.
337 FirstThread
= (ApLocation
->Package
* CpuStatus
->MaxCoreCount
+ ApLocation
->Core
) * CpuStatus
->MaxThreadCount
;
338 CurrentThread
= FirstThread
+ ApLocation
->Thread
;
340 // First Notify all threads in current Core that this thread has ready.
342 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
343 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
346 // Second, check whether all valid threads in current core have ready.
348 for (ProcessorIndex
= 0; ProcessorIndex
< CpuStatus
->MaxThreadCount
; ProcessorIndex
++) {
349 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
354 SemaphorePtr
= CpuFlags
->PackageSemaphoreCount
;
355 ValidCoreCountPerPackage
= (UINT32
*)(UINTN
)CpuStatus
->ValidCoreCountPerPackage
;
357 // Get Offset info for the first thread in the package which current thread belongs to.
359 FirstThread
= ApLocation
->Package
* CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
;
361 // Get the possible threads count for current package.
363 PackageThreadsCount
= CpuStatus
->MaxThreadCount
* CpuStatus
->MaxCoreCount
;
364 CurrentThread
= FirstThread
+ CpuStatus
->MaxThreadCount
* ApLocation
->Core
+ ApLocation
->Thread
;
366 // Get the valid thread count for current package.
368 ValidThreadCount
= CpuStatus
->MaxThreadCount
* ValidCoreCountPerPackage
[ApLocation
->Package
];
371 // Different packages may have different valid cores in them. If driver maintail clearly
372 // cores number in different packages, the logic will be much complicated.
373 // Here driver just simply records the max core number in all packages and use it as expect
374 // core number for all packages.
375 // In below two steps logic, first current thread will Release semaphore for each thread
376 // in current package. Maybe some threads are not valid in this package, but driver don't
377 // care. Second, driver will let current thread wait semaphore for all valid threads in
378 // current package. Because only the valid threads will do release semaphore for this
379 // thread, driver here only need to wait the valid thread count.
383 // First Notify all threads in current package that this thread has ready.
385 for (ProcessorIndex
= 0; ProcessorIndex
< PackageThreadsCount
; ProcessorIndex
++) {
386 S3ReleaseSemaphore (&SemaphorePtr
[FirstThread
+ ProcessorIndex
]);
389 // Second, check whether all valid threads in current package have ready.
391 for (ProcessorIndex
= 0; ProcessorIndex
< ValidThreadCount
; ProcessorIndex
++) {
392 S3WaitForSemaphore (&SemaphorePtr
[CurrentThread
]);
409 Set Processor register for one AP.
411 @param PreSmmRegisterTable Use pre Smm register table or register table.
416 IN BOOLEAN PreSmmRegisterTable
419 CPU_REGISTER_TABLE
*RegisterTable
;
420 CPU_REGISTER_TABLE
*RegisterTables
;
425 if (PreSmmRegisterTable
) {
426 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
;
428 RegisterTables
= (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
;
431 InitApicId
= GetInitialApicId ();
432 RegisterTable
= NULL
;
433 ProcIndex
= (UINTN
)-1;
434 for (Index
= 0; Index
< mAcpiCpuData
.NumberOfCpus
; Index
++) {
435 if (RegisterTables
[Index
].InitialApicId
== InitApicId
) {
436 RegisterTable
= &RegisterTables
[Index
];
441 ASSERT (RegisterTable
!= NULL
);
443 if (mAcpiCpuData
.ApLocation
!= 0) {
444 ProgramProcessorRegister (
446 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)mAcpiCpuData
.ApLocation
+ ProcIndex
,
447 &mAcpiCpuData
.CpuStatus
,
451 ProgramProcessorRegister (
454 &mAcpiCpuData
.CpuStatus
,
461 AP initialization before then after SMBASE relocation in the S3 boot path.
471 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
476 // Count down the number with lock mechanism.
478 InterlockedDecrement (&mNumberToFinish
);
481 // Wait for BSP to signal SMM Base relocation done.
483 while (!mInitApsAfterSmmBaseReloc
) {
487 ProgramVirtualWireMode ();
488 DisableLvtInterrupts ();
493 // Place AP into the safe code, count down the number with lock mechanism in the safe code.
495 TopOfStack
= (UINTN
) Stack
+ sizeof (Stack
);
496 TopOfStack
&= ~(UINTN
) (CPU_STACK_ALIGNMENT
- 1);
497 CopyMem ((VOID
*) (UINTN
) mApHltLoopCode
, mApHltLoopCodeTemplate
, sizeof (mApHltLoopCodeTemplate
));
498 TransferApToSafeState ((UINTN
)mApHltLoopCode
, TopOfStack
, (UINTN
)&mNumberToFinish
);
502 Prepares startup vector for APs.
504 This function prepares startup vector for APs.
506 @param WorkingBuffer The address of the work buffer.
509 PrepareApStartupVector (
510 EFI_PHYSICAL_ADDRESS WorkingBuffer
513 EFI_PHYSICAL_ADDRESS StartupVector
;
514 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
517 // Get the address map of startup code for AP,
518 // including code size, and offset of long jump instructions to redirect.
520 ZeroMem (&AddressMap
, sizeof (AddressMap
));
521 AsmGetAddressMap (&AddressMap
);
523 StartupVector
= WorkingBuffer
;
526 // Copy AP startup code to startup vector, and then redirect the long jump
527 // instructions for mode switching.
529 CopyMem ((VOID
*) (UINTN
) StartupVector
, AddressMap
.RendezvousFunnelAddress
, AddressMap
.Size
);
530 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.FlatJumpOffset
+ 3) = (UINT32
) (StartupVector
+ AddressMap
.PModeEntryOffset
);
531 if (AddressMap
.LongJumpOffset
!= 0) {
532 *(UINT32
*) (UINTN
) (StartupVector
+ AddressMap
.LongJumpOffset
+ 2) = (UINT32
) (StartupVector
+ AddressMap
.LModeEntryOffset
);
536 // Get the start address of exchange data between BSP and AP.
538 mExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (StartupVector
+ AddressMap
.Size
);
539 ZeroMem ((VOID
*) mExchangeInfo
, sizeof (MP_CPU_EXCHANGE_INFO
));
541 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->GdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
542 CopyMem ((VOID
*) (UINTN
) &mExchangeInfo
->IdtrProfile
, (VOID
*) (UINTN
) mAcpiCpuData
.IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
544 mExchangeInfo
->StackStart
= (VOID
*) (UINTN
) mAcpiCpuData
.StackAddress
;
545 mExchangeInfo
->StackSize
= mAcpiCpuData
.StackSize
;
546 mExchangeInfo
->BufferStart
= (UINT32
) StartupVector
;
547 mExchangeInfo
->Cr3
= (UINT32
) (AsmReadCr3 ());
548 mExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
552 The function is invoked before SMBASE relocation in S3 path to restores CPU status.
554 The function is invoked before SMBASE relocation in S3 path. It does first time microcode load
555 and restores MTRRs for both BSP and APs.
559 InitializeCpuBeforeRebase (
563 LoadMtrrData (mAcpiCpuData
.MtrrTable
);
567 ProgramVirtualWireMode ();
569 PrepareApStartupVector (mAcpiCpuData
.StartupVector
);
571 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
572 mExchangeInfo
->ApFunction
= (VOID
*) (UINTN
) InitializeAp
;
575 // Execute code for before SmmBaseReloc. Note: This flag is maintained across S3 boots.
577 mInitApsAfterSmmBaseReloc
= FALSE
;
580 // Send INIT IPI - SIPI to all APs
582 SendInitSipiSipiAllExcludingSelf ((UINT32
)mAcpiCpuData
.StartupVector
);
584 while (mNumberToFinish
> 0) {
590 The function is invoked after SMBASE relocation in S3 path to restores CPU status.
592 The function is invoked after SMBASE relocation in S3 path. It restores configuration according to
593 data saved by normal boot path for both BSP and APs.
597 InitializeCpuAfterRebase (
601 mNumberToFinish
= mAcpiCpuData
.NumberOfCpus
- 1;
604 // Signal that SMM base relocation is complete and to continue initialization for all APs.
606 mInitApsAfterSmmBaseReloc
= TRUE
;
609 // Must begin set register after all APs have continue their initialization.
610 // This is a requirement to support semaphore mechanism in register table.
611 // Because if semaphore's dependence type is package type, semaphore will wait
612 // for all Aps in one package finishing their tasks before set next register
613 // for all APs. If the Aps not begin its task during BSP doing its task, the
614 // BSP thread will hang because it is waiting for other Aps in the same
615 // package finishing their task.
619 while (mNumberToFinish
> 0) {
625 Restore SMM Configuration in S3 boot path.
629 RestoreSmmConfigurationInS3 (
633 if (!mAcpiS3Enable
) {
638 // Restore SMM Configuration in S3 boot path.
640 if (mRestoreSmmConfigurationInS3
) {
642 // Need make sure gSmst is correct because below function may use them.
644 gSmst
->SmmStartupThisAp
= gSmmCpuPrivate
->SmmCoreEntryContext
.SmmStartupThisAp
;
645 gSmst
->CurrentlyExecutingCpu
= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
;
646 gSmst
->NumberOfCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
647 gSmst
->CpuSaveStateSize
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveStateSize
;
648 gSmst
->CpuSaveState
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveState
;
651 // Configure SMM Code Access Check feature if available.
653 ConfigSmmCodeAccessCheck ();
655 SmmCpuFeaturesCompleteSmmReadyToLock ();
657 mRestoreSmmConfigurationInS3
= FALSE
;
662 Perform SMM initialization for all processors in the S3 boot path.
664 For a native platform, MP initialization in the S3 boot path is also performed in this function.
672 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
673 IA32_DESCRIPTOR Ia32Idtr
;
674 IA32_DESCRIPTOR X64Idtr
;
675 IA32_IDT_GATE_DESCRIPTOR IdtEntryTable
[EXCEPTION_VECTOR_NUMBER
];
678 DEBUG ((EFI_D_INFO
, "SmmRestoreCpu()\n"));
683 // See if there is enough context to resume PEI Phase
685 if (mSmmS3ResumeState
== NULL
) {
686 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
690 SmmS3ResumeState
= mSmmS3ResumeState
;
691 ASSERT (SmmS3ResumeState
!= NULL
);
693 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
695 // Save the IA32 IDT Descriptor
697 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
700 // Setup X64 IDT table
702 ZeroMem (IdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32);
703 X64Idtr
.Base
= (UINTN
) IdtEntryTable
;
704 X64Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32 - 1);
705 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &X64Idtr
);
708 // Setup the default exception handler
710 Status
= InitializeCpuExceptionHandlers (NULL
);
711 ASSERT_EFI_ERROR (Status
);
714 // Initialize Debug Agent to support source level debug
716 InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
, (VOID
*)&Ia32Idtr
, NULL
);
720 // Skip initialization if mAcpiCpuData is not valid
722 if (mAcpiCpuData
.NumberOfCpus
> 0) {
724 // First time microcode load and restore MTRRs
726 InitializeCpuBeforeRebase ();
730 // Restore SMBASE for BSP and all APs
735 // Skip initialization if mAcpiCpuData is not valid
737 if (mAcpiCpuData
.NumberOfCpus
> 0) {
739 // Restore MSRs for BSP and all APs
741 InitializeCpuAfterRebase ();
745 // Set a flag to restore SMM configuration in S3 path.
747 mRestoreSmmConfigurationInS3
= TRUE
;
749 DEBUG (( EFI_D_INFO
, "SMM S3 Return CS = %x\n", SmmS3ResumeState
->ReturnCs
));
750 DEBUG (( EFI_D_INFO
, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState
->ReturnEntryPoint
));
751 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState
->ReturnContext1
));
752 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState
->ReturnContext2
));
753 DEBUG (( EFI_D_INFO
, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState
->ReturnStackPointer
));
756 // If SMM is in 32-bit mode, then use SwitchStack() to resume PEI Phase
758 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_32
) {
759 DEBUG ((EFI_D_INFO
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
762 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)SmmS3ResumeState
->ReturnEntryPoint
,
763 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext1
,
764 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext2
,
765 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnStackPointer
770 // If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase
772 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
773 DEBUG ((EFI_D_INFO
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
775 // Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.
777 SaveAndSetDebugTimerInterrupt (FALSE
);
779 // Restore IA32 IDT table
781 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
783 SmmS3ResumeState
->ReturnCs
,
784 (UINT32
)SmmS3ResumeState
->ReturnEntryPoint
,
785 (UINT32
)SmmS3ResumeState
->ReturnContext1
,
786 (UINT32
)SmmS3ResumeState
->ReturnContext2
,
787 (UINT32
)SmmS3ResumeState
->ReturnStackPointer
792 // Can not resume PEI Phase
794 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
799 Initialize SMM S3 resume state structure used during S3 Resume.
801 @param[in] Cr3 The base address of the page tables to use in SMM.
805 InitSmmS3ResumeState (
810 EFI_SMRAM_DESCRIPTOR
*SmramDescriptor
;
811 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
812 EFI_PHYSICAL_ADDRESS Address
;
815 if (!mAcpiS3Enable
) {
819 GuidHob
= GetFirstGuidHob (&gEfiAcpiVariableGuid
);
820 if (GuidHob
== NULL
) {
823 "ERROR:%a(): HOB(gEfiAcpiVariableGuid=%g) needed by S3 resume doesn't exist!\n",
825 &gEfiAcpiVariableGuid
829 SmramDescriptor
= (EFI_SMRAM_DESCRIPTOR
*) GET_GUID_HOB_DATA (GuidHob
);
831 DEBUG ((EFI_D_INFO
, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor
));
832 DEBUG ((EFI_D_INFO
, "SMM S3 Structure = %x\n", SmramDescriptor
->CpuStart
));
834 SmmS3ResumeState
= (SMM_S3_RESUME_STATE
*)(UINTN
)SmramDescriptor
->CpuStart
;
835 ZeroMem (SmmS3ResumeState
, sizeof (SMM_S3_RESUME_STATE
));
837 mSmmS3ResumeState
= SmmS3ResumeState
;
838 SmmS3ResumeState
->Smst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gSmst
;
840 SmmS3ResumeState
->SmmS3ResumeEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmRestoreCpu
;
842 SmmS3ResumeState
->SmmS3StackSize
= SIZE_32KB
;
843 SmmS3ResumeState
->SmmS3StackBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN
)SmmS3ResumeState
->SmmS3StackSize
));
844 if (SmmS3ResumeState
->SmmS3StackBase
== 0) {
845 SmmS3ResumeState
->SmmS3StackSize
= 0;
848 SmmS3ResumeState
->SmmS3Cr0
= mSmmCr0
;
849 SmmS3ResumeState
->SmmS3Cr3
= Cr3
;
850 SmmS3ResumeState
->SmmS3Cr4
= mSmmCr4
;
852 if (sizeof (UINTN
) == sizeof (UINT64
)) {
853 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_64
;
855 if (sizeof (UINTN
) == sizeof (UINT32
)) {
856 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_32
;
860 // Patch SmmS3ResumeState->SmmS3Cr3
866 // Allocate safe memory in ACPI NVS for AP to execute hlt loop in
867 // protected mode on S3 path
869 Address
= BASE_4GB
- 1;
870 Status
= gBS
->AllocatePages (
873 EFI_SIZE_TO_PAGES (sizeof (mApHltLoopCodeTemplate
)),
876 ASSERT_EFI_ERROR (Status
);
877 mApHltLoopCode
= (UINT8
*) (UINTN
) Address
;
881 Copy register table from ACPI NVS memory into SMRAM.
883 @param[in] DestinationRegisterTableList Points to destination register table.
884 @param[in] SourceRegisterTableList Points to source register table.
885 @param[in] NumberOfCpus Number of CPUs.
890 IN CPU_REGISTER_TABLE
*DestinationRegisterTableList
,
891 IN CPU_REGISTER_TABLE
*SourceRegisterTableList
,
892 IN UINT32 NumberOfCpus
896 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
898 CopyMem (DestinationRegisterTableList
, SourceRegisterTableList
, NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
899 for (Index
= 0; Index
< NumberOfCpus
; Index
++) {
900 if (DestinationRegisterTableList
[Index
].AllocatedSize
!= 0) {
901 RegisterTableEntry
= AllocateCopyPool (
902 DestinationRegisterTableList
[Index
].AllocatedSize
,
903 (VOID
*)(UINTN
)SourceRegisterTableList
[Index
].RegisterTableEntry
905 ASSERT (RegisterTableEntry
!= NULL
);
906 DestinationRegisterTableList
[Index
].RegisterTableEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RegisterTableEntry
;
920 ACPI_CPU_DATA
*AcpiCpuData
;
921 IA32_DESCRIPTOR
*Gdtr
;
922 IA32_DESCRIPTOR
*Idtr
;
925 VOID
*MachineCheckHandlerForAp
;
926 CPU_STATUS_INFORMATION
*CpuStatus
;
928 if (!mAcpiS3Enable
) {
933 // Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0
935 mAcpiCpuData
.NumberOfCpus
= 0;
938 // If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM
940 AcpiCpuData
= (ACPI_CPU_DATA
*)(UINTN
)PcdGet64 (PcdCpuS3DataAddress
);
941 if (AcpiCpuData
== 0) {
946 // For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.
948 CopyMem (&mAcpiCpuData
, AcpiCpuData
, sizeof (mAcpiCpuData
));
950 mAcpiCpuData
.MtrrTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (MTRR_SETTINGS
));
951 ASSERT (mAcpiCpuData
.MtrrTable
!= 0);
953 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.MtrrTable
, (VOID
*)(UINTN
)AcpiCpuData
->MtrrTable
, sizeof (MTRR_SETTINGS
));
955 mAcpiCpuData
.GdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
956 ASSERT (mAcpiCpuData
.GdtrProfile
!= 0);
958 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.GdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
960 mAcpiCpuData
.IdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
961 ASSERT (mAcpiCpuData
.IdtrProfile
!= 0);
963 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.IdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
965 mAcpiCpuData
.PreSmmInitRegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
966 ASSERT (mAcpiCpuData
.PreSmmInitRegisterTable
!= 0);
969 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
,
970 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->PreSmmInitRegisterTable
,
971 mAcpiCpuData
.NumberOfCpus
974 mAcpiCpuData
.RegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
975 ASSERT (mAcpiCpuData
.RegisterTable
!= 0);
978 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
,
979 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->RegisterTable
,
980 mAcpiCpuData
.NumberOfCpus
984 // Copy AP's GDT, IDT and Machine Check handler into SMRAM.
986 Gdtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.GdtrProfile
;
987 Idtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.IdtrProfile
;
989 GdtForAp
= AllocatePool ((Gdtr
->Limit
+ 1) + (Idtr
->Limit
+ 1) + mAcpiCpuData
.ApMachineCheckHandlerSize
);
990 ASSERT (GdtForAp
!= NULL
);
991 IdtForAp
= (VOID
*) ((UINTN
)GdtForAp
+ (Gdtr
->Limit
+ 1));
992 MachineCheckHandlerForAp
= (VOID
*) ((UINTN
)IdtForAp
+ (Idtr
->Limit
+ 1));
994 CopyMem (GdtForAp
, (VOID
*)Gdtr
->Base
, Gdtr
->Limit
+ 1);
995 CopyMem (IdtForAp
, (VOID
*)Idtr
->Base
, Idtr
->Limit
+ 1);
996 CopyMem (MachineCheckHandlerForAp
, (VOID
*)(UINTN
)mAcpiCpuData
.ApMachineCheckHandlerBase
, mAcpiCpuData
.ApMachineCheckHandlerSize
);
998 Gdtr
->Base
= (UINTN
)GdtForAp
;
999 Idtr
->Base
= (UINTN
)IdtForAp
;
1000 mAcpiCpuData
.ApMachineCheckHandlerBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MachineCheckHandlerForAp
;
1002 CpuStatus
= &mAcpiCpuData
.CpuStatus
;
1003 CopyMem (CpuStatus
, &AcpiCpuData
->CpuStatus
, sizeof (CPU_STATUS_INFORMATION
));
1004 if (AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
!= 0) {
1005 CpuStatus
->ValidCoreCountPerPackage
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1006 sizeof (UINT32
) * CpuStatus
->PackageCount
,
1007 (UINT32
*)(UINTN
)AcpiCpuData
->CpuStatus
.ValidCoreCountPerPackage
1009 ASSERT (CpuStatus
->ValidCoreCountPerPackage
!= 0);
1011 if (AcpiCpuData
->ApLocation
!= 0) {
1012 mAcpiCpuData
.ApLocation
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocateCopyPool (
1013 mAcpiCpuData
.NumberOfCpus
* sizeof (EFI_CPU_PHYSICAL_LOCATION
),
1014 (EFI_CPU_PHYSICAL_LOCATION
*)(UINTN
)AcpiCpuData
->ApLocation
1016 ASSERT (mAcpiCpuData
.ApLocation
!= 0);
1018 if (CpuStatus
->PackageCount
!= 0) {
1019 mCpuFlags
.CoreSemaphoreCount
= AllocateZeroPool (
1020 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1021 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1023 ASSERT (mCpuFlags
.CoreSemaphoreCount
!= NULL
);
1024 mCpuFlags
.PackageSemaphoreCount
= AllocateZeroPool (
1025 sizeof (UINT32
) * CpuStatus
->PackageCount
*
1026 CpuStatus
->MaxCoreCount
* CpuStatus
->MaxThreadCount
1028 ASSERT (mCpuFlags
.PackageSemaphoreCount
!= NULL
);
1030 InitializeSpinLock((SPIN_LOCK
*) &mCpuFlags
.MemoryMappedLock
);
1031 InitializeSpinLock((SPIN_LOCK
*) &mCpuFlags
.ConsoleLogLock
);
1035 Get ACPI S3 enable flag.
1039 GetAcpiS3EnableFlag (
1043 mAcpiS3Enable
= PcdGetBool (PcdAcpiS3Enable
);