Skip to contentSkip to conversation

coco/tdx-host: Introduce trusted DMA initialization support

PATCH Superseded Lu Baolu 6 participants 27 messages Oct 15 – Nov 5

Subsystems (5)
x86 KVM IOMMU Intel IOMMU (VT-d) +1 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

v2 cover coco/tdx-host: Introduce trusted DMA initialization support

Hi,

This series adds trusted DMA initialization support to the tdx-host and intel-iommu drivers, as groundwork for TEE I/O trusted DMA support.

Following Jason Gunthorpe's suggestion on v1, this version moves the host/TDX-module interactions out of the IOMMU driver and into the TDX host (TSM) driver. It is posted to collect review feedback from TDX, TSM and IOMMU stakeholders; it is not yet intended for merging.

This is part of the effort to implement the TDX PCIe Link Encryption feature, which includes the following components:

[ ] TDX module extensions initialization, posted in [3]
[X] Introduce trusted DMA initialization support
[ ] SEAMCALL helpers for TDX PCIe Link Encryption
[ ] PCIe Link Encryption Establishment via TDX platform services

Background:

Intel VT-d hardware gains a second set of DMA translation structures, owned by the TDX module and used only for devices assigned to confidential guests.

From the TDX Connect architecture perspective, VT-d is enhanced with:

1) Trusted DMA translation root table
- A second root-table address register, restricted to SEAM SAI.
- Programmed by the TDX module with the root of TDX-managed DMA translation tables for TD-assigned devices.

2) Trusted invalidation queue
- A second invalidation-queue address register plus dedicated head/tail registers, also restricted to SEAM SAI.

3) IOTLB / DID isolation
- When TDX Connect is enabled, the TDX module owns the half of the domain ID space with the MSB set; the VMM must confine itself to the lower half.

This posting covers initialization only: the host hands the TDX module the memory it needs and asks it to take over trusted DMA management on each capable IOMMU. The non-trusted DMA path is unchanged, except that the host's usable domain ID space on TDX-capable IOMMUs is halved. The TDX guest and device flows come in a later series.

Breakdown:

- Patch 1: Adds the TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR SEAMCALL wrappers, exported only to the tdx-host module.

- Patch 2: Reads the IOMMU_MT_PAGES_COUNT global metadata field when TDX_FEATURES0 advertises TDX Connect. It tells the host how many metadata pages the TDX module needs per IOMMU.

- Patch 3: Adds dmar_tdxcs_iommu_init_all() and dmar_tdxcs_iommu_release() to the VT-d driver. init_all() walks the active DMAR units, checks the preconditions TDH.IOMMU.SETUP requires, confines the host to the lower half of the DID space, and hands each TDX-capable unit to a tdx-host callback.

- Patch 4: Implements the setup() callback in the tdx-host driver:
allocates the trusted invalidation-queue buffers and metadata pages on the IOMMU's NUMA node, builds the IOMMU_MT parameter page, and issues TDH.IOMMU.SETUP. Units the TDX module does not cover are skipped. Teardown uses TDH.IOMMU.CLEAR; if that fails, the pages are intentionally leaked.

Change log:

v2:
- Moved the TDX module interactions (page allocation, IOMMU_MT construction, SEAMCALLs) from the VT-d driver to the tdx-host driver. The VT-d driver now only does the DRHD walk, precondition checks and DID reservation, and exposes them via a callback-based API.
- Renamed tdx-host.c to main.c and added iommu.c.

v1:
- https://lore.kernel.org/r/[email protected]

ABI History:

TDH.IOMMU.CLEAR previously required the host to provide, as input, the parameter page used for TDH.IOMMU.SETUP. The ABI has changed to take only the TDX IOMMU ID. This change is not yet reflected in the public ABI specification [1], so the two may differ.

Reference:

Implementation details follow the interfaces and behavior defined in the TDX Connect Application Binary Interface (ABI) Reference Specification and Intel TDX Connect Architecture Specification [1].

A full preview of these dependencies and follow-up changes is available at [4].

Please review. Any feedback is highly appreciated.

Thanks,
Baolu

Lu Baolu (4):
x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear x86/virt/tdx: Read global metadata for trusted IOMMU iommu/vt-d: Add an API for tdx-host when enabling trusted DMA coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

 drivers/virt/coco/tdx-host/Kconfig            |   6 +
 drivers/virt/coco/tdx-host/Makefile           |   3 +
 arch/x86/include/asm/tdx.h                    |   3 +
 arch/x86/include/asm/tdx_global_metadata.h    |   5 +
 arch/x86/virt/vmx/tdx/tdx.h                   |   2 +
 drivers/iommu/intel/iommu.h                   |   6 +
 drivers/virt/coco/tdx-host/private.h          |  33 +++
 include/linux/dmar.h                          |   5 +
 arch/x86/virt/vmx/tdx/tdx.c                   |  56 ++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c   |  20 ++
 drivers/iommu/intel/dmar.c                    | 103 +++++++
 drivers/iommu/intel/iommu.c                   |  19 ++
 drivers/virt/coco/tdx-host/iommu.c            | 255 ++++++++++++++++++
 .../virt/coco/tdx-host/{tdx-host.c => main.c} |  21 +-
 14 files changed, 536 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virt/coco/tdx-host/private.h
 create mode 100644 drivers/virt/coco/tdx-host/iommu.c
 rename drivers/virt/coco/tdx-host/{tdx-host.c => main.c} (92%)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 1/4 x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

Add SEAMCALL wrappers for TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR.

TDH.IOMMU.SETUP transitions an IOMMU and related I/O stack into Secure TDX Mode. During this transition, the TDX module validates and protects relevant I/O resources from further untrusted host access, providing the trusted foundation required before enabling TEE-IO security protocols (e.g. SPDM/IDE).

TDH.IOMMU.CLEAR performs the reverse operation and tears down the Secure TDX Mode state for the target IOMMU.

The caller is responsible for allocating the required pages and populating the parameter page in the ABI-defined format.

See Intel TDX Connect ABI Specification [1], Section 3.2 (TDX Connect Host-Side (SEAMCALL) Interface Functions).

Thanks to Xu Yilun for the initial draft.

[1] https://cdrdv2.intel.com/v1/dl/getContent/858625

---
 arch/x86/include/asm/tdx.h  |  2 ++
 arch/x86/virt/vmx/tdx/tdx.h |  2 ++
 arch/x86/virt/vmx/tdx/tdx.c | 56 +++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
arch/x86/include/asm/tdx.h +2 -0
index 89e97d5761d8..8b5411d3eb67 100644
@@ -176,6 +176,8 @@ u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ex
 u64 tdh_phymem_cache_wb(bool resume);
 u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
 u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn);
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id);
+u64 tdh_iommu_clear(u64 tdx_iommu_id);
 #else
 static inline void tdx_init(void) { }
 static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
arch/x86/virt/vmx/tdx/tdx.h +2 -0
index bdfd0e1e337a..d7beb42cf972 100644
@@ -49,6 +49,8 @@
 #define TDH_SYS_SHUTDOWN		52
 #define TDH_SYS_UPDATE			53
 #define TDH_SYS_DISABLE			69
+#define TDH_IOMMU_SETUP			128
+#define TDH_IOMMU_CLEAR			129
 
 /*
  * SEAMCALL leaf:
arch/x86/virt/vmx/tdx/tdx.c +56 -0

59 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH v2 2/4 x86/virt/tdx: Read global metadata for trusted IOMMU

In reply to Lu Baolu

Add support for reading the global metadata of the trusted IOMMU.

The trusted IOMMU feature is optionally enumerated via TDX_FEATURES0. Check this feature bit before reading the metadata to avoid causing the entire TDX initialization to fail on platforms that do not support it.

The read value represents the number of pages required for the IOMMU metadata. The host will allocate this count of pages and provide them to the TDX module when it configures the IOMMU to operate in Secure TDX mode via the TDH.IOMMU.SETUP SEAMCALL.

Thanks to Xu Yilun for the initial draft.

---
 arch/x86/include/asm/tdx.h                  |  1 +
 arch/x86/include/asm/tdx_global_metadata.h  |  5 +++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+)
arch/x86/include/asm/tdx.h +1 -0
index 8b5411d3eb67..9c9168822274 100644
@@ -35,6 +35,7 @@
 
 /* Bit definitions of TDX_FEATURES0 metadata field */
 #define TDX_FEATURES0_TD_PRESERVING	BIT_ULL(1)
