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