2 Negotiate SMI features with QEMU, and configure UefiCpuPkg/PiSmmCpuDxeSmm
5 Copyright (C) 2016-2017, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/MemEncryptSevLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/QemuFwCfgLib.h>
16 #include <Library/QemuFwCfgS3Lib.h>
18 #include "SmiFeatures.h"
21 // The following bit value stands for "broadcast SMI" in the
22 // "etc/smi/supported-features" and "etc/smi/requested-features" fw_cfg files.
24 #define ICH9_LPC_SMI_F_BROADCAST BIT0
26 // The following bit value stands for "enable CPU hotplug, and inject an SMI
27 // with control value ICH9_APM_CNT_CPU_HOTPLUG upon hotplug", in the
28 // "etc/smi/supported-features" and "etc/smi/requested-features" fw_cfg files.
30 #define ICH9_LPC_SMI_F_CPU_HOTPLUG BIT1
32 // The following bit value stands for "enable CPU hot-unplug, and inject an SMI
33 // with control value ICH9_APM_CNT_CPU_HOTPLUG upon hot-unplug", in the
34 // "etc/smi/supported-features" and "etc/smi/requested-features" fw_cfg files.
36 #define ICH9_LPC_SMI_F_CPU_HOT_UNPLUG BIT2
39 // Provides a scratch buffer (allocated in EfiReservedMemoryType type memory)
40 // for the S3 boot script fragment to write to and read from.
50 // These carry the selector keys of the "etc/smi/requested-features" and
51 // "etc/smi/features-ok" fw_cfg files from NegotiateSmiFeatures() to
52 // AppendFwCfgBootScript().
54 STATIC FIRMWARE_CONFIG_ITEM mRequestedFeaturesItem
;
55 STATIC FIRMWARE_CONFIG_ITEM mFeaturesOkItem
;
58 // Carries the negotiated SMI features from NegotiateSmiFeatures() to
59 // AppendFwCfgBootScript().
61 STATIC UINT64 mSmiFeatures
;
64 Negotiate SMI features with QEMU.
66 @retval FALSE If SMI feature negotiation is not supported by QEMU. This is
67 not an error, it just means that SaveSmiFeatures() should not
70 @retval TRUE SMI feature negotiation is supported, and it has completed
71 successfully as well. (Failure to negotiate is a fatal error
72 and the function never returns in that case.)
75 NegotiateSmiFeatures (
79 FIRMWARE_CONFIG_ITEM SupportedFeaturesItem
;
80 UINTN SupportedFeaturesSize
;
81 UINTN RequestedFeaturesSize
;
83 UINT64 RequestedFeaturesMask
;
86 // Look up the fw_cfg files used for feature negotiation. The selector keys
87 // of "etc/smi/requested-features" and "etc/smi/features-ok" are saved
88 // statically. If the files are missing, then QEMU doesn't support SMI
89 // feature negotiation.
91 if (RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/supported-features",
92 &SupportedFeaturesItem
, &SupportedFeaturesSize
)) ||
93 RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/requested-features",
94 &mRequestedFeaturesItem
, &RequestedFeaturesSize
)) ||
95 RETURN_ERROR (QemuFwCfgFindFile ("etc/smi/features-ok",
96 &mFeaturesOkItem
, &FeaturesOkSize
))) {
97 DEBUG ((DEBUG_INFO
, "%a: SMI feature negotiation unavailable\n",
103 // If the files are present but their sizes disagree with us, that's a fatal
104 // error (we can't trust the behavior of SMIs either way).
106 if (SupportedFeaturesSize
!= sizeof mSmiFeatures
||
107 RequestedFeaturesSize
!= sizeof mSmiFeatures
||
108 FeaturesOkSize
!= sizeof (UINT8
)) {
109 DEBUG ((DEBUG_ERROR
, "%a: size mismatch in feature negotiation\n",
115 // Get the features supported by the host.
117 QemuFwCfgSelectItem (SupportedFeaturesItem
);
118 QemuFwCfgReadBytes (sizeof mSmiFeatures
, &mSmiFeatures
);
121 // We want broadcast SMI, SMI on CPU hotplug, SMI on CPU hot-unplug
124 RequestedFeaturesMask
= ICH9_LPC_SMI_F_BROADCAST
;
125 if (!MemEncryptSevIsEnabled ()) {
127 // For now, we only support hotplug with SEV disabled.
129 RequestedFeaturesMask
|= ICH9_LPC_SMI_F_CPU_HOTPLUG
;
130 RequestedFeaturesMask
|= ICH9_LPC_SMI_F_CPU_HOT_UNPLUG
;
132 mSmiFeatures
&= RequestedFeaturesMask
;
133 QemuFwCfgSelectItem (mRequestedFeaturesItem
);
134 QemuFwCfgWriteBytes (sizeof mSmiFeatures
, &mSmiFeatures
);
137 // Invoke feature validation in QEMU. If the selection is accepted, the
138 // features will be locked down. If the selection is rejected, feature
139 // negotiation remains open; however we don't know what to do in that case,
140 // so that's a fatal error.
142 QemuFwCfgSelectItem (mFeaturesOkItem
);
143 if (QemuFwCfgRead8 () != 1) {
144 DEBUG ((DEBUG_ERROR
, "%a: negotiation failed for feature bitmap 0x%Lx\n",
145 __FUNCTION__
, mSmiFeatures
));
149 if ((mSmiFeatures
& ICH9_LPC_SMI_F_BROADCAST
) == 0) {
151 // If we can't get broadcast SMIs from QEMU, that's acceptable too,
152 // although not optimal.
154 DEBUG ((DEBUG_INFO
, "%a: SMI broadcast unavailable\n", __FUNCTION__
));
157 // Configure the traditional AP sync / SMI delivery mode for
158 // PiSmmCpuDxeSmm. Effectively, restore the UefiCpuPkg defaults, from which
159 // the original QEMU behavior (i.e., unicast SMI) used to differ.
161 if (RETURN_ERROR (PcdSet64S (PcdCpuSmmApSyncTimeout
, 1000000)) ||
162 RETURN_ERROR (PcdSet8S (PcdCpuSmmSyncMode
, 0x00))) {
163 DEBUG ((DEBUG_ERROR
, "%a: PiSmmCpuDxeSmm PCD configuration failed\n",
167 DEBUG ((DEBUG_INFO
, "%a: using SMI broadcast\n", __FUNCTION__
));
170 if ((mSmiFeatures
& ICH9_LPC_SMI_F_CPU_HOTPLUG
) == 0) {
171 DEBUG ((DEBUG_INFO
, "%a: CPU hotplug not negotiated\n", __FUNCTION__
));
173 DEBUG ((DEBUG_INFO
, "%a: CPU hotplug with SMI negotiated\n",
177 if ((mSmiFeatures
& ICH9_LPC_SMI_F_CPU_HOT_UNPLUG
) == 0) {
178 DEBUG ((DEBUG_INFO
, "%a: CPU hot-unplug not negotiated\n", __FUNCTION__
));
180 DEBUG ((DEBUG_INFO
, "%a: CPU hot-unplug with SMI negotiated\n",
185 // Negotiation successful (although we may not have gotten the optimal
194 // Keep the compiler happy.
200 FW_CFG_BOOT_SCRIPT_CALLBACK_FUNCTION provided to QemuFwCfgS3Lib.
205 AppendFwCfgBootScript (
206 IN OUT VOID
*Context
, OPTIONAL
207 IN OUT VOID
*ExternalScratchBuffer
210 SCRATCH_BUFFER
*ScratchBuffer
;
211 RETURN_STATUS Status
;
213 ScratchBuffer
= ExternalScratchBuffer
;
216 // Write the negotiated feature bitmap into "etc/smi/requested-features".
218 ScratchBuffer
->Features
= mSmiFeatures
;
219 Status
= QemuFwCfgS3ScriptWriteBytes (mRequestedFeaturesItem
,
220 sizeof ScratchBuffer
->Features
);
221 if (RETURN_ERROR (Status
)) {
226 // Read back "etc/smi/features-ok". This invokes the feature validation &
227 // lockdown. (The validation succeeded at first boot.)
229 Status
= QemuFwCfgS3ScriptReadBytes (mFeaturesOkItem
,
230 sizeof ScratchBuffer
->FeaturesOk
);
231 if (RETURN_ERROR (Status
)) {
236 // If "etc/smi/features-ok" read as 1, we're good. Otherwise, hang the S3
239 Status
= QemuFwCfgS3ScriptCheckValue (&ScratchBuffer
->FeaturesOk
,
240 sizeof ScratchBuffer
->FeaturesOk
, MAX_UINT8
, 1);
241 if (RETURN_ERROR (Status
)) {
245 DEBUG ((DEBUG_VERBOSE
, "%a: SMI feature negotiation boot script saved\n",
256 Append a boot script fragment that will re-select the previously negotiated
257 SMI features during S3 resume.
264 RETURN_STATUS Status
;
267 // We are already running at TPL_CALLBACK, on the stack of
268 // OnS3SaveStateInstalled(). But that's okay, we can easily queue more
269 // notification functions while executing a notification function.
271 Status
= QemuFwCfgS3CallWhenBootScriptReady (AppendFwCfgBootScript
, NULL
,
272 sizeof (SCRATCH_BUFFER
));
273 if (RETURN_ERROR (Status
)) {