+#define TDX_FEATURES0_TDXCONNECT	BIT_ULL(6)
 #define TDX_FEATURES0_NO_RBP_MOD	BIT_ULL(18)
 
 #ifndef __ASSEMBLER__
arch/x86/include/asm/tdx_global_metadata.h +5 -0
index 41150d546589..cf64452fe94f 100644
@@ -44,12 +44,17 @@ struct tdx_sys_info_handoff {
 	u16 module_hv;
 };
 
+struct tdx_sys_info_connect {
+	u16 iommu_mt_page_count;
+};
+
 struct tdx_sys_info {
 	struct tdx_sys_info_version version;
 	struct tdx_sys_info_features features;
 	struct tdx_sys_info_tdmr tdmr;
 	struct tdx_sys_info_td_ctrl td_ctrl;
 	struct tdx_sys_info_td_conf td_conf;
+	struct tdx_sys_info_connect tdx_connect;
 };
 
 #endif
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +20 -0
index e49c300f23d4..2ba1c5dcdbe6 100644
@@ -113,6 +113,20 @@ static int get_tdx_sys_info_handoff(struct tdx_sys_info_handoff *sysinfo_handoff
 	return 0;
 }
 
+static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_connect)
+{
+	int ret;
+	u64 val;
+
+	ret = read_sys_metadata_field(0x3000000100000003, &val);
+	if (ret)
+		return ret;
+
+	sysinfo_connect->iommu_mt_page_count = val;
+
+	return 0;
+}
+
 static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 {
 	int ret = 0;
@@ -129,5 +143,11 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
 	ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
 
+	if (ret)
+		return ret;
+
+	if (sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT)
+		ret = get_tdx_sys_info_connect(&sysinfo->tdx_connect);
+
 	return ret;
 }
Signature · 1 line
2.43.0
author maintainer
#9 lore raw

PATCH v2 3/4 iommu/vt-d: Add an API for tdx-host when enabling trusted DMA

In reply to Lu Baolu

TDX Connect requires each Intel IOMMU that supports the trusted DMA extension (ECAP.TDXCS) to be transitioned into Secure TDX Mode by the TDX module. The transition itself is driven by TDH.IOMMU.SETUP, which is issued by the tdx-host driver. The VT-d driver owns the hardware state that the TDX module depends on, so it must validate that state and adjust its resource management around it.

Add dmar_tdxcs_iommu_init_all() and dmar_tdxcs_iommu_release() as the interface between the two. dmar_tdxcs_iommu_init_all() walks the active DMAR units, and for every unit advertising ECAP.TDXCS it:

- Verifies the preconditions that TDH.IOMMU.SETUP checks: translation enabled, queued invalidation enabled, and scalable mode in use. This allows a misconfiguration to be reported as -EINVAL rather than as an opaque TDX_IOMMU_INVALID_STATE status later on.

- Splits the domain ID namespace. When an IOMMU runs in Secure TDX Mode, the TDX module owns the MSB-tagged half of the DID space and always sets that bit for trusted DMA translations, so the host must confine itself to the lower half.

- Invokes the caller-supplied setup() callback, which performs the actual TDX module interaction, and marks the unit as being in TDX mode on success. On failure the DID cap is restored and the error is propagated.

dmar_tdxcs_iommu_release() reverses the DID restriction and clears the TDX mode flag for a single unit.

The DMAR unit is passed to the callback so the tdx-host driver can obtain the register base address, which is how the TDX module identifies an IOMMU, and the NUMA node, which it needs in order to allocate the metadata pages local to that IOMMU.

A DMAR unit in TDX mode cannot be hot-removed, because the TDX module holds references to its resources. Reject such attempts with -EBUSY. Conversely, an IOMMU that is hot-added after bring-up is not enrolled into Secure TDX Mode because no platform currently supports both TDX Connect and DMAR hotplug, so the flow cannot be validated on hardware.

TDH.IOMMU.SETUP requires that no enhanced command is in progress on the target IOMMU. Block enhanced command submission on the target IOMMU for the duration of TDH.IOMMU.SETUP.

The symbols are exported only to the tdx-host module.

---
 drivers/iommu/intel/iommu.h |   6 +++
 include/linux/dmar.h        |   5 ++
 drivers/iommu/intel/dmar.c  | 103 ++++++++++++++++++++++++++++++++++++
 drivers/iommu/intel/iommu.c |  19 +++++++
 4 files changed, 133 insertions(+)
drivers/iommu/intel/iommu.h +6 -0
index 23dbe6c24439..a334226c4324 100644
@@ -187,6 +187,7 @@
  */
 
 #define ecap_pms(e)		(((e) >> 51) & 0x1)
+#define ecap_tdxcs(e)		(((e) >> 50) & 0x1)
 #define ecap_rps(e)		(((e) >> 49) & 0x1)
 #define ecap_smpwc(e)		(((e) >> 48) & 0x1)
 #define ecap_flts(e)		(((e) >> 47) & 0x1)
@@ -689,6 +690,11 @@ 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 */
+	/*
+	 * Enhanced command submission is rejected due to TDH.IOMMU.SETUP.
+	 * Protected by register_lock.
+	 */
+	bool		ecmd_blocked;
 	int		seq_id;	/* sequence id of the iommu */
 	int		agaw; /* agaw of this iommu */
 	int		msagaw; /* max sagaw of this iommu */
include/linux/dmar.h +5 -0
index 63e35df2cef4..4205945f099f 100644
@@ -47,6 +47,8 @@ struct dmar_drhd_unit {
 	u8	ignored:1; 		/* ignore drhd		*/
 	u8	include_all:1;
 	u8	gfx_dedicated:1;	/* graphic dedicated	*/
+	bool	tdx_mode;		/* in Secure TDX Mode	*/
+	int	node;
 	struct intel_iommu *iommu;
 };
 
@@ -87,6 +89,9 @@ extern struct list_head dmar_drhd_units;
 				dmar_rcu_check())			\
 		if (i=drhd->iommu, 0) {} else 
 
