]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuCommonFeaturesLib/FeatureControl.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / Library / CpuCommonFeaturesLib / FeatureControl.c
CommitLineData
80c4b236
JF
1/** @file\r
2 Features in MSR_IA32_FEATURE_CONTROL register.\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
80c4b236
JF
11/**\r
12 Detects if VMX 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 VMX feature is supported.\r
23 @retval FALSE VMX feature is not supported.\r
24\r
25 @note This service could be called by BSP/APs.\r
26**/\r
27BOOLEAN\r
28EFIAPI\r
29VmxSupport (\r
30 IN UINTN ProcessorNumber,\r
31 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
32 IN VOID *ConfigData OPTIONAL\r
33 )\r
34{\r
80c4b236
JF
35 return (CpuInfo->CpuIdVersionInfoEcx.Bits.VMX == 1);\r
36}\r
37\r
38/**\r
b1fe2029 39 Initializes VMX feature to specific state.\r
80c4b236
JF
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
b1fe2029
ED
48 @param[in] State If TRUE, then the VMX feature must be enabled.\r
49 If FALSE, then the VMX feature must be disabled.\r
80c4b236 50\r
b1fe2029 51 @retval RETURN_SUCCESS VMX feature is initialized.\r
80c4b236
JF
52\r
53 @note This service could be called by BSP only.\r
54**/\r
55RETURN_STATUS\r
56EFIAPI\r
b1fe2029 57VmxInitialize (\r
80c4b236
JF
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
d28daadd
ED
64 //\r
65 // The scope of EnableVmxOutsideSmx bit in the MSR_IA32_FEATURE_CONTROL is core for\r
66 // below processor type, only program MSR_IA32_FEATURE_CONTROL for thread 0 in each\r
67 // core.\r
68 //\r
69 if (IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
70 IS_GOLDMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
053e878b
MK
71 IS_GOLDMONT_PLUS_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel))\r
72 {\r
d28daadd
ED
73 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {\r
74 return RETURN_SUCCESS;\r
75 }\r
76 }\r
77\r
9c90d39b
ED
78 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
79 ProcessorNumber,\r
80 Msr,\r
81 MSR_IA32_FEATURE_CONTROL,\r
82 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
83 Bits.EnableVmxOutsideSmx,\r
84 (State) ? 1 : 0\r
85 );\r
86\r
80c4b236
JF
87 return RETURN_SUCCESS;\r
88}\r
89\r
90/**\r
91 Detects if Lock Feature Control Register feature supported on current processor.\r
92\r
93 @param[in] ProcessorNumber The index of the CPU executing this function.\r
94 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
95 structure for the CPU executing this function.\r
96 @param[in] ConfigData A pointer to the configuration buffer returned\r
97 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
98 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
99 RegisterCpuFeature().\r
100\r
101 @retval TRUE Lock Feature Control Register feature is supported.\r
102 @retval FALSE Lock Feature Control Register feature is not supported.\r
103\r
104 @note This service could be called by BSP/APs.\r
105**/\r
106BOOLEAN\r
107EFIAPI\r
108LockFeatureControlRegisterSupport (\r
109 IN UINTN ProcessorNumber,\r
110 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
111 IN VOID *ConfigData OPTIONAL\r
112 )\r
113{\r
80c4b236
JF
114 return TRUE;\r
115}\r
116\r
117/**\r
118 Initializes Lock Feature Control Register feature to specific state.\r
119\r
120 @param[in] ProcessorNumber The index of the CPU executing this function.\r
121 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
122 structure for the CPU executing this function.\r
123 @param[in] ConfigData A pointer to the configuration buffer returned\r
124 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
125 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
126 RegisterCpuFeature().\r
127 @param[in] State If TRUE, then the Lock Feature Control Register feature must be enabled.\r
128 If FALSE, then the Lock Feature Control Register feature must be disabled.\r
129\r
130 @retval RETURN_SUCCESS Lock Feature Control Register feature is initialized.\r
131\r
132 @note This service could be called by BSP only.\r
133**/\r
134RETURN_STATUS\r
135EFIAPI\r
136LockFeatureControlRegisterInitialize (\r
137 IN UINTN ProcessorNumber,\r
138 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 139 IN VOID *ConfigData OPTIONAL,\r
80c4b236
JF
140 IN BOOLEAN State\r
141 )\r
142{\r
d28daadd
ED
143 //\r
144 // The scope of Lock bit in the MSR_IA32_FEATURE_CONTROL is core for\r
145 // below processor type, only program MSR_IA32_FEATURE_CONTROL for thread 0 in each\r
146 // core.\r
147 //\r
148 if (IS_SILVERMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
149 IS_GOLDMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
053e878b
MK
150 IS_GOLDMONT_PLUS_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel))\r
151 {\r
d28daadd
ED
152 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {\r
153 return RETURN_SUCCESS;\r
154 }\r
155 }\r
156\r
9c90d39b
ED
157 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
158 ProcessorNumber,\r
159 Msr,\r
160 MSR_IA32_FEATURE_CONTROL,\r
161 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
162 Bits.Lock,\r
163 1\r
164 );\r
165\r
80c4b236
JF
166 return RETURN_SUCCESS;\r
167}\r
168\r
169/**\r
170 Detects if SMX feature supported on current processor.\r
171\r
172 @param[in] ProcessorNumber The index of the CPU executing this function.\r
173 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
174 structure for the CPU executing this function.\r
175 @param[in] ConfigData A pointer to the configuration buffer returned\r
176 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
177 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
178 RegisterCpuFeature().\r
179\r
180 @retval TRUE SMX feature is supported.\r
181 @retval FALSE SMX feature is not supported.\r
182\r
183 @note This service could be called by BSP/APs.\r
184**/\r
185BOOLEAN\r
186EFIAPI\r
187SmxSupport (\r
188 IN UINTN ProcessorNumber,\r
189 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
190 IN VOID *ConfigData OPTIONAL\r
191 )\r
192{\r
80c4b236
JF
193 return (CpuInfo->CpuIdVersionInfoEcx.Bits.SMX == 1);\r
194}\r
195\r
196/**\r
b1fe2029 197 Initializes SMX feature to specific state.\r
80c4b236
JF
198\r
199 @param[in] ProcessorNumber The index of the CPU executing this function.\r
200 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION\r
201 structure for the CPU executing this function.\r
202 @param[in] ConfigData A pointer to the configuration buffer returned\r
203 by CPU_FEATURE_GET_CONFIG_DATA. NULL if\r
204 CPU_FEATURE_GET_CONFIG_DATA was not provided in\r
205 RegisterCpuFeature().\r
b1fe2029
ED
206 @param[in] State If TRUE, then SMX feature must be enabled.\r
207 If FALSE, then SMX feature must be disabled.\r
80c4b236 208\r
b1fe2029
ED
209 @retval RETURN_SUCCESS SMX feature is initialized.\r
210 @retval RETURN_UNSUPPORTED VMX not initialized.\r
80c4b236
JF
211\r
212 @note This service could be called by BSP only.\r
213**/\r
214RETURN_STATUS\r
215EFIAPI\r
b1fe2029 216SmxInitialize (\r
80c4b236
JF
217 IN UINTN ProcessorNumber,\r
218 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,\r
4ec586b9 219 IN VOID *ConfigData OPTIONAL,\r
80c4b236
JF
220 IN BOOLEAN State\r
221 )\r
222{\r
053e878b 223 RETURN_STATUS Status;\r
b1fe2029 224\r
d28daadd
ED
225 //\r
226 // The scope of Lock bit in the MSR_IA32_FEATURE_CONTROL is core for\r
227 // below processor type, only program MSR_IA32_FEATURE_CONTROL for thread 0 in each\r
228 // core.\r
229 //\r
230 if (IS_GOLDMONT_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel) ||\r
053e878b
MK
231 IS_GOLDMONT_PLUS_PROCESSOR (CpuInfo->DisplayFamily, CpuInfo->DisplayModel))\r
232 {\r
d28daadd
ED
233 if (CpuInfo->ProcessorInfo.Location.Thread != 0) {\r
234 return RETURN_SUCCESS;\r
235 }\r
236 }\r
237\r
b1fe2029
ED
238 Status = RETURN_SUCCESS;\r
239\r
240 if (State && (!IsCpuFeatureInSetting (CPU_FEATURE_VMX))) {\r
241 DEBUG ((DEBUG_WARN, "Warning :: Can't enable SMX feature when VMX feature not enabled, disable it.\n"));\r
053e878b 242 State = FALSE;\r
b1fe2029
ED
243 Status = RETURN_UNSUPPORTED;\r
244 }\r
80c4b236 245\r
1f0d8096
JV
246 CPU_REGISTER_TABLE_WRITE_FIELD (\r
247 ProcessorNumber,\r
248 ControlRegister,\r
249 4,\r
250 IA32_CR4,\r
251 Bits.SMXE,\r
252 (State) ? 1 : 0\r
053e878b 253 )\r
1f0d8096 254\r
9c90d39b
ED
255 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
256 ProcessorNumber,\r
257 Msr,\r
258 MSR_IA32_FEATURE_CONTROL,\r
259 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
260 Bits.SenterLocalFunctionEnables,\r
261 (State) ? 0x7F : 0\r
262 );\r
263\r
264 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
265 ProcessorNumber,\r
266 Msr,\r
267 MSR_IA32_FEATURE_CONTROL,\r
268 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
269 Bits.SenterGlobalEnable,\r
270 (State) ? 1 : 0\r
271 );\r
272\r
273 CPU_REGISTER_TABLE_TEST_THEN_WRITE_FIELD (\r
274 ProcessorNumber,\r
275 Msr,\r
276 MSR_IA32_FEATURE_CONTROL,\r
277 MSR_IA32_FEATURE_CONTROL_REGISTER,\r
278 Bits.EnableVmxInsideSmx,\r
279 (State) ? 1 : 0\r
280 );\r
281\r
b1fe2029 282 return Status;\r
80c4b236 283}\r