4 Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // According to IA32 SDM, MTRRs number and MSR offset are always consistent
20 // for IA32 processor family
24 // The semantics of below macro is MAX_MTRR_NUMBER_OF_VARIABLE_MTRR, the real number can be read out from MTRR_CAP register.
26 #define MTRR_NUMBER_OF_VARIABLE_MTRR 32
28 // Firmware need reserve 2 MTRR for OS
29 // Note: It is replaced by PCD PcdCpuNumberOfReservedVariableMtrrs
31 #define RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER 2
33 #define MTRR_NUMBER_OF_FIXED_MTRR 11
36 // Structure to describe a fixed MTRR
45 // Structure to describe a variable MTRR
57 // Structure to hold base and mask pair for variable MTRR register
59 typedef struct _MTRR_VARIABLE_SETTING_
{
62 } MTRR_VARIABLE_SETTING
;
65 // Array for variable MTRRs
67 typedef struct _MTRR_VARIABLE_SETTINGS_
{
68 MTRR_VARIABLE_SETTING Mtrr
[MTRR_NUMBER_OF_VARIABLE_MTRR
];
69 } MTRR_VARIABLE_SETTINGS
;
72 // Array for fixed MTRRs
74 typedef struct _MTRR_FIXED_SETTINGS_
{
75 UINT64 Mtrr
[MTRR_NUMBER_OF_FIXED_MTRR
];
76 } MTRR_FIXED_SETTINGS
;
79 // Structure to hold all MTRRs
81 typedef struct _MTRR_SETTINGS_
{
82 MTRR_FIXED_SETTINGS Fixed
;
83 MTRR_VARIABLE_SETTINGS Variables
;
92 CacheWriteCombining
= 1,
93 CacheWriteThrough
= 4,
94 CacheWriteProtected
= 5,
97 } MTRR_MEMORY_CACHE_TYPE
;
99 #define MTRR_CACHE_UNCACHEABLE 0
100 #define MTRR_CACHE_WRITE_COMBINING 1
101 #define MTRR_CACHE_WRITE_THROUGH 4
102 #define MTRR_CACHE_WRITE_PROTECTED 5
103 #define MTRR_CACHE_WRITE_BACK 6
104 #define MTRR_CACHE_INVALID_TYPE 7
109 MTRR_MEMORY_CACHE_TYPE Type
;
113 Returns the variable MTRR count for the CPU.
115 @return Variable MTRR count
120 GetVariableMtrrCount (
125 Returns the firmware usable variable MTRR count for the CPU.
127 @return Firmware usable variable MTRR count
132 GetFirmwareVariableMtrrCount (
137 This function attempts to set the attributes for a memory range.
139 @param[in] BaseAddress The physical address that is the start
140 address of a memory region.
141 @param[in] Length The size in bytes of the memory region.
142 @param[in] Attribute The bit mask of attributes to set for the
145 @retval RETURN_SUCCESS The attributes were set for the memory
147 @retval RETURN_INVALID_PARAMETER Length is zero.
148 @retval RETURN_UNSUPPORTED The processor does not support one or
149 more bytes of the memory resource range
150 specified by BaseAddress and Length.
151 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support
152 for the memory resource range specified
153 by BaseAddress and Length.
154 @retval RETURN_ACCESS_DENIED The attributes for the memory resource
155 range specified by BaseAddress and Length
157 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to
158 modify the attributes of the memory
160 @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.
161 Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify
162 external scratch buffer.
166 MtrrSetMemoryAttribute (
167 IN PHYSICAL_ADDRESS BaseAddress
,
169 IN MTRR_MEMORY_CACHE_TYPE Attribute
174 This function will get the memory cache type of the specific address.
175 This function is mainly for debugging purposes.
177 @param[in] Address The specific address
179 @return The memory cache type of the specific address
182 MTRR_MEMORY_CACHE_TYPE
184 MtrrGetMemoryAttribute (
185 IN PHYSICAL_ADDRESS Address
190 This function will get the raw value in variable MTRRs
192 @param[out] VariableSettings A buffer to hold variable MTRRs content.
194 @return The buffer point to MTRR_VARIABLE_SETTINGS in which holds the content of the variable MTRR
197 MTRR_VARIABLE_SETTINGS
*
199 MtrrGetVariableMtrr (
200 OUT MTRR_VARIABLE_SETTINGS
*VariableSettings
205 This function sets variable MTRRs
207 @param[in] VariableSettings A buffer to hold variable MTRRs content.
209 @return The pointer of VariableSettings
212 MTRR_VARIABLE_SETTINGS
*
214 MtrrSetVariableMtrr (
215 IN MTRR_VARIABLE_SETTINGS
*VariableSettings
220 This function gets the content in fixed MTRRs
222 @param[out] FixedSettings A buffer to hold fixed MTRRs content.
224 @return The pointer of FixedSettings
230 OUT MTRR_FIXED_SETTINGS
*FixedSettings
235 This function sets fixed MTRRs
237 @param[in] FixedSettings A buffer holding fixed MTRRs content.
239 @return The pointer of FixedSettings
245 IN MTRR_FIXED_SETTINGS
*FixedSettings
250 This function gets the content in all MTRRs (variable and fixed)
252 @param[out] MtrrSetting A buffer to hold all MTRRs content.
254 @return The pointer of MtrrSetting
260 OUT MTRR_SETTINGS
*MtrrSetting
265 This function sets all MTRRs (variable and fixed)
267 @param[in] MtrrSetting A buffer to hold all MTRRs content.
269 @return The pointer of MtrrSetting
275 IN MTRR_SETTINGS
*MtrrSetting
280 Get the attribute of variable MTRRs.
282 This function shadows the content of variable MTRRs into
283 an internal array: VariableMtrr
285 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR
286 @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in
287 MTRR must align to 4K, so valid address mask equal to
288 MtrrValidBitsMask & 0xfffffffffffff000ULL
289 @param[out] VariableMtrr The array to shadow variable MTRRs content
291 @return The return value of this parameter indicates the number of
292 MTRRs which has been used.
296 MtrrGetMemoryAttributeInVariableMtrr (
297 IN UINT64 MtrrValidBitsMask
,
298 IN UINT64 MtrrValidAddressMask
,
299 OUT VARIABLE_MTRR
*VariableMtrr
304 This function prints all MTRRs for debugging.
308 MtrrDebugPrintAllMtrrs (
313 Checks if MTRR is supported.
315 @retval TRUE MTRR is supported.
316 @retval FALSE MTRR is not supported.
326 Returns the default MTRR cache type for the system.
328 @return The default MTRR cache type.
331 MTRR_MEMORY_CACHE_TYPE
333 MtrrGetDefaultMemoryType (
338 This function attempts to set the attributes into MTRR setting buffer for a memory range.
340 @param[in, out] MtrrSetting MTRR setting buffer to be set.
341 @param[in] BaseAddress The physical address that is the start address
343 @param[in] Length The size in bytes of the memory region.
344 @param[in] Attribute The bit mask of attributes to set for the
347 @retval RETURN_SUCCESS The attributes were set for the memory region.
348 @retval RETURN_INVALID_PARAMETER Length is zero.
349 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
350 memory resource range specified by BaseAddress and Length.
351 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
352 range specified by BaseAddress and Length.
353 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
354 BaseAddress and Length cannot be modified.
355 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
356 the memory resource range.
357 @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.
358 Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify
359 external scratch buffer.
363 MtrrSetMemoryAttributeInMtrrSettings (
364 IN OUT MTRR_SETTINGS
*MtrrSetting
,
365 IN PHYSICAL_ADDRESS BaseAddress
,
367 IN MTRR_MEMORY_CACHE_TYPE Attribute
371 This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.
373 @param[in, out] MtrrSetting MTRR setting buffer to be set.
374 @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.
375 @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.
376 It may be updated to the actual required size when the calculation
377 needs more scratch buffer.
378 @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.
379 When range overlap happens, the last one takes higher priority.
380 When the function returns, either all the attributes are set successfully,
381 or none of them is set.
382 @param[in] RangeCount Count of MTRR_MEMORY_RANGE.
384 @retval RETURN_SUCCESS The attributes were set for all the memory ranges.
385 @retval RETURN_INVALID_PARAMETER Length in any range is zero.
386 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the
387 memory resource range specified by BaseAddress and Length in any range.
388 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource
389 range specified by BaseAddress and Length in any range.
390 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
391 the memory resource ranges.
392 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by
393 BaseAddress and Length cannot be modified.
394 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.
398 MtrrSetMemoryAttributesInMtrrSettings (
399 IN OUT MTRR_SETTINGS
*MtrrSetting
,
401 IN OUT UINTN
*ScratchSize
,
402 IN CONST MTRR_MEMORY_RANGE
*Ranges
,
405 #endif // _MTRR_LIB_H_