+int dmar_tdxcs_iommu_init_all(int (*setup)(struct dmar_drhd_unit *));
+void dmar_tdxcs_iommu_release(struct dmar_drhd_unit *drhd);
+
 static inline bool dmar_rcu_check(void)
 {
 	return rwsem_is_locked(&dmar_global_lock) ||
drivers/iommu/intel/dmar.c +103 -0

124 lines not on this page · raw message

drivers/iommu/intel/iommu.c +19 -0
index 2e3b3ab216f8..37c8c375c020 100644
@@ -2189,6 +2189,15 @@ int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
 		return -EINVAL;
 
 	if (insert) {
+		/*
+		 * A hot-added IOMMU is not enrolled into Secure TDX Mode.
+		 * Currently, there is no platform that supports both TDX Connect and
+		 * DMAR hotplug, so enrolling on hotplug cannot be validated on real
+		 * hardware. Log this limitation to make it visible.
+		 */
+		if (ecap_tdxcs(iommu->ecap))
+			pr_info("Trusted DMA for TEE is not enabled on hot-added IOMMU %s\n",
+				iommu->name);
 		ret = intel_iommu_add(dmaru);
 	} else {
 		disable_dmar_iommu(iommu);
@@ -4227,6 +4236,16 @@ int ecmd_submit_sync(struct intel_iommu *iommu, u8 ecmd, u64 oa, u64 ob)
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
 
+	/*
+	 * TDH.IOMMU.SETUP requires that no enhanced command is in
+	 * progress. This may be called from atomic context (e.g. perf
+	 * pmu::start/stop), so don't sleep; just reject the request.
+	 */
+	if (iommu->ecmd_blocked) {
+		ret = -EBUSY;
+		goto err;
+	}
+
 	res = readq(iommu->reg + DMAR_ECRSP_REG);
 	if (res & DMA_ECMD_ECRSP_IP) {
 		ret = -EBUSY;
Signature · 1 line
2.43.0
author maintainer
#10 lore raw

PATCH v2 4/4 coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Lu Baolu

Assigning a device to a TD for private memory access requires that DMA from that device be translated by structures the TDX module controls. An Intel IOMMU that implements the trusted DMA extension provides a parallel set of translation and invalidation resources for this purpose. These become usable only after the TDX module has placed the IOMMU into TDX mode, at which point the trusted resources belong exclusively to the TDX module, while the host retains control of the regular translation path. The transition is performed by TDH.IOMMU.SETUP, which takes the register base address of the target IOMMU together with a parameter page describing memory the host has allocated for the TDX module to manage trusted DMA on that unit.

Perform this initialization from the tdx-host driver by walking every IOMMU unit through dmar_tdxcs_iommu_init() and initialize the ones whose IOMMU implements the extension.

For each such unit the driver builds the IOMMU_MT parameter layout that TDH.IOMMU.SETUP expects: two contiguous buffers for the trusted invalidation queue, followed by the number of metadata pages the TDX module reports in its global metadata. The pages are allocated on the NUMA node of the IOMMU. The root page is passed to the TDX module, which returns an identifier naming the initialized IOMMU. That identifier and the pages backing it are tracked so the state can be released later.

Releasing the state is the reverse: TDH.IOMMU.CLEAR tears down the trusted DMA resources established by TDH.IOMMU.SETUP, after which the metadata pages can be freed and the VT-d driver told to lift the domain ID restriction applied during initialization. If TDH.IOMMU.CLEAR fails the pages are not freed, as the TDX module may still hold references to them.

Rename tdx-host.c to main.c and build the module from a tdx-host-y object list instead. New functionality can then be added as separate source files instead of putting everything in a single file.

The code is built only when its dependencies are present, gated by CONFIG_TDX_CONNECT.

---
 drivers/virt/coco/tdx-host/Kconfig            |   6 +
 drivers/virt/coco/tdx-host/Makefile           |   3 +
 drivers/virt/coco/tdx-host/private.h          |  33 +++
 drivers/virt/coco/tdx-host/iommu.c            | 255 ++++++++++++++++++
 .../virt/coco/tdx-host/{tdx-host.c => main.c} |  21 +-
 5 files changed, 317 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virt/coco/tdx-host/private.h
 create mode 100644 drivers/virt/coco/tdx-host/iommu.c
 rename drivers/virt/coco/tdx-host/{tdx-host.c => main.c} (92%)
drivers/virt/coco/tdx-host/Kconfig +6 -0
index 57d0c01a4357..62d91d4b41fa 100644
@@ -4,3 +4,9 @@ config TDX_HOST_SERVICES
 	select FW_LOADER
 	select FW_UPLOAD
 	default m
+
+config TDX_CONNECT
+	def_bool y
+	depends on TDX_HOST_SERVICES
+	depends on PCI_TSM
+	depends on INTEL_IOMMU
drivers/virt/coco/tdx-host/Makefile +3 -0
index e61e749a8dff..67bfe5a6e290 100644
@@ -1 +1,4 @@
 obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
+
+tdx-host-y := main.o
+tdx-host-$(CONFIG_TDX_CONNECT) += iommu.o
drivers/virt/coco/tdx-host/private.h new file +33 -0
new file mode 100644
index 000000000000..131c1782cbc1
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2026 Intel Corporation */
+#ifndef __TDX_HOST_PRIVATE_H
+#define __TDX_HOST_PRIVATE_H
+
+#include <linux/dmar.h>
+
+/*
+ * Represents a list of pages for TDX module defined IOMMU_MT object.
+ * Typically it uses a "root page" as the medium to exchange a list of
+ * data pages between host and TDX module.
+ */
+struct tdx_iommu_pages {
+	u64 *root;
+	void **pages;
+	unsigned int nr_entries;
+};
+
+struct tdx_iommu_state {
+	struct dmar_drhd_unit *drhd;
+	u64 tdx_iommu_id;
+	struct tdx_iommu_pages *mt_pages;
+};
+
+#ifdef CONFIG_TDX_CONNECT
+int tdx_iommu_enable_all(struct device *dev);
+#else
+static inline int tdx_iommu_enable_all(struct device *dev)
+{
+	return -EINVAL;
+}
+#endif /* CONFIG_TDX_CONNECT */
+#endif /* __TDX_HOST_PRIVATE_H */
drivers/virt/coco/tdx-host/iommu.c new file +255 -0

255 lines not on this page · raw message

drivers/virt/coco/tdx-host/main.c renamed from drivers/virt/coco/tdx-host/tdx-host.c +20 -1
similarity index 92%
rename from drivers/virt/coco/tdx-host/tdx-host.c
rename to drivers/virt/coco/tdx-host/main.c
index e8ed8dfa526e..018bdc475715 100644
@@ -14,6 +14,8 @@
 #include <asm/seamldr.h>
 #include <asm/tdx.h>
 
+#include "private.h"
+
 static const struct x86_cpu_id tdx_host_ids[] = {
 	X86_MATCH_FEATURE(X86_FEATURE_TDX_HOST_PLATFORM, NULL),
 	{}
@@ -202,9 +204,26 @@ static int seamldr_init(struct device *dev)
 	return devm_add_action_or_reset(dev, seamldr_deinit, tdx_fwl);
 }
 
+static int tdx_connect_init(struct device *dev)
+{
+	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+
+	if (!IS_ENABLED(CONFIG_TDX_CONNECT) || !tdx_sysinfo ||
+	    !(tdx_sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return 0;
+
+	return tdx_iommu_enable_all(dev);
+}
+
 static int tdx_host_probe(struct faux_device *fdev)
 {
-	return seamldr_init(&fdev->dev);
+	int ret;
+
+	ret = seamldr_init(&fdev->dev);
+	if (ret)
+		return ret;
+
+	return tdx_connect_init(&fdev->dev);
 }
 
 static const struct faux_device_ops tdx_host_ops = {
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/4] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to Lu Baolu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] x86/virt/tdx: missing clflush on IOMMU parameter pages --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/virt/tdx: missing clflush on IOMMU parameter pages
--

commit ea38aa1a74969b72895a018349277b8a5b88cfb3
Author: Lu Baolu <[email protected]>

x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

This commit adds SEAMCALL wrappers for TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR. These functions transition an IOMMU into and out of Secure TDX Mode to provide the trusted foundation required before enabling TEE-IO security protocols.

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index 1b9ff749dd8e0..5d31c1b0890ae 100644

[ ... ]

Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +8 -0
@@ -2034,3 +2034,59 @@ void tdx_sys_disable(void)
 	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
 		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
 }
+
+/*
+ * Wrapper for TDH.IOMMU.SETUP leaf, which transitions the IOMMU and related
+ * I/O stack into Secure TDX Mode.
+ *
+ * The @root page format is defined by the TDX Connect ABI spec (Table 3.35,
+ * Structure of IOMMU_MT Parameter): the first two entries describe the size
+ * and HPA (Host Physical Address) of the two contiguous buffers for the

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2054

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+ * invalidation queue; the remaining entries provide HPAs of
+ * IOMMU_MT_PAGES_COUNT metadata pages. IOMMU_MT_PAGES_COUNT is obtained from
+ * trusted IOMMU global metadata.
+ */
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(root),
+	};

[Severity: High]
Does the root page need to be flushed from the CPU cache before being passed to the TDX module?

The TDX module accesses memory using physical addresses. If CPU-cached, unflushed memory pages are passed to the module during trusted DMA initialization, it can lead to cache paradoxes and machine checks, potentially causing host kernel panics.

Other TDX page-adding wrappers typically ensure pages are flushed. While the caller is responsible for populating the parameter page, the caller introduced in the follow-up patch (tdx_iommu_setup in iommu.c) allocates and populates the metadata pages but does not appear to perform a cache flush either.

Should tdh_iommu_setup() perform the flush, or does the caller need to be updated to handle it?

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+	u64 ret;
+
+	/*
+	 * Don't loop forever:
+	 *
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 */

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#11 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Lu Baolu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] virt/coco/tdx-host: desynchronized host/hardware state on xa_insert failure --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virt/coco/tdx-host: desynchronized host/hardware state on xa_insert failure --

commit 83cda44603ec97cb10640905999d9a617eced100
Author: Lu Baolu <[email protected]>

coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

Assigning a device to a TD for private memory access requires that DMA from that device be translated by structures the TDX module controls. An Intel IOMMU that implements the trusted DMA extension provides a parallel set of translation and invalidation resources for this purpose.

Quoted 5 lines
drivers/virt/coco/tdx-host/iommu.c new file +0 -0
new file mode 100644
index 0000000000000..a80e57bf26a12

[ ... ]

Quoted 2 lines
drivers/virt/coco/tdx-host/iommu.c new file +2 -0
+static int tdx_iommu_setup(struct dmar_drhd_unit *drhd)
+{

[ ... ]

drivers/virt/coco/tdx-host/iommu.c line 211

Quoted 10 lines
drivers/virt/coco/tdx-host/iommu.c new file +10 -0
+	state = no_free_ptr(iommu_state);
+	state->drhd = drhd;
+	state->tdx_iommu_id = tdx_iommu_id;
+	state->mt_pages = no_free_ptr(iommu_mt);
+	ret = xa_insert(&tdx_iommu_xa, (unsigned long)tdx_iommu_id, state, GFP_KERNEL);
+	if (ret) {
+		WARN_ON(tdx_iommu_clear(state));
+		kfree(state);
+		return ret;
+	}

[Severity: High]
If xa_insert() fails and tdx_iommu_clear(state) also fails, this code unconditionally frees state and returns an error. Does this leave the hardware state desynchronized?

When this function returns an error, the caller dmar_tdxcs_iommu_init_all() in drivers/iommu/intel/dmar.c restores iommu->max_domain_id to its full un-halved range and leaves drhd->tdx_mode = false:

	ret = setup(drhd);
	...
	if (ret) {
		scoped_guard(mutex, &iommu->did_lock)
			iommu->max_domain_id = ndoms;
		...
		return ret;
	}

Because tdx_iommu_clear() failed, the physical hardware is permanently left in Secure TDX Mode, but the software state now believes it is not. Will this cause hardware faults or fatal domain ID conflicts when the host attempts to use domain IDs that now belong exclusively to TDX?

Unlike tdx_iommu_disable_all() which safely detects a clear failure and leaves tdx_mode true to confine the host to the lower half of the DID space, this error path completely loses track of the hardware state, breaking isolation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#3 lore raw

Re: [PATCH v2 1/4] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to [email protected]
Quoted 37 lines · On 10/15/2026 9:28 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] x86/virt/tdx: missing clflush on IOMMU parameter pages
--

commit ea38aa1a74969b72895a018349277b8a5b88cfb3
Author: Lu Baolu<[email protected]>

x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

This commit adds SEAMCALL wrappers for TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR. These functions transition an IOMMU into and out of Secure TDX Mode to provide the trusted foundation required before enabling TEE-IO security protocols.

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index 1b9ff749dd8e0..5d31c1b0890ae 100644

[ ... ]

Quoted 17 lines
arch/x86/virt/vmx/tdx/tdx.c +13 -0
@@ -2034,3 +2034,59 @@ void tdx_sys_disable(void)
  	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
  		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
  }
+
+/*
+ * Wrapper for TDH.IOMMU.SETUP leaf, which transitions the IOMMU and related
+ * I/O stack into Secure TDX Mode.
+ *
+ * The @root page format is defined by the TDX Connect ABI spec (Table 3.35,
+ * Structure of IOMMU_MT Parameter): the first two entries describe the size
+ * and HPA (Host Physical Address) of the two contiguous buffers for the
+ * invalidation queue; the remaining entries provide HPAs of
+ * IOMMU_MT_PAGES_COUNT metadata pages. IOMMU_MT_PAGES_COUNT is obtained from
+ * trusted IOMMU global metadata.
+ */
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id)
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2054

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(root),
+	};

