]>
Commit | Line | Data |
---|---|---|
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 | |
38 | typedef 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 | |
47 | typedef 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 | |
59 | typedef 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 | |
67 | typedef 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 |
74 | typedef 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 | |
81 | typedef 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 | |
90 | typedef 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 | |
2bbd7e2f RN |
106 | typedef struct {\r |
107 | UINT64 BaseAddress;\r | |
108 | UINT64 Length;\r | |
109 | MTRR_MEMORY_CACHE_TYPE Type;\r | |
110 | } MTRR_MEMORY_RANGE;\r | |
111 | \r | |
3b9be416 JY |
112 | /**\r |
113 | Returns the variable MTRR count for the CPU.\r | |
114 | \r | |
115 | @return Variable MTRR count\r | |
116 | \r | |
117 | **/\r | |
118 | UINT32\r | |
ed8dfd7b | 119 | EFIAPI\r |
3b9be416 JY |
120 | GetVariableMtrrCount (\r |
121 | VOID\r | |
122 | );\r | |
123 | \r | |
124 | /**\r | |
125 | Returns the firmware usable variable MTRR count for the CPU.\r | |
126 | \r | |
127 | @return Firmware usable variable MTRR count\r | |
128 | \r | |
129 | **/\r | |
130 | UINT32\r | |
ed8dfd7b | 131 | EFIAPI\r |
3b9be416 JY |
132 | GetFirmwareVariableMtrrCount (\r |
133 | VOID\r | |
134 | );\r | |
135 | \r | |
e50466da | 136 | /**\r |
137 | This function attempts to set the attributes for a memory range.\r | |
138 | \r | |
76b4cae3 MK |
139 | @param[in] BaseAddress The physical address that is the start\r |
140 | address of a memory region.\r | |
141 | @param[in] Length The size in bytes of the memory region.\r | |
142 | @param[in] Attribute The bit mask of attributes to set for the\r | |
143 | memory region.\r | |
e50466da | 144 | \r |
76b4cae3 MK |
145 | @retval RETURN_SUCCESS The attributes were set for the memory\r |
146 | region.\r | |
e50466da | 147 | @retval RETURN_INVALID_PARAMETER Length is zero.\r |
76b4cae3 MK |
148 | @retval RETURN_UNSUPPORTED The processor does not support one or\r |
149 | more bytes of the memory resource range\r | |
150 | specified by BaseAddress and Length.\r | |
151 | @retval RETURN_UNSUPPORTED The bit mask of attributes is not support\r | |
152 | for the memory resource range specified\r | |
153 | by BaseAddress and Length.\r | |
154 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource\r | |
155 | range specified by BaseAddress and Length\r | |
156 | cannot be modified.\r | |
157 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to\r | |
158 | modify the attributes of the memory\r | |
159 | resource range.\r | |
2bbd7e2f | 160 | @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r |
e50466da | 161 | **/\r |
162 | RETURN_STATUS\r | |
163 | EFIAPI\r | |
164 | MtrrSetMemoryAttribute (\r | |
165 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
166 | IN UINT64 Length,\r | |
167 | IN MTRR_MEMORY_CACHE_TYPE Attribute\r | |
168 | );\r | |
169 | \r | |
170 | \r | |
171 | /**\r | |
172 | This function will get the memory cache type of the specific address.\r | |
173 | This function is mainly for debugging purposes.\r | |
174 | \r | |
76b4cae3 | 175 | @param[in] Address The specific address\r |
e50466da | 176 | \r |
177 | @return The memory cache type of the specific address\r | |
178 | \r | |
179 | **/\r | |
180 | MTRR_MEMORY_CACHE_TYPE\r | |
181 | EFIAPI\r | |
182 | MtrrGetMemoryAttribute (\r | |
183 | IN PHYSICAL_ADDRESS Address\r | |
184 | );\r | |
185 | \r | |
186 | \r | |
187 | /**\r | |
188 | This function will get the raw value in variable MTRRs\r | |
189 | \r | |
76b4cae3 | 190 | @param[out] VariableSettings A buffer to hold variable MTRRs content.\r |
e50466da | 191 | \r |
341fea64 | 192 | @return The buffer point to MTRR_VARIABLE_SETTINGS in which holds the content of the variable MTRR\r |
e50466da | 193 | \r |
194 | **/\r | |
195 | MTRR_VARIABLE_SETTINGS*\r | |
196 | EFIAPI\r | |
197 | MtrrGetVariableMtrr (\r | |
198 | OUT MTRR_VARIABLE_SETTINGS *VariableSettings\r | |
199 | );\r | |
200 | \r | |
201 | \r | |
202 | /**\r | |
341fea64 | 203 | This function sets variable MTRRs\r |
e50466da | 204 | \r |
76b4cae3 | 205 | @param[in] VariableSettings A buffer to hold variable MTRRs content.\r |
e50466da | 206 | \r |
207 | @return The pointer of VariableSettings\r | |
208 | \r | |
209 | **/\r | |
210 | MTRR_VARIABLE_SETTINGS*\r | |
211 | EFIAPI\r | |
212 | MtrrSetVariableMtrr (\r | |
213 | IN MTRR_VARIABLE_SETTINGS *VariableSettings\r | |
214 | );\r | |
215 | \r | |
216 | \r | |
217 | /**\r | |
218 | This function gets the content in fixed MTRRs\r | |
219 | \r | |
76b4cae3 | 220 | @param[out] FixedSettings A buffer to hold fixed MTRRs content.\r |
e50466da | 221 | \r |
222 | @return The pointer of FixedSettings\r | |
223 | \r | |
224 | **/\r | |
225 | MTRR_FIXED_SETTINGS*\r | |
226 | EFIAPI\r | |
227 | MtrrGetFixedMtrr (\r | |
228 | OUT MTRR_FIXED_SETTINGS *FixedSettings\r | |
229 | );\r | |
230 | \r | |
231 | \r | |
232 | /**\r | |
233 | This function sets fixed MTRRs\r | |
234 | \r | |
76b4cae3 | 235 | @param[in] FixedSettings A buffer holding fixed MTRRs content.\r |
e50466da | 236 | \r |
237 | @return The pointer of FixedSettings\r | |
238 | \r | |
239 | **/\r | |
240 | MTRR_FIXED_SETTINGS*\r | |
241 | EFIAPI\r | |
242 | MtrrSetFixedMtrr (\r | |
243 | IN MTRR_FIXED_SETTINGS *FixedSettings\r | |
244 | );\r | |
245 | \r | |
246 | \r | |
247 | /**\r | |
248 | This function gets the content in all MTRRs (variable and fixed)\r | |
249 | \r | |
76b4cae3 | 250 | @param[out] MtrrSetting A buffer to hold all MTRRs content.\r |
e50466da | 251 | \r |
252 | @return The pointer of MtrrSetting\r | |
253 | \r | |
254 | **/\r | |
255 | MTRR_SETTINGS *\r | |
256 | EFIAPI\r | |
257 | MtrrGetAllMtrrs (\r | |
258 | OUT MTRR_SETTINGS *MtrrSetting\r | |
259 | );\r | |
260 | \r | |
261 | \r | |
262 | /**\r | |
263 | This function sets all MTRRs (variable and fixed)\r | |
264 | \r | |
76b4cae3 | 265 | @param[in] MtrrSetting A buffer to hold all MTRRs content.\r |
e50466da | 266 | \r |
267 | @return The pointer of MtrrSetting\r | |
268 | \r | |
269 | **/\r | |
270 | MTRR_SETTINGS *\r | |
271 | EFIAPI\r | |
272 | MtrrSetAllMtrrs (\r | |
273 | IN MTRR_SETTINGS *MtrrSetting\r | |
274 | );\r | |
275 | \r | |
276 | \r | |
277 | /**\r | |
278 | Get the attribute of variable MTRRs.\r | |
279 | \r | |
280 | This function shadows the content of variable MTRRs into\r | |
281 | an internal array: VariableMtrr\r | |
282 | \r | |
76b4cae3 MK |
283 | @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR\r |
284 | @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in\r | |
285 | MTRR must align to 4K, so valid address mask equal to\r | |
286 | MtrrValidBitsMask & 0xfffffffffffff000ULL\r | |
287 | @param[out] VariableMtrr The array to shadow variable MTRRs content\r | |
288 | \r | |
438f1766 | 289 | @return The return value of this parameter indicates the number of\r |
3ba736f3 | 290 | MTRRs which has been used.\r |
e50466da | 291 | **/\r |
3ba736f3 | 292 | UINT32\r |
e50466da | 293 | EFIAPI\r |
294 | MtrrGetMemoryAttributeInVariableMtrr (\r | |
295 | IN UINT64 MtrrValidBitsMask,\r | |
296 | IN UINT64 MtrrValidAddressMask,\r | |
297 | OUT VARIABLE_MTRR *VariableMtrr\r | |
298 | );\r | |
299 | \r | |
300 | \r | |
301 | /**\r | |
302 | This function prints all MTRRs for debugging.\r | |
303 | **/\r | |
304 | VOID\r | |
ed8dfd7b | 305 | EFIAPI\r |
e50466da | 306 | MtrrDebugPrintAllMtrrs (\r |
430fbbe0 | 307 | VOID\r |
e50466da | 308 | );\r |
309 | \r | |
947a573a | 310 | /**\r |
311 | Checks if MTRR is supported.\r | |
312 | \r | |
313 | @retval TRUE MTRR is supported.\r | |
314 | @retval FALSE MTRR is not supported.\r | |
315 | \r | |
316 | **/\r | |
317 | BOOLEAN\r | |
ed8dfd7b | 318 | EFIAPI\r |
947a573a | 319 | IsMtrrSupported (\r |
320 | VOID\r | |
321 | );\r | |
322 | \r | |
91ec7824 | 323 | /**\r |
324 | Returns the default MTRR cache type for the system.\r | |
325 | \r | |
326 | @return The default MTRR cache type.\r | |
327 | \r | |
328 | **/\r | |
329 | MTRR_MEMORY_CACHE_TYPE\r | |
330 | EFIAPI\r | |
331 | MtrrGetDefaultMemoryType (\r | |
332 | VOID\r | |
333 | );\r | |
334 | \r | |
b970ed68 MK |
335 | /**\r |
336 | This function attempts to set the attributes into MTRR setting buffer for a memory range.\r | |
337 | \r | |
338 | @param[in, out] MtrrSetting MTRR setting buffer to be set.\r | |
339 | @param[in] BaseAddress The physical address that is the start address\r | |
340 | of a memory region.\r | |
341 | @param[in] Length The size in bytes of the memory region.\r | |
342 | @param[in] Attribute The bit mask of attributes to set for the\r | |
343 | memory region.\r | |
344 | \r | |
345 | @retval RETURN_SUCCESS The attributes were set for the memory region.\r | |
346 | @retval RETURN_INVALID_PARAMETER Length is zero.\r | |
347 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r | |
348 | memory resource range specified by BaseAddress and Length.\r | |
349 | @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r | |
350 | range specified by BaseAddress and Length.\r | |
351 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
352 | BaseAddress and Length cannot be modified.\r | |
353 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
354 | the memory resource range.\r | |
2bbd7e2f | 355 | @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r |
b970ed68 MK |
356 | **/\r |
357 | RETURN_STATUS\r | |
358 | EFIAPI\r | |
359 | MtrrSetMemoryAttributeInMtrrSettings (\r | |
360 | IN OUT MTRR_SETTINGS *MtrrSetting,\r | |
361 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
362 | IN UINT64 Length,\r | |
363 | IN MTRR_MEMORY_CACHE_TYPE Attribute\r | |
364 | );\r | |
365 | \r | |
2bbd7e2f RN |
366 | /**\r |
367 | This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.\r | |
368 | \r | |
369 | @param[in, out] MtrrSetting MTRR setting buffer to be set.\r | |
370 | @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.\r | |
371 | @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.\r | |
372 | It may be updated to the actual required size when the calculation\r | |
373 | needs more scratch buffer.\r | |
374 | @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.\r | |
375 | When range overlap happens, the last one takes higher priority.\r | |
376 | When the function returns, either all the attributes are set successfully,\r | |
377 | or none of them is set.\r | |
378 | @param[in] Count of MTRR_MEMORY_RANGE.\r | |
379 | \r | |
380 | @retval RETURN_SUCCESS The attributes were set for all the memory ranges.\r | |
381 | @retval RETURN_INVALID_PARAMETER Length in any range is zero.\r | |
382 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r | |
383 | memory resource range specified by BaseAddress and Length in any range.\r | |
384 | @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r | |
385 | range specified by BaseAddress and Length in any range.\r | |
386 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
387 | the memory resource ranges.\r | |
388 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
389 | BaseAddress and Length cannot be modified.\r | |
390 | @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r | |
391 | **/\r | |
392 | RETURN_STATUS\r | |
393 | EFIAPI\r | |
394 | MtrrSetMemoryAttributesInMtrrSettings (\r | |
395 | IN OUT MTRR_SETTINGS *MtrrSetting,\r | |
396 | IN VOID *Scratch,\r | |
397 | IN OUT UINTN *ScratchSize,\r | |
398 | IN CONST MTRR_MEMORY_RANGE *Ranges,\r | |
399 | IN UINTN RangeCount\r | |
400 | );\r | |
e50466da | 401 | #endif // _MTRR_LIB_H_\r |