Re: [PATCH v3 4/9] iommu/vt-d: Add interface for tdx-host when enabling trusted DMA

From: Xu Yilun

Date: Mon Nov 23 2026 - 10:20:59 EST


> Add a per-device interface so that an IOMMU is put into Secure TDX Mode
> only when a device behind it needs trusted DMA. tdx-host tracks how many
> devices use each IOMMU and calls into the VT-d driver only for the first
> and the last of them:
>
> - dmar_tdxc_get_params() looks up the DMAR unit translating the device

I think tdxc is not a good name since "TDX Connect" is not preferred as
a technical term. These APIs would be used by x86/tdx, so can we at
least avoid using tdxc for these APIs?

[...]

> +static void dmar_tdxc_fill_params(struct dmar_drhd_unit *drhd,
> + struct dmar_tdxc_params *params)
> +{
> + params->reg_base_addr = drhd->reg_base_addr;
> + params->node = drhd->iommu->node;
> + /*
> + * params->drhd stays valid because trusted DMA-capable units are
> + * never hot-removed.
> + */
> + params->drhd = drhd;

Why we store drhd in the params, any information to expose to TDX via
drhd?

[...]

> +/*
> + * dmar_tdxc_get_params() - Get the IOMMU information for a device
> + * @dev: PCI device that will use trusted DMA
> + * @params: Filled in on success
> + *
> + * Look up the IOMMU that translates @dev and return what tdx-host needs
> + * to issue TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR on it. This only reads
> + * information and doesn't change any state. Devices behind the same
> + * IOMMU get the same @params->reg_base_addr.
> + *
> + * Return 0 on success and error code on failure.
> + */
> +int dmar_tdxc_get_params(struct pci_dev *pdev, struct dmar_tdxc_params *params)
> +{
> + struct dmar_drhd_unit *drhd = dmar_tdxc_find_drhd(pdev);
> +
> + if (IS_ERR(drhd))
> + return PTR_ERR(drhd);
> +
> + dmar_tdxc_fill_params(drhd, params);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_FOR_MODULES(dmar_tdxc_get_params, "tdx-host");
> +
> +/*
> + * dmar_tdxc_acquire() - Acquire an IOMMU for TDH.IOMMU.SETUP
> + * @dev: PCI device that will use trusted DMA
> + * @params: Returned by dmar_tdxc_get_params() for @dev
> + *
> + * Call this right before issuing TDH.IOMMU.SETUP for the first user of
> + * the IOMMU. On success, the caller must issue TDH.IOMMU.SETUP and call
> + * dmar_tdxc_release() if it fails. The caller owns the per-IOMMU refcount
> + * and must serialize all calls for the same IOMMU.
> + *
> + * Return: 0 on success and error code on failure.
> + */
> +int dmar_tdxc_acquire(struct pci_dev *pdev,
> + const struct dmar_tdxc_params *params)
> +{
> + struct dmar_drhd_unit *drhd = params->drhd;
> + struct intel_iommu *iommu = drhd->iommu;
> + int ret;
> +
> + if (WARN_ON(dmar_tdxc_find_drhd(pdev) != drhd))

We can get drhd via pci_dev, then why we require callers to input
this info and judge? Like an intelligence test?

Thanks,
Yilun

> + return -EINVAL;
> +
> + guard(mutex)(&drhd->tdxc_lock);
> + /* Still set if TDH.IOMMU.CLEAR failed and tdx-host was reloaded. */
> + if (iommu->tdxc_acquired)
> + return -EBUSY;
> +
> + /*
> + * TDH.IOMMU.SETUP requires DMA translation, queued invalidation and
> + * scalable mode to be enabled. Catch these here so the error is
> + * -EINVAL rather than an opaque TDX_IOMMU_INVALID_STATE.
> + */
> + if (!(iommu->gcmd & DMA_GCMD_TE) || !iommu->qi || !sm_supported(iommu))
> + return -EINVAL;
> +
> + ret = dmar_tdxc_reserve_did(iommu);
> + if (ret)
> + return ret;
> +
> + dmar_tdxc_set_acquired(iommu, true);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_FOR_MODULES(dmar_tdxc_acquire, "tdx-host");
> +
> +/*
> + * dmar_tdxc_release() - Take an IOMMU out of Secure TDX Mode
> + * @dev: PCI device whose IOMMU was acquired via dmar_tdxc_acquire()
> + * @params: Returned by dmar_tdxc_get_params() for @dev
> + *
> + * Call this if TDH.IOMMU.SETUP fails, or for the last user of the IOMMU
> + * after TDH.IOMMU.CLEAR has succeeded.
> + */
> +void dmar_tdxc_release(struct pci_dev *pdev,
> + const struct dmar_tdxc_params *params)
> +{
> + struct dmar_drhd_unit *drhd = params->drhd;
> +
> + if (WARN_ON(dmar_tdxc_find_drhd(pdev) != drhd))
> + return;
> +
> + guard(mutex)(&drhd->tdxc_lock);
> + if (WARN_ON(!drhd->iommu->tdxc_acquired))
> + return;
> +
> + dmar_tdxc_set_acquired(drhd->iommu, false);
> + dmar_tdxc_unreserve_did(drhd->iommu);
> +}
> +EXPORT_SYMBOL_FOR_MODULES(dmar_tdxc_release, "tdx-host");
> --
> 2.43.0
>