[Severity: High]
Does the root page need to be flushed from the CPU cache before being passed to the TDX module?

No. The hardware handles cache coherency when memory moves between KeyIDs, so the VMM doesn't need to flush pages before handing them to the TDX module. The TDX architecture is being updated to drop the requirement for VMM-assisted flushing, and the existing flushes in the TDX code are being removed by this series:

https://lore.kernel.org/r/[email protected]

Thanks,
baolu

author maintainer
#12 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to [email protected]
Quoted 52 lines · On 10/15/2026 9:31 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virt/coco/tdx-host: desynchronized host/hardware state on xa_insert failure --

commit 83cda44603ec97cb10640905999d9a617eced100
Author: Lu Baolu <[email protected]>

coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

Assigning a device to a TD for private memory access requires that DMA from that device be translated by structures the TDX module controls. An Intel IOMMU that implements the trusted DMA extension provides a parallel set of translation and invalidation resources for this purpose.

Quoted 5 lines
drivers/virt/coco/tdx-host/iommu.c new file +0 -0
new file mode 100644
index 0000000000000..a80e57bf26a12

[ ... ]

Quoted 2 lines
drivers/virt/coco/tdx-host/iommu.c new file +2 -0
+static int tdx_iommu_setup(struct dmar_drhd_unit *drhd)
+{

[ ... ]

drivers/virt/coco/tdx-host/iommu.c line 211

Quoted 10 lines
drivers/virt/coco/tdx-host/iommu.c new file +10 -0
+	state = no_free_ptr(iommu_state);
+	state->drhd = drhd;
+	state->tdx_iommu_id = tdx_iommu_id;
+	state->mt_pages = no_free_ptr(iommu_mt);
+	ret = xa_insert(&tdx_iommu_xa, (unsigned long)tdx_iommu_id, state, GFP_KERNEL);
+	if (ret) {
+		WARN_ON(tdx_iommu_clear(state));
+		kfree(state);
+		return ret;
+	}

[Severity: High]
If xa_insert() fails and tdx_iommu_clear(state) also fails, this code unconditionally frees state and returns an error. Does this leave the hardware state desynchronized?

When this function returns an error, the caller dmar_tdxcs_iommu_init_all() in drivers/iommu/intel/dmar.c restores iommu->max_domain_id to its full un-halved range and leaves drhd->tdx_mode = false:

	ret = setup(drhd);
	...
	if (ret) {
		scoped_guard(mutex, &iommu->did_lock)
			iommu->max_domain_id = ndoms;
		...
		return ret;
	}
Quoted 8 lines

Because tdx_iommu_clear() failed, the physical hardware is permanently left in Secure TDX Mode, but the software state now believes it is not. Will this cause hardware faults or fatal domain ID conflicts when the host attempts to use domain IDs that now belong exclusively to TDX?

Unlike tdx_iommu_disable_all() which safely detects a clear failure and leaves tdx_mode true to confine the host to the lower half of the DID space, this error path completely loses track of the hardware state, breaking isolation.

That's correct in theory, but I don't think the extra complexity is worth it here.

The path needs two independent failures happening together:

- xa_insert() has to fail. In practice that means a small GFP_KERNEL allocation failing, or the TDX module returning a duplicate tdx_iommu_id, which would be a TDX module bug.

- TDH.IOMMU.CLEAR then has to fail on an IOMMU the TDX module set up successfully moments earlier, with nothing in between.

If both happen, the system is already badly broken, and the WARN_ON() makes it loud. Adding more tracking just for this case would complicate the code without making a working system more robust.

Thanks,
baolu

#4 lore raw

Re: [PATCH v2 1/4] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to Lu Baolu
Quoted 1 line · Lu Baolu

Add SEAMCALL wrappers for TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR.

We need this patch been reviewed by x86 reviewers. x86 convention doesn't start with the implementation, so remove it.

We can start with a generic description of what the 2 SEAMCALLs are doing in general and why. Maybe move some of your description in next paragraph ahead.

Quoted 10 lines

TDH.IOMMU.SETUP transitions an IOMMU and related I/O stack into Secure TDX Mode. During this transition, the TDX module validates and protects relevant I/O resources from further untrusted host access, providing the trusted foundation required before enabling TEE-IO security protocols (e.g. SPDM/IDE).

TDH.IOMMU.CLEAR performs the reverse operation and tears down the Secure TDX Mode state for the target IOMMU.

Quoted 7 lines

The caller is responsible for allocating the required pages and populating the parameter page in the ABI-defined format.

See Intel TDX Connect ABI Specification [1], Section 3.2 (TDX Connect Host-Side (SEAMCALL) Interface Functions).

Thanks to Xu Yilun for the initial draft.

Feel free to drop it. Not that important.

Quoted 41 lines

[1] https://cdrdv2.intel.com/v1/dl/getContent/858625

---
 arch/x86/include/asm/tdx.h  |  2 ++
 arch/x86/virt/vmx/tdx/tdx.h |  2 ++
 arch/x86/virt/vmx/tdx/tdx.c | 56 +++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
arch/x86/include/asm/tdx.h +2 -0
index 89e97d5761d8..8b5411d3eb67 100644
@@ -176,6 +176,8 @@ u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ex
 u64 tdh_phymem_cache_wb(bool resume);
 u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
 u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn);
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id);
+u64 tdh_iommu_clear(u64 tdx_iommu_id);
 #else
 static inline void tdx_init(void) { }
 static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
