RE: [PATCH v3 5/9] iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module
From: Tian, Kevin
Date: Tue Nov 24 2026 - 22:49:51 EST
> From: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
> Sent: Thursday, November 12, 2026 12:31 PM
>
> Once tdx-host acquires an IOMMU for TDH.IOMMU.SETUP, the TDX module
> owns its Secure TDX Mode state. The VT-d driver must not change the
> global state of the IOMMU behind the TDX module's back, nor issue
just part of the global state, right?
> enhanced commands, until the IOMMU is released after TDH.IOMMU.CLEAR.
>
> Use the tdxc_acquired flag of struct intel_iommu, which is protected
> by register_lock, to block both. dmar_tdxc_acquire() now fails with
'both' means what?
> -EBUSY if an enhanced command is still in progress when it sets the
> flag. All GCMD writers hold register_lock and wait for completion before
> dropping it, so setting the flag under the same lock guarantees no GCMD
> operation is in flight once acquire() returns. While the flag is set,
> ecmd_submit_sync() fails with -EBUSY rather than waiting, as it may be
> called from atomic context.
this needs a re-write for why GCMD/ECMD are blocked differently.
>
> Write buffer flushing is the only GCMD write reachable on the runtime
> map path. Rather than adding a check there, refuse to acquire IOMMUs
> that require it (rwbf_quirk or CAP.RWBF). Move rwbf_quirk and
> rwbf_required() to iommu.h so dmar.c can use them.
good to mention that this quirk is on early old platforms.
>
> The remaining GCMD writers (root table, translation enable/disable,
> queued invalidation and interrupt remapping) are only used at init,
> hotplug and suspend/resume. Acquired units are never hot-removed,
> hibernation is unavailable with TDX, and the following patch refuses
> system suspend while an IOMMU is acquired. Guard them with
> WARN_ON_ONCE() ahead of their companion register writes (RTADDR,
> IQT/IQA, IRTA).
>
> intel_iommu_shutdown() is the exception. It is only called from
> native_machine_shutdown() via machine_shutdown(). On restart, halt,
> power-off and kexec, syscore_shutdown() runs first and tdx_shutdown()
> issues TDH.SYS.DISABLE, which takes every IOMMU out of Secure TDX Mode.
> The crash kexec path doesn't call it. Clear tdxc_acquired there so
> translation is still turned off.
>
> Signed-off-by: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
> ---
> drivers/iommu/intel/iommu.h | 8 ++++-
> drivers/iommu/intel/dmar.c | 50 ++++++++++++++++++++++++-----
> drivers/iommu/intel/iommu.c | 33 +++++++++++++++++--
> drivers/iommu/intel/irq_remapping.c | 10 ++++++
> 4 files changed, 89 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
> index 18fef91753a0..26135248675d 100644
> --- a/drivers/iommu/intel/iommu.h
> +++ b/drivers/iommu/intel/iommu.h
> @@ -690,7 +690,10 @@ struct intel_iommu {
> u64 ecmdcap[DMA_MAX_NUM_ECMDCAP];
> u32 gcmd; /* Holds TE, EAFL. Don't need SRTP, SFL, WBF
> */
> raw_spinlock_t register_lock; /* protect register handling */
> - /* Acquired for the TDX module. Protected by register_lock. */
> + /*
> + * GCMD and ECMD writes are rejected while the TDX module owns
> this
> + * IOMMU. Protected by register_lock.
> + */
> bool tdxc_acquired;
No need to update this comment. It's still for acquiring the tdx mode.
explanation of restrictions in the tdx mode belongs to other places.
> -static void dmar_tdxc_set_acquired(struct intel_iommu *iommu, bool
> acquired)
> +static int dmar_tdxc_block_regs(struct intel_iommu *iommu)
You could keep the original name with comment internally to explain
the need of blocking certain registers.
> {
> - guard(raw_spinlock_irqsave)(&iommu->register_lock);
> - iommu->tdxc_acquired = acquired;
> + unsigned long flags;
> + int ret = 0;
> +
> + raw_spin_lock_irqsave(&iommu->register_lock, flags);
> + if (cap_ecmds(iommu->cap) &&
> + (readq(iommu->reg + DMAR_ECRSP_REG) &
> DMA_ECMD_ECRSP_IP))
> + ret = -EBUSY;
here a comment is appreciated, for both why a check on ecmd is required
but not for gcmd.
> @@ -2669,11 +2695,19 @@ int dmar_tdxc_acquire(struct pci_dev *pdev,
> if (!(iommu->gcmd & DMA_GCMD_TE) || !iommu->qi
> || !sm_supported(iommu))
> return -EINVAL;
>
> + /* Write buffer flushing needs GCMD, which is blocked once acquired.
> */
> + if (rwbf_required(iommu))
> + return -EOPNOTSUPP;
> +
let's be clear here that WBF is incompatible with tdx mode. It's an old
quirk hence simply rejecting it if required.
> @@ -4336,6 +4353,16 @@ int ecmd_submit_sync(struct intel_iommu
> *iommu, u8 ecmd, u64 oa, u64 ob)
>
> raw_spin_lock_irqsave(&iommu->register_lock, flags);
>
> + /*
> + * The TDX module owns this IOMMU. This may be called from
the TDX module doesn't own the entire IOMMU
> atomic
> + * context (e.g. perf pmu::start/stop), so don't sleep; just reject
> + * the request.
> + */
> + if (iommu->tdxc_acquired) {
> + ret = -EBUSY;
> + goto err;
> + }
> +
iommu_pmu_stop() doesn't check the return value of this. It's a void
function. So returning error here still breaks the perf feature.
we may need a way to disable iommu perf completely in the acquire
function.