]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Include/Library/MtrrLib.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / Include / Library / MtrrLib.h
CommitLineData
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
053e878b 27#define MTRR_NUMBER_OF_FIXED_MTRR 11\r
e50466da 28\r
29//\r
30// Structure to describe a fixed MTRR\r
31//\r
32typedef struct {\r
053e878b
MK
33 UINT32 Msr;\r
34 UINT32 BaseAddress;\r
35 UINT32 Length;\r
e50466da 36} FIXED_MTRR;\r
37\r
38//\r
39// Structure to describe a variable MTRR\r
40//\r
41typedef struct {\r
053e878b
MK
42 UINT64 BaseAddress;\r
43 UINT64 Length;\r
44 UINT64 Type;\r
45 UINT32 Msr;\r
46 BOOLEAN Valid;\r
47 BOOLEAN Used;\r
e50466da 48} VARIABLE_MTRR;\r
49\r
50//\r
51// Structure to hold base and mask pair for variable MTRR register\r
52//\r
53typedef 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
61typedef struct _MTRR_VARIABLE_SETTINGS_ {\r
053e878b 62 MTRR_VARIABLE_SETTING Mtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];\r
341fea64 63} MTRR_VARIABLE_SETTINGS;\r
e50466da 64\r
65//\r
341fea64 66// Array for fixed MTRRs\r
e50466da 67//\r
68typedef struct _MTRR_FIXED_SETTINGS_ {\r
053e878b 69 UINT64 Mtrr[MTRR_NUMBER_OF_FIXED_MTRR];\r
e50466da 70} MTRR_FIXED_SETTINGS;\r
71\r
72//\r
73// Structure to hold all MTRRs\r
74//\r
75typedef 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
84typedef 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 100typedef struct {\r
053e878b
MK
101 UINT64 BaseAddress;\r
102 UINT64 Length;\r
103 MTRR_MEMORY_CACHE_TYPE Type;\r
2bbd7e2f
RN
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
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
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
163RETURN_STATUS\r
164EFIAPI\r
165MtrrSetMemoryAttribute (\r
166 IN PHYSICAL_ADDRESS BaseAddress,\r
167 IN UINT64 Length,\r
168 IN MTRR_MEMORY_CACHE_TYPE Attribute\r
169 );\r
170\r
e50466da 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
180MTRR_MEMORY_CACHE_TYPE\r
181EFIAPI\r
182MtrrGetMemoryAttribute (\r
053e878b 183 IN PHYSICAL_ADDRESS Address\r
e50466da 184 );\r
185\r
e50466da 186/**\r
187 This function gets the content in fixed MTRRs\r
188\r
76b4cae3 189 @param[out] FixedSettings A buffer to hold fixed MTRRs content.\r
e50466da 190\r
191 @return The pointer of FixedSettings\r
192\r
193**/\r
053e878b 194MTRR_FIXED_SETTINGS *\r
e50466da 195EFIAPI\r
196MtrrGetFixedMtrr (\r
053e878b 197 OUT MTRR_FIXED_SETTINGS *FixedSettings\r
e50466da 198 );\r
199\r
e50466da 200/**\r
201 This function gets the content in all MTRRs (variable and fixed)\r
202\r
76b4cae3 203 @param[out] MtrrSetting A buffer to hold all MTRRs content.\r
e50466da 204\r
205 @return The pointer of MtrrSetting\r
206\r
207**/\r
208MTRR_SETTINGS *\r
209EFIAPI\r
210MtrrGetAllMtrrs (\r
053e878b 211 OUT MTRR_SETTINGS *MtrrSetting\r
e50466da 212 );\r
213\r
e50466da 214/**\r
215 This function sets all MTRRs (variable and fixed)\r
216\r
76b4cae3 217 @param[in] MtrrSetting A buffer to hold all MTRRs content.\r
e50466da 218\r
219 @return The pointer of MtrrSetting\r
220\r
221**/\r
222MTRR_SETTINGS *\r
223EFIAPI\r
224MtrrSetAllMtrrs (\r
053e878b 225 IN MTRR_SETTINGS *MtrrSetting\r
e50466da 226 );\r
227\r
e50466da 228/**\r
229 Get the attribute of variable MTRRs.\r
230\r
231 This function shadows the content of variable MTRRs into\r
232 an internal array: VariableMtrr\r
233\r
76b4cae3
MK
234 @param[in] MtrrValidBitsMask The mask for the valid bit of the MTRR\r
235 @param[in] MtrrValidAddressMask The valid address mask for MTRR since the base address in\r
236 MTRR must align to 4K, so valid address mask equal to\r
237 MtrrValidBitsMask & 0xfffffffffffff000ULL\r
238 @param[out] VariableMtrr The array to shadow variable MTRRs content\r
239\r
438f1766 240 @return The return value of this parameter indicates the number of\r
3ba736f3 241 MTRRs which has been used.\r
e50466da 242**/\r
3ba736f3 243UINT32\r
e50466da 244EFIAPI\r
245MtrrGetMemoryAttributeInVariableMtrr (\r
053e878b
MK
246 IN UINT64 MtrrValidBitsMask,\r
247 IN UINT64 MtrrValidAddressMask,\r
248 OUT VARIABLE_MTRR *VariableMtrr\r
e50466da 249 );\r
250\r
e50466da 251/**\r
252 This function prints all MTRRs for debugging.\r
253**/\r
254VOID\r
ed8dfd7b 255EFIAPI\r
e50466da 256MtrrDebugPrintAllMtrrs (\r
430fbbe0 257 VOID\r
e50466da 258 );\r
259\r
947a573a 260/**\r
261 Checks if MTRR is supported.\r
262\r
263 @retval TRUE MTRR is supported.\r
264 @retval FALSE MTRR is not supported.\r
265\r
266**/\r
267BOOLEAN\r
ed8dfd7b 268EFIAPI\r
947a573a 269IsMtrrSupported (\r
270 VOID\r
271 );\r
272\r
91ec7824 273/**\r
274 Returns the default MTRR cache type for the system.\r
275\r
276 @return The default MTRR cache type.\r
277\r
278**/\r
279MTRR_MEMORY_CACHE_TYPE\r
280EFIAPI\r
281MtrrGetDefaultMemoryType (\r
282 VOID\r
283 );\r
284\r
b970ed68
MK
285/**\r
286 This function attempts to set the attributes into MTRR setting buffer for a memory range.\r
287\r
288 @param[in, out] MtrrSetting MTRR setting buffer to be set.\r
289 @param[in] BaseAddress The physical address that is the start address\r
290 of a memory region.\r
291 @param[in] Length The size in bytes of the memory region.\r
292 @param[in] Attribute The bit mask of attributes to set for the\r
293 memory region.\r
294\r
295 @retval RETURN_SUCCESS The attributes were set for the memory region.\r
296 @retval RETURN_INVALID_PARAMETER Length is zero.\r
297 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r
298 memory resource range specified by BaseAddress and Length.\r
299 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r
300 range specified by BaseAddress and Length.\r
301 @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r
302 BaseAddress and Length cannot be modified.\r
303 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
304 the memory resource range.\r
bc6a8b78
RN
305 Multiple memory range attributes setting by calling this API multiple\r
306 times may fail with status RETURN_OUT_OF_RESOURCES. It may not mean\r
307 the number of CPU MTRRs are too small to set such memory attributes.\r
308 Pass the multiple memory range attributes to one call of\r
309 MtrrSetMemoryAttributesInMtrrSettings() may succeed.\r
8b2eb7b3
RN
310 @retval RETURN_BUFFER_TOO_SMALL The fixed internal scratch buffer is too small for MTRR calculation.\r
311 Caller should use MtrrSetMemoryAttributesInMtrrSettings() to specify\r
312 external scratch buffer.\r
b970ed68
MK
313**/\r
314RETURN_STATUS\r
315EFIAPI\r
316MtrrSetMemoryAttributeInMtrrSettings (\r
317 IN OUT MTRR_SETTINGS *MtrrSetting,\r
318 IN PHYSICAL_ADDRESS BaseAddress,\r
319 IN UINT64 Length,\r
320 IN MTRR_MEMORY_CACHE_TYPE Attribute\r
321 );\r
322\r
2bbd7e2f
RN
323/**\r
324 This function attempts to set the attributes into MTRR setting buffer for multiple memory ranges.\r
325\r
326 @param[in, out] MtrrSetting MTRR setting buffer to be set.\r
327 @param[in] Scratch A temporary scratch buffer that is used to perform the calculation.\r
328 @param[in, out] ScratchSize Pointer to the size in bytes of the scratch buffer.\r
329 It may be updated to the actual required size when the calculation\r
330 needs more scratch buffer.\r
331 @param[in] Ranges Pointer to an array of MTRR_MEMORY_RANGE.\r
332 When range overlap happens, the last one takes higher priority.\r
333 When the function returns, either all the attributes are set successfully,\r
334 or none of them is set.\r
f6194f5a 335 @param[in] RangeCount Count of MTRR_MEMORY_RANGE.\r
2bbd7e2f
RN
336\r
337 @retval RETURN_SUCCESS The attributes were set for all the memory ranges.\r
338 @retval RETURN_INVALID_PARAMETER Length in any range is zero.\r
339 @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the\r
340 memory resource range specified by BaseAddress and Length in any range.\r
341 @retval RETURN_UNSUPPORTED The bit mask of attributes is not support for the memory resource\r
342 range specified by BaseAddress and Length in any range.\r
343 @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
344 the memory resource ranges.\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_BUFFER_TOO_SMALL The scratch buffer is too small for MTRR calculation.\r
348**/\r
349RETURN_STATUS\r
350EFIAPI\r
351MtrrSetMemoryAttributesInMtrrSettings (\r
053e878b
MK
352 IN OUT MTRR_SETTINGS *MtrrSetting,\r
353 IN VOID *Scratch,\r
354 IN OUT UINTN *ScratchSize,\r
355 IN CONST MTRR_MEMORY_RANGE *Ranges,\r
356 IN UINTN RangeCount\r
2bbd7e2f 357 );\r
053e878b 358\r
e50466da 359#endif // _MTRR_LIB_H_\r