arch/x86/virt/vmx/tdx/tdx.h +2 -0
index bdfd0e1e337a..d7beb42cf972 100644
@@ -49,6 +49,8 @@
 #define TDH_SYS_SHUTDOWN		52
 #define TDH_SYS_UPDATE			53
 #define TDH_SYS_DISABLE			69
+#define TDH_IOMMU_SETUP			128
+#define TDH_IOMMU_CLEAR			129
 
 /*
  * SEAMCALL leaf:
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index 1b9ff749dd8e..5d31c1b0890a 100644

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2040

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
@@ -2034,3 +2034,59 @@ void tdx_sys_disable(void)
 	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
 		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
 }
+
+/*
+ * Wrapper for TDH.IOMMU.SETUP leaf, which transitions the IOMMU and related
+ * I/O stack into Secure TDX Mode.

Other tdh_*() wrappers don't have the general descriptions. We all know these are wrappers for some SEAMCALL leafs.

So if there is no special thing to note, remove it.

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2049

Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +9 -0
+ *
+ * The @root page format is defined by the TDX Connect ABI spec (Table 3.35,
+ * Structure of IOMMU_MT Parameter): the first two entries describe the size
+ * and HPA (Host Physical Address) of the two contiguous buffers for the
+ * invalidation queue; the remaining entries provide HPAs of
+ * IOMMU_MT_PAGES_COUNT metadata pages. IOMMU_MT_PAGES_COUNT is obtained from
+ * trusted IOMMU global metadata.
+ */
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id)

IIUC, the void *root is a vague type, no type safty. Is it better we define an explicit structure that mirrors the in-memory ABI? Something like:

  struct tdx_iommu_metadata {
	u64 iq1_pa;
	u64 iq2_pa;
	DECLARE_FLEX_ARRAY(u64, mt_pa);

};

Or,

  struct tdx_iommu_metadata {
	u64 iq1_pa;
	u64 iq2_pa;
	u64 mt_pa[510];

};

  static_assert(sizeof(struct tdx_iommu_metadata) == 4096);
Quoted 46 lines
arch/x86/virt/vmx/tdx/tdx.c +43 -0
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(root),
+	};
+	u64 ret;
+
+	/*
+	 * Don't loop forever:
+	 *
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 */
+	do {
+		ret = seamcall_ret(TDH_IOMMU_SETUP, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	*tdx_iommu_id = args.rcx;
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_setup, "tdx-host");
+
+/*
+ * Wrapper for TDH.IOMMU.CLEAR leaf, which terminates Secure TDX Mode for the
+ * target IOMMU and restores control of related hardware resources to the host.
+ */
+u64 tdh_iommu_clear(u64 tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = tdx_iommu_id,
+	};
+	u64 ret;
+
+	do {
+		ret = seamcall(TDH_IOMMU_CLEAR, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_clear, "tdx-host");
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2 2/4] x86/virt/tdx: Read global metadata for trusted IOMMU

In reply to Lu Baolu
Quoted 3 lines · Lu Baolu

Add support for reading the global metadata of the trusted IOMMU.

The trusted IOMMU feature is optionally enumerated via TDX_FEATURES0.

The feature name is not trusted IOMMU, the name in the spec is TDX_CONNECT, but I believe people want a name with technical meaning, maybe TDX TDISP? Open question for all reviewers.

Quoted 23 lines

Check this feature bit before reading the metadata to avoid causing the entire TDX initialization to fail on platforms that do not support it.

The read value represents the number of pages required for the IOMMU metadata. The host will allocate this count of pages and provide them to the TDX module when it configures the IOMMU to operate in Secure TDX mode via the TDH.IOMMU.SETUP SEAMCALL.

Thanks to Xu Yilun for the initial draft.

---
 arch/x86/include/asm/tdx.h                  |  1 +
 arch/x86/include/asm/tdx_global_metadata.h  |  5 +++++
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+)
arch/x86/include/asm/tdx.h +0 -0
index 8b5411d3eb67..9c9168822274 100644

arch/x86/include/asm/tdx.h line 38

Quoted 4 lines
arch/x86/include/asm/tdx.h +1 -0
@@ -35,6 +35,7 @@
 
 /* Bit definitions of TDX_FEATURES0 metadata field */
 #define TDX_FEATURES0_TD_PRESERVING	BIT_ULL(1)
+#define TDX_FEATURES0_TDXCONNECT	BIT_ULL(6)

TDX_FEATURES0_TDX_CONNECT?

My actual point is that the name XX_TDX_CONNECT is good here.

#13 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Lu Baolu
Quoted 6 lines · Lu Baolu

Assigning a device to a TD for private memory access requires that DMA from that device be translated by structures the TDX module controls. An Intel IOMMU that implements the trusted DMA extension provides a parallel set of translation and invalidation resources for this purpose. These become usable only after the TDX module has placed the IOMMU into TDX mode, at which point the trusted resources belong exclusively to the

I think at this point we first need to reason about the connection between IOMMU TDX mode and the PCIe link encryption. The device private memory access can be placed in cover-letter as the big picture of TDISP.

I can see we may need to touch a bit about trusted DMA translation at this phase, because the TDH.IOMMU.SETUP requires the VMM to provide 2 contiguous Invalidation Queue buffers, which leaks the trusted DMA internals a bit. But I feel this should not be a first topic.

Quoted 49 lines

TDX module, while the host retains control of the regular translation path. The transition is performed by TDH.IOMMU.SETUP, which takes the register base address of the target IOMMU together with a parameter page describing memory the host has allocated for the TDX module to manage trusted DMA on that unit.

Perform this initialization from the tdx-host driver by walking every IOMMU unit through dmar_tdxcs_iommu_init() and initialize the ones whose IOMMU implements the extension.

For each such unit the driver builds the IOMMU_MT parameter layout that TDH.IOMMU.SETUP expects: two contiguous buffers for the trusted invalidation queue, followed by the number of metadata pages the TDX module reports in its global metadata. The pages are allocated on the NUMA node of the IOMMU. The root page is passed to the TDX module, which returns an identifier naming the initialized IOMMU. That identifier and the pages backing it are tracked so the state can be released later.

Releasing the state is the reverse: TDH.IOMMU.CLEAR tears down the trusted DMA resources established by TDH.IOMMU.SETUP, after which the metadata pages can be freed and the VT-d driver told to lift the domain ID restriction applied during initialization. If TDH.IOMMU.CLEAR fails the pages are not freed, as the TDX module may still hold references to them.

Rename tdx-host.c to main.c and build the module from a tdx-host-y object list instead. New functionality can then be added as separate source files instead of putting everything in a single file.

The code is built only when its dependencies are present, gated by CONFIG_TDX_CONNECT.

---
 drivers/virt/coco/tdx-host/Kconfig            |   6 +
 drivers/virt/coco/tdx-host/Makefile           |   3 +
 drivers/virt/coco/tdx-host/private.h          |  33 +++
 drivers/virt/coco/tdx-host/iommu.c            | 255 ++++++++++++++++++
 .../virt/coco/tdx-host/{tdx-host.c => main.c} |  21 +-
 5 files changed, 317 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virt/coco/tdx-host/private.h
 create mode 100644 drivers/virt/coco/tdx-host/iommu.c
 rename drivers/virt/coco/tdx-host/{tdx-host.c => main.c} (92%)
drivers/virt/coco/tdx-host/Kconfig +0 -0
index 57d0c01a4357..62d91d4b41fa 100644

drivers/virt/coco/tdx-host/Kconfig config TDX_HOST_SERVICES · line 8

Quoted 5 lines
drivers/virt/coco/tdx-host/Kconfig +2 -0
@@ -4,3 +4,9 @@ config TDX_HOST_SERVICES
 	select FW_LOADER
 	select FW_UPLOAD
 	default m
+
+config TDX_CONNECT

Maybe we don't need the configuration. Now we turn on all TDX features that Linux knows about all the time.

Architecturally, TDX Connect is an default-off feature. But Linux enables it regardlessly. It is questionable we then have a configuration that disable the driver that supports the feature.

Quoted 4 lines
drivers/virt/coco/tdx-host/Kconfig +4 -0
+	def_bool y
+	depends on TDX_HOST_SERVICES
+	depends on PCI_TSM
+	depends on INTEL_IOMMU

