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