2 The Quark CPU specific programming for PiSmmCpuDxeSmm module.
4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/SmmCpuFeaturesLib.h>
11 #include <Register/SmramSaveStateMap.h>
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/QNCAccessLib.h>
16 #define EFI_MSR_SMRR_PHYS_MASK_VALID BIT11
17 #define EFI_MSR_SMRR_MASK 0xFFFFF000
20 Called during the very first SMI into System Management Mode to initialize
21 CPU features, including SMBASE, for the currently executing CPU. Since this
22 is the first SMI, the SMRAM Save State Map is at the default address of
23 SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET. The currently executing
24 CPU is specified by CpuIndex and CpuIndex can be used to access information
25 about the currently executing CPU in the ProcessorInfo array and the
26 HotPlugCpuData data structure.
28 @param[in] CpuIndex The index of the CPU to initialize. The value
29 must be between 0 and the NumberOfCpus field in
30 the System Management System Table (SMST).
31 @param[in] IsMonarch TRUE if the CpuIndex is the index of the CPU that
32 was elected as monarch during System Management
34 FALSE if the CpuIndex is not the index of the CPU
35 that was elected as monarch during System
36 Management Mode initialization.
37 @param[in] ProcessorInfo Pointer to an array of EFI_PROCESSOR_INFORMATION
38 structures. ProcessorInfo[CpuIndex] contains the
39 information for the currently executing CPU.
40 @param[in] CpuHotPlugData Pointer to the CPU_HOT_PLUG_DATA structure that
41 contains the ApidId and SmBase arrays.
45 SmmCpuFeaturesInitializeProcessor (
48 IN EFI_PROCESSOR_INFORMATION
*ProcessorInfo
,
49 IN CPU_HOT_PLUG_DATA
*CpuHotPlugData
52 SMRAM_SAVE_STATE_MAP
*CpuState
;
57 CpuState
= (SMRAM_SAVE_STATE_MAP
*)(UINTN
)(SMM_DEFAULT_SMBASE
+ SMRAM_SAVE_STATE_MAP_OFFSET
);
58 CpuState
->x86
.SMBASE
= CpuHotPlugData
->SmBase
[CpuIndex
];
61 // SMRR size cannot be less than 4-KBytes
62 // SMRR size must be of length 2^n
63 // SMRR base alignment cannot be less than SMRR length
65 if ((CpuHotPlugData
->SmrrSize
< SIZE_4KB
) ||
66 (CpuHotPlugData
->SmrrSize
!= GetPowerOfTwo32 (CpuHotPlugData
->SmrrSize
)) ||
67 ((CpuHotPlugData
->SmrrBase
& ~(CpuHotPlugData
->SmrrSize
- 1)) != CpuHotPlugData
->SmrrBase
)) {
68 DEBUG ((EFI_D_ERROR
, "SMM Base/Size does not meet alignment/size requirement!\n"));
73 // Use QNC to initialize SMRR on Quark
75 QNCPortWrite(QUARK_NC_HOST_BRIDGE_SB_PORT_ID
, QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSBASE
, CpuHotPlugData
->SmrrBase
);
76 QNCPortWrite(QUARK_NC_HOST_BRIDGE_SB_PORT_ID
, QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSMASK
, (~(CpuHotPlugData
->SmrrSize
- 1) & EFI_MSR_SMRR_MASK
) | EFI_MSR_SMRR_PHYS_MASK_VALID
);
80 This function updates the SMRAM save state on the currently executing CPU
81 to resume execution at a specific address after an RSM instruction. This
82 function must evaluate the SMRAM save state to determine the execution mode
83 the RSM instruction resumes and update the resume execution address with
84 either NewInstructionPointer32 or NewInstructionPoint. The auto HALT restart
85 flag in the SMRAM save state must always be cleared. This function returns
86 the value of the instruction pointer from the SMRAM save state that was
87 replaced. If this function returns 0, then the SMRAM save state was not
90 This function is called during the very first SMI on each CPU after
91 SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution mode
92 to signal that the SMBASE of each CPU has been updated before the default
93 SMBASE address is used for the first SMI to the next CPU.
95 @param[in] CpuIndex The index of the CPU to hook. The value
96 must be between 0 and the NumberOfCpus
97 field in the System Management System Table
99 @param[in] CpuState Pointer to SMRAM Save State Map for the
100 currently executing CPU.
101 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to
102 32-bit execution mode from 64-bit SMM.
103 @param[in] NewInstructionPointer Instruction pointer to use if resuming to
104 same execution mode as SMM.
106 @retval 0 This function did modify the SMRAM save state.
107 @retval > 0 The original instruction pointer value from the SMRAM save state
108 before it was replaced.
112 SmmCpuFeaturesHookReturnFromSmm (
114 IN SMRAM_SAVE_STATE_MAP
*CpuState
,
115 IN UINT64 NewInstructionPointer32
,
116 IN UINT64 NewInstructionPointer
123 Hook point in normal execution mode that allows the one CPU that was elected
124 as monarch during System Management Mode initialization to perform additional
125 initialization actions immediately after all of the CPUs have processed their
126 first SMI and called SmmCpuFeaturesInitializeProcessor() relocating SMBASE
127 into a buffer in SMRAM and called SmmCpuFeaturesHookReturnFromSmm().
131 SmmCpuFeaturesSmmRelocationComplete (
138 Return the size, in bytes, of a custom SMI Handler in bytes. If 0 is
139 returned, then a custom SMI handler is not provided by this library,
140 and the default SMI handler must be used.
142 @retval 0 Use the default SMI handler.
143 @retval > 0 Use the SMI handler installed by SmmCpuFeaturesInstallSmiHandler()
144 The caller is required to allocate enough SMRAM for each CPU to
145 support the size of the custom SMI handler.
149 SmmCpuFeaturesGetSmiHandlerSize (
157 Install a custom SMI handler for the CPU specified by CpuIndex. This function
158 is only called if SmmCpuFeaturesGetSmiHandlerSize() returns a size is greater
159 than zero and is called by the CPU that was elected as monarch during System
160 Management Mode initialization.
162 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.
163 The value must be between 0 and the NumberOfCpus field
164 in the System Management System Table (SMST).
165 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.
166 @param[in] SmiStack The stack to use when an SMI is processed by the
167 the CPU specified by CpuIndex.
168 @param[in] StackSize The size, in bytes, if the stack used when an SMI is
169 processed by the CPU specified by CpuIndex.
170 @param[in] GdtBase The base address of the GDT to use when an SMI is
171 processed by the CPU specified by CpuIndex.
172 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is
173 processed by the CPU specified by CpuIndex.
174 @param[in] IdtBase The base address of the IDT to use when an SMI is
175 processed by the CPU specified by CpuIndex.
176 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is
177 processed by the CPU specified by CpuIndex.
178 @param[in] Cr3 The base address of the page tables to use when an SMI
179 is processed by the CPU specified by CpuIndex.
183 SmmCpuFeaturesInstallSmiHandler (
198 Determines if MTRR registers must be configured to set SMRAM cache-ability
199 when executing in System Management Mode.
201 @retval TRUE MTRR registers must be configured to set SMRAM cache-ability.
202 @retval FALSE MTRR registers do not need to be configured to set SMRAM
207 SmmCpuFeaturesNeedConfigureMtrrs (
215 Disable SMRR register if SMRR is supported and SmmCpuFeaturesNeedConfigureMtrrs()
220 SmmCpuFeaturesDisableSmrr (
225 // Use QNC to disable SMRR on Quark
228 QUARK_NC_HOST_BRIDGE_SB_PORT_ID
,
229 QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSMASK
,
230 QNCPortRead(QUARK_NC_HOST_BRIDGE_SB_PORT_ID
, QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSMASK
) & ~EFI_MSR_SMRR_PHYS_MASK_VALID
235 Enable SMRR register if SMRR is supported and SmmCpuFeaturesNeedConfigureMtrrs()
240 SmmCpuFeaturesReenableSmrr (
245 // Use QNC to enable SMRR on Quark
248 QUARK_NC_HOST_BRIDGE_SB_PORT_ID
,
249 QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSMASK
,
250 QNCPortRead(QUARK_NC_HOST_BRIDGE_SB_PORT_ID
, QUARK_NC_HOST_BRIDGE_IA32_MTRR_SMRR_PHYSMASK
) | EFI_MSR_SMRR_PHYS_MASK_VALID
255 Processor specific hook point each time a CPU enters System Management Mode.
257 @param[in] CpuIndex The index of the CPU that has entered SMM. The value
258 must be between 0 and the NumberOfCpus field in the
259 System Management System Table (SMST).
263 SmmCpuFeaturesRendezvousEntry (
270 Processor specific hook point each time a CPU exits System Management Mode.
272 @param[in] CpuIndex The index of the CPU that is exiting SMM. The value must
273 be between 0 and the NumberOfCpus field in the System
274 Management System Table (SMST).
278 SmmCpuFeaturesRendezvousExit (
285 Check to see if an SMM register is supported by a specified CPU.
287 @param[in] CpuIndex The index of the CPU to check for SMM register support.
288 The value must be between 0 and the NumberOfCpus field
289 in the System Management System Table (SMST).
290 @param[in] RegName Identifies the SMM register to check for support.
292 @retval TRUE The SMM register specified by RegName is supported by the CPU
293 specified by CpuIndex.
294 @retval FALSE The SMM register specified by RegName is not supported by the
295 CPU specified by CpuIndex.
299 SmmCpuFeaturesIsSmmRegisterSupported (
301 IN SMM_REG_NAME RegName
308 Returns the current value of the SMM register for the specified CPU.
309 If the SMM register is not supported, then 0 is returned.
311 @param[in] CpuIndex The index of the CPU to read the SMM register. The
312 value must be between 0 and the NumberOfCpus field in
313 the System Management System Table (SMST).
314 @param[in] RegName Identifies the SMM register to read.
316 @return The value of the SMM register specified by RegName from the CPU
317 specified by CpuIndex.
321 SmmCpuFeaturesGetSmmRegister (
323 IN SMM_REG_NAME RegName
330 Sets the value of an SMM register on a specified CPU.
331 If the SMM register is not supported, then no action is performed.
333 @param[in] CpuIndex The index of the CPU to write the SMM register. The
334 value must be between 0 and the NumberOfCpus field in
335 the System Management System Table (SMST).
336 @param[in] RegName Identifies the SMM register to write.
337 registers are read-only.
338 @param[in] Value The value to write to the SMM register.
342 SmmCpuFeaturesSetSmmRegister (
344 IN SMM_REG_NAME RegName
,
351 Read an SMM Save State register on the target processor. If this function
352 returns EFI_UNSUPPORTED, then the caller is responsible for reading the
353 SMM Save Sate register.
355 @param[in] CpuIndex The index of the CPU to read the SMM Save State. The
356 value must be between 0 and the NumberOfCpus field in
357 the System Management System Table (SMST).
358 @param[in] Register The SMM Save State register to read.
359 @param[in] Width The number of bytes to read from the CPU save state.
360 @param[out] Buffer Upon return, this holds the CPU register value read
363 @retval EFI_SUCCESS The register was read from Save State.
364 @retval EFI_INVALID_PARAMTER Buffer is NULL.
365 @retval EFI_UNSUPPORTED This function does not support reading Register.
370 SmmCpuFeaturesReadSaveStateRegister (
372 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
377 return EFI_UNSUPPORTED
;
381 Writes an SMM Save State register on the target processor. If this function
382 returns EFI_UNSUPPORTED, then the caller is responsible for writing the
383 SMM Save Sate register.
385 @param[in] CpuIndex The index of the CPU to write the SMM Save State. The
386 value must be between 0 and the NumberOfCpus field in
387 the System Management System Table (SMST).
388 @param[in] Register The SMM Save State register to write.
389 @param[in] Width The number of bytes to write to the CPU save state.
390 @param[in] Buffer Upon entry, this holds the new CPU register value.
392 @retval EFI_SUCCESS The register was written to Save State.
393 @retval EFI_INVALID_PARAMTER Buffer is NULL.
394 @retval EFI_UNSUPPORTED This function does not support writing Register.
398 SmmCpuFeaturesWriteSaveStateRegister (
400 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
402 IN CONST VOID
*Buffer
405 return EFI_UNSUPPORTED
;
409 This function is hook point called after the gEfiSmmReadyToLockProtocolGuid
410 notification is completely processed.
414 SmmCpuFeaturesCompleteSmmReadyToLock (
421 This API provides a method for a CPU to allocate a specific region for storing page tables.
423 This API can be called more once to allocate memory for page tables.
425 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
426 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
427 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
430 This function can also return NULL if there is no preference on where the page tables are allocated in SMRAM.
432 @param Pages The number of 4 KB pages to allocate.
434 @return A pointer to the allocated buffer for page tables.
435 @retval NULL Fail to allocate a specific region for storing page tables,
436 Or there is no preference on where the page tables are allocated in SMRAM.
441 SmmCpuFeaturesAllocatePageTableMemory (