]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/SmmControl2Dxe/SmmControl2Dxe.c
BaseTools: Library hashing fix and optimization for --hash feature
[mirror_edk2.git] / OvmfPkg / SmmControl2Dxe / SmmControl2Dxe.c
CommitLineData
c40e1a0c
LE
1/** @file\r
2\r
3 A DXE_RUNTIME_DRIVER providing synchronous SMI activations via the\r
4 EFI_SMM_CONTROL2_PROTOCOL.\r
5\r
6 We expect the PEI phase to have covered the following:\r
7 - ensure that the underlying QEMU machine type be Q35\r
8 (responsible: OvmfPkg/SmmAccess/SmmAccessPei.inf)\r
9 - ensure that the ACPI PM IO space be configured\r
10 (responsible: OvmfPkg/PlatformPei/PlatformPei.inf)\r
11\r
12 Our own entry point is responsible for confirming the SMI feature and for\r
13 configuring it.\r
14\r
15 Copyright (C) 2013, 2015, Red Hat, Inc.<BR>\r
16 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
17\r
b26f0cf9 18 SPDX-License-Identifier: BSD-2-Clause-Patent\r
c40e1a0c
LE
19\r
20**/\r
21\r
22#include <IndustryStandard/Q35MchIch9.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/IoLib.h>\r
26#include <Library/PcdLib.h>\r
27#include <Library/PciLib.h>\r
28#include <Library/QemuFwCfgLib.h>\r
687f7521 29#include <Library/QemuFwCfgS3Lib.h>\r
c40e1a0c
LE
30#include <Library/UefiBootServicesTableLib.h>\r
31#include <Protocol/S3SaveState.h>\r
32#include <Protocol/SmmControl2.h>\r
33\r
a316d7ac
LE
34#include "SmiFeatures.h"\r
35\r
c40e1a0c
LE
36//\r
37// Forward declaration.\r
38//\r
39STATIC\r
40VOID\r
41EFIAPI\r
42OnS3SaveStateInstalled (\r
43 IN EFI_EVENT Event,\r
44 IN VOID *Context\r
45 );\r
46\r
47//\r
48// The absolute IO port address of the SMI Control and Enable Register. It is\r
49// only used to carry information from the entry point function to the\r
50// S3SaveState protocol installation callback, strictly before the runtime\r
51// phase.\r
52//\r
53STATIC UINTN mSmiEnable;\r
54\r
a316d7ac
LE
55//\r
56// Captures whether SMI feature negotiation is supported. The variable is only\r
57// used to carry this information from the entry point function to the\r
58// S3SaveState protocol installation callback.\r
59//\r
60STATIC BOOLEAN mSmiFeatureNegotiation;\r
61\r
c40e1a0c
LE
62//\r
63// Event signaled when an S3SaveState protocol interface is installed.\r
64//\r
65STATIC EFI_EVENT mS3SaveStateInstalled;\r
66\r
67/**\r
68 Invokes SMI activation from either the preboot or runtime environment.\r
69\r
70 This function generates an SMI.\r
71\r
72 @param[in] This The EFI_SMM_CONTROL2_PROTOCOL instance.\r
73 @param[in,out] CommandPort The value written to the command port.\r
74 @param[in,out] DataPort The value written to the data port.\r
75 @param[in] Periodic Optional mechanism to engender a periodic\r
76 stream.\r
77 @param[in] ActivationInterval Optional parameter to repeat at this\r
78 period one time or, if the Periodic\r
79 Boolean is set, periodically.\r
80\r
81 @retval EFI_SUCCESS The SMI/PMI has been engendered.\r
82 @retval EFI_DEVICE_ERROR The timing is unsupported.\r
83 @retval EFI_INVALID_PARAMETER The activation period is unsupported.\r
84 @retval EFI_INVALID_PARAMETER The last periodic activation has not been\r
85 cleared.\r
86 @retval EFI_NOT_STARTED The SMM base service has not been initialized.\r
87**/\r
88STATIC\r
89EFI_STATUS\r
90EFIAPI\r
91SmmControl2DxeTrigger (\r
92 IN CONST EFI_SMM_CONTROL2_PROTOCOL *This,\r
93 IN OUT UINT8 *CommandPort OPTIONAL,\r
94 IN OUT UINT8 *DataPort OPTIONAL,\r
95 IN BOOLEAN Periodic OPTIONAL,\r
96 IN UINTN ActivationInterval OPTIONAL\r
97 )\r
98{\r
99 //\r
100 // No support for queued or periodic activation.\r
101 //\r
102 if (Periodic || ActivationInterval > 0) {\r
103 return EFI_DEVICE_ERROR;\r
104 }\r
105\r
106 //\r
107 // The so-called "Advanced Power Management Status Port Register" is in fact\r
108 // a generic data passing register, between the caller and the SMI\r
109 // dispatcher. The ICH9 spec calls it "scratchpad register" -- calling it\r
110 // "status" elsewhere seems quite the misnomer. Status registers usually\r
111 // report about hardware status, while this register is fully governed by\r
112 // software.\r
113 //\r
114 // Write to the status register first, as this won't trigger the SMI just\r
115 // yet. Then write to the control register.\r
116 //\r
117 IoWrite8 (ICH9_APM_STS, DataPort == NULL ? 0 : *DataPort);\r
118 IoWrite8 (ICH9_APM_CNT, CommandPort == NULL ? 0 : *CommandPort);\r
119 return EFI_SUCCESS;\r
120}\r
121\r
122/**\r
123 Clears any system state that was created in response to the Trigger() call.\r
124\r
125 This function acknowledges and causes the deassertion of the SMI activation\r
126 source.\r
127\r
128 @param[in] This The EFI_SMM_CONTROL2_PROTOCOL instance.\r
129 @param[in] Periodic Optional parameter to repeat at this period\r
130 one time\r
131\r
132 @retval EFI_SUCCESS The SMI/PMI has been engendered.\r
133 @retval EFI_DEVICE_ERROR The source could not be cleared.\r
134 @retval EFI_INVALID_PARAMETER The service did not support the Periodic input\r
135 argument.\r
136**/\r
137STATIC\r
138EFI_STATUS\r
139EFIAPI\r
140SmmControl2DxeClear (\r
141 IN CONST EFI_SMM_CONTROL2_PROTOCOL *This,\r
142 IN BOOLEAN Periodic OPTIONAL\r
143 )\r
144{\r
145 if (Periodic) {\r
146 return EFI_INVALID_PARAMETER;\r
147 }\r
148\r
149 //\r
150 // The PI spec v1.4 explains that Clear() is only supposed to clear software\r
151 // status; it is not in fact responsible for deasserting the SMI. It gives\r
152 // two reasons for this: (a) many boards clear the SMI automatically when\r
153 // entering SMM, (b) if Clear() actually deasserted the SMI, then it could\r
154 // incorrectly suppress an SMI that was asynchronously asserted between the\r
155 // last return of the SMI handler and the call made to Clear().\r
156 //\r
157 // In fact QEMU automatically deasserts CPU_INTERRUPT_SMI in:\r
158 // - x86_cpu_exec_interrupt() [target-i386/seg_helper.c], and\r
159 // - kvm_arch_pre_run() [target-i386/kvm.c].\r
160 //\r
161 // So, nothing to do here.\r
162 //\r
163 return EFI_SUCCESS;\r
164}\r
165\r
166STATIC EFI_SMM_CONTROL2_PROTOCOL mControl2 = {\r
167 &SmmControl2DxeTrigger,\r
168 &SmmControl2DxeClear,\r
169 MAX_UINTN // MinimumTriggerPeriod -- we don't support periodic SMIs\r
170};\r
171\r
172//\r
173// Entry point of this driver.\r
174//\r
175EFI_STATUS\r
176EFIAPI\r
177SmmControl2DxeEntryPoint (\r
178 IN EFI_HANDLE ImageHandle,\r
179 IN EFI_SYSTEM_TABLE *SystemTable\r
180 )\r
181{\r
182 UINT32 PmBase;\r
183 UINT32 SmiEnableVal;\r
184 EFI_STATUS Status;\r
185\r
186 //\r
187 // This module should only be included if SMRAM support is required.\r
188 //\r
189 ASSERT (FeaturePcdGet (PcdSmmSmramRequire));\r
190\r
191 //\r
192 // Calculate the absolute IO port address of the SMI Control and Enable\r
193 // Register. (As noted at the top, the PEI phase has left us with a working\r
194 // ACPI PM IO space.)\r
195 //\r
196 PmBase = PciRead32 (POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE)) &\r
197 ICH9_PMBASE_MASK;\r
198 mSmiEnable = PmBase + ICH9_PMBASE_OFS_SMI_EN;\r
199\r
200 //\r
201 // If APMC_EN is pre-set in SMI_EN, that's QEMU's way to tell us that SMI\r
202 // support is not available. (For example due to KVM lacking it.) Otherwise,\r
203 // this bit is clear after each reset.\r
204 //\r
205 SmiEnableVal = IoRead32 (mSmiEnable);\r
206 if ((SmiEnableVal & ICH9_SMI_EN_APMC_EN) != 0) {\r
207 DEBUG ((EFI_D_ERROR, "%a: this Q35 implementation lacks SMI\n",\r
208 __FUNCTION__));\r
209 goto FatalError;\r
210 }\r
211\r
212 //\r
213 // Otherwise, configure the board to inject an SMI when ICH9_APM_CNT is\r
214 // written to. (See the Trigger() method above.)\r
215 //\r
216 SmiEnableVal |= ICH9_SMI_EN_APMC_EN | ICH9_SMI_EN_GBL_SMI_EN;\r
217 IoWrite32 (mSmiEnable, SmiEnableVal);\r
218\r
219 //\r
220 // Prevent software from undoing the above (until platform reset).\r
221 //\r
222 PciOr16 (POWER_MGMT_REGISTER_Q35 (ICH9_GEN_PMCON_1),\r
223 ICH9_GEN_PMCON_1_SMI_LOCK);\r
224\r
225 //\r
226 // If we can clear GBL_SMI_EN now, that means QEMU's SMI support is not\r
227 // appropriate.\r
228 //\r
229 IoWrite32 (mSmiEnable, SmiEnableVal & ~(UINT32)ICH9_SMI_EN_GBL_SMI_EN);\r
230 if (IoRead32 (mSmiEnable) != SmiEnableVal) {\r
231 DEBUG ((EFI_D_ERROR, "%a: failed to lock down GBL_SMI_EN\n",\r
232 __FUNCTION__));\r
233 goto FatalError;\r
234 }\r
235\r
a316d7ac
LE
236 //\r
237 // QEMU can inject SMIs in different ways, negotiate our preferences.\r
238 //\r
239 mSmiFeatureNegotiation = NegotiateSmiFeatures ();\r
240\r
c40e1a0c
LE
241 if (QemuFwCfgS3Enabled ()) {\r
242 VOID *Registration;\r
243\r
244 //\r
245 // On S3 resume the above register settings have to be repeated. Register a\r
246 // protocol notify callback that, when boot script saving becomes\r
247 // available, saves operations equivalent to the above to the boot script.\r
248 //\r
249 Status = gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK,\r
250 OnS3SaveStateInstalled, NULL /* Context */,\r
251 &mS3SaveStateInstalled);\r
252 if (EFI_ERROR (Status)) {\r
253 DEBUG ((EFI_D_ERROR, "%a: CreateEvent: %r\n", __FUNCTION__, Status));\r
254 goto FatalError;\r
255 }\r
256\r
257 Status = gBS->RegisterProtocolNotify (&gEfiS3SaveStateProtocolGuid,\r
258 mS3SaveStateInstalled, &Registration);\r
259 if (EFI_ERROR (Status)) {\r
260 DEBUG ((EFI_D_ERROR, "%a: RegisterProtocolNotify: %r\n", __FUNCTION__,\r
261 Status));\r
262 goto ReleaseEvent;\r
263 }\r
264\r
265 //\r
266 // Kick the event right now -- maybe the boot script is already saveable.\r
267 //\r
268 Status = gBS->SignalEvent (mS3SaveStateInstalled);\r
269 if (EFI_ERROR (Status)) {\r
270 DEBUG ((EFI_D_ERROR, "%a: SignalEvent: %r\n", __FUNCTION__, Status));\r
271 goto ReleaseEvent;\r
272 }\r
273 }\r
274\r
275 //\r
276 // We have no pointers to convert to virtual addresses. The handle itself\r
277 // doesn't matter, as protocol services are not accessible at runtime.\r
278 //\r
279 Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,\r
280 &gEfiSmmControl2ProtocolGuid, &mControl2,\r
281 NULL);\r
282 if (EFI_ERROR (Status)) {\r
283 DEBUG ((EFI_D_ERROR, "%a: InstallMultipleProtocolInterfaces: %r\n",\r
284 __FUNCTION__, Status));\r
285 goto ReleaseEvent;\r
286 }\r
287\r
288 return EFI_SUCCESS;\r
289\r
290ReleaseEvent:\r
291 if (mS3SaveStateInstalled != NULL) {\r
292 gBS->CloseEvent (mS3SaveStateInstalled);\r
293 }\r
294\r
295FatalError:\r
296 //\r
297 // We really don't want to continue in this case.\r
298 //\r
299 ASSERT (FALSE);\r
300 CpuDeadLoop ();\r
301 return EFI_UNSUPPORTED;\r
302}\r
303\r
304/**\r
305 Notification callback for S3SaveState installation.\r
306\r
307 @param[in] Event Event whose notification function is being invoked.\r
308\r
309 @param[in] Context The pointer to the notification function's context, which\r
310 is implementation-dependent.\r
311**/\r
312STATIC\r
313VOID\r
314EFIAPI\r
315OnS3SaveStateInstalled (\r
316 IN EFI_EVENT Event,\r
317 IN VOID *Context\r
318 )\r
319{\r
320 EFI_STATUS Status;\r
321 EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState;\r
322 UINT32 SmiEnOrMask, SmiEnAndMask;\r
7ecfa0aa 323 UINT64 GenPmCon1Address;\r
c40e1a0c
LE
324 UINT16 GenPmCon1OrMask, GenPmCon1AndMask;\r
325\r
326 ASSERT (Event == mS3SaveStateInstalled);\r
327\r
328 Status = gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid,\r
329 NULL /* Registration */, (VOID **)&S3SaveState);\r
330 if (EFI_ERROR (Status)) {\r
331 return;\r
332 }\r
333\r
334 //\r
335 // These operations were originally done, verified and explained in the entry\r
336 // point function of the driver.\r
337 //\r
338 SmiEnOrMask = ICH9_SMI_EN_APMC_EN | ICH9_SMI_EN_GBL_SMI_EN;\r
339 SmiEnAndMask = MAX_UINT32;\r
340 Status = S3SaveState->Write (\r
341 S3SaveState,\r
342 EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE,\r
343 EfiBootScriptWidthUint32,\r
344 (UINT64)mSmiEnable,\r
345 &SmiEnOrMask,\r
346 &SmiEnAndMask\r
347 );\r
348 if (EFI_ERROR (Status)) {\r
349 DEBUG ((EFI_D_ERROR, "%a: EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE: %r\n",\r
350 __FUNCTION__, Status));\r
351 ASSERT (FALSE);\r
352 CpuDeadLoop ();\r
353 }\r
354\r
7ecfa0aa
LE
355 GenPmCon1Address = POWER_MGMT_REGISTER_Q35_EFI_PCI_ADDRESS (\r
356 ICH9_GEN_PMCON_1);\r
c40e1a0c
LE
357 GenPmCon1OrMask = ICH9_GEN_PMCON_1_SMI_LOCK;\r
358 GenPmCon1AndMask = MAX_UINT16;\r
359 Status = S3SaveState->Write (\r
360 S3SaveState,\r
361 EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE,\r
362 EfiBootScriptWidthUint16,\r
7ecfa0aa 363 GenPmCon1Address,\r
c40e1a0c
LE
364 &GenPmCon1OrMask,\r
365 &GenPmCon1AndMask\r
366 );\r
367 if (EFI_ERROR (Status)) {\r
368 DEBUG ((EFI_D_ERROR,\r
369 "%a: EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE: %r\n", __FUNCTION__,\r
370 Status));\r
371 ASSERT (FALSE);\r
372 CpuDeadLoop ();\r
373 }\r
374\r
36a6aa6c
LE
375 DEBUG ((DEBUG_VERBOSE, "%a: chipset boot script saved\n", __FUNCTION__));\r
376\r
a316d7ac
LE
377 //\r
378 // Append a boot script fragment that re-selects the negotiated SMI features.\r
379 //\r
380 if (mSmiFeatureNegotiation) {\r
36a6aa6c 381 SaveSmiFeatures ();\r
a316d7ac
LE
382 }\r
383\r
c40e1a0c
LE
384 gBS->CloseEvent (Event);\r
385 mS3SaveStateInstalled = NULL;\r
386}\r