[..]

Quoted 11 lines
drivers/virt/coco/tdx-host/Kconfig +11 -0
+static int tdx_iommu_setup(struct dmar_drhd_unit *drhd)
+{
+	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+	unsigned int mt_page_count = tdx_sysinfo->tdx_connect.iommu_mt_page_count;
+	struct tdx_iommu_state *state;
+	u64 r, tdx_iommu_id;
+	int ret;
+
+	struct tdx_iommu_state *iommu_state __free(kfree) = kzalloc_obj(*iommu_state);
+	if (!iommu_state)
+		return -ENOMEM;

drivers/virt/coco/tdx-host/Kconfig

Quoted 10 lines
drivers/virt/coco/tdx-host/Kconfig +10 -0
+
+	struct tdx_iommu_pages *iommu_mt __free(free_mt_pages) =
+			tdx_iommu_alloc_mt_pages(drhd, mt_page_count);
+	if (!iommu_mt)
+		return -ENOMEM;
+
+	r = tdh_iommu_setup(drhd->reg_base_addr, iommu_mt->root, &tdx_iommu_id);
+	/* TDX Extension is not supported on this IOMMU. Nothing to do. */
+	if ((r & TDX_SEAMCALL_STATUS_MASK) == TDX_OPERAND_INVALID)
+		return -EOPNOTSUPP;

Enable all IOMMUs regardless brings this problem. We need to try on every IOMMU, check the TDX error code to see if it is not supported (then skip) or failed. It's also sad to me that -EOPNOTSUPP is defined as the API between tdx-host and intel iommu driver.

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

Open to discussion.

#14 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Jason

author maintainer
#15 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the physical iommu information from the arm-smmu-v3 driver;
- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Intel differs in two ways:

- ARM queries the physical iommu information from the iommu driver in the viommu_init() path. On Intel, the secure iommu must be set up during TSM connect, because it is part of establishing PCIe link encryption.

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

So the Intel iommu driver will probably end up with interfaces like:

- dmar_tdxcs_iommu_init(struct device *dev, ...);
- dmar_tdxcs_iommu_release(struct device *dev, ...);

These would be called from the connect and disconnect callbacks of the TDX link ops:

static struct pci_tsm_ops tdx_tsm_link_ops = {
.owner = THIS_MODULE,
.probe = tdx_tsm_link_probe,
.remove = tdx_tsm_link_remove,
.connect = tdx_tsm_link_connect,
.disconnect = tdx_tsm_link_disconnect,
...
};

Any thoughts?

Thanks,
baolu

#16 lore raw

RE: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

+Aneesh.

Quoted 22 lines

From: Baolu Lu <[email protected]>
Sent: Friday, October 16, 2026 3:03 PM

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the

    physical iommu information from the arm-smmu-v3 driver;
Quoted 8 lines

- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Intel differs in two ways:

- ARM queries the physical iommu information from the iommu driver in the viommu_init() path. On Intel, the secure iommu must be set up during TSM connect, because it is part of establishing PCIe link encryption.

My understanding is that the psmmu is required for link encryption, so it's similar ordering here i.e. activated before tsm connect?

Quoted 9 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

Quoted 22 lines

So the Intel iommu driver will probably end up with interfaces like:

- dmar_tdxcs_iommu_init(struct device *dev, ...);
- dmar_tdxcs_iommu_release(struct device *dev, ...);

These would be called from the connect and disconnect callbacks of the TDX link ops:

static struct pci_tsm_ops tdx_tsm_link_ops = {
.owner = THIS_MODULE,
.probe = tdx_tsm_link_probe,
.remove = tdx_tsm_link_remove,
.connect = tdx_tsm_link_connect,
.disconnect = tdx_tsm_link_disconnect,
...
};

Any thoughts?

Thanks,
baolu

#8 lore raw

RE: [PATCH v2 2/4] x86/virt/tdx: Read global metadata for trusted IOMMU

In reply to Xu Yilun
Quoted 12 lines

From: Xu Yilun <[email protected]>
Sent: Thursday, October 15, 2026 2:48 PM

Quoted 3 lines · Lu Baolu

Add support for reading the global metadata of the trusted IOMMU.

The trusted IOMMU feature is optionally enumerated via TDX_FEATURES0.

The feature name is not trusted IOMMU, the name in the spec is TDX_CONNECT, but I believe people want a name with technical meaning, maybe TDX TDISP? Open question for all reviewers.

According to the TDX Connect spec:

" This document outlines the system architecture specifications for Intel(r) TDX Connect, a TDX extension technology for secure and efficient integration with trusted I/O devices. "

So "trusted I/O" sounds a good fit here.

Or "TEE-I/O" is also clear, based on the TDISP spec:

"Trusted Execution Environments (TEEs) that include a composition of resources from one or more devices and the host require mechanisms to establish and manage trust relationships. Here we will use the term TEE-I/O to refer to a conceptual framework for performing such operations."

#17 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Tian, Kevin
Quoted 28 lines · Kevin

+Aneesh.

Quoted 26 lines

From: Baolu Lu <[email protected]>
Sent: Friday, October 16, 2026 3:03 PM

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the

    physical iommu information from the arm-smmu-v3 driver;

- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Intel differs in two ways:

Quoted 7 lines
Quoted 4 lines

- ARM queries the physical iommu information from the iommu driver in the viommu_init() path. On Intel, the secure iommu must be set up during TSM connect, because it is part of establishing PCIe link encryption.

My understanding is that the psmmu is required for link encryption, so it's similar ordering here i.e. activated before tsm connect?

No, AFAIK? The psmmu has to be linked before creating the vdev, and in the current spec the only way to get the measurements is through the vdev, but I am expecting that to change.

I have no idea what brilliant idea caused the VT-d iommu HW to become involved in DOE and link IDE configuration in the root port. That makes very little sense from an architecture perspective.

Quoted 9 lines
Quoted 8 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Quoted 7 lines

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

That's going to be a bit of a PITA, since the DIDs can be in use when the tdx module is start potentially.

I suppose you can start out by just failing if this has happened.

But as soon as reasonably possible once the system opts into a TDX operation the VT-d drivers should stop using the upper did space.

Otherwise you will have to implement live DID renaming, which is a PITA in an iommu driver.

Jason

#23 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Baolu Lu
Quoted 14 lines · Baolu Lu
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

Quoted 6 lines

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the physical iommu information from the arm-smmu-v3 driver;
- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Okay, I haven't looked, not sure what is in cca_psmmu_get() yet

Quoted 7 lines

So the Intel iommu driver will probably end up with interfaces like:

- dmar_tdxcs_iommu_init(struct device *dev, ...);
- dmar_tdxcs_iommu_release(struct device *dev, ...);

These would be called from the connect and disconnect callbacks of the TDX link ops:

It seems OK

It sounds like you will have a one time init of the secure iommu at the time the first PCI device wants to use the TSM? It seems like no problem.

Jason

#25 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Lu Baolu
Quoted 8 lines
drivers/virt/coco/tdx-host/iommu.c new file +8 -0
+static int tdx_iommu_setup(struct dmar_drhd_unit *drhd)
+{
+	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+	unsigned int mt_page_count = tdx_sysinfo->tdx_connect.iommu_mt_page_count;
+	struct tdx_iommu_state *state;
+	u64 r, tdx_iommu_id;
+	int ret;
+

drivers/virt/coco/tdx-host/iommu.c line 192

Quoted 10 lines
drivers/virt/coco/tdx-host/iommu.c new file +10 -0
+	struct tdx_iommu_state *iommu_state __free(kfree) = kzalloc_obj(*iommu_state);
+	if (!iommu_state)
+		return -ENOMEM;
+
+	struct tdx_iommu_pages *iommu_mt __free(free_mt_pages) =
+			tdx_iommu_alloc_mt_pages(drhd, mt_page_count);
+	if (!iommu_mt)
+		return -ENOMEM;
+
+	r = tdh_iommu_setup(drhd->reg_base_addr, iommu_mt->root, &tdx_iommu_id);

Dynamic PAMT was merged in 7.3. With it enabled, the PAMT for 4K pages is no longer allocated up front, and the kernel must add PAMT backing before giving a page to the TDX module. So, the IQ buffers and the metadata pages need tdx_pamt_get() before TDH.IOMMU.SETUP and tdx_pamt_put() after TDH.IOMMU.CLEAR hands them back.

author maintainer
#20 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Tian, Kevin
Quoted 8 lines · Kevin
Quoted 8 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Quoted 7 lines

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

You are right, "atomically" was too strong. Only the ECMD block has to bracket TDH.IOMMU.SETUP. So I plan to restructure it as follows:

- dmar_tdxcs_init_prepare(dev, &params): resolve the device's DRHD and take a reference on its secure-mode state. For the first user, validate the preconditions (TE/QI/SM enabled), reserve the upper half of the DID space (failing with -EBUSY if any of it is in use), block ECMD and return what tdx-host needs (register base, NUMA node, etc). If the unit is already in Secure TDX mode, just report that so tdx-host skips TDH.IOMMU.SETUP.

- dmar_tdxcs_init_done(dev, result): unblock ECMD, then either mark the unit as being in Secure TDX mode or roll back (including the DID reservation), based on the SEAMCALL result. It must be called whenever init_prepare() returns success.

- dmar_tdxcs_release(dev): drop the reference; the last user releases the DID reservation and clears the Secure TDX mode state after tdx-host issues TDH.IOMMU.CLEAR.

These are called from tdx_tsm_link_connect()/disconnect(). Any failure, whether a precondition, DID reservation or the SEAMCALL itself, aborts connect with the corresponding error.

Thanks,
baolu

author maintainer
#18 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Jason Gunthorpe
Quoted 18 lines · Jason Gunthorpe
Quoted 8 lines
Quoted 8 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Quoted 7 lines

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

That's going to be a bit of a PITA, since the DIDs can be in use when the tdx module is start potentially.

Quoted 7 lines

I suppose you can start out by just failing if this has happened.

But as soon as reasonably possible once the system opts into a TDX operation the VT-d drivers should stop using the upper did space.

Otherwise you will have to implement live DID renaming, which is a PITA in an iommu driver.

Yes, agreed.

Thanks,
baolu

author maintainer
#24 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Jason Gunthorpe
Quoted 27 lines · Jason Gunthorpe
Quoted 13 lines · Baolu Lu
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

Quoted 6 lines

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the

    physical iommu information from the arm-smmu-v3 driver;

- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Okay, I haven't looked, not sure what is in cca_psmmu_get() yet

Quoted 5 lines

So the Intel iommu driver will probably end up with interfaces like:

- dmar_tdxcs_iommu_init(struct device *dev, ...);
- dmar_tdxcs_iommu_release(struct device *dev, ...);

Quoted 7 lines
Quoted 2 lines

These would be called from the connect and disconnect callbacks of the TDX link ops:

It seems OK

It sounds like you will have a one time init of the secure iommu at the time the first PCI device wants to use the TSM? It seems like no problem.

Yes.

author maintainer
#26 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Chao Gao
Quoted 12 lines · Chao Gao
Quoted 12 lines
drivers/virt/coco/tdx-host/iommu.c new file +12 -0
+static int tdx_iommu_setup(struct dmar_drhd_unit *drhd)
+{
+	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+	unsigned int mt_page_count = tdx_sysinfo->tdx_connect.iommu_mt_page_count;
+	struct tdx_iommu_state *state;
+	u64 r, tdx_iommu_id;
+	int ret;
+
+	struct tdx_iommu_state *iommu_state __free(kfree) = kzalloc_obj(*iommu_state);
+	if (!iommu_state)
+		return -ENOMEM;
+
Quoted 8 lines

drivers/virt/coco/tdx-host/iommu.c line 192

Quoted 6 lines
drivers/virt/coco/tdx-host/iommu.c new file +6 -0
+	struct tdx_iommu_pages *iommu_mt __free(free_mt_pages) =
+			tdx_iommu_alloc_mt_pages(drhd, mt_page_count);
+	if (!iommu_mt)
+		return -ENOMEM;
+
+	r = tdh_iommu_setup(drhd->reg_base_addr, iommu_mt->root, &tdx_iommu_id);

Dynamic PAMT was merged in 7.3. With it enabled, the PAMT for 4K pages

I guess you mean v7.4-rc1? v7.3 has already been finalized. Either way, I will add dynamic PAMT support in the next version when I rebase.

Quoted 4 lines

is no longer allocated up front, and the kernel must add PAMT backing before giving a page to the TDX module. So, the IQ buffers and the metadata pages need tdx_pamt_get() before TDH.IOMMU.SETUP and tdx_pamt_put() after TDH.IOMMU.CLEAR hands them back.

I will add something like the following:

- call tdx_pamt_get() on every 4K page of the IQ buffers and the metadata pages before TDH.IOMMU.SETUP, and tdx_pamt_put() on the SETUP failure pah;
- call tdx_pamt_put() after TDH.IOMMU.CLEAR succeeds. If CLEAR fails, the pages and their PAMT backing stay with the TDX module and are leaked;
- probably it is needed to export the PAMT helpers to tdx-host.

Thanks,
baolu

#19 lore raw

RE: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Jason Gunthorpe
Quoted 37 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 16, 2026 10:59 PM

Quoted 33 lines · Kevin

+Aneesh.

Quoted 30 lines

From: Baolu Lu <[email protected]>
Sent: Friday, October 16, 2026 3:03 PM

Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

An alternative could be, bringing up one IOMMU when an associated device requests link encryption. Then a no-tdx-support iommu fails the encryption request naturally.

That is what ARM is planning to do. I think it is reasonable to trigger creation of the secure iommu when the TSM is connected to TDX.

Fail connect if the iommu could not be created.

Yes, agreed. The Intel iommu driver could use per-device interfaces, just as ARM is planning. There are some differences, though.

ARM's interaction with the iommu driver in viommu_init() appears to consist of two steps:

- arm_smmu_get_realm_params(dev, &params): get and validate the

    physical iommu information from the arm-smmu-v3 driver;

- cca_psmmu_get(&params): activate the physical smmu for Realm use.

Intel differs in two ways:

- ARM queries the physical iommu information from the iommu driver in the viommu_init() path. On Intel, the secure iommu must be set up during TSM connect, because it is part of establishing PCIe link encryption.

Quoted 6 lines
Quoted 2 lines

My understanding is that the psmmu is required for link encryption, so it's similar ordering here i.e. activated before tsm connect?

No, AFAIK? The psmmu has to be linked before creating the vdev, and in the current spec the only way to get the measurements is through the vdev, but I am expecting that to change.

Okay seems I misread psmmu as pdev. It's the latter created for setting up IDE link. As you said psmmu is only for creating the vdev.

Quoted 4 lines

I have no idea what brilliant idea caused the VT-d iommu HW to become involved in DOE and link IDE configuration in the root port. That makes very little sense from an architecture perspective.

Agree.

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

Quoted 8 lines
Quoted 3 lines

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

That's going to be a bit of a PITA, since the DIDs can be in use when the tdx module is start potentially.

I suppose you can start out by just failing if this has happened.

This is the plan

Quoted 7 lines

But as soon as reasonably possible once the system opts into a TDX operation the VT-d drivers should stop using the upper did space.

Otherwise you will have to implement live DID renaming, which is a PITA in an iommu driver.

yeah nobody wants to do that way.

#21 lore raw

RE: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Baolu Lu
Quoted 31 lines

From: Baolu Lu <[email protected]>
Sent: Monday, October 19, 2026 4:12 PM

Quoted 8 lines · Kevin
Quoted 8 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Quoted 7 lines

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

You are right, "atomically" was too strong. Only the ECMD block has to bracket TDH.IOMMU.SETUP. So I plan to restructure it as follows:

- dmar_tdxcs_init_prepare(dev, &params): resolve the device's DRHD and take a reference on its secure-mode state. For the first user, validate the preconditions (TE/QI/SM enabled), reserve the upper half of the DID space (failing with -EBUSY if any of it is in use), block ECMD and return what tdx-host needs (register base, NUMA node, etc). If the unit is already in Secure TDX mode, just report that so tdx-host skips TDH.IOMMU.SETUP.

Quoted 4 lines

- dmar_tdxcs_init_done(dev, result): unblock ECMD, then either mark the unit as being in Secure TDX mode or roll back (including the DID reservation), based on the SEAMCALL result. It must be called whenever init_prepare() returns success.

We should continue blocking ECMD after the IOMMU successfully enters the TDX mode.

Quoted 11 lines

- dmar_tdxcs_release(dev): drop the reference; the last user releases the DID reservation and clears the Secure TDX mode state after tdx-host issues TDH.IOMMU.CLEAR.

These are called from tdx_tsm_link_connect()/disconnect(). Any failure, whether a precondition, DID reservation or the SEAMCALL itself, aborts connect with the corresponding error.

Thanks,
baolu

author maintainer
#5 lore raw

Re: [PATCH v2 1/4] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to Xu Yilun
Quoted 9 lines · Xu Yilun
Quoted 1 line · Lu Baolu

Add SEAMCALL wrappers for TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR.

We need this patch been reviewed by x86 reviewers. x86 convention doesn't start with the implementation, so remove it.

We can start with a generic description of what the 2 SEAMCALLs are doing in general and why. Maybe move some of your description in next paragraph ahead.

Agreed.

Quoted 14 lines
Quoted 14 lines

TDH.IOMMU.SETUP transitions an IOMMU and related I/O stack into Secure TDX Mode. During this transition, the TDX module validates and protects relevant I/O resources from further untrusted host access, providing the trusted foundation required before enabling TEE-IO security protocols (e.g. SPDM/IDE).

TDH.IOMMU.CLEAR performs the reverse operation and tears down the Secure TDX Mode state for the target IOMMU.

The caller is responsible for allocating the required pages and populating the parameter page in the ABI-defined format.

Quoted 6 lines
Quoted 4 lines

See Intel TDX Connect ABI Specification [1], Section 3.2 (TDX Connect Host-Side (SEAMCALL) Interface Functions).

Thanks to Xu Yilun for the initial draft.

Feel free to drop it. Not that important.

Dropped.

Quoted 53 lines
Quoted 42 lines

[1] https://cdrdv2.intel.com/v1/dl/getContent/858625

---
  arch/x86/include/asm/tdx.h  |  2 ++
  arch/x86/virt/vmx/tdx/tdx.h |  2 ++
  arch/x86/virt/vmx/tdx/tdx.c | 56 +++++++++++++++++++++++++++++++++++++
  3 files changed, 60 insertions(+)
arch/x86/include/asm/tdx.h +2 -0
index 89e97d5761d8..8b5411d3eb67 100644
@@ -176,6 +176,8 @@ u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ex
  u64 tdh_phymem_cache_wb(bool resume);
  u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
  u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn);
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id);
+u64 tdh_iommu_clear(u64 tdx_iommu_id);
  #else
  static inline void tdx_init(void) { }
  static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
arch/x86/virt/vmx/tdx/tdx.h +2 -0
index bdfd0e1e337a..d7beb42cf972 100644
@@ -49,6 +49,8 @@
  #define TDH_SYS_SHUTDOWN		52
  #define TDH_SYS_UPDATE			53
  #define TDH_SYS_DISABLE			69
+#define TDH_IOMMU_SETUP			128
+#define TDH_IOMMU_CLEAR			129
 
  /*
   * SEAMCALL leaf:
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index 1b9ff749dd8e..5d31c1b0890a 100644

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2040

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
@@ -2034,3 +2034,59 @@ void tdx_sys_disable(void)
  	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
  		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
  }
+
+/*
+ * Wrapper for TDH.IOMMU.SETUP leaf, which transitions the IOMMU and related
+ * I/O stack into Secure TDX Mode.

Other tdh_*() wrappers don't have the general descriptions. We all know these are wrappers for some SEAMCALL leafs.

Quoted 1 line

So if there is no special thing to note, remove it.

Removed for both wrappers.

Quoted 23 lines

arch/x86/virt/vmx/tdx/tdx.c void tdx_sys_disable(void) · line 2049

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +9 -0
+ *
+ * The @root page format is defined by the TDX Connect ABI spec (Table 3.35,
+ * Structure of IOMMU_MT Parameter): the first two entries describe the size
+ * and HPA (Host Physical Address) of the two contiguous buffers for the
+ * invalidation queue; the remaining entries provide HPAs of
+ * IOMMU_MT_PAGES_COUNT metadata pages. IOMMU_MT_PAGES_COUNT is obtained from
+ * trusted IOMMU global metadata.
+ */
+u64 tdh_iommu_setup(u64 reg_base, void *root, u64 *tdx_iommu_id)

IIUC, the void *root is a vague type, no type safty. Is it better we define an explicit structure that mirrors the in-memory ABI? Something like:

   struct tdx_iommu_metadata {
	u64 iq1_pa;
	u64 iq2_pa;
	DECLARE_FLEX_ARRAY(u64, mt_pa);
   };

Or,

Quoted 7 lines
   struct tdx_iommu_metadata {
	u64 iq1_pa;
	u64 iq2_pa;
	u64 mt_pa[510];
   };

   static_assert(sizeof(struct tdx_iommu_metadata) == 4096);

Good point. I updated the code with the following:

#define TDX_IOMMU_MT_IQ_NR 2
#define TDX_IOMMU_MT_MAX_PAGES (PAGE_SIZE / sizeof(u64) -
TDX_IOMMU_MT_IQ_NR)

  /* IOMMU_MT parameter page for TDH.IOMMU.SETUP (ABI spec Table 3.35). */
  struct tdx_iommu_mt_param {
      u64 iq[TDX_IOMMU_MT_IQ_NR];	/* HPA | number of 4K pages */
      u64 mt[TDX_IOMMU_MT_MAX_PAGES];	/* HPA of each metadata page */
  };
  static_assert(sizeof(struct tdx_iommu_mt_param) == PAGE_SIZE);

