]>
Commit | Line | Data |
---|---|---|
e50466da | 1 | /** @file\r |
2 | MTRR setting library\r | |
3 | \r | |
abc16e25 | 4 | Copyright (c) 2008 - 2020, Intel Corporation. All rights reserved.<BR>\r |
0acd8697 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
e50466da | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #ifndef _MTRR_LIB_H_\r | |
10 | #define _MTRR_LIB_H_\r | |
11 | \r | |
12 | //\r | |
341fea64 | 13 | // According to IA32 SDM, MTRRs number and MSR offset are always consistent\r |
e50466da | 14 | // for IA32 processor family\r |
15 | //\r | |
3b9be416 JY |
16 | \r |
17 | //\r | |
3ba736f3 | 18 | // 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 | 19 | //\r |
3ba736f3 | 20 | #define MTRR_NUMBER_OF_VARIABLE_MTRR 32\r |
3b9be416 JY |
21 | //\r |
22 | // Firmware need reserve 2 MTRR for OS\r | |
46309b11 | 23 | // Note: It is replaced by PCD PcdCpuNumberOfReservedVariableMtrrs\r |
3b9be416 JY |
24 | //\r |
25 | #define RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER 2\r | |
26 | \r | |
e50466da | 27 | #define MTRR_NUMBER_OF_FIXED_MTRR 11\r |
e50466da | 28 | \r |
29 | //\r | |
30 | // Structure to describe a fixed MTRR\r | |
31 | //\r | |
32 | typedef struct {\r | |
33 | UINT32 Msr;\r | |
34 | UINT32 BaseAddress;\r | |
35 | UINT32 Length;\r | |
36 | } FIXED_MTRR;\r | |
37 | \r | |
38 | //\r | |
39 | // Structure to describe a variable MTRR\r | |
40 | //\r | |
41 | typedef struct {\r | |
42 | UINT64 BaseAddress;\r | |
43 | UINT64 Length;\r | |
44 | UINT64 Type;\r | |
45 | UINT32 Msr;\r | |
46 | BOOLEAN Valid;\r | |
47 | BOOLEAN Used;\r | |
48 | } VARIABLE_MTRR;\r | |
49 | \r | |
50 | //\r | |
51 | // Structure to hold base and mask pair for variable MTRR register\r | |
52 | //\r | |
53 | typedef struct _MTRR_VARIABLE_SETTING_ {\r | |
76b4cae3 MK |
54 | UINT64 Base;\r |
55 | UINT64 Mask;\r | |
e50466da | 56 | } MTRR_VARIABLE_SETTING;\r |
57 | \r | |
58 | //\r | |
59 | // Array for variable MTRRs\r | |
60 | //\r | |
61 | typedef struct _MTRR_VARIABLE_SETTINGS_ {\r | |
341fea64 RN |
62 | MTRR_VARIABLE_SETTING Mtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r |
63 | } MTRR_VARIABLE_SETTINGS;\r | |
e50466da | 64 | \r |
65 | //\r | |
341fea64 | 66 | // Array for fixed MTRRs\r |
e50466da | 67 | //\r |
68 | typedef struct _MTRR_FIXED_SETTINGS_ {\r | |
69 | UINT64 Mtrr[MTRR_NUMBER_OF_FIXED_MTRR];\r | |
70 | } MTRR_FIXED_SETTINGS;\r | |
71 | \r | |
72 | //\r | |
73 | // Structure to hold all MTRRs\r | |
74 | //\r | |
75 | typedef struct _MTRR_SETTINGS_ {\r | |
76 | MTRR_FIXED_SETTINGS Fixed;\r | |
77 | MTRR_VARIABLE_SETTINGS Variables;\r | |
78 | UINT64 MtrrDefType;\r | |
79 | } MTRR_SETTINGS;\r | |
80 | \r | |
81 | //\r | |
82 | // Memory cache types\r | |
83 | //\r | |
84 | typedef enum {\r | |
76b4cae3 MK |
85 | CacheUncacheable = 0,\r |
86 | CacheWriteCombining = 1,\r | |
87 | CacheWriteThrough = 4,\r | |
88 | CacheWriteProtected = 5,\r | |
78c49925 RN |
89 | CacheWriteBack = 6,\r |
90 | CacheInvalid = 7\r | |
e50466da | 91 | } MTRR_MEMORY_CACHE_TYPE;\r |
92 | \r | |
93 | #define MTRR_CACHE_UNCACHEABLE 0\r | |
94 | #define MTRR_CACHE_WRITE_COMBINING 1\r | |
95 | #define MTRR_CACHE_WRITE_THROUGH 4\r | |
96 | #define MTRR_CACHE_WRITE_PROTECTED 5\r | |
97 | #define MTRR_CACHE_WRITE_BACK 6\r | |
98 | #define MTRR_CACHE_INVALID_TYPE 7\r | |
99 | \r | |
2bbd7e2f RN |
100 | typedef struct {\r |
101 | UINT64 BaseAddress;\r | |
102 | UINT64 Length;\r | |
103 | MTRR_MEMORY_CACHE_TYPE Type;\r | |
104 | } MTRR_MEMORY_RANGE;\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 | |
112 | UINT32\r | |
ed8dfd7b | 113 | EFIAPI\r |
3b9be416 JY |
114 | GetVariableMtrrCount (\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 | |
124 | UINT32\r | |
ed8dfd7b | 125 | EFIAPI\r |
3b9be416 JY |
126 | GetFirmwareVariableMtrrCount (\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 | |
bc6a8b78 RN |
154 | Multiple memory range attributes setting by calling this API multiple\r |
155 | times may fail with status RETURN_OUT_OF_RESOURCES. It may not mean\r | |
156 | the number of CPU MTRRs are too small to set such memory attributes.\r | |
157 | Pass the multiple memory range attributes to one call of\r | |
158 | MtrrSetMemoryAttributesInMtrrSettings() may succeed.\r | |
8b2eb7b3 RN |
159 | @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.\r |
160 | Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify\r | |
161 | external scratch buffer.\r | |
e50466da | 162 | **/\r |
163 | RETURN_STATUS\r | |
164 | EFIAPI\r | |
165 | MtrrSetMemoryAttribute (\r | |
166 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
167 | IN UINT64 Length,\r | |
168 | IN MTRR_MEMORY_CACHE_TYPE Attribute\r | |
169 | );\r | |
170 | \r | |
171 | \r | |
172 | /**\r | |
173 | This function will get the memory cache type of the specific address.\r | |
174 | This function is mainly for debugging purposes.\r | |
175 | \r | |
76b4cae3 | 176 | @param[in] Address The specific address\r |
e50466da | 177 | \r |
178 | @return The memory cache type of the specific address\r | |
179 | \r | |
180 | **/\r | |
181 | MTRR_MEMORY_CACHE_TYPE\r | |
182 | EFIAPI\r | |
183 | MtrrGetMemoryAttribute (\r | |
184 | IN PHYSICAL_ADDRESS Address\r | |
185 | );\r | |
186 | \r | |
187 | \r | |
e50466da | 188 | /**\r |
189 | This function gets the content in fixed MTRRs\r | |
190 | \r | |
76b4cae3 | 191 | @param[out] FixedSettings A buffer to hold fixed MTRRs content.\r |
e50466da | 192 | \r |
193 | @return The pointer of FixedSettings\r | |
194 | \r | |
195 | **/\r | |
196 | MTRR_FIXED_SETTINGS*\r | |
197 | EFIAPI\r | |
198 | MtrrGetFixedMtrr (\r | |
199 | OUT MTRR_FIXED_SETTINGS *FixedSettings\r | |
200 | );\r | |
201 | \r | |
202 | \r | |
203 | /**\r | |
204 | This function sets fixed MTRRs\r | |
205 | \r | |
76b4cae3 | 206 | @param[in] FixedSettings A buffer holding fixed MTRRs content.\r |
e50466da | 207 | \r |
208 | @return The pointer of FixedSettings\r | |
209 | \r | |
210 | **/\r | |
211 | MTRR_FIXED_SETTINGS*\r | |
212 | EFIAPI\r | |
213 | MtrrSetFixedMtrr (\r | |
214 | IN MTRR_FIXED_SETTINGS *FixedSettings\r | |
215 | );\r | |
216 | \r | |
217 | \r | |
218 | /**\r | |
219 | This function gets the content in all MTRRs (variable and fixed)\r | |
220 | \r | |
76b4cae3 | 221 | @param[out] MtrrSetting A buffer to hold all MTRRs content.\r |
e50466da | 222 | \r |
223 | @return The pointer of MtrrSetting\r | |
224 | \r | |
225 | **/\r | |
226 | MTRR_SETTINGS *\r | |
227 | EFIAPI\r | |
228 | MtrrGetAllMtrrs (\r | |
229 | OUT MTRR_SETTINGS *MtrrSetting\r | |
230 | );\r | |
231 | \r | |
232 | \r | |
233 | /**\r | |
234 | This function sets all MTRRs (variable and fixed)\r | |
235 | \r | |
76b4cae3 | 236 | @param[in] MtrrSetting A buffer to hold all MTRRs content.\r |
e50466da | 237 | \r |
238 | @return The pointer of MtrrSetting\r | |
239 | \r | |
240 | **/\r | |
241 | MTRR_SETTINGS *\r | |
242 | EFIAPI\r | |
243 | MtrrSetAllMtrrs (\r | |
244 | IN MTRR_SETTINGS *MtrrSetting\r | |
245 | );\r | |
246 | \r | |
247 | \r | |
248 | /**\r | |
249 | Get the attribute of variable MTRRs.\r | |
250 | \r | |
251 | This function shadows the content of variable MTRRs into\r | |
252 | an internal array: VariableMtrr\r | |
253 | \r | |
76b4cae3 MK |
254 | @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR\r |
255 | @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in\r | |
256 | MTRR must align to 4K, so valid address mask equal to\r | |
257 | MtrrValidBitsMask & 0xfffffffffffff000ULL\r | |
258 | @param[out] VariableMtrr The array to shadow variable MTRRs content\r | |
259 | \r | |
438f1766 | 260 | @return The return value of this parameter indicates the number of\r |
3ba736f3 | 261 | MTRRs which has been used.\r |
e50466da | 262 | **/\r |
3ba736f3 | 263 | UINT32\r |
e50466da | 264 | EFIAPI\r |
265 | MtrrGetMemoryAttributeInVariableMtrr (\r | |
266 | IN UINT64 MtrrValidBitsMask,\r | |
267 | IN UINT64 MtrrValidAddressMask,\r | |
268 | OUT VARIABLE_MTRR *VariableMtrr\r | |
269 | );\r | |
270 | \r | |
271 | \r | |
272 | /**\r | |
273 | This function prints all MTRRs for debugging.\r | |
274 | **/\r | |
275 | VOID\r | |
ed8dfd7b | 276 | EFIAPI\r |
e50466da | 277 | MtrrDebugPrintAllMtrrs (\r |
430fbbe0 | 278 | VOID\r |
e50466da | 279 | );\r |
280 | \r | |
947a573a | 281 | /**\r |
282 | Checks if MTRR is supported.\r | |
283 | \r | |
284 | @retval TRUE MTRR is supported.\r | |
285 | @retval FALSE MTRR is not supported.\r | |
286 | \r | |
287 | **/\r | |
288 | BOOLEAN\r | |
ed8dfd7b | 289 | EFIAPI\r |
947a573a | 290 | IsMtrrSupported (\r |
291 | VOID\r | |
292 | );\r | |
293 | \r | |
91ec7824 | 294 | /**\r |
295 | Returns the default MTRR cache type for the system.\r | |
296 | \r | |
297 | @return The default MTRR cache type.\r | |
298 | \r | |
299 | **/\r | |
300 | MTRR_MEMORY_CACHE_TYPE\r | |
301 | EFIAPI\r | |
302 | MtrrGetDefaultMemoryType (\r | |
303 | VOID\r | |
304 | );\r | |
305 | \r | |
b970ed68 MK |
306 | /**\r |
307 | This function attempts to set the attributes into MTRR setting buffer for a memory range.\r | |
308 | \r | |
309 | @param[in, out] MtrrSetting MTRR setting buffer to be set.\r | |
310 | @param[in] BaseAddress The physical address that is the start address\r | |
311 | of a memory region.\r | |
312 | @param[in] Length The size in bytes of the memory region.\r | |
313 | @param[in] Attribute The bit mask of attributes to set for the\r | |
314 | memory region.\r | |
315 | \r | |
316 | @retval RETURN_SUCCESS The attributes were set for the memory region.\r | |
317 | @retval RETURN_INVALID_PARAMETER Length is zero.\r | |
318 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r | |
319 | memory resource range specified by BaseAddress and Length.\r | |
320 | @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r | |
321 | range specified by BaseAddress and Length.\r | |
322 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
323 | BaseAddress and Length cannot be modified.\r | |
324 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
325 | the memory resource range.\r | |
bc6a8b78 RN |
326 | Multiple memory range attributes setting by calling this API multiple\r |
327 | times may fail with status RETURN_OUT_OF_RESOURCES. It may not mean\r | |
328 | the number of CPU MTRRs are too small to set such memory attributes.\r | |
329 | Pass the multiple memory range attributes to one call of\r | |
330 | MtrrSetMemoryAttributesInMtrrSettings() may succeed.\r | |
8b2eb7b3 RN |
331 | @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.\r |
332 | Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify\r | |
333 | external scratch buffer.\r | |
b970ed68 MK |
334 | **/\r |
335 | RETURN_STATUS\r | |
336 | EFIAPI\r | |
337 | MtrrSetMemoryAttributeInMtrrSettings (\r | |
338 | IN OUT MTRR_SETTINGS *MtrrSetting,\r | |
339 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
340 | IN UINT64 Length,\r | |
341 | IN MTRR_MEMORY_CACHE_TYPE Attribute\r | |
342 | );\r | |
343 | \r | |
2bbd7e2f RN |
344 | /**\r |
345 | This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.\r | |
346 | \r | |
347 | @param[in, out] MtrrSetting MTRR setting buffer to be set.\r | |
348 | @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.\r | |
349 | @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.\r | |
350 | It may be updated to the actual required size when the calculation\r | |
351 | needs more scratch buffer.\r | |
352 | @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.\r | |
353 | When range overlap happens, the last one takes higher priority.\r | |
354 | When the function returns, either all the attributes are set successfully,\r | |
355 | or none of them is set.\r | |
f6194f5a | 356 | @param[in] RangeCount Count of MTRR_MEMORY_RANGE.\r |
2bbd7e2f RN |
357 | \r |
358 | @retval RETURN_SUCCESS The attributes were set for all the memory ranges.\r | |
359 | @retval RETURN_INVALID_PARAMETER Length in any range is zero.\r | |
360 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r | |
361 | memory resource range specified by BaseAddress and Length in any range.\r | |
362 | @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r | |
363 | range specified by BaseAddress and Length in any range.\r | |
364 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
365 | the memory resource ranges.\r | |
366 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
367 | BaseAddress and Length cannot be modified.\r | |
368 | @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r | |
369 | **/\r | |
370 | RETURN_STATUS\r | |
371 | EFIAPI\r | |
372 | MtrrSetMemoryAttributesInMtrrSettings (\r | |
373 | IN OUT MTRR_SETTINGS *MtrrSetting,\r | |
374 | IN VOID *Scratch,\r | |
375 | IN OUT UINTN *ScratchSize,\r | |
376 | IN CONST MTRR_MEMORY_RANGE *Ranges,\r | |
377 | IN UINTN RangeCount\r | |
378 | );\r | |
e50466da | 379 | #endif // _MTRR_LIB_H_\r |