UefiCpuPkg: Enhance feature dependency check
[mirror_edk2.git] / UefiCpuPkg / Library / CpuCommonFeaturesLib / MachineCheck.c
CommitLineData
80c4b236
JF
1/** @file\r
2 Machine Check features.\r
3\r
4 Copyright (c) 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#include "CpuCommonFeatures.h"\r
16\r
17/**\r
18 Detects if Machine Check Exception feature supported on current processor.\r
19\r
20 @param[in] ProcessorNumber The index of the CPU executing this function.\r
21 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
22 structure for the CPU executing this function.\r
23 @param[in] ConfigData A pointer to the configuration buffer returned\r
24 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
25 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
26 RegisterCpuFeature().\r
27\r
28 @retval TRUE Machine Check Exception feature is supported.\r
29 @retval FALSE Machine Check Exception feature is not supported.\r
30\r
31 @note This service could be called by BSP/APs.\r
32**/\r
33BOOLEAN\r
34EFIAPI\r
35MceSupport (\r
36 IN UINTN ProcessorNumber,\r
37 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
38 IN VOID *ConfigData OPTIONAL\r
39 )\r
40{\r
41 return (CpuInfo->CpuIdVersionInfoEdx.Bits.MCE == 1);\r
42}\r
43\r
44/**\r
45 Initializes Machine Check Exception feature to specific state.\r
46\r
47 @param[in] ProcessorNumber The index of the CPU executing this function.\r
48 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
49 structure for the CPU executing this function.\r
50 @param[in] ConfigData A pointer to the configuration buffer returned\r
51 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
52 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
53 RegisterCpuFeature().\r
54 @param[in] State If TRUE, then the Machine Check Exception feature must be enabled.\r
55 If FALSE, then the Machine Check Exception feature must be disabled.\r
56\r
57 @retval RETURN_SUCCESS Machine Check Exception feature is initialized.\r
58\r
59 @note This service could be called by BSP only.\r
60**/\r
61RETURN_STATUS\r
62EFIAPI\r
63MceInitialize (\r
64 IN UINTN ProcessorNumber,\r
65 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
66 IN VOID *ConfigData, OPTIONAL\r
67 IN BOOLEAN State\r
68 )\r
69{\r
70 //\r
71 // Set MCE bit in CR4\r
72 //\r
73 CPU_REGISTER_TABLE_WRITE_FIELD (\r
74 ProcessorNumber,\r
75 ControlRegister,\r
76 4,\r
77 IA32_CR4,\r
78 Bits.MCE,\r
79 (State) ? 1 : 0\r
80 );\r
81 return RETURN_SUCCESS;\r
82}\r
83\r
84/**\r
85 Detects if Machine Check Architecture feature supported on current processor.\r
86\r
87 @param[in] ProcessorNumber The index of the CPU executing this function.\r
88 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
89 structure for the CPU executing this function.\r
90 @param[in] ConfigData A pointer to the configuration buffer returned\r
91 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
92 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
93 RegisterCpuFeature().\r
94\r
95 @retval TRUE Machine Check Architecture feature is supported.\r
96 @retval FALSE Machine Check Architecture feature is not supported.\r
97\r
98 @note This service could be called by BSP/APs.\r
99**/\r
100BOOLEAN\r
101EFIAPI\r
102McaSupport (\r
103 IN UINTN ProcessorNumber,\r
104 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
105 IN VOID *ConfigData OPTIONAL\r
106 )\r
107{\r
57d1afc8
SB
108 if (!MceSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
109 return FALSE;\r
110 }\r
80c4b236
JF
111 return (CpuInfo->CpuIdVersionInfoEdx.Bits.MCA == 1);\r
112}\r
113\r
114/**\r
115 Initializes Machine Check Architecture feature to specific state.\r
116\r
117 @param[in] ProcessorNumber The index of the CPU executing this function.\r
118 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
119 structure for the CPU executing this function.\r
120 @param[in] ConfigData A pointer to the configuration buffer returned\r
121 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
122 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
123 RegisterCpuFeature().\r
124 @param[in] State If TRUE, then the Machine Check Architecture feature must be enabled.\r
125 If FALSE, then the Machine Check Architecture feature must be disabled.\r
126\r
127 @retval RETURN_SUCCESS Machine Check Architecture feature is initialized.\r
128\r
129 @note This service could be called by BSP only.\r
130**/\r
131RETURN_STATUS\r
132EFIAPI\r
133McaInitialize (\r
134 IN UINTN ProcessorNumber,\r
135 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
136 IN VOID *ConfigData, OPTIONAL\r
137 IN BOOLEAN State\r
138 )\r
139{\r
140 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
141 UINT32 BankIndex;\r
142\r
143 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
144 for (BankIndex = 0; BankIndex < (UINT32) McgCap.Bits.Count; BankIndex++) {\r
145 CPU_REGISTER_TABLE_WRITE64 (\r
146 ProcessorNumber,\r
147 Msr,\r
148 MSR_IA32_MC0_CTL + BankIndex * 4,\r
149 MAX_UINT64\r
150 );\r
151 }\r
152\r
153 if (PcdGetBool (PcdIsPowerOnReset)) {\r
154 for (BankIndex = 0; BankIndex < (UINTN) McgCap.Bits.Count; BankIndex++) {\r
155 CPU_REGISTER_TABLE_WRITE64 (\r
156 ProcessorNumber,\r
157 Msr,\r
158 MSR_IA32_MC0_STATUS + BankIndex * 4,\r
159 0\r
160 );\r
161 }\r
162 }\r
163\r
164 return RETURN_SUCCESS;\r
165}\r
166\r
167/**\r
168 Detects if IA32_MCG_CTL feature supported on current processor.\r
169\r
170 @param[in] ProcessorNumber The index of the CPU executing this function.\r
171 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
172 structure for the CPU executing this function.\r
173 @param[in] ConfigData A pointer to the configuration buffer returned\r
174 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
175 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
176 RegisterCpuFeature().\r
177\r
178 @retval TRUE IA32_MCG_CTL feature is supported.\r
179 @retval FALSE IA32_MCG_CTL feature is not supported.\r
180\r
181 @note This service could be called by BSP/APs.\r
182**/\r
183BOOLEAN\r
184EFIAPI\r
185McgCtlSupport (\r
186 IN UINTN ProcessorNumber,\r
187 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
188 IN VOID *ConfigData OPTIONAL\r
189 )\r
190{\r
191 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
192\r
193 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
194 return FALSE;\r
195 }\r
196 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
197 return (McgCap.Bits.MCG_CTL_P == 1);\r
198}\r
199\r
200/**\r
201 Initializes IA32_MCG_CTL feature to specific state.\r
202\r
203 @param[in] ProcessorNumber The index of the CPU executing this function.\r
204 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
205 structure for the CPU executing this function.\r
206 @param[in] ConfigData A pointer to the configuration buffer returned\r
207 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
208 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
209 RegisterCpuFeature().\r
210 @param[in] State If TRUE, then the IA32_MCG_CTL feature must be enabled.\r
211 If FALSE, then the IA32_MCG_CTL feature must be disabled.\r
212\r
213 @retval RETURN_SUCCESS IA32_MCG_CTL feature is initialized.\r
214\r
215 @note This service could be called by BSP only.\r
216**/\r
217RETURN_STATUS\r
218EFIAPI\r
219McgCtlInitialize (\r
220 IN UINTN ProcessorNumber,\r
221 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
222 IN VOID *ConfigData, OPTIONAL\r
223 IN BOOLEAN State\r
224 )\r
225{\r
226 CPU_REGISTER_TABLE_WRITE64 (\r
227 ProcessorNumber,\r
228 Msr,\r
229 MSR_IA32_MCG_CTL,\r
230 (State)? MAX_UINT64 : 0\r
231 );\r
232 return RETURN_SUCCESS;\r
233}\r
234\r
306a5bcc
ED
235/**\r
236 Detects if Local machine check exception feature supported on current \r
237 processor.\r
238\r
239 @param[in] ProcessorNumber The index of the CPU executing this function.\r
240 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
241 structure for the CPU executing this function.\r
242 @param[in] ConfigData A pointer to the configuration buffer returned\r
243 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
244 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
245 RegisterCpuFeature().\r
246\r
247 @retval TRUE Local machine check exception feature is supported.\r
248 @retval FALSE Local machine check exception feature is not supported.\r
249\r
250 @note This service could be called by BSP/APs.\r
251**/\r
252BOOLEAN\r
253EFIAPI\r
254LmceSupport (\r
255 IN UINTN ProcessorNumber,\r
256 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
257 IN VOID *ConfigData OPTIONAL\r
258 )\r
259{\r
260 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
261\r
262 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
263 return FALSE;\r
264 }\r
265\r
266 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
267 if (ProcessorNumber == 0) {\r
268 DEBUG ((EFI_D_INFO, "LMCE eanble = %x\n", (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0)));\r
269 }\r
270 return (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0);\r
271}\r
272\r
273/**\r
274 Initializes Local machine check exception feature to specific state.\r
275\r
276 @param[in] ProcessorNumber The index of the CPU executing this function.\r
277 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
278 structure for the CPU executing this function.\r
279 @param[in] ConfigData A pointer to the configuration buffer returned\r
280 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
281 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
282 RegisterCpuFeature().\r
283 @param[in] State If TRUE, then the Local machine check exception\r
284 feature must be enabled.\r
285 If FALSE, then the Local machine check exception\r
286 feature must be disabled.\r
287\r
288 @retval RETURN_SUCCESS Local machine check exception feature is initialized.\r
289\r
290**/\r
291RETURN_STATUS\r
292EFIAPI\r
293LmceInitialize (\r
294 IN UINTN ProcessorNumber,\r
295 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
296 IN VOID *ConfigData, OPTIONAL\r
297 IN BOOLEAN State\r
298 )\r
299{\r
300 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;\r
301\r
302 ASSERT (ConfigData != NULL);\r
303 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;\r
304 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {\r
305 CPU_REGISTER_TABLE_WRITE_FIELD (\r
306 ProcessorNumber,\r
307 Msr,\r
308 MSR_IA32_FEATURE_CONTROL,\r
309 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
310 Bits.LmceOn,\r
311 (State) ? 1 : 0\r
312 );\r
313 }\r
314 return RETURN_SUCCESS;\r
315}\r