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