]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Include/Library/MtrrLib.h
UefiCpuPkg/MtrrLib: Optimize MtrrLibLeastAlignment()
[mirror_edk2.git] / UefiCpuPkg / Include / Library / MtrrLib.h
CommitLineData
e50466da 1/** @file\r
2 MTRR setting library\r
3\r
341fea64 4 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>\r
01a1c0fc 5 This program and the accompanying materials\r
e50466da 6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#ifndef _MTRR_LIB_H_\r
16#define _MTRR_LIB_H_\r
17\r
18//\r
341fea64 19// According to IA32 SDM, MTRRs number and MSR offset are always consistent\r
e50466da 20// for IA32 processor family\r
21//\r
3b9be416
JY
22\r
23//\r
3ba736f3 24// The semantics of below macro is MAX_MTRR_NUMBER_OF_VARIABLE_MTRR, the real number can be read out from MTRR_CAP register.\r
3b9be416 25//\r
3ba736f3 26#define MTRR_NUMBER_OF_VARIABLE_MTRR 32\r
3b9be416
JY
27//\r
28// Firmware need reserve 2 MTRR for OS\r
46309b11 29// Note: It is replaced by PCD PcdCpuNumberOfReservedVariableMtrrs\r
3b9be416
JY
30//\r
31#define RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER 2\r
32\r
e50466da 33#define MTRR_NUMBER_OF_FIXED_MTRR 11\r
e50466da 34\r
35//\r
36// Structure to describe a fixed MTRR\r
37//\r
38typedef struct {\r
39 UINT32 Msr;\r
40 UINT32 BaseAddress;\r
41 UINT32 Length;\r
42} FIXED_MTRR;\r
43\r
44//\r
45// Structure to describe a variable MTRR\r
46//\r
47typedef struct {\r
48 UINT64 BaseAddress;\r
49 UINT64 Length;\r
50 UINT64 Type;\r
51 UINT32 Msr;\r
52 BOOLEAN Valid;\r
53 BOOLEAN Used;\r
54} VARIABLE_MTRR;\r
55\r
56//\r
57// Structure to hold base and mask pair for variable MTRR register\r
58//\r
59typedef struct _MTRR_VARIABLE_SETTING_ {\r
76b4cae3
MK
60 UINT64 Base;\r
61 UINT64 Mask;\r
e50466da 62} MTRR_VARIABLE_SETTING;\r
63\r
64//\r
65// Array for variable MTRRs\r
66//\r
67typedef struct _MTRR_VARIABLE_SETTINGS_ {\r
341fea64
RN
68 MTRR_VARIABLE_SETTING Mtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r
69} MTRR_VARIABLE_SETTINGS;\r
e50466da 70\r
71//\r
341fea64 72// Array for fixed MTRRs\r
e50466da 73//\r
74typedef struct _MTRR_FIXED_SETTINGS_ {\r
75 UINT64 Mtrr[MTRR_NUMBER_OF_FIXED_MTRR];\r
76} MTRR_FIXED_SETTINGS;\r
77\r
78//\r
79// Structure to hold all MTRRs\r
80//\r
81typedef struct _MTRR_SETTINGS_ {\r
82 MTRR_FIXED_SETTINGS Fixed;\r
83 MTRR_VARIABLE_SETTINGS Variables;\r
84 UINT64 MtrrDefType;\r
85} MTRR_SETTINGS;\r
86\r
87//\r
88// Memory cache types\r
89//\r
90typedef enum {\r
76b4cae3
MK
91 CacheUncacheable = 0,\r
92 CacheWriteCombining = 1,\r
93 CacheWriteThrough = 4,\r
94 CacheWriteProtected = 5,\r
78c49925
RN
95 CacheWriteBack = 6,\r
96 CacheInvalid = 7\r
e50466da 97} MTRR_MEMORY_CACHE_TYPE;\r
98\r
99#define MTRR_CACHE_UNCACHEABLE 0\r
100#define MTRR_CACHE_WRITE_COMBINING 1\r
101#define MTRR_CACHE_WRITE_THROUGH 4\r
102#define MTRR_CACHE_WRITE_PROTECTED 5\r
103#define MTRR_CACHE_WRITE_BACK 6\r
104#define MTRR_CACHE_INVALID_TYPE 7\r
105\r
3b9be416
JY
106/**\r
107 Returns the variable MTRR count for the CPU.\r
108\r
109 @return Variable MTRR count\r
110\r
111**/\r
112UINT32\r
ed8dfd7b 113EFIAPI\r
3b9be416
JY
114GetVariableMtrrCount (\r
115 VOID\r
116 );\r
117\r
118/**\r
119 Returns the firmware usable variable MTRR count for the CPU.\r
120\r
121 @return Firmware usable variable MTRR count\r
122\r
123**/\r
124UINT32\r
ed8dfd7b 125EFIAPI\r
3b9be416
JY
126GetFirmwareVariableMtrrCount (\r
127 VOID\r
128 );\r
129\r
e50466da 130/**\r
131 This function attempts to set the attributes for a memory range.\r
132\r
76b4cae3
MK
133 @param[in] BaseAddress The physical address that is the start\r
134 address of a memory region.\r
135 @param[in] Length The size in bytes of the memory region.\r
136 @param[in] Attribute The bit mask of attributes to set for the\r
137 memory region.\r
e50466da 138\r
76b4cae3
MK
139 @retval RETURN_SUCCESS The attributes were set for the memory\r
140 region.\r
e50466da 141 @retval RETURN_INVALID_PARAMETER Length is zero.\r
76b4cae3
MK
142 @retval RETURN_UNSUPPORTED The processor does not support one or\r
143 more bytes of the memory resource range\r
144 specified by BaseAddress and Length.\r
145 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support\r
146 for the memory resource range specified\r
147 by BaseAddress and Length.\r
148 @retval RETURN_ACCESS_DENIED The attributes for the memory resource\r
149 range specified by BaseAddress and Length\r
150 cannot be modified.\r
151 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to\r
152 modify the attributes of the memory\r
153 resource range.\r
e50466da 154\r
155**/\r
156RETURN_STATUS\r
157EFIAPI\r
158MtrrSetMemoryAttribute (\r
159 IN PHYSICAL_ADDRESS BaseAddress,\r
160 IN UINT64 Length,\r
161 IN MTRR_MEMORY_CACHE_TYPE Attribute\r
162 );\r
163\r
164\r
165/**\r
166 This function will get the memory cache type of the specific address.\r
167 This function is mainly for debugging purposes.\r
168\r
76b4cae3 169 @param[in] Address The specific address\r
e50466da 170\r
171 @return The memory cache type of the specific address\r
172\r
173**/\r
174MTRR_MEMORY_CACHE_TYPE\r
175EFIAPI\r
176MtrrGetMemoryAttribute (\r
177 IN PHYSICAL_ADDRESS Address\r
178 );\r
179\r
180\r
181/**\r
182 This function will get the raw value in variable MTRRs\r
183\r
76b4cae3 184 @param[out] VariableSettings A buffer to hold variable MTRRs content.\r
e50466da 185\r
341fea64 186 @return The buffer point to MTRR_VARIABLE_SETTINGS in which holds the content of the variable MTRR\r
e50466da 187\r
188**/\r
189MTRR_VARIABLE_SETTINGS*\r
190EFIAPI\r
191MtrrGetVariableMtrr (\r
192 OUT MTRR_VARIABLE_SETTINGS *VariableSettings\r
193 );\r
194\r
195\r
196/**\r
341fea64 197 This function sets variable MTRRs\r
e50466da 198\r
76b4cae3 199 @param[in] VariableSettings A buffer to hold variable MTRRs content.\r
e50466da 200\r
201 @return The pointer of VariableSettings\r
202\r
203**/\r
204MTRR_VARIABLE_SETTINGS*\r
205EFIAPI\r
206MtrrSetVariableMtrr (\r
207 IN MTRR_VARIABLE_SETTINGS *VariableSettings\r
208 );\r
209\r
210\r
211/**\r
212 This function gets the content in fixed MTRRs\r
213\r
76b4cae3 214 @param[out] FixedSettings A buffer to hold fixed MTRRs content.\r
e50466da 215\r
216 @return The pointer of FixedSettings\r
217\r
218**/\r
219MTRR_FIXED_SETTINGS*\r
220EFIAPI\r
221MtrrGetFixedMtrr (\r
222 OUT MTRR_FIXED_SETTINGS *FixedSettings\r
223 );\r
224\r
225\r
226/**\r
227 This function sets fixed MTRRs\r
228\r
76b4cae3 229 @param[in] FixedSettings A buffer holding fixed MTRRs content.\r
e50466da 230\r
231 @return The pointer of FixedSettings\r
232\r
233**/\r
234MTRR_FIXED_SETTINGS*\r
235EFIAPI\r
236MtrrSetFixedMtrr (\r
237 IN MTRR_FIXED_SETTINGS *FixedSettings\r
238 );\r
239\r
240\r
241/**\r
242 This function gets the content in all MTRRs (variable and fixed)\r
243\r
76b4cae3 244 @param[out] MtrrSetting A buffer to hold all MTRRs content.\r
e50466da 245\r
246 @return The pointer of MtrrSetting\r
247\r
248**/\r
249MTRR_SETTINGS *\r
250EFIAPI\r
251MtrrGetAllMtrrs (\r
252 OUT MTRR_SETTINGS *MtrrSetting\r
253 );\r
254\r
255\r
256/**\r
257 This function sets all MTRRs (variable and fixed)\r
258\r
76b4cae3 259 @param[in] MtrrSetting A buffer to hold all MTRRs content.\r
e50466da 260\r
261 @return The pointer of MtrrSetting\r
262\r
263**/\r
264MTRR_SETTINGS *\r
265EFIAPI\r
266MtrrSetAllMtrrs (\r
267 IN MTRR_SETTINGS *MtrrSetting\r
268 );\r
269\r
270\r
271/**\r
272 Get the attribute of variable MTRRs.\r
273\r
274 This function shadows the content of variable MTRRs into\r
275 an internal array: VariableMtrr\r
276\r
76b4cae3
MK
277 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR\r
278 @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in\r
279 MTRR must align to 4K, so valid address mask equal to\r
280 MtrrValidBitsMask & 0xfffffffffffff000ULL\r
281 @param[out] VariableMtrr The array to shadow variable MTRRs content\r
282\r
438f1766 283 @return The return value of this parameter indicates the number of\r
3ba736f3 284 MTRRs which has been used.\r
e50466da 285**/\r
3ba736f3 286UINT32\r
e50466da 287EFIAPI\r
288MtrrGetMemoryAttributeInVariableMtrr (\r
289 IN UINT64 MtrrValidBitsMask,\r
290 IN UINT64 MtrrValidAddressMask,\r
291 OUT VARIABLE_MTRR *VariableMtrr\r
292 );\r
293\r
294\r
295/**\r
296 This function prints all MTRRs for debugging.\r
297**/\r
298VOID\r
ed8dfd7b 299EFIAPI\r
e50466da 300MtrrDebugPrintAllMtrrs (\r
430fbbe0 301 VOID\r
e50466da 302 );\r
303\r
947a573a 304/**\r
305 Checks if MTRR is supported.\r
306\r
307 @retval TRUE MTRR is supported.\r
308 @retval FALSE MTRR is not supported.\r
309\r
310**/\r
311BOOLEAN\r
ed8dfd7b 312EFIAPI\r
947a573a 313IsMtrrSupported (\r
314 VOID\r
315 );\r
316\r
91ec7824 317/**\r
318 Returns the default MTRR cache type for the system.\r
319\r
320 @return The default MTRR cache type.\r
321\r
322**/\r
323MTRR_MEMORY_CACHE_TYPE\r
324EFIAPI\r
325MtrrGetDefaultMemoryType (\r
326 VOID\r
327 );\r
328\r
b970ed68
MK
329/**\r
330 This function attempts to set the attributes into MTRR setting buffer for a memory range.\r
331\r
332 @param[in, out] MtrrSetting MTRR setting buffer to be set.\r
333 @param[in] BaseAddress The physical address that is the start address\r
334 of a memory region.\r
335 @param[in] Length The size in bytes of the memory region.\r
336 @param[in] Attribute The bit mask of attributes to set for the\r
337 memory region.\r
338\r
339 @retval RETURN_SUCCESS The attributes were set for the memory region.\r
340 @retval RETURN_INVALID_PARAMETER Length is zero.\r
341 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r
342 memory resource range specified by BaseAddress and Length.\r
343 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r
344 range specified by BaseAddress and Length.\r
345 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r
346 BaseAddress and Length cannot be modified.\r
347 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
348 the memory resource range.\r
349\r
350**/\r
351RETURN_STATUS\r
352EFIAPI\r
353MtrrSetMemoryAttributeInMtrrSettings (\r
354 IN OUT MTRR_SETTINGS *MtrrSetting,\r
355 IN PHYSICAL_ADDRESS BaseAddress,\r
356 IN UINT64 Length,\r
357 IN MTRR_MEMORY_CACHE_TYPE Attribute\r
358 );\r
359\r
e50466da 360#endif // _MTRR_LIB_H_\r