4 Copyright (c) 2008 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 // According to IA32 SDM, MTRRs number and MSR offset are always consistent
14 // for IA32 processor family
18 // The semantics of below macro is MAX_MTRR_NUMBER_OF_VARIABLE_MTRR, the real number can be read out from MTRR_CAP register.
20 #define MTRR_NUMBER_OF_VARIABLE_MTRR 32
22 // Firmware need reserve 2 MTRR for OS
23 // Note: It is replaced by PCD PcdCpuNumberOfReservedVariableMtrrs
25 #define RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER 2
27 #define MTRR_NUMBER_OF_FIXED_MTRR 11
30 // Structure to describe a fixed MTRR
39 // Structure to describe a variable MTRR
51 // Structure to hold base and mask pair for variable MTRR register
53 typedef struct _MTRR_VARIABLE_SETTING_
{
56 } MTRR_VARIABLE_SETTING
;
59 // Array for variable MTRRs
61 typedef struct _MTRR_VARIABLE_SETTINGS_
{
62 MTRR_VARIABLE_SETTING Mtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
63 } MTRR_VARIABLE_SETTINGS
;
66 // Array for fixed MTRRs
68 typedef struct _MTRR_FIXED_SETTINGS_
{
69 UINT64 Mtrr
[MTRR_NUMBER_OF_FIXED_MTRR
];
70 } MTRR_FIXED_SETTINGS
;
73 // Structure to hold all MTRRs
75 typedef struct _MTRR_SETTINGS_
{
76 MTRR_FIXED_SETTINGS Fixed
;
77 MTRR_VARIABLE_SETTINGS Variables
;
86 CacheWriteCombining
= 1,
87 CacheWriteThrough
= 4,
88 CacheWriteProtected
= 5,
91 } MTRR_MEMORY_CACHE_TYPE
;
93 #define MTRR_CACHE_UNCACHEABLE 0
94 #define MTRR_CACHE_WRITE_COMBINING 1
95 #define MTRR_CACHE_WRITE_THROUGH 4
96 #define MTRR_CACHE_WRITE_PROTECTED 5
97 #define MTRR_CACHE_WRITE_BACK 6
98 #define MTRR_CACHE_INVALID_TYPE 7
103 MTRR_MEMORY_CACHE_TYPE Type
;
107 Returns the variable MTRR count for the CPU.
109 @return Variable MTRR count
114 GetVariableMtrrCount (
119 Returns the firmware usable variable MTRR count for the CPU.
121 @return Firmware usable variable MTRR count
126 GetFirmwareVariableMtrrCount (
131 This function attempts to set the attributes for a memory range.
133 @param[in] BaseAddress The physical address that is the start
134 address of a memory region.
135 @param[in] Length The size in bytes of the memory region.
136 @param[in] Attribute The bit mask of attributes to set for the
139 @retval RETURN_SUCCESS The attributes were set for the memory
141 @retval RETURN_INVALID_PARAMETER Length is zero.
142 @retval RETURN_UNSUPPORTED The processor does not support one or
143 more bytes of the memory resource range
144 specified by BaseAddress and Length.
145 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
146 for the memory resource range specified
147 by BaseAddress and Length.
148 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
149 range specified by BaseAddress and Length
151 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
152 modify the attributes of the memory
154 Multiple memory range attributes setting by calling this API multiple
155 times may fail with status RETURN_OUT_OF_RESOURCES. It may not mean
156 the number of CPU MTRRs are too small to set such memory attributes.
157 Pass the multiple memory range attributes to one call of
158 MtrrSetMemoryAttributesInMtrrSettings() may succeed.
159 @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.
160 Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify
161 external scratch buffer.
165 MtrrSetMemoryAttribute (
166 IN PHYSICAL_ADDRESS BaseAddress
,
168 IN MTRR_MEMORY_CACHE_TYPE Attribute
172 This function will get the memory cache type of the specific address.
173 This function is mainly for debugging purposes.
175 @param[in] Address The specific address
177 @return The memory cache type of the specific address
180 MTRR_MEMORY_CACHE_TYPE
182 MtrrGetMemoryAttribute (
183 IN PHYSICAL_ADDRESS Address
187 This function gets the content in fixed MTRRs
189 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
191 @return The pointer of FixedSettings
194 MTRR_FIXED_SETTINGS
*
197 OUT MTRR_FIXED_SETTINGS
*FixedSettings
201 This function gets the content in all MTRRs (variable and fixed)
203 @param[out] MtrrSetting A buffer to hold all MTRRs content.
205 @return The pointer of MtrrSetting
211 OUT MTRR_SETTINGS
*MtrrSetting
215 This function sets all MTRRs (variable and fixed)
217 @param[in] MtrrSetting A buffer to hold all MTRRs content.
219 @return The pointer of MtrrSetting
225 IN MTRR_SETTINGS
*MtrrSetting
229 Get the attribute of variable MTRRs.
231 This function shadows the content of variable MTRRs into
232 an internal array: VariableMtrr
234 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
235 @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in
236 MTRR must align to 4K, so valid address mask equal to
237 MtrrValidBitsMask & 0xfffffffffffff000ULL
238 @param[out] VariableMtrr The array to shadow variable MTRRs content
240 @return The return value of this parameter indicates the number of
241 MTRRs which has been used.
245 MtrrGetMemoryAttributeInVariableMtrr (
246 IN UINT64 MtrrValidBitsMask
,
247 IN UINT64 MtrrValidAddressMask
,
248 OUT VARIABLE_MTRR
*VariableMtrr
252 This function prints all MTRRs for debugging.
256 MtrrDebugPrintAllMtrrs (
261 Checks if MTRR is supported.
263 @retval TRUE MTRR is supported.
264 @retval FALSE MTRR is not supported.
274 Returns the default MTRR cache type for the system.
276 @return The default MTRR cache type.
279 MTRR_MEMORY_CACHE_TYPE
281 MtrrGetDefaultMemoryType (
286 This function attempts to set the attributes into MTRR setting buffer for a memory range.
288 @param[in, out] MtrrSetting MTRR setting buffer to be set.
289 @param[in] BaseAddress The physical address that is the start address
291 @param[in] Length The size in bytes of the memory region.
292 @param[in] Attribute The bit mask of attributes to set for the
295 @retval RETURN_SUCCESS The attributes were set for the memory region.
296 @retval RETURN_INVALID_PARAMETER Length is zero.
297 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
298 memory resource range specified by BaseAddress and Length.
299 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
300 range specified by BaseAddress and Length.
301 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
302 BaseAddress and Length cannot be modified.
303 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
304 the memory resource range.
305 Multiple memory range attributes setting by calling this API multiple
306 times may fail with status RETURN_OUT_OF_RESOURCES. It may not mean
307 the number of CPU MTRRs are too small to set such memory attributes.
308 Pass the multiple memory range attributes to one call of
309 MtrrSetMemoryAttributesInMtrrSettings() may succeed.
310 @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.
311 Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify
312 external scratch buffer.
316 MtrrSetMemoryAttributeInMtrrSettings (
317 IN OUT MTRR_SETTINGS
*MtrrSetting
,
318 IN PHYSICAL_ADDRESS BaseAddress
,
320 IN MTRR_MEMORY_CACHE_TYPE Attribute
324 This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.
326 @param[in, out] MtrrSetting MTRR setting buffer to be set.
327 @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.
328 @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.
329 It may be updated to the actual required size when the calculation
330 needs more scratch buffer.
331 @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.
332 When range overlap happens, the last one takes higher priority.
333 When the function returns, either all the attributes are set successfully,
334 or none of them is set.
335 @param[in] RangeCount Count of MTRR_MEMORY_RANGE.
337 @retval RETURN_SUCCESS The attributes were set for all the memory ranges.
338 @retval RETURN_INVALID_PARAMETER Length in any range is zero.
339 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
340 memory resource range specified by BaseAddress and Length in any range.
341 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
342 range specified by BaseAddress and Length in any range.
343 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
344 the memory resource ranges.
345 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
346 BaseAddress and Length cannot be modified.
347 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
351 MtrrSetMemoryAttributesInMtrrSettings (
352 IN OUT MTRR_SETTINGS
*MtrrSetting
,
354 IN OUT UINTN
*ScratchSize
,
355 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
359 #endif // _MTRR_LIB_H_