]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/Include/Library/MtrrLib.h
UefiCpuPkg: Check invalid RegisterCpuFeature parameter
[mirror_edk2.git] / UefiCpuPkg / Include / Library / MtrrLib.h
... / ...
CommitLineData
1/** @file\r
2 MTRR setting library\r
3\r
4 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
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
19// According to IA32 SDM, MTRRs number and MSR offset are always consistent\r
20// for IA32 processor family\r
21//\r
22\r
23//\r
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
25//\r
26#define MTRR_NUMBER_OF_VARIABLE_MTRR 32\r
27//\r
28// Firmware need reserve 2 MTRR for OS\r
29// Note: It is replaced by PCD PcdCpuNumberOfReservedVariableMtrrs\r
30//\r
31#define RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER 2\r
32\r
33#define MTRR_NUMBER_OF_FIXED_MTRR 11\r
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
60 UINT64 Base;\r
61 UINT64 Mask;\r
62} MTRR_VARIABLE_SETTING;\r
63\r
64//\r
65// Array for variable MTRRs\r
66//\r
67typedef struct _MTRR_VARIABLE_SETTINGS_ {\r
68 MTRR_VARIABLE_SETTING Mtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r
69} MTRR_VARIABLE_SETTINGS;\r
70\r
71//\r
72// Array for fixed MTRRs\r
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
91 CacheUncacheable = 0,\r
92 CacheWriteCombining = 1,\r
93 CacheWriteThrough = 4,\r
94 CacheWriteProtected = 5,\r
95 CacheWriteBack = 6,\r
96 CacheInvalid = 7\r
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
106typedef struct {\r
107 UINT64 BaseAddress;\r
108 UINT64 Length;\r
109 MTRR_MEMORY_CACHE_TYPE Type;\r
110} MTRR_MEMORY_RANGE;\r
111\r
112/**\r
113 Returns the variable MTRR count for the CPU.\r
114\r
115 @return Variable MTRR count\r
116\r
117**/\r
118UINT32\r
119EFIAPI\r
120GetVariableMtrrCount (\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
130UINT32\r
131EFIAPI\r
132GetFirmwareVariableMtrrCount (\r
133 VOID\r
134 );\r
135\r
136/**\r
137 This function attempts to set the attributes for a memory range.\r
138\r
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
144\r
145 @retval RETURN_SUCCESS The attributes were set for the memory\r
146 region.\r
147 @retval RETURN_INVALID_PARAMETER Length is zero.\r
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
160 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r
161**/\r
162RETURN_STATUS\r
163EFIAPI\r
164MtrrSetMemoryAttribute (\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
175 @param[in] Address The specific address\r
176\r
177 @return The memory cache type of the specific address\r
178\r
179**/\r
180MTRR_MEMORY_CACHE_TYPE\r
181EFIAPI\r
182MtrrGetMemoryAttribute (\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
190 @param[out] VariableSettings A buffer to hold variable MTRRs content.\r
191\r
192 @return The buffer point to MTRR_VARIABLE_SETTINGS in which holds the content of the variable MTRR\r
193\r
194**/\r
195MTRR_VARIABLE_SETTINGS*\r
196EFIAPI\r
197MtrrGetVariableMtrr (\r
198 OUT MTRR_VARIABLE_SETTINGS *VariableSettings\r
199 );\r
200\r
201\r
202/**\r
203 This function sets variable MTRRs\r
204\r
205 @param[in] VariableSettings A buffer to hold variable MTRRs content.\r
206\r
207 @return The pointer of VariableSettings\r
208\r
209**/\r
210MTRR_VARIABLE_SETTINGS*\r
211EFIAPI\r
212MtrrSetVariableMtrr (\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
220 @param[out] FixedSettings A buffer to hold fixed MTRRs content.\r
221\r
222 @return The pointer of FixedSettings\r
223\r
224**/\r
225MTRR_FIXED_SETTINGS*\r
226EFIAPI\r
227MtrrGetFixedMtrr (\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
235 @param[in] FixedSettings A buffer holding fixed MTRRs content.\r
236\r
237 @return The pointer of FixedSettings\r
238\r
239**/\r
240MTRR_FIXED_SETTINGS*\r
241EFIAPI\r
242MtrrSetFixedMtrr (\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
250 @param[out] MtrrSetting A buffer to hold all MTRRs content.\r
251\r
252 @return The pointer of MtrrSetting\r
253\r
254**/\r
255MTRR_SETTINGS *\r
256EFIAPI\r
257MtrrGetAllMtrrs (\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
265 @param[in] MtrrSetting A buffer to hold all MTRRs content.\r
266\r
267 @return The pointer of MtrrSetting\r
268\r
269**/\r
270MTRR_SETTINGS *\r
271EFIAPI\r
272MtrrSetAllMtrrs (\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
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
289 @return The return value of this parameter indicates the number of\r
290 MTRRs which has been used.\r
291**/\r
292UINT32\r
293EFIAPI\r
294MtrrGetMemoryAttributeInVariableMtrr (\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
304VOID\r
305EFIAPI\r
306MtrrDebugPrintAllMtrrs (\r
307 VOID\r
308 );\r
309\r
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
317BOOLEAN\r
318EFIAPI\r
319IsMtrrSupported (\r
320 VOID\r
321 );\r
322\r
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
329MTRR_MEMORY_CACHE_TYPE\r
330EFIAPI\r
331MtrrGetDefaultMemoryType (\r
332 VOID\r
333 );\r
334\r
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
355 @retval RETURN_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r
356**/\r
357RETURN_STATUS\r
358EFIAPI\r
359MtrrSetMemoryAttributeInMtrrSettings (\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
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] RangeCount 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
392RETURN_STATUS\r
393EFIAPI\r
394MtrrSetMemoryAttributesInMtrrSettings (\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
401#endif // _MTRR_LIB_H_\r