]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuCommonFeaturesLib/MachineCheck.c
UefiCpuPkg/CpuCommonFeaturesLib: Register MSR base on scope Info.
[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 //
144 // The scope of MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS is core for below processor type, only program
145 // MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS for thread 0 in each core.
146 //
147 if (IS_ATOM_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
148 IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
149 IS_SANDY_BRIDGE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
150 IS_SKYLAKE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
151 IS_XEON_PHI_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
152 IS_PENTIUM_4_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
153 IS_CORE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel)) {
154 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {
155 return RETURN_SUCCESS;
156 }
157 }
158
159 //
160 // The scope of MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS is package for below processor type, only program
161 // MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS for thread 0 core 0 in each package.
162 //
163 if (IS_NEHALEM_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel)) {
164 if ((CpuInfo->ProcessorInfo.Location.Thread != 0) || (CpuInfo->ProcessorInfo.Location.Core != 0)) {
165 return RETURN_SUCCESS;
166 }
167 }
168
169 if (State) {
170 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
171 for (BankIndex = 0; BankIndex < (UINT32) McgCap.Bits.Count; BankIndex++) {
172 CPU_REGISTER_TABLE_WRITE64 (
173 ProcessorNumber,
174 Msr,
175 MSR_IA32_MC0_CTL + BankIndex * 4,
176 MAX_UINT64
177 );
178 }
179
180 if (PcdGetBool (PcdIsPowerOnReset)) {
181 for (BankIndex = 0; BankIndex < (UINTN) McgCap.Bits.Count; BankIndex++) {
182 CPU_REGISTER_TABLE_WRITE64 (
183 ProcessorNumber,
184 Msr,
185 MSR_IA32_MC0_STATUS + BankIndex * 4,
186 0
187 );
188 }
189 }
190 }
191
192 return RETURN_SUCCESS;
193 }
194
195 /**
196 Detects if IA32_MCG_CTL feature supported on current processor.
197
198 @param[in] ProcessorNumber The index of the CPU executing this function.
199 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
200 structure for the CPU executing this function.
201 @param[in] ConfigData A pointer to the configuration buffer returned
202 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
203 CPU_FEATURE_GET_CONFIG_DATA was not provided in
204 RegisterCpuFeature().
205
206 @retval TRUE IA32_MCG_CTL feature is supported.
207 @retval FALSE IA32_MCG_CTL feature is not supported.
208
209 @note This service could be called by BSP/APs.
210 **/
211 BOOLEAN
212 EFIAPI
213 McgCtlSupport (
214 IN UINTN ProcessorNumber,
215 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
216 IN VOID *ConfigData OPTIONAL
217 )
218 {
219 MSR_IA32_MCG_CAP_REGISTER McgCap;
220
221 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {
222 return FALSE;
223 }
224 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
225 return (McgCap.Bits.MCG_CTL_P == 1);
226 }
227
228 /**
229 Initializes IA32_MCG_CTL feature to specific state.
230
231 @param[in] ProcessorNumber The index of the CPU executing this function.
232 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
233 structure for the CPU executing this function.
234 @param[in] ConfigData A pointer to the configuration buffer returned
235 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
236 CPU_FEATURE_GET_CONFIG_DATA was not provided in
237 RegisterCpuFeature().
238 @param[in] State If TRUE, then the IA32_MCG_CTL feature must be enabled.
239 If FALSE, then the IA32_MCG_CTL feature must be disabled.
240
241 @retval RETURN_SUCCESS IA32_MCG_CTL feature is initialized.
242
243 @note This service could be called by BSP only.
244 **/
245 RETURN_STATUS
246 EFIAPI
247 McgCtlInitialize (
248 IN UINTN ProcessorNumber,
249 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
250 IN VOID *ConfigData, OPTIONAL
251 IN BOOLEAN State
252 )
253 {
254 CPU_REGISTER_TABLE_WRITE64 (
255 ProcessorNumber,
256 Msr,
257 MSR_IA32_MCG_CTL,
258 (State)? MAX_UINT64 : 0
259 );
260 return RETURN_SUCCESS;
261 }
262
263 /**
264 Detects if Local machine check exception feature supported on current
265 processor.
266
267 @param[in] ProcessorNumber The index of the CPU executing this function.
268 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
269 structure for the CPU executing this function.
270 @param[in] ConfigData A pointer to the configuration buffer returned
271 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
272 CPU_FEATURE_GET_CONFIG_DATA was not provided in
273 RegisterCpuFeature().
274
275 @retval TRUE Local machine check exception feature is supported.
276 @retval FALSE Local machine check exception feature is not supported.
277
278 @note This service could be called by BSP/APs.
279 **/
280 BOOLEAN
281 EFIAPI
282 LmceSupport (
283 IN UINTN ProcessorNumber,
284 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
285 IN VOID *ConfigData OPTIONAL
286 )
287 {
288 MSR_IA32_MCG_CAP_REGISTER McgCap;
289
290 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {
291 return FALSE;
292 }
293
294 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);
295 if (ProcessorNumber == 0) {
296 DEBUG ((EFI_D_INFO, "LMCE eanble = %x\n", (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0)));
297 }
298 return (BOOLEAN) (McgCap.Bits.MCG_LMCE_P != 0);
299 }
300
301 /**
302 Initializes Local machine check exception feature to specific state.
303
304 @param[in] ProcessorNumber The index of the CPU executing this function.
305 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
306 structure for the CPU executing this function.
307 @param[in] ConfigData A pointer to the configuration buffer returned
308 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
309 CPU_FEATURE_GET_CONFIG_DATA was not provided in
310 RegisterCpuFeature().
311 @param[in] State If TRUE, then the Local machine check exception
312 feature must be enabled.
313 If FALSE, then the Local machine check exception
314 feature must be disabled.
315
316 @retval RETURN_SUCCESS Local machine check exception feature is initialized.
317
318 **/
319 RETURN_STATUS
320 EFIAPI
321 LmceInitialize (
322 IN UINTN ProcessorNumber,
323 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
324 IN VOID *ConfigData, OPTIONAL
325 IN BOOLEAN State
326 )
327 {
328 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
329
330 //
331 // The scope of FastStrings bit in the MSR_IA32_MISC_ENABLE is core for below processor type, only program
332 // MSR_IA32_MISC_ENABLE for thread 0 in each core.
333 //
334 if (IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
335 IS_GOLDMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||
336 IS_PENTIUM_4_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel)) {
337 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {
338 return RETURN_SUCCESS;
339 }
340 }
341
342 ASSERT (ConfigData != NULL);
343 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
344 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {
345 CPU_REGISTER_TABLE_WRITE_FIELD (
346 ProcessorNumber,
347 Msr,
348 MSR_IA32_FEATURE_CONTROL,
349 MSR_IA32_FEATURE_CONTROL_REGISTER,
350 Bits.LmceOn,
351 (State) ? 1 : 0
352 );
353 }
354 return RETURN_SUCCESS;
355 }