UefiCpuPkg/CpuCommonFeaturesLib: Merge machine check code to same file.
[mirror_edk2.git] / UefiCpuPkg / Library / CpuCommonFeaturesLib / MachineCheck.c
1 /** @file
2 Machine Check features.
3
4 Copyright (c) 2017, 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 return (CpuInfo->CpuIdVersionInfoEdx.Bits.MCA == 1);
109 }
110
111 /**
112 Initializes Machine Check Architecture feature to specific state.
113
114 @param[in] ProcessorNumber The index of the CPU executing this function.
115 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
116 structure for the CPU executing this function.
117 @param[in] ConfigData A pointer to the configuration buffer returned
118 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
119 CPU_FEATURE_GET_CONFIG_DATA was not provided in
120 RegisterCpuFeature().
121 @param[in] State If TRUE, then the Machine Check Architecture feature must be enabled.
122 If FALSE, then the Machine Check Architecture feature must be disabled.
123
124 @retval RETURN_SUCCESS Machine Check Architecture feature is initialized.
125
126 @note This service could be called by BSP only.
127 **/
128 RETURN_STATUS
129 EFIAPI
130 McaInitialize (
131 IN UINTN ProcessorNumber,
132 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
133 IN VOID *ConfigData, OPTIONAL
134 IN BOOLEAN State
135 )
136 {
137 MSR_IA32_MCG_CAP_REGISTER McgCap;
138 UINT32 BankIndex;
139
140 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
141 for (BankIndex = 0; BankIndex < (UINT32) McgCap.Bits.Count; BankIndex++) {
142 CPU_REGISTER_TABLE_WRITE64 (
143 ProcessorNumber,
144 Msr,
145 MSR_IA32_MC0_CTL + BankIndex * 4,
146 MAX_UINT64
147 );
148 }
149
150 if (PcdGetBool (PcdIsPowerOnReset)) {
151 for (BankIndex = 0; BankIndex < (UINTN) McgCap.Bits.Count; BankIndex++) {
152 CPU_REGISTER_TABLE_WRITE64 (
153 ProcessorNumber,
154 Msr,
155 MSR_IA32_MC0_STATUS + BankIndex * 4,
156 0
157 );
158 }
159 }
160
161 return RETURN_SUCCESS;
162 }
163
164 /**
165 Detects if IA32_MCG_CTL feature supported on current processor.
166
167 @param[in] ProcessorNumber The index of the CPU executing this function.
168 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
169 structure for the CPU executing this function.
170 @param[in] ConfigData A pointer to the configuration buffer returned
171 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
172 CPU_FEATURE_GET_CONFIG_DATA was not provided in
173 RegisterCpuFeature().
174
175 @retval TRUE IA32_MCG_CTL feature is supported.
176 @retval FALSE IA32_MCG_CTL feature is not supported.
177
178 @note This service could be called by BSP/APs.
179 **/
180 BOOLEAN
181 EFIAPI
182 McgCtlSupport (
183 IN UINTN ProcessorNumber,
184 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
185 IN VOID *ConfigData OPTIONAL
186 )
187 {
188 MSR_IA32_MCG_CAP_REGISTER McgCap;
189
190 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {
191 return FALSE;
192 }
193 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
194 return (McgCap.Bits.MCG_CTL_P == 1);
195 }
196
197 /**
198 Initializes IA32_MCG_CTL feature to specific state.
199
200 @param[in] ProcessorNumber The index of the CPU executing this function.
201 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
202 structure for the CPU executing this function.
203 @param[in] ConfigData A pointer to the configuration buffer returned
204 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
205 CPU_FEATURE_GET_CONFIG_DATA was not provided in
206 RegisterCpuFeature().
207 @param[in] State If TRUE, then the IA32_MCG_CTL feature must be enabled.
208 If FALSE, then the IA32_MCG_CTL feature must be disabled.
209
210 @retval RETURN_SUCCESS IA32_MCG_CTL feature is initialized.
211
212 @note This service could be called by BSP only.
213 **/
214 RETURN_STATUS
215 EFIAPI
216 McgCtlInitialize (
217 IN UINTN ProcessorNumber,
218 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
219 IN VOID *ConfigData, OPTIONAL
220 IN BOOLEAN State
221 )
222 {
223 CPU_REGISTER_TABLE_WRITE64 (
224 ProcessorNumber,
225 Msr,
226 MSR_IA32_MCG_CTL,
227 (State)? MAX_UINT64 : 0
228 );
229 return RETURN_SUCCESS;
230 }
231
232 /**
233 Detects if Local machine check exception feature supported on current
234 processor.
235
236 @param[in] ProcessorNumber The index of the CPU executing this function.
237 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
238 structure for the CPU executing this function.
239 @param[in] ConfigData A pointer to the configuration buffer returned
240 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
241 CPU_FEATURE_GET_CONFIG_DATA was not provided in
242 RegisterCpuFeature().
243
244 @retval TRUE Local machine check exception feature is supported.
245 @retval FALSE Local machine check exception feature is not supported.
246
247 @note This service could be called by BSP/APs.
248 **/
249 BOOLEAN
250 EFIAPI
251 LmceSupport (
252 IN UINTN ProcessorNumber,
253 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
254 IN VOID *ConfigData OPTIONAL
255 )
256 {
257 MSR_IA32_MCG_CAP_REGISTER McgCap;
258
259 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {
260 return FALSE;
261 }
262
263 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
264 if (ProcessorNumber == 0) {
265 DEBUG ((EFI_D_INFO, "LMCE eanble = %x\n", (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0)));
266 }
267 return (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0);
268 }
269
270 /**
271 Initializes Local machine check exception feature to specific state.
272
273 @param[in] ProcessorNumber The index of the CPU executing this function.
274 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
275 structure for the CPU executing this function.
276 @param[in] ConfigData A pointer to the configuration buffer returned
277 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
278 CPU_FEATURE_GET_CONFIG_DATA was not provided in
279 RegisterCpuFeature().
280 @param[in] State If TRUE, then the Local machine check exception
281 feature must be enabled.
282 If FALSE, then the Local machine check exception
283 feature must be disabled.
284
285 @retval RETURN_SUCCESS Local machine check exception feature is initialized.
286
287 **/
288 RETURN_STATUS
289 EFIAPI
290 LmceInitialize (
291 IN UINTN ProcessorNumber,
292 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
293 IN VOID *ConfigData, OPTIONAL
294 IN BOOLEAN State
295 )
296 {
297 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
298
299 ASSERT (ConfigData != NULL);
300 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
301 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {
302 CPU_REGISTER_TABLE_WRITE_FIELD (
303 ProcessorNumber,
304 Msr,
305 MSR_IA32_FEATURE_CONTROL,
306 MSR_IA32_FEATURE_CONTROL_REGISTER,
307 Bits.LmceOn,
308 (State) ? 1 : 0
309 );
310 }
311 return RETURN_SUCCESS;
312 }