]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuCommonFeaturesLib/MachineCheck.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / Library / CpuCommonFeaturesLib / MachineCheck.c
CommitLineData
80c4b236
JF
1/** @file\r
2 Machine Check features.\r
3\r
9c90d39b 4 Copyright (c) 2017 - 2019, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
80c4b236
JF
6\r
7**/\r
8\r
9#include "CpuCommonFeatures.h"\r
10\r
11/**\r
12 Detects if Machine Check Exception feature supported on current processor.\r
13\r
14 @param[in] ProcessorNumber The index of the CPU executing this function.\r
15 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
16 structure for the CPU executing this function.\r
17 @param[in] ConfigData A pointer to the configuration buffer returned\r
18 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
19 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
20 RegisterCpuFeature().\r
21\r
22 @retval TRUE Machine Check Exception feature is supported.\r
23 @retval FALSE Machine Check Exception feature is not supported.\r
24\r
25 @note This service could be called by BSP/APs.\r
26**/\r
27BOOLEAN\r
28EFIAPI\r
29MceSupport (\r
30 IN UINTN ProcessorNumber,\r
31 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
32 IN VOID *ConfigData OPTIONAL\r
33 )\r
34{\r
35 return (CpuInfo->CpuIdVersionInfoEdx.Bits.MCE == 1);\r
36}\r
37\r
38/**\r
39 Initializes Machine Check Exception feature to specific state.\r
40\r
41 @param[in] ProcessorNumber The index of the CPU executing this function.\r
42 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
43 structure for the CPU executing this function.\r
44 @param[in] ConfigData A pointer to the configuration buffer returned\r
45 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
46 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
47 RegisterCpuFeature().\r
48 @param[in] State If TRUE, then the Machine Check Exception feature must be enabled.\r
49 If FALSE, then the Machine Check Exception feature must be disabled.\r
50\r
51 @retval RETURN_SUCCESS Machine Check Exception feature is initialized.\r
52\r
53 @note This service could be called by BSP only.\r
54**/\r
55RETURN_STATUS\r
56EFIAPI\r
57MceInitialize (\r
58 IN UINTN ProcessorNumber,\r
59 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 60 IN VOID *ConfigData OPTIONAL,\r
80c4b236
JF
61 IN BOOLEAN State\r
62 )\r
63{\r
64 //\r
65 // Set MCE bit in CR4\r
66 //\r
67 CPU_REGISTER_TABLE_WRITE_FIELD (\r
68 ProcessorNumber,\r
69 ControlRegister,\r
70 4,\r
71 IA32_CR4,\r
72 Bits.MCE,\r
73 (State) ? 1 : 0\r
74 );\r
75 return RETURN_SUCCESS;\r
76}\r
77\r
78/**\r
79 Detects if Machine Check Architecture feature supported on current processor.\r
80\r
81 @param[in] ProcessorNumber The index of the CPU executing this function.\r
82 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
83 structure for the CPU executing this function.\r
84 @param[in] ConfigData A pointer to the configuration buffer returned\r
85 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
86 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
87 RegisterCpuFeature().\r
88\r
89 @retval TRUE Machine Check Architecture feature is supported.\r
90 @retval FALSE Machine Check Architecture feature is not supported.\r
91\r
92 @note This service could be called by BSP/APs.\r
93**/\r
94BOOLEAN\r
95EFIAPI\r
96McaSupport (\r
97 IN UINTN ProcessorNumber,\r
98 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
99 IN VOID *ConfigData OPTIONAL\r
100 )\r
101{\r
57d1afc8
SB
102 if (!MceSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
103 return FALSE;\r
104 }\r
053e878b 105\r
80c4b236
JF
106 return (CpuInfo->CpuIdVersionInfoEdx.Bits.MCA == 1);\r
107}\r
108\r
109/**\r
110 Initializes Machine Check Architecture feature to specific state.\r
111\r
112 @param[in] ProcessorNumber The index of the CPU executing this function.\r
113 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
114 structure for the CPU executing this function.\r
115 @param[in] ConfigData A pointer to the configuration buffer returned\r
116 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
117 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
118 RegisterCpuFeature().\r
119 @param[in] State If TRUE, then the Machine Check Architecture feature must be enabled.\r
120 If FALSE, then the Machine Check Architecture feature must be disabled.\r
121\r
122 @retval RETURN_SUCCESS Machine Check Architecture feature is initialized.\r
123\r
124 @note This service could be called by BSP only.\r
125**/\r
126RETURN_STATUS\r
127EFIAPI\r
128McaInitialize (\r
129 IN UINTN ProcessorNumber,\r
130 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 131 IN VOID *ConfigData OPTIONAL,\r
80c4b236
JF
132 IN BOOLEAN State\r
133 )\r
134{\r
135 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
136 UINT32 BankIndex;\r
137\r
d28daadd
ED
138 //\r
139 // The scope of MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS is core for below processor type, only program\r
140 // MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS for thread 0 in each core.\r
141 //\r
142 if (IS_ATOM_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
143 IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
144 IS_SANDY_BRIDGE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
145 IS_SKYLAKE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
146 IS_XEON_PHI_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
147 IS_PENTIUM_4_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
053e878b
MK
148 IS_CORE_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel))\r
149 {\r
d28daadd
ED
150 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {\r
151 return RETURN_SUCCESS;\r
152 }\r
153 }\r
154\r
155 //\r
156 // The scope of MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS is package for below processor type, only program\r
6cfeeb71 157 // MSR_IA32_MC*_CTL/MSR_IA32_MC*_STATUS once for each package.\r
d28daadd
ED
158 //\r
159 if (IS_NEHALEM_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel)) {\r
6cfeeb71 160 if ((CpuInfo->First.Thread == 0) || (CpuInfo->First.Core == 0)) {\r
d28daadd
ED
161 return RETURN_SUCCESS;\r
162 }\r
163 }\r
164\r
e01d92d4 165 if (State) {\r
dc7363f8 166 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
053e878b 167 for (BankIndex = 0; BankIndex < (UINT32)McgCap.Bits.Count; BankIndex++) {\r
80c4b236
JF
168 CPU_REGISTER_TABLE_WRITE64 (\r
169 ProcessorNumber,\r
170 Msr,\r
dc7363f8
RN
171 MSR_IA32_MC0_CTL + BankIndex * 4,\r
172 MAX_UINT64\r
80c4b236
JF
173 );\r
174 }\r
dc7363f8
RN
175\r
176 if (PcdGetBool (PcdIsPowerOnReset)) {\r
053e878b 177 for (BankIndex = 0; BankIndex < (UINTN)McgCap.Bits.Count; BankIndex++) {\r
dc7363f8
RN
178 CPU_REGISTER_TABLE_WRITE64 (\r
179 ProcessorNumber,\r
180 Msr,\r
181 MSR_IA32_MC0_STATUS + BankIndex * 4,\r
182 0\r
183 );\r
184 }\r
185 }\r
80c4b236
JF
186 }\r
187\r
188 return RETURN_SUCCESS;\r
189}\r
190\r
191/**\r
192 Detects if IA32_MCG_CTL feature supported on current processor.\r
193\r
194 @param[in] ProcessorNumber The index of the CPU executing this function.\r
195 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
196 structure for the CPU executing this function.\r
197 @param[in] ConfigData A pointer to the configuration buffer returned\r
198 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
199 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
200 RegisterCpuFeature().\r
201\r
202 @retval TRUE IA32_MCG_CTL feature is supported.\r
203 @retval FALSE IA32_MCG_CTL feature is not supported.\r
204\r
205 @note This service could be called by BSP/APs.\r
206**/\r
207BOOLEAN\r
208EFIAPI\r
209McgCtlSupport (\r
210 IN UINTN ProcessorNumber,\r
211 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
212 IN VOID *ConfigData OPTIONAL\r
213 )\r
214{\r
215 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
216\r
217 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
218 return FALSE;\r
219 }\r
053e878b 220\r
80c4b236
JF
221 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
222 return (McgCap.Bits.MCG_CTL_P == 1);\r
223}\r
224\r
225/**\r
226 Initializes IA32_MCG_CTL feature to specific state.\r
227\r
228 @param[in] ProcessorNumber The index of the CPU executing this function.\r
229 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
230 structure for the CPU executing this function.\r
231 @param[in] ConfigData A pointer to the configuration buffer returned\r
232 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
233 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
234 RegisterCpuFeature().\r
235 @param[in] State If TRUE, then the IA32_MCG_CTL feature must be enabled.\r
236 If FALSE, then the IA32_MCG_CTL feature must be disabled.\r
237\r
238 @retval RETURN_SUCCESS IA32_MCG_CTL feature is initialized.\r
239\r
240 @note This service could be called by BSP only.\r
241**/\r
242RETURN_STATUS\r
243EFIAPI\r
244McgCtlInitialize (\r
245 IN UINTN ProcessorNumber,\r
246 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 247 IN VOID *ConfigData OPTIONAL,\r
80c4b236
JF
248 IN BOOLEAN State\r
249 )\r
250{\r
251 CPU_REGISTER_TABLE_WRITE64 (\r
252 ProcessorNumber,\r
253 Msr,\r
254 MSR_IA32_MCG_CTL,\r
053e878b 255 (State) ? MAX_UINT64 : 0\r
80c4b236
JF
256 );\r
257 return RETURN_SUCCESS;\r
258}\r
259\r
306a5bcc 260/**\r
7367cc6c 261 Detects if Local machine check exception feature supported on current\r
306a5bcc
ED
262 processor.\r
263\r
264 @param[in] ProcessorNumber The index of the CPU executing this function.\r
265 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
266 structure for the CPU executing this function.\r
267 @param[in] ConfigData A pointer to the configuration buffer returned\r
268 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
269 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
270 RegisterCpuFeature().\r
271\r
272 @retval TRUE Local machine check exception feature is supported.\r
273 @retval FALSE Local machine check exception feature is not supported.\r
274\r
275 @note This service could be called by BSP/APs.\r
276**/\r
277BOOLEAN\r
278EFIAPI\r
279LmceSupport (\r
280 IN UINTN ProcessorNumber,\r
281 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
282 IN VOID *ConfigData OPTIONAL\r
283 )\r
284{\r
053e878b 285 MSR_IA32_MCG_CAP_REGISTER McgCap;\r
306a5bcc
ED
286\r
287 if (!McaSupport (ProcessorNumber, CpuInfo, ConfigData)) {\r
288 return FALSE;\r
289 }\r
290\r
291 McgCap.Uint64 = AsmReadMsr64 (MSR_IA32_MCG_CAP);\r
292 if (ProcessorNumber == 0) {\r
053e878b 293 DEBUG ((DEBUG_INFO, "LMCE enable = %x\n", (BOOLEAN)(McgCap.Bits.MCG_LMCE_P != 0)));\r
306a5bcc 294 }\r
053e878b
MK
295\r
296 return (BOOLEAN)(McgCap.Bits.MCG_LMCE_P != 0);\r
306a5bcc
ED
297}\r
298\r
299/**\r
300 Initializes Local machine check exception feature to specific state.\r
301\r
302 @param[in] ProcessorNumber The index of the CPU executing this function.\r
303 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
304 structure for the CPU executing this function.\r
305 @param[in] ConfigData A pointer to the configuration buffer returned\r
306 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
307 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
308 RegisterCpuFeature().\r
309 @param[in] State If TRUE, then the Local machine check exception\r
310 feature must be enabled.\r
311 If FALSE, then the Local machine check exception\r
312 feature must be disabled.\r
313\r
314 @retval RETURN_SUCCESS Local machine check exception feature is initialized.\r
315\r
316**/\r
317RETURN_STATUS\r
318EFIAPI\r
319LmceInitialize (\r
320 IN UINTN ProcessorNumber,\r
321 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 322 IN VOID *ConfigData OPTIONAL,\r
306a5bcc
ED
323 IN BOOLEAN State\r
324 )\r
325{\r
d28daadd 326 //\r
de2204a9 327 // The scope of LcmeOn bit in the MSR_IA32_MISC_ENABLE is core for below processor type, only program\r
d28daadd
ED
328 // MSR_IA32_MISC_ENABLE for thread 0 in each core.\r
329 //\r
330 if (IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
331 IS_GOLDMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
053e878b
MK
332 IS_PENTIUM_4_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel))\r
333 {\r
d28daadd
ED
334 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {\r
335 return RETURN_SUCCESS;\r
336 }\r
337 }\r
338\r
9c90d39b
ED
339 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
340 ProcessorNumber,\r
341 Msr,\r
342 MSR_IA32_FEATURE_CONTROL,\r
343 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
344 Bits.LmceOn,\r
345 (State) ? 1 : 0\r
346 );\r
347\r
306a5bcc
ED
348 return RETURN_SUCCESS;\r
349}\r