u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,

              u64 *tdx_iommu_id);
Quoted 39 lines
Quoted 39 lines
arch/x86/virt/vmx/tdx/tdx.c +38 -0
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(root),
+	};
+	u64 ret;
+
+	/*
+	 * Don't loop forever:
+	 *
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 */
+	do {
+		ret = seamcall_ret(TDH_IOMMU_SETUP, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	*tdx_iommu_id = args.rcx;
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_setup, "tdx-host");
+
+/*
+ * Wrapper for TDH.IOMMU.CLEAR leaf, which terminates Secure TDX Mode for the
+ * target IOMMU and restores control of related hardware resources to the host.
+ */
+u64 tdh_iommu_clear(u64 tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = tdx_iommu_id,
+	};
+	u64 ret;
+
+	do {
+		ret = seamcall(TDH_IOMMU_CLEAR, &args);
Quoted 8 lines
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_clear, "tdx-host");
Signature · 1 line
2.43.0

Thanks,
baolu

author maintainer
#22 lore raw

Re: [PATCH v2 4/4] coco/tdx-host: Initialize trusted DMA on all capable IOMMUs

In reply to Tian, Kevin
Quoted 30 lines · Kevin
Quoted 30 lines

From: Baolu Lu<[email protected]>
Sent: Monday, October 19, 2026 4:12 PM

Quoted 8 lines · Kevin
Quoted 8 lines

- The TDX Connect architecture places requirements on the host iommu driver while the secure IOMMU is being set up. For example, the host iommu driver must block the enhanced command (ECMD) interface while the TDH.IOMMU.SETUP SEAMCALL runs, and it must reserve the upper half of the domain ID space. These steps have to happen atomically under the iommu driver's own locks. As a result, the Intel iommu driver needs to merge the two steps above into one.

Quoted 7 lines

Didn't get this. Both ECMD and DID reservation are TDX specific things. How are they related to the two steps in the ARM sides and why calls for merging?

and why are they must be done atomically? they are conditions to execute TDH.IOMMU.SETUP, but it is perfectly fine to reserve DID at an early point then block ECMD later...

You are right, "atomically" was too strong. Only the ECMD block has to bracket TDH.IOMMU.SETUP. So I plan to restructure it as follows:

- dmar_tdxcs_init_prepare(dev, &params): resolve the device's DRHD and take a reference on its secure-mode state. For the first user, validate the preconditions (TE/QI/SM enabled), reserve the upper half of the DID space (failing with -EBUSY if any of it is in use), block ECMD and return what tdx-host needs (register base, NUMA node, etc). If the unit is already in Secure TDX mode, just report that so tdx-host skips TDH.IOMMU.SETUP.

Quoted 6 lines
Quoted 4 lines

- dmar_tdxcs_init_done(dev, result): unblock ECMD, then either mark the unit as being in Secure TDX mode or roll back (including the DID reservation), based on the SEAMCALL result. It must be called whenever init_prepare() returns success.

We should continue blocking ECMD after the IOMMU successfully enters the TDX mode.

Updated.

Thanks,
baolu