]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuCommonFeaturesLib/FeatureControl.c
UefiCpuPkg/CpuCommonFeaturesLib: Fix the documentation of PpinSupport().
[mirror_edk2.git] / UefiCpuPkg / Library / CpuCommonFeaturesLib / FeatureControl.c
1 /** @file
2 Features in MSR_IA32_FEATURE_CONTROL register.
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 Prepares for the data used by CPU feature detection and initialization.
19
20 @param[in] NumberOfProcessors The number of CPUs in the platform.
21
22 @return Pointer to a buffer of CPU related configuration data.
23
24 @note This service could be called by BSP only.
25 **/
26 VOID *
27 EFIAPI
28 FeatureControlGetConfigData (
29 IN UINTN NumberOfProcessors
30 )
31 {
32 VOID *ConfigData;
33
34 ConfigData = AllocateZeroPool (sizeof (MSR_IA32_FEATURE_CONTROL_REGISTER) * NumberOfProcessors);
35 ASSERT (ConfigData != NULL);
36 return ConfigData;
37 }
38
39 /**
40 Detects if VMX feature supported on current processor.
41
42 @param[in] ProcessorNumber The index of the CPU executing this function.
43 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
44 structure for the CPU executing this function.
45 @param[in] ConfigData A pointer to the configuration buffer returned
46 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
47 CPU_FEATURE_GET_CONFIG_DATA was not provided in
48 RegisterCpuFeature().
49
50 @retval TRUE VMX feature is supported.
51 @retval FALSE VMX feature is not supported.
52
53 @note This service could be called by BSP/APs.
54 **/
55 BOOLEAN
56 EFIAPI
57 VmxSupport (
58 IN UINTN ProcessorNumber,
59 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
60 IN VOID *ConfigData OPTIONAL
61 )
62 {
63 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
64
65 ASSERT (ConfigData != NULL);
66 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
67 MsrRegister[ProcessorNumber].Uint64 = AsmReadMsr64 (MSR_IA32_FEATURE_CONTROL);
68 return (CpuInfo->CpuIdVersionInfoEcx.Bits.VMX == 1);
69 }
70
71 /**
72 Initializes VMX feature to specific state.
73
74 @param[in] ProcessorNumber The index of the CPU executing this function.
75 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
76 structure for the CPU executing this function.
77 @param[in] ConfigData A pointer to the configuration buffer returned
78 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
79 CPU_FEATURE_GET_CONFIG_DATA was not provided in
80 RegisterCpuFeature().
81 @param[in] State If TRUE, then the VMX feature must be enabled.
82 If FALSE, then the VMX feature must be disabled.
83
84 @retval RETURN_SUCCESS VMX feature is initialized.
85
86 @note This service could be called by BSP only.
87 **/
88 RETURN_STATUS
89 EFIAPI
90 VmxInitialize (
91 IN UINTN ProcessorNumber,
92 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
93 IN VOID *ConfigData, OPTIONAL
94 IN BOOLEAN State
95 )
96 {
97 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
98
99 ASSERT (ConfigData != NULL);
100 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
101 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {
102 CPU_REGISTER_TABLE_WRITE_FIELD (
103 ProcessorNumber,
104 Msr,
105 MSR_IA32_FEATURE_CONTROL,
106 MSR_IA32_FEATURE_CONTROL_REGISTER,
107 Bits.EnableVmxOutsideSmx,
108 (State) ? 1 : 0
109 );
110 }
111 return RETURN_SUCCESS;
112 }
113
114 /**
115 Detects if Lock Feature Control Register feature supported on current processor.
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
125 @retval TRUE Lock Feature Control Register feature is supported.
126 @retval FALSE Lock Feature Control Register feature is not supported.
127
128 @note This service could be called by BSP/APs.
129 **/
130 BOOLEAN
131 EFIAPI
132 LockFeatureControlRegisterSupport (
133 IN UINTN ProcessorNumber,
134 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
135 IN VOID *ConfigData OPTIONAL
136 )
137 {
138 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
139
140 ASSERT (ConfigData != NULL);
141 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
142 MsrRegister[ProcessorNumber].Uint64 = AsmReadMsr64 (MSR_IA32_FEATURE_CONTROL);
143 return TRUE;
144 }
145
146 /**
147 Initializes Lock Feature Control Register feature to specific state.
148
149 @param[in] ProcessorNumber The index of the CPU executing this function.
150 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
151 structure for the CPU executing this function.
152 @param[in] ConfigData A pointer to the configuration buffer returned
153 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
154 CPU_FEATURE_GET_CONFIG_DATA was not provided in
155 RegisterCpuFeature().
156 @param[in] State If TRUE, then the Lock Feature Control Register feature must be enabled.
157 If FALSE, then the Lock Feature Control Register feature must be disabled.
158
159 @retval RETURN_SUCCESS Lock Feature Control Register feature is initialized.
160
161 @note This service could be called by BSP only.
162 **/
163 RETURN_STATUS
164 EFIAPI
165 LockFeatureControlRegisterInitialize (
166 IN UINTN ProcessorNumber,
167 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
168 IN VOID *ConfigData, OPTIONAL
169 IN BOOLEAN State
170 )
171 {
172 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
173
174 ASSERT (ConfigData != NULL);
175 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
176 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {
177 CPU_REGISTER_TABLE_WRITE_FIELD (
178 ProcessorNumber,
179 Msr,
180 MSR_IA32_FEATURE_CONTROL,
181 MSR_IA32_FEATURE_CONTROL_REGISTER,
182 Bits.Lock,
183 1
184 );
185 }
186 return RETURN_SUCCESS;
187 }
188
189 /**
190 Detects if SMX feature supported on current processor.
191
192 @param[in] ProcessorNumber The index of the CPU executing this function.
193 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
194 structure for the CPU executing this function.
195 @param[in] ConfigData A pointer to the configuration buffer returned
196 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
197 CPU_FEATURE_GET_CONFIG_DATA was not provided in
198 RegisterCpuFeature().
199
200 @retval TRUE SMX feature is supported.
201 @retval FALSE SMX feature is not supported.
202
203 @note This service could be called by BSP/APs.
204 **/
205 BOOLEAN
206 EFIAPI
207 SmxSupport (
208 IN UINTN ProcessorNumber,
209 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
210 IN VOID *ConfigData OPTIONAL
211 )
212 {
213 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
214
215 ASSERT (ConfigData != NULL);
216 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
217 MsrRegister[ProcessorNumber].Uint64 = AsmReadMsr64 (MSR_IA32_FEATURE_CONTROL);
218 return (CpuInfo->CpuIdVersionInfoEcx.Bits.SMX == 1);
219 }
220
221 /**
222 Initializes SMX feature to specific state.
223
224 @param[in] ProcessorNumber The index of the CPU executing this function.
225 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
226 structure for the CPU executing this function.
227 @param[in] ConfigData A pointer to the configuration buffer returned
228 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
229 CPU_FEATURE_GET_CONFIG_DATA was not provided in
230 RegisterCpuFeature().
231 @param[in] State If TRUE, then SMX feature must be enabled.
232 If FALSE, then SMX feature must be disabled.
233
234 @retval RETURN_SUCCESS SMX feature is initialized.
235 @retval RETURN_UNSUPPORTED VMX not initialized.
236
237 @note This service could be called by BSP only.
238 **/
239 RETURN_STATUS
240 EFIAPI
241 SmxInitialize (
242 IN UINTN ProcessorNumber,
243 IN REGISTER_CPU_FEATURE_INFORMATION *CpuInfo,
244 IN VOID *ConfigData, OPTIONAL
245 IN BOOLEAN State
246 )
247 {
248 MSR_IA32_FEATURE_CONTROL_REGISTER *MsrRegister;
249 RETURN_STATUS Status;
250
251 Status = RETURN_SUCCESS;
252
253 if (State && (!IsCpuFeatureInSetting (CPU_FEATURE_VMX))) {
254 DEBUG ((DEBUG_WARN, "Warning :: Can't enable SMX feature when VMX feature not enabled, disable it.\n"));
255 State = FALSE;
256 Status = RETURN_UNSUPPORTED;
257 }
258
259 ASSERT (ConfigData != NULL);
260 MsrRegister = (MSR_IA32_FEATURE_CONTROL_REGISTER *) ConfigData;
261 if (MsrRegister[ProcessorNumber].Bits.Lock == 0) {
262 CPU_REGISTER_TABLE_WRITE_FIELD (
263 ProcessorNumber,
264 Msr,
265 MSR_IA32_FEATURE_CONTROL,
266 MSR_IA32_FEATURE_CONTROL_REGISTER,
267 Bits.SenterLocalFunctionEnables,
268 (State) ? 0x7F : 0
269 );
270
271 CPU_REGISTER_TABLE_WRITE_FIELD (
272 ProcessorNumber,
273 Msr,
274 MSR_IA32_FEATURE_CONTROL,
275 MSR_IA32_FEATURE_CONTROL_REGISTER,
276 Bits.SenterGlobalEnable,
277 (State) ? 1 : 0
278 );
279
280 CPU_REGISTER_TABLE_WRITE_FIELD (
281 ProcessorNumber,
282 Msr,
283 MSR_IA32_FEATURE_CONTROL,
284 MSR_IA32_FEATURE_CONTROL_REGISTER,
285 Bits.EnableVmxInsideSmx,
286 (State) ? 1 : 0
287 );
288 }
289 return Status;
290 }