Skip to contentSkip to conversation

coco/tdx-host: Introduce trusted DMA initialization support

PATCH In review Lu Baolu 6 participants 45 messages Nov 12 – Nov 27 1 message not in the archive

Subsystems (5)
x86 KVM IOMMU Intel IOMMU (VT-d) +1 more subsystems
lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

v3 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.

As in v2, the host/TDX-module interactions live in the tdx-host (TSM) driver, and the VT-d driver only provides the iommu-side pieces. v3 moves trusted DMA setup from driver probe to the TSM connect path. 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

The series is based on the latest tip/x86/tdx branch, with these dependent series applied:

- TDX module extensions initialization

The full stack is available at:

https://github.com/LuBaolu/intel-iommu/commits/iommu/tdxc/enablement-v3

Background:

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to confidential guests. Per the TDX Connect architecture specification [2], 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
- While an IOMMU is in Secure TDX Mode, 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 IOMMU initialization only. The current Link TSM connect callback obtains and immediately drops a trusted-DMA reference, so it exercises TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR, then returns -ENXIO; it does not establish a TSM connection. Once TDISP connect is implemented, the intended lifetime is for the first connected device behind a trusted DMA-capable IOMMU to trigger TDH.IOMMU.SETUP and the last disconnect to trigger TDH.IOMMU.CLEAR. The untrusted DMA path is unchanged, except that while an IOMMU is in Secure TDX Mode the host is confined to the lower half of its domain ID space and must not touch its global command (GCMD) or enhanced command (ECMD) interfaces. 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, together with a structure that mirrors the IOMMU_MT parameter page layout.

- 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: Exports the Dynamic PAMT helpers to tdx-host, which needs PAMT backing for the pages it hands to the TDX module.

- Patch 4: Adds a per-device interface to the VT-d driver:
dmar_tdxc_get_params(), dmar_tdxc_acquire() and dmar_tdxc_release(). acquire() checks the preconditions TDH.IOMMU.SETUP requires and confines the host to the lower half of the DID space; release() undoes it. Trusted DMA-capable units are excluded from DMAR hotplug.

- Patch 5: Blocks GCMD and ECMD on an IOMMU while it is acquired. IOMMUs that need write buffer flushing are refused, and the remaining GCMD writers are guarded. intel_iommu_shutdown() is exempt because TDH.SYS.DISABLE has already run by then.

- Patch 6: Refuses system suspend while any IOMMU is acquired by the TDX module, avoiding suspend/resume callbacks that would change its state.

- Patch 7: Adds the tdx-host Link TSM skeleton that the TSM connect and disconnect callbacks hang off. It is carried here as a dependency.

- Patch 8: Renames tdx-host.c to main.c in preparation for adding more source files. No functional change.

- Patch 9: Adds per-IOMMU reference-counted trusted DMA setup and teardown and invokes it from the TSM connect callback. The setup path allocates the trusted invalidation-queue buffers and metadata pages on the IOMMU's NUMA node, takes Dynamic PAMT backing for them, and builds the IOMMU_MT parameter page. If TDH.IOMMU.CLEAR fails, the pages are intentionally leaked and the IOMMU is not reused.

Change log:

v3:
- Replaced the callback-based "initialize all IOMMUs at probe time" API with a per-device interface. Trusted DMA is now set up on TSM device connect and torn down on disconnect, refcounted per IOMMU.
- Blocked both the GCMD and ECMD interfaces while an IOMMU is in Secure TDX Mode, and refused IOMMUs that need write buffer flushing.
- Add a patch to refuse system suspend while an IOMMU has been acquired by the TDX module to avoid global state change in iommu_suspend/resume().
- Added Dynamic PAMT backing for the IOMMU_MT pages.
- Replaced the untyped parameter page with a dedicated structure that mirrors the IOMMU_MT parameter layout in the ABI spec.
- Split the tdx-host.c to main.c rename into its own patch, and renamed the new file from iommu.c to viommu.c.
- Reworked commit messages per review feedback.

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.
- https://lore.kernel.org/r/[email protected]

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.

The TDX Connect ABI specification should include a dedicated section listing all IOMMU registers whose use must be coordinated between legacy, host-managed operation and trusted, TDX-module-managed operation. This would let host IOMMU drivers avoid accesses that interfere with the TDX module while an IOMMU is in Secure TDX Mode. GCMD and ECMD are confirmed to be in scope; additional registers may be identified as the specification is clarified.

Reference:

Implementation details follow the interfaces and behavior defined in the Intel TDX Connect ABI Specification [1] and the Intel TDX Connect Architecture Specification [2].

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 (9):
x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear x86/virt/tdx: Read global metadata for trusted IOMMU x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host iommu/vt-d: Add interface for tdx-host when enabling trusted DMA iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module iommu/vt-d: Refuse system suspend while acquired by the TDX module coco/tdx-host: Add skeleton to support PCI link encryption coco/tdx-host: Rename tdx-host.c to main.c coco/tdx-host: Enable trusted DMA on IOMMUs on device connect

 drivers/virt/coco/tdx-host/Kconfig            |   7 +
 drivers/virt/coco/tdx-host/Makefile           |   3 +
 arch/x86/include/asm/tdx.h                    |  23 +
 arch/x86/include/asm/tdx_global_metadata.h    |   5 +
 arch/x86/virt/vmx/tdx/tdx.h                   |   2 +
 drivers/iommu/intel/iommu.h                   |  10 +
 drivers/virt/coco/tdx-host/private.h          |  21 +
 include/linux/dmar.h                          |  25 ++
 arch/x86/virt/vmx/tdx/tdx.c                   |  57 ++-
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c   |  20 +
 drivers/iommu/intel/dmar.c                    | 278 +++++++++++-
 drivers/iommu/intel/iommu.c                   |  34 +-
 drivers/iommu/intel/irq_remapping.c           |  10 +
 .../virt/coco/tdx-host/{tdx-host.c => main.c} | 129 +++++-
 drivers/virt/coco/tdx-host/viommu.c           | 401 ++++++++++++++++++
 15 files changed, 1016 insertions(+), 9 deletions(-)
 create mode 100644 drivers/virt/coco/tdx-host/private.h
 rename drivers/virt/coco/tdx-host/{tdx-host.c => main.c} (66%)
 create mode 100644 drivers/virt/coco/tdx-host/viommu.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v3 1/9 x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to TDs. Each IOMMU has a trusted root-table address register and a trusted invalidation queue, both accessible only from SEAM. The existing untrusted DMA translation path of the IOMMU stays under host control.

Before trusted DMA can be used through an IOMMU, the TDX module must initialize these resources. TDH.IOMMU.SETUP does this: it puts the IOMMU into Secure TDX Mode, programs the trusted root table and the trusted invalidation queue, and protects the related I/O resources from further untrusted host access. The host provides the memory for this through a parameter page that lists the invalidation queue buffers and the metadata pages the TDX module needs for the IOMMU.

TDH.IOMMU.CLEAR does the reverse. It tears down the Secure TDX Mode state of the IOMMU, after which the host can reclaim the memory.

Add wrappers for both SEAMCALLs and export them to the tdx-host driver, which allocates the memory and issues them.

TDH.IOMMU.SETUP and TDH.IOMMU.CLEAR are defined in the Intel TDX Connect ABI Specification [1].

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

---
 arch/x86/include/asm/tdx.h  | 14 ++++++++++
 arch/x86/virt/vmx/tdx/tdx.h |  2 ++
 arch/x86/virt/vmx/tdx/tdx.c | 53 +++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)
arch/x86/include/asm/tdx.h +14 -0
index 89e7628796b0..d077990802b9 100644
@@ -162,6 +162,17 @@ struct tdx_td {
 	int tdcx_nr_pages;
 };
 
+#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);
+
 struct tdx_vp {
 	/* TDVP root page */
 	struct page *tdvpr_page;
@@ -202,6 +213,9 @@ 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, struct tdx_iommu_mt_param *param,
+		    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 cac0a5abbff1..693adda89311 100644
@@ -53,6 +53,8 @@
 #define TDH_EXT_INIT			60
 #define TDH_EXT_MEM_ADD			61
 #define TDH_SYS_DISABLE			69
+#define TDH_IOMMU_SETUP			128
+#define TDH_IOMMU_CLEAR			129
 
 /* TDX page types */
 #define	PT_NDA		0x0
arch/x86/virt/vmx/tdx/tdx.c +53 -0

56 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#10 lore raw

PATCH v3 2/9 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.

---
 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 d077990802b9..0ed0062c9b0a 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)
 #define TDX_FEATURES0_DYNAMIC_PAMT	BIT_ULL(36)
 #define TDX_FEATURES0_EXT		BIT_ULL(39)
arch/x86/include/asm/tdx_global_metadata.h +5 -0
index f18e63b671c2..e5e54159ad36 100644
@@ -51,12 +51,17 @@ struct tdx_sys_info_ext {
 	u32 required_pages;
 };
 
+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 9ab4d021017f..9b46f34cb469 100644
@@ -139,6 +139,20 @@ static __init int get_tdx_sys_info_ext(struct tdx_sys_info_ext *sysinfo_ext)
 	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;
@@ -164,5 +178,11 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
 		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
 
+	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
#15 lore raw

PATCH v3 3/9 x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host

In reply to Lu Baolu

With Dynamic PAMT, any page handed to the TDX module needs PAMT backing first, via tdx_pamt_get(), and must drop it with tdx_pamt_put() once the TDX module gives the page back. Besides KVM, the tdx-host driver now hands pages to the TDX module: the IOMMU_MT pages for TDH.IOMMU.SETUP.

Export the two helpers to tdx-host as well. A symbol can only be exported once, so replace EXPORT_SYMBOL_FOR_KVM() with an explicit list. Only kvm-intel calls these helpers in KVM.

---
 arch/x86/virt/vmx/tdx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +2 -2
index a1f35188a272..3b9cfab6cf68 100644
@@ -2426,7 +2426,7 @@ int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)
 
 	return ret;
 }
-EXPORT_SYMBOL_FOR_KVM(tdx_pamt_get);
+EXPORT_SYMBOL_FOR_MODULES(tdx_pamt_get, "kvm-intel,tdx-host");
 
 /* Drop DPAMT refcount for the given pfn and free DPAMT backing if needed. */
 void tdx_pamt_put(kvm_pfn_t pfn)
@@ -2470,7 +2470,7 @@ void tdx_pamt_put(kvm_pfn_t pfn)
 out_unlock:
 	spin_unlock(&dpamt_lock);
 }
-EXPORT_SYMBOL_FOR_KVM(tdx_pamt_put);
+EXPORT_SYMBOL_FOR_MODULES(tdx_pamt_put, "kvm-intel,tdx-host");
 
 void tdx_free_pamt_cache(struct tdx_pamt_cache *cache)
 {
Signature · 1 line
2.43.0
author maintainer
#19 lore raw

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

In reply to Lu Baolu

An Intel IOMMU that supports trusted DMA (ECAP.TDXCS) provides a second set of DMA translation resources, managed by the TDX module and used only for devices assigned to TDs. Before they can be used, the tdx-host driver issues TDH.IOMMU.SETUP to put the IOMMU into Secure TDX Mode, and issues TDH.IOMMU.CLEAR to take it out again.

The VT-d driver owns the hardware state these SEAMCALLs depend on, so it checks that state and keeps its own use of the IOMMU out of the TDX module's way. This is cooperation, not enforcement: the TDX module protects trusted DMA regardless, and a misbehaving host can only cause failures.

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 and returns its register base address, which is how the TDX module identifies an IOMMU, and its NUMA node, so that tdx-host can allocate the TDX module's metadata pages local to that IOMMU.

- dmar_tdxc_acquire() is called right before TDH.IOMMU.SETUP. It checks the preconditions TDH.IOMMU.SETUP requires (DMA translation, queued invalidation and scalable mode enabled) and confines the host to the lower half of the domain ID space, since the TDX module owns the half with the MSB set.

- dmar_tdxc_release() undoes acquire(). tdx-host calls it if TDH.IOMMU.SETUP fails, or after TDH.IOMMU.CLEAR succeeds for the last user. If TDH.IOMMU.CLEAR fails, tdx-host doesn't call it, so the upper half of the domain ID space stays reserved for the TDX module.

DMAR units advertising ECAP.TDXCS are excluded from DMAR hotplug. No platform currently supports both trusted DMA and DMAR hotplug, so that flow can't be validated on hardware. Hot-removing such a unit is also unsafe, because tdx-host may hold a reference to it and the TDX module may own its trusted DMA resources. This also keeps the unit handle returned by dmar_tdxc_get_params() valid for the lifetime of the system.

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

---
 drivers/iommu/intel/iommu.h |   3 +
 include/linux/dmar.h        |  23 +++++
 drivers/iommu/intel/dmar.c  | 185 ++++++++++++++++++++++++++++++++++++
 3 files changed, 211 insertions(+)
drivers/iommu/intel/iommu.h +3 -0
index 5b3d234ae265..18fef91753a0 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,8 @@ 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. */
+	bool		tdxc_acquired;
 	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 +23 -0
index 63e35df2cef4..a39ca13f13dc 100644
@@ -47,9 +47,32 @@ struct dmar_drhd_unit {
 	u8	ignored:1; 		/* ignore drhd		*/
 	u8	include_all:1;
 	u8	gfx_dedicated:1;	/* graphic dedicated	*/
+	struct mutex tdxc_lock;		/* serializes acquire/release */
 	struct intel_iommu *iommu;
 };
 
+/*
+ * struct dmar_tdxc_params - IOMMU information for driving TDH.IOMMU.*
+ * @reg_base_addr: Register base address of the IOMMU. The TDX module
+ *		   identifies an IOMMU by it.
+ * @node: NUMA node of the IOMMU, for allocating the TDX module's
+ *	  metadata pages local to it.
+ * @drhd: VT-d internal handle; the caller must not dereference it. It
+ *	  stays valid for the lifetime of the system because TDX
+ *	  Connect-capable units are never hot-removed.
+ */
+struct dmar_tdxc_params {
+	u64 reg_base_addr;
+	int node;
+	struct dmar_drhd_unit *drhd;
+};
+
+int dmar_tdxc_get_params(struct pci_dev *pdev, struct dmar_tdxc_params *params);
+int dmar_tdxc_acquire(struct pci_dev *pdev,
+		      const struct dmar_tdxc_params *params);
+void dmar_tdxc_release(struct pci_dev *pdev,
+		       const struct dmar_tdxc_params *params);
+
 struct dmar_pci_path {
 	u8 bus;
 	u8 device;
drivers/iommu/intel/dmar.c +185 -0

212 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#27 lore raw

PATCH v3 5/9 iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module

In reply to Lu Baolu

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 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 -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.

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.

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.

---
 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(-)
drivers/iommu/intel/iommu.h +7 -1
index 18fef91753a0..26135248675d 100644
@@ -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;
 	int		seq_id;	/* sequence id of the iommu */
 	int		agaw; /* agaw of this iommu */
@@ -1354,6 +1357,9 @@ static inline bool intel_domain_is_ss_paging(struct dmar_domain *domain)
 }
 
 extern int intel_iommu_sm;
+extern int rwbf_quirk;
+
+#define rwbf_required(iommu)	(rwbf_quirk || cap_rwbf((iommu)->cap))
 int iommu_calculate_agaw(struct intel_iommu *iommu);
 int iommu_calculate_max_sagaw(struct intel_iommu *iommu);
 int ecmd_submit_sync(struct intel_iommu *iommu, u8 ecmd, u64 oa, u64 ob);
drivers/iommu/intel/dmar.c +42 -8

97 lines not on this page · raw message

drivers/iommu/intel/iommu.c +30 -3
index 5a2e705a2f2e..69e8c2c8680a 100644
@@ -46,9 +46,7 @@
 static void __init check_tylersburg_isoch(void);
 static int intel_iommu_set_dirty_tracking(struct iommu_domain *domain,
 					  bool enable);
-static int rwbf_quirk;
-
-#define rwbf_required(iommu)	(rwbf_quirk || cap_rwbf((iommu)->cap))
+int rwbf_quirk;
 
 /*
  * Skip forcing iommu on and avoid tboot-related kernel panics during
@@ -699,6 +697,10 @@ static void iommu_set_root_entry(struct intel_iommu *iommu)
 		addr |= DMA_RTADDR_SMT;
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flag);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired)) {
+		raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
+		return;
+	}
 	writeq(addr, iommu->reg + DMAR_RTADDR_REG);
 
 	writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
@@ -731,12 +733,15 @@ void iommu_flush_write_buffer(struct intel_iommu *iommu)
 		return;
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flag);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired))
+		goto out;
 	writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
 
 	/* Make sure hardware complete it */
 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
 		      readl, (!(val & DMA_GSTS_WBFS)), val);
 
+out:
 	raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
 }
 
@@ -956,6 +961,8 @@ static void iommu_enable_translation(struct intel_iommu *iommu)
 	unsigned long flags;
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired))
+		goto out;
 	iommu->gcmd |= DMA_GCMD_TE;
 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
 
@@ -963,6 +970,7 @@ static void iommu_enable_translation(struct intel_iommu *iommu)
 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
 		      readl, (sts & DMA_GSTS_TES), sts);
 
+out:
 	raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
 }
 
@@ -976,6 +984,8 @@ static void iommu_disable_translation(struct intel_iommu *iommu)
 		return;
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flag);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired))
+		goto out;
 	iommu->gcmd &= ~DMA_GCMD_TE;
 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
 
@@ -983,6 +993,7 @@ static void iommu_disable_translation(struct intel_iommu *iommu)
 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
 		      readl, (!(sts & DMA_GSTS_TES)), sts);
 
+out:
 	raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
 }
 
@@ -2513,6 +2524,12 @@ void intel_iommu_shutdown(void)
 		/* Disable PMRs explicitly here. */
 		iommu_disable_protect_mem_regions(iommu);
 
+		/*
+		 * Only reached via machine_shutdown(), after syscore_shutdown()
+		 * ran TDH.SYS.DISABLE and took the IOMMU out of Secure TDX Mode.
+		 */
+		iommu->tdxc_acquired = false;
+
 		/* Make sure the IOMMUs are switched off */
 		iommu_disable_translation(iommu);
 	}
@@ -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 atomic
+	 * context (e.g. perf pmu::start/stop), so don't sleep; just reject
+	 * the request.
+	 */
+	if (iommu->tdxc_acquired) {
+		ret = -EBUSY;
+		goto err;
+	}
+
 	res = readq(iommu->reg + DMAR_ECRSP_REG);
 	if (res & DMA_ECMD_ECRSP_IP) {
 		ret = -EBUSY;
drivers/iommu/intel/irq_remapping.c +10 -0
index 25c26f706984..6e1fe0fcafd9 100644
@@ -464,6 +464,10 @@ static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
 	addr = virt_to_phys((void *)iommu->ir_table->base);
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired)) {
+		raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
+		return;
+	}
 
 	writeq((addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE,
 	       iommu->reg + DMAR_IRTA_REG);
@@ -489,6 +493,8 @@ static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
 	u32 sts;
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
+	if (WARN_ON_ONCE(iommu->tdxc_acquired))
+		goto out;
 
 	/* Enable interrupt-remapping */
 	iommu->gcmd |= DMA_GCMD_IRE;
@@ -514,6 +520,7 @@ static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
 			"Compatibility-format IRQs enabled despite intr remapping;\n"
 			"you are vulnerable to IRQ injection.\n");
 
+out:
 	raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
 }
 
@@ -658,6 +665,9 @@ static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
 
+	if (WARN_ON_ONCE(iommu->tdxc_acquired))
+		goto end;
+
 	sts = readl(iommu->reg + DMAR_GSTS_REG);
 	if (!(sts & DMA_GSTS_IRES))
 		goto end;
Signature · 1 line
2.43.0
author maintainer
#30 lore raw

PATCH v3 6/9 iommu/vt-d: Refuse system suspend while acquired by the TDX module

In reply to Lu Baolu

While tdx-host has acquired an IOMMU, the VT-d driver blocks its GCMD writes. TDX disables S3 by clearing acpi_suspend_lowlevel, but that is only checked after the syscore suspend callbacks have run, so an S3 request, e.g. "echo mem > /sys/power/state", still runs iommu_suspend() and then iommu_resume():

suspend_enter()
syscore_suspend()
iommu_suspend() <- runs on the acquired IOMMU
suspend_ops->enter() = acpi_suspend_enter()
if (!acpi_suspend_lowlevel) <- TDX's "S3 disabled" check
return -ENOSYS
syscore_resume()
iommu_resume() <- runs on the acquired IOMMU

Register a PM notifier for each DMAR unit that supports trusted DMA and refuse PM_SUSPEND_PREPARE with -EBUSY while the unit is acquired. The suspend then fails before tasks are frozen and before syscore callbacks run.

A check at PM_SUSPEND_PREPARE alone leaves a window: user space is only frozen after the notifiers run, so a device connect may still acquire the unit in between. Close it with a per-unit tdxc_suspending flag, set when the notifier lets the suspend proceed and cleared at PM_POST_SUSPEND. dmar_tdxc_acquire() fails with -EBUSY while it is set. Both the notifier and dmar_tdxc_acquire() hold the unit's tdxc_lock, so the check and the acquisition can't interleave.

The notifiers are registered at init only for units advertising ECAP.TDXCS. Such units are never hot-added or hot-removed, so the set doesn't change afterwards and the notifiers are never unregistered.

---
 drivers/iommu/intel/iommu.h |  1 +
 include/linux/dmar.h        |  4 ++-
 drivers/iommu/intel/dmar.c  | 53 +++++++++++++++++++++++++++++++++++++
 drivers/iommu/intel/iommu.c |  1 +
 4 files changed, 58 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.h +1 -0
index 26135248675d..f7554df8cd2d 100644
@@ -1363,6 +1363,7 @@ extern int rwbf_quirk;
 int iommu_calculate_agaw(struct intel_iommu *iommu);
 int iommu_calculate_max_sagaw(struct intel_iommu *iommu);
 int ecmd_submit_sync(struct intel_iommu *iommu, u8 ecmd, u64 oa, u64 ob);
+void dmar_tdxc_pm_init(void);
 
 static inline bool ecmd_has_pmu_essential(struct intel_iommu *iommu)
 {
include/linux/dmar.h +3 -1
index a39ca13f13dc..8b407bc6d179 100644
@@ -47,7 +47,9 @@ struct dmar_drhd_unit {
 	u8	ignored:1; 		/* ignore drhd		*/
 	u8	include_all:1;
 	u8	gfx_dedicated:1;	/* graphic dedicated	*/
-	struct mutex tdxc_lock;		/* serializes acquire/release */
+	struct mutex tdxc_lock;		/* serializes acquire/release/suspend */
+	struct notifier_block tdxc_pm_nb;
+	bool	tdxc_suspending;	/* protected by tdxc_lock */
 	struct intel_iommu *iommu;
 };
 
drivers/iommu/intel/dmar.c +53 -0

68 lines not on this page · raw message

drivers/iommu/intel/iommu.c +1 -0
index 69e8c2c8680a..d8b9610ae791 100644
@@ -2041,6 +2041,7 @@ static struct syscore iommu_syscore = {
 static void __init init_iommu_pm_ops(void)
 {
 	register_syscore(&iommu_syscore);
+	dmar_tdxc_pm_init();
 }
 
 #else
Signature · 1 line
2.43.0
author maintainer
#33 lore raw

PATCH v3 7/9 coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

---
 drivers/virt/coco/tdx-host/Kconfig    |   2 +
 arch/x86/include/asm/tdx.h            |   8 ++
 drivers/virt/coco/tdx-host/tdx-host.c | 118 +++++++++++++++++++++++++-
 3 files changed, 127 insertions(+), 1 deletion(-)
drivers/virt/coco/tdx-host/Kconfig +2 -0
index 57d0c01a4357..f8c8cc29d46c 100644
@@ -1,6 +1,8 @@
 config TDX_HOST_SERVICES
 	tristate
 	depends on INTEL_TDX_HOST
+	depends on PCI
 	select FW_LOADER
 	select FW_UPLOAD
+	select PCI_TSM
 	default m
arch/x86/include/asm/tdx.h +8 -0
index 0ed0062c9b0a..fc284a555984 100644
@@ -142,6 +142,14 @@ int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);
 int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);
 void tdx_pamt_put(kvm_pfn_t pfn);
 
+static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)
+{
+	if (!(sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return false;
+
+	return false; /* To be enabled when kernel is ready */
+}
+
 int tdx_guest_keyid_alloc(void);
 u32 tdx_get_nr_guest_keyids(void);
 void tdx_guest_keyid_free(unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +117 -1

140 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#41 lore raw

PATCH v3 8/9 coco/tdx-host: Rename tdx-host.c to main.c

In reply to Lu Baolu

The tdx-host driver is about to gain more source files. Rename tdx-host.c to main.c and build the tdx-host module from it, so the following changes can add objects to the module without renaming the main file at the same time.

No functional change.

---
 drivers/virt/coco/tdx-host/Makefile               | 2 ++
 drivers/virt/coco/tdx-host/{tdx-host.c => main.c} | 0
 2 files changed, 2 insertions(+)
 rename drivers/virt/coco/tdx-host/{tdx-host.c => main.c} (100%)
drivers/virt/coco/tdx-host/Makefile +2 -0
index e61e749a8dff..a90ffba13a49 100644
@@ -1 +1,3 @@
 obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
+
+tdx-host-y := main.o
drivers/virt/coco/tdx-host/main.c renamed from drivers/virt/coco/tdx-host/tdx-host.c +0 -0
similarity index 100%
rename from drivers/virt/coco/tdx-host/tdx-host.c
rename to drivers/virt/coco/tdx-host/main.c
Signature · 1 line
2.43.0
author maintainer
#42 lore raw

PATCH v3 9/9 coco/tdx-host: Enable trusted DMA on IOMMUs on device connect

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. They become usable once TDH.IOMMU.SETUP puts the IOMMU into Secure TDX Mode and the TDX module initializes them. The host keeps the regular translation path.

Issue TDH.IOMMU.SETUP on demand, when the first device behind an IOMMU is connected through the TSM, rather than for every capable IOMMU at probe time. Secure TDX Mode costs host resources: the TDX module's metadata pages and half of the IOMMU's domain ID space. That cost should only be paid for IOMMUs that actually serve such devices.

tdx_iommu_get() and tdx_iommu_put() keep a per-IOMMU refcount. The first get() issues TDH.IOMMU.SETUP and the last put() issues TDH.IOMMU.CLEAR. A single mutex covers both the refcount and the SEAMCALL that follows, so a device cannot join an IOMMU that is being cleared or whose setup has not finished. The refcount lives here rather than in the VT-d driver because tdx-host already owns the per-IOMMU state that must outlive TDH.IOMMU.SETUP: the IOMMU_MT pages and the TDX IOMMU ID.

The IOMMU_MT parameter page lists two contiguous buffers for the trusted invalidation queue, followed by the metadata pages. The TDX module reports how many metadata pages it needs in its global metadata.

With Dynamic PAMT, every 4K page given to the TDX module needs PAMT backing first. Take it for each page of the invalidation queue buffers and the metadata pages before TDH.IOMMU.SETUP, and drop it if setup fails or once TDH.IOMMU.CLEAR succeeds. The parameter page itself only carries the page list into the SEAMCALL and needs no backing.

If TDH.IOMMU.CLEAR fails, the TDX module may still reference the IOMMU_MT pages, so they are leaked together with their PAMT backing, and the IOMMU is marked unusable. Later get() calls for that IOMMU fail with -EBUSY rather than reuse an IOMMU in an unknown state. tdx-host pins itself while such an IOMMU exists, so unloading can't lose track of it. If the failure happens during unload itself, only the tracking entry is freed; the VT-d driver still has the IOMMU acquired and keeps rejecting it.

---
 drivers/virt/coco/tdx-host/Kconfig   |   5 +
 drivers/virt/coco/tdx-host/Makefile  |   1 +
 drivers/virt/coco/tdx-host/private.h |  21 ++
 drivers/virt/coco/tdx-host/main.c    |  11 +
 drivers/virt/coco/tdx-host/viommu.c  | 401 +++++++++++++++++++++++++++
 5 files changed, 439 insertions(+)
 create mode 100644 drivers/virt/coco/tdx-host/private.h
 create mode 100644 drivers/virt/coco/tdx-host/viommu.c
drivers/virt/coco/tdx-host/Kconfig +5 -0
index f8c8cc29d46c..1395929f05bb 100644
@@ -6,3 +6,8 @@ config TDX_HOST_SERVICES
 	select FW_UPLOAD
 	select PCI_TSM
 	default m
+
+config TDX_CONNECT
+	def_bool y
+	depends on TDX_HOST_SERVICES
+	depends on INTEL_IOMMU
drivers/virt/coco/tdx-host/Makefile +1 -0
index a90ffba13a49..bfef8567f16a 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
 
 tdx-host-y := main.o
+tdx-host-$(CONFIG_TDX_CONNECT) += viommu.o
drivers/virt/coco/tdx-host/private.h new file +21 -0
new file mode 100644
index 000000000000..53a250ddd695
@@ -0,0 +1,21 @@
+/* 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>
+
+struct pci_dev;
+
+#ifdef CONFIG_TDX_CONNECT
+int tdx_iommu_get(struct pci_dev *pdev);
+void tdx_iommu_put(struct pci_dev *pdev);
+#else
+static inline int tdx_iommu_get(struct pci_dev *pdev)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void tdx_iommu_put(struct pci_dev *pdev) { }
+#endif /* CONFIG_TDX_CONNECT */
+#endif /* __TDX_HOST_PRIVATE_H */
drivers/virt/coco/tdx-host/main.c +11 -0
index 0908aaaa28ac..a815dbe5127a 100644
@@ -18,6 +18,8 @@
 #include <asm/tdx.h>
 #include <asm/tdx_global_metadata.h>
 
+#include "private.h"
+
 static const struct x86_cpu_id tdx_host_ids[] = {
 	X86_MATCH_FEATURE(X86_FEATURE_TDX_HOST_PLATFORM, NULL),
 	{}
@@ -35,11 +37,20 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
 
 static int tdx_tsm_link_connect(struct pci_dev *pdev)
 {
+	int ret;
+
+	ret = tdx_iommu_get(pdev);
+	if (ret)
+		return ret;
+
+	/* TDISP connect is not implemented yet. */
+	tdx_iommu_put(pdev);
 	return -ENXIO;
 }
 
 static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
 {
+	tdx_iommu_put(pdev);
 }
 
 static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
drivers/virt/coco/tdx-host/viommu.c new file +401 -0

401 lines not on this page · raw message

Signature · 1 line
2.43.0
#34 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Lu Baolu
Quoted 3 lines · Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

s/TDX Module/TDX module for the whole patch. Existing code and docs mostly use "TDX module".

Also s/tdx_feature0/tdx_features0.

Quoted 10 lines

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

This changelog is outdated. TDX module initialization has already moved earlier in x86 initialization.

Quoted 36 lines

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

---

drivers/virt/coco/tdx-host/Kconfig    |   2 +
arch/x86/include/asm/tdx.h            |   8 ++
drivers/virt/coco/tdx-host/tdx-host.c | 118 +++++++++++++++++++++++++-
3 files changed, 127 insertions(+), 1 deletion(-)
drivers/virt/coco/tdx-host/Kconfig +0 -0
index 57d0c01a4357..f8c8cc29d46c 100644
@@ -1,6 +1,8 @@

config TDX_HOST_SERVICES
tristate
depends on INTEL_TDX_HOST

drivers/virt/coco/tdx-host/Kconfig +1 -0
+	depends on PCI

select FW_LOADER
select FW_UPLOAD

drivers/virt/coco/tdx-host/Kconfig +1 -0
+	select PCI_TSM

default m

arch/x86/include/asm/tdx.h +0 -0
index 0ed0062c9b0a..fc284a555984 100644

arch/x86/include/asm/tdx.h int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages); · line 151

Quoted 10 lines
arch/x86/include/asm/tdx.h +7 -0
@@ -142,6 +142,14 @@ int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);
 int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);
 void tdx_pamt_put(kvm_pfn_t pfn);
 
+static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)
+{
+	if (!(sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return false;
+
+	return false; /* To be enabled when kernel is ready */
+}

Patch 2 open-codes the same TDX_FEATURES0_TDXCONNECT check. Can this helper be moved to patch 2 and used there?

Quoted 41 lines
arch/x86/include/asm/tdx.h +1 -0
+
 int tdx_guest_keyid_alloc(void);
 u32 tdx_get_nr_guest_keyids(void);
 void tdx_guest_keyid_free(unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +18 -0
index e8ed8dfa526e..0908aaaa28ac 100644
@@ -9,10 +9,14 @@
 #include <linux/firmware.h>
 #include <linux/module.h>
 #include <linux/sysfs.h>
+#include <linux/pci.h>
+#include <linux/pci-tsm.h>
+#include <linux/tsm.h>
 
 #include <asm/cpu_device_id.h>
 #include <asm/seamldr.h>
 #include <asm/tdx.h>
+#include <asm/tdx_global_metadata.h>
 
 static const struct x86_cpu_id tdx_host_ids[] = {
 	X86_MATCH_FEATURE(X86_FEATURE_TDX_HOST_PLATFORM, NULL),
@@ -20,6 +24,112 @@ static const struct x86_cpu_id tdx_host_ids[] = {
 };
 MODULE_DEVICE_TABLE(x86cpu, tdx_host_ids);
 
+struct tdx_tsm_link {
+	struct pci_tsm_pf0 pci;
+};
+
+static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
+{
+	return container_of(tsm, struct tdx_tsm_link, pci.base_tsm);
+}
+
+static int tdx_tsm_link_connect(struct pci_dev *pdev)
+{
+	return -ENXIO;
+}
+

drivers/virt/coco/tdx-host/tdx-host.c static const struct x86_cpu_id tdx_host_ids[] = { · line 50

Quoted 10 lines
drivers/virt/coco/tdx-host/tdx-host.c +10 -0
+static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
+{
+}
+
+static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
+					      struct pci_dev *pdev)
+{
+	int rc;
+
+	struct tdx_tsm_link *tlink __free(kfree) = kzalloc_obj(*tlink);

tdx-host goes through the tip tree. It looks like Dave prefers plain error handling over __free() [*]. I think we should drop __free() for tdx-host code. Same for tdx_tsm_link_fn_probe() below.

*: https://lore.kernel.org/kvm/[email protected]/

Quoted 33 lines
drivers/virt/coco/tdx-host/tdx-host.c +33 -0
+	if (!tlink)
+		return NULL;
+
+	rc = pci_tsm_pf0_constructor(pdev, &tlink->pci, tsm_dev);
+	if (rc)
+		return NULL;
+
+	return &no_free_ptr(tlink)->pci.base_tsm;
+}
+
+static void tdx_tsm_link_pf0_remove(struct pci_tsm *tsm)
+{
+	struct tdx_tsm_link *tlink = to_tdx_tsm_link(tsm);
+
+	pci_tsm_pf0_destructor(&tlink->pci);
+	kfree(tlink);
+}
+
+static struct pci_tsm *tdx_tsm_link_fn_probe(struct tsm_dev *tsm_dev,
+					     struct pci_dev *pdev)
+{
+	int rc;
+
+	struct pci_tsm *pci_tsm __free(kfree) = kzalloc_obj(*pci_tsm);
+	if (!pci_tsm)
+		return NULL;
+
+	rc = pci_tsm_link_constructor(pdev, pci_tsm, tsm_dev);
+	if (rc)
+		return NULL;
+
+	return no_free_ptr(pci_tsm);
+}
#43 lore raw

Re: [PATCH v3 9/9] coco/tdx-host: Enable trusted DMA on IOMMUs on device connect

In reply to Lu Baolu
Quoted 32 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. They become usable once TDH.IOMMU.SETUP puts the IOMMU into Secure TDX Mode and the TDX module initializes them. The host keeps the regular translation path.

Issue TDH.IOMMU.SETUP on demand, when the first device behind an IOMMU is connected through the TSM, rather than for every capable IOMMU at probe time. Secure TDX Mode costs host resources: the TDX module's metadata pages and half of the IOMMU's domain ID space. That cost should only be paid for IOMMUs that actually serve such devices.

tdx_iommu_get() and tdx_iommu_put() keep a per-IOMMU refcount. The first get() issues TDH.IOMMU.SETUP and the last put() issues TDH.IOMMU.CLEAR. A single mutex covers both the refcount and the SEAMCALL that follows, so a device cannot join an IOMMU that is being cleared or whose setup has not finished. The refcount lives here rather than in the VT-d driver because tdx-host already owns the per-IOMMU state that must outlive TDH.IOMMU.SETUP: the IOMMU_MT pages and the TDX IOMMU ID.

The IOMMU_MT parameter page lists two contiguous buffers for the trusted invalidation queue, followed by the metadata pages. The TDX module reports how many metadata pages it needs in its global metadata.

With Dynamic PAMT, every 4K page given to the TDX module needs PAMT backing first. Take it for each page of the invalidation queue buffers and the metadata pages before TDH.IOMMU.SETUP, and drop it if setup fails or once TDH.IOMMU.CLEAR succeeds. The parameter page itself only carries the page list into the SEAMCALL and needs no backing.

Quoted 8 lines

If TDH.IOMMU.CLEAR fails, the TDX module may still reference the IOMMU_MT pages, so they are leaked together with their PAMT backing, and the IOMMU is marked unusable. Later get() calls for that IOMMU fail with -EBUSY rather than reuse an IOMMU in an unknown state. tdx-host pins itself while such an IOMMU exists, so unloading can't lose track of it. If the failure happens during unload itself, only the tracking entry is freed; the VT-d driver still has the IOMMU acquired and keeps rejecting it.

If the pin can fail anyway, what's the point of taking it at all?

If tdx-host is reloaded, we can rely on the VT-d driver or the TDX module to reject transitioning the IOMMU to secure mode.

Quoted 17 lines

---

drivers/virt/coco/tdx-host/Kconfig   |   5 +
drivers/virt/coco/tdx-host/Makefile  |   1 +
drivers/virt/coco/tdx-host/private.h |  21 ++
drivers/virt/coco/tdx-host/main.c    |  11 +
drivers/virt/coco/tdx-host/viommu.c  | 401 +++++++++++++++++++++++++++
5 files changed, 439 insertions(+)

create mode 100644 drivers/virt/coco/tdx-host/private.h create mode 100644 drivers/virt/coco/tdx-host/viommu.c

drivers/virt/coco/tdx-host/Kconfig +0 -0
index f8c8cc29d46c..1395929f05bb 100644

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

Quoted 8 lines
drivers/virt/coco/tdx-host/Kconfig +5 -0
@@ -6,3 +6,8 @@ config TDX_HOST_SERVICES
 	select FW_UPLOAD
 	select PCI_TSM
 	default m
+
+config TDX_CONNECT
+	def_bool y
+	depends on TDX_HOST_SERVICES
+	depends on INTEL_IOMMU

Do we need this TDX_CONNECT config? We can just add "depends on INTEL_IOMMU" to TDX_HOST_SERVICES, and then drop the stub functions in private.h. Patch 7 already makes TDX_HOST_SERVICES depend on PCI in the same way.

The cost is that without INTEL_IOMMU, none of the tdx-host services are available, including TDX module updates. But I doubt anyone would want to launch CoCo VMs while compiling out INTEL_IOMMU entirely.

We can always add this option back if such a configuration turns out to be practical.

Quoted 6 lines
drivers/virt/coco/tdx-host/Kconfig +6 -0
+/*
+ * Serializes the refcounts together with TDH.IOMMU.SETUP and
+ * TDH.IOMMU.CLEAR, so a device can't join an IOMMU that is being
+ * cleared, or one whose setup hasn't finished.
+ */
+static DEFINE_MUTEX(tdx_iommu_lock);

drivers/virt/coco/tdx-host/Kconfig

Quoted 10 lines
drivers/virt/coco/tdx-host/Kconfig +10 -0
+static LIST_HEAD(tdx_iommu_list);
+
+static void *tdx_iommu_alloc_pages(int node, size_t size)
+{
+	struct folio *folio;
+
+	if (node == NUMA_NO_NODE)
+		node = numa_mem_id();
+
+	folio = __folio_alloc_node(GFP_KERNEL | __GFP_ZERO, get_order(size), node);

Would kzalloc_node() work here? kzalloc_node() handles NUMA_NO_NODE itself, and there's no need to convert between folios and void *.

Quoted 90 lines
drivers/virt/coco/tdx-host/Kconfig +90 -0
+	if (!folio)
+		return NULL;
+
+	return folio_address(folio);
+}
+
+static void tdx_iommu_free_pages(void *virt)
+{
+	if (!virt)
+		return;
+
+	folio_put(virt_to_folio(virt));
+}
+
+/* Number of 4K pages backing IOMMU_MT entry @i. */
+static unsigned int mt_entry_nr_pages(unsigned int i)
+{
+	return i < TDX_IOMMU_MT_IQ_NR ? IQ_BUFFER_SIZE >> PAGE_SHIFT : 1;
+}
+
+/*
+ * Drop the PAMT backing of the first @nr 4K pages handed to the TDX
+ * module, in the order mt_pamt_get() took it. Pass UINT_MAX for all.
+ */
+static void mt_pamt_put(struct tdx_iommu_pages *mt, unsigned int nr)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < mt->nr_entries && nr; i++) {
+		unsigned long pfn = PHYS_PFN(__pa(mt->pages[i]));
+
+		for (j = 0; j < mt_entry_nr_pages(i) && nr; j++, nr--)
+			tdx_pamt_put(pfn + j);
+	}
+}
+
+/*
+ * With Dynamic PAMT, every 4K page handed to the TDX module needs PAMT
+ * backing first. This covers the IQ buffers and the metadata pages, not
+ * the root page, which only carries the list of pages into the
+ * SEAMCALL. tdx_pamt_get() is a no-op without Dynamic PAMT.
+ */
+static int mt_pamt_get(struct tdx_iommu_pages *mt)
+{
+	unsigned int i, j, nr = 0;
+	int ret;
+
+	for (i = 0; i < mt->nr_entries; i++) {
+		unsigned long pfn = PHYS_PFN(__pa(mt->pages[i]));
+
+		for (j = 0; j < mt_entry_nr_pages(i); j++) {
+			ret = tdx_pamt_get(pfn + j, NULL);
+			if (ret) {
+				mt_pamt_put(mt, nr);
+				return ret;
+			}
+			nr++;
+		}
+	}
+
+	return 0;
+}
+
+static void free_mt_pages(struct tdx_iommu_pages *array)
+{
+	unsigned int i;
+
+	if (IS_ERR_OR_NULL(array))
+		return;
+
+	mt_pamt_put(array, UINT_MAX);
+
+	for (i = 0; i < array->nr_entries; i++)
+		tdx_iommu_free_pages(array->pages[i]);
+
+	tdx_iommu_free_pages(array->root);
+	kfree(array->pages);
+	kfree(array);
+}
+
+DEFINE_FREE(free_mt_pages, struct tdx_iommu_pages *, free_mt_pages(_T))
+
+static void **alloc_mt_pages(unsigned int nr_entries, int node)
+{
+	unsigned int i;
+	void **pages;
+	void *vaddr;
+
+	pages = kzalloc_objs(*pages, nr_entries);
+	if (!pages)

drivers/virt/coco/tdx-host/Kconfig

Quoted 10 lines
drivers/virt/coco/tdx-host/Kconfig +10 -0
+		return NULL;
+
+	/* Allocate two contiguous buffers for the invalidation queue. */
+	pages[0] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
+	if (!pages[0])
+		goto free_pages;
+
+	pages[1] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
+	if (!pages[1])
+		goto free_pages;

You can use mt_entry_nr_pages() to fold the two IQ buffer allocations into the loop below.

Quoted 18 lines
drivers/virt/coco/tdx-host/Kconfig +18 -0
+
+	/* Allocate the required number of pages for the IOMMU metadata. */
+	for (i = TDX_IOMMU_MT_IQ_NR; i < nr_entries; i++) {
+		vaddr = tdx_iommu_alloc_pages(node, SZ_4K);
+		if (!vaddr)
+			goto free_pages;
+		pages[i] = vaddr;
+	}
+
+	return pages;
+
+free_pages:
+	for (i = 0; i < nr_entries; i++)
+		tdx_iommu_free_pages(pages[i]);
+	kfree(pages);
+
+	return NULL;
+}
#2 lore raw

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

In reply to Lu Baolu

On 11/12/2026 12:30 PM, Lu Baolu wrote:
[...]

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

Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -0
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs. */

Nit:
It's a bit confusing without the all the background.
Once Rick's patch [1] removes tdx_clflush_page() and its comment, a reader may wonder why this wrapper explains the cache flush but the others don't.

Is it better to drop the comment and explain it in the change log?

[1] https://lore.kernel.org/all/[email protected]/

Quoted 51 lines
arch/x86/virt/vmx/tdx/tdx.c +51 -0
+u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,
+		    u64 *tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(param),
+	};
+	u64 ret;
+
+	/*
+	 * Retry only on TDX_INTERRUPTED_RESUMABLE:
+	 *
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 *
+	 * The ABI spec also allows retrying on TDX_OPERAND_BUSY, but that
+	 * status doesn't guarantee forward progress, so retrying could spin
+	 * forever. The host serializes all TDH.IOMMU.SETUP and
+	 * TDH.IOMMU.CLEAR calls for an IOMMU, so the TDX module should never
+	 * report the operand as busy.
+	 */
+	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");
+
+u64 tdh_iommu_clear(u64 tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = tdx_iommu_id,
+	};
+	u64 ret;
+
+	/*
+	 * Don't retry on TDX_OPERAND_BUSY. See the comment in
+	 * tdh_iommu_setup().
+	 */
+	do {
+		ret = seamcall(TDH_IOMMU_CLEAR, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_clear, "tdx-host");
#11 lore raw

Re: [PATCH v3 2/9] 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.

Nit:
The feature enumerated is TDX Connect, I don't think they are interchangeable. Trusted IOMMU is part of TDX Connect, right?

Quoted 87 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.

---
 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 d077990802b9..0ed0062c9b0a 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)
 #define TDX_FEATURES0_DYNAMIC_PAMT	BIT_ULL(36)
 #define TDX_FEATURES0_EXT		BIT_ULL(39)
arch/x86/include/asm/tdx_global_metadata.h +5 -0
index f18e63b671c2..e5e54159ad36 100644
@@ -51,12 +51,17 @@ struct tdx_sys_info_ext {
 	u32 required_pages;
 };
 
+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 9ab4d021017f..9b46f34cb469 100644
@@ -139,6 +139,20 @@ static __init int get_tdx_sys_info_ext(struct tdx_sys_info_ext *sysinfo_ext)
 	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;
@@ -164,5 +178,11 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
 	if (!ret && tdx_supports_dynamic_pamt(sysinfo))
 		ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
 
+	if (ret)
+		return ret;
+
+	if (sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT)
+		ret = get_tdx_sys_info_connect(&sysinfo->tdx_connect);
+
 	return ret;
 }
#38 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Lu Baolu
Quoted 25 lines · Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

[...]

drivers/virt/coco/tdx-host/tdx-host.c static int seamldr_init(struct device *dev) · line 323

Quoted 11 lines

static int tdx_host_probe(struct faux_device *fdev) {

drivers/virt/coco/tdx-host/tdx-host.c +7 -1
-	return seamldr_init(&fdev->dev);
+	int ret;
+
+	ret = seamldr_init(&fdev->dev);
+	if (ret)
+		return ret;
+
+	return tdx_tdisp_init(&fdev->dev);

If tdx_tdisp_init() fails, it will also disable TDX module update and the version sysfs attributes. If this is intended, could you add some description in the change log about it?

Quoted 3 lines

}

static const struct faux_device_ops tdx_host_ops = {

author maintainer
#35 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Chao Gao
Quoted 9 lines · Chao Gao
Quoted 3 lines · Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

s/TDX Module/TDX module for the whole patch. Existing code and docs mostly use "TDX module".

Also s/tdx_feature0/tdx_features0.

Updated. Thanks!

Quoted 11 lines
Quoted 10 lines

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

Quoted 2 lines

This changelog is outdated. TDX module initialization has already moved earlier in x86 initialization.

Updated. Thanks!

Quoted 40 lines
Quoted 40 lines

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

---

drivers/virt/coco/tdx-host/Kconfig    |   2 +
arch/x86/include/asm/tdx.h            |   8 ++
drivers/virt/coco/tdx-host/tdx-host.c | 118 +++++++++++++++++++++++++-
3 files changed, 127 insertions(+), 1 deletion(-)
drivers/virt/coco/tdx-host/Kconfig +0 -0
index 57d0c01a4357..f8c8cc29d46c 100644
@@ -1,6 +1,8 @@

config TDX_HOST_SERVICES
tristate
depends on INTEL_TDX_HOST

drivers/virt/coco/tdx-host/Kconfig +1 -0
+	depends on PCI

select FW_LOADER
select FW_UPLOAD

drivers/virt/coco/tdx-host/Kconfig +1 -0
+	select PCI_TSM

default m

arch/x86/include/asm/tdx.h +0 -0
index 0ed0062c9b0a..fc284a555984 100644
@@ -142,6 +142,14 @@ int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);
 int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);
 void tdx_pamt_put(kvm_pfn_t pfn);
 
Quoted 10 lines

arch/x86/include/asm/tdx.h int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages); · line 151

Quoted 7 lines
arch/x86/include/asm/tdx.h +7 -0
+static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)
+{
+	if (!(sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return false;
+
+	return false; /* To be enabled when kernel is ready */
+}

Patch 2 open-codes the same TDX_FEATURES0_TDXCONNECT check. Can this helper be moved to patch 2 and used there?

These checks serve different purposes: the metadata patch checks whether the TDX module supports TDX Connect; tdx_supports_tdisp() decides whether the kernel is ready to register the link tsm. The latter may include additional checks, such as quirks, so I would like to keep the helper for the tsm registration.

Quoted 57 lines
Quoted 42 lines
arch/x86/include/asm/tdx.h +1 -0
+
 int tdx_guest_keyid_alloc(void);
 u32 tdx_get_nr_guest_keyids(void);
 void tdx_guest_keyid_free(unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +18 -0
index e8ed8dfa526e..0908aaaa28ac 100644
@@ -9,10 +9,14 @@
 #include <linux/firmware.h>
 #include <linux/module.h>
 #include <linux/sysfs.h>
+#include <linux/pci.h>
+#include <linux/pci-tsm.h>
+#include <linux/tsm.h>
 
 #include <asm/cpu_device_id.h>
 #include <asm/seamldr.h>
 #include <asm/tdx.h>
+#include <asm/tdx_global_metadata.h>
 
 static const struct x86_cpu_id tdx_host_ids[] = {
 	X86_MATCH_FEATURE(X86_FEATURE_TDX_HOST_PLATFORM, NULL),
@@ -20,6 +24,112 @@ static const struct x86_cpu_id tdx_host_ids[] = {
 };
 MODULE_DEVICE_TABLE(x86cpu, tdx_host_ids);
 
+struct tdx_tsm_link {
+	struct pci_tsm_pf0 pci;
+};
+
+static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
+{
+	return container_of(tsm, struct tdx_tsm_link, pci.base_tsm);
+}
+
+static int tdx_tsm_link_connect(struct pci_dev *pdev)
+{
+	return -ENXIO;
+}
+

drivers/virt/coco/tdx-host/tdx-host.c static const struct x86_cpu_id tdx_host_ids[] = { · line 50

Quoted 10 lines
drivers/virt/coco/tdx-host/tdx-host.c +10 -0
+static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
+{
+}
+
+static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
+					      struct pci_dev *pdev)
+{
+	int rc;
+
+	struct tdx_tsm_link *tlink __free(kfree) = kzalloc_obj(*tlink);

tdx-host goes through the tip tree. It looks like Dave prefers plain error handling over __free() [*]. I think we should drop __free() for tdx-host code. Same for tdx_tsm_link_fn_probe() below.

Quoted 1 line

*: https://lore.kernel.org/kvm/[email protected]/

Okay, so I will update it like this:

drivers/virt/coco/tdx-host/main.c +16 -6
index a815dbe5127a..af887c49a027 100644
@@ -56,17 +56,22 @@ static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
 static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
 					      struct pci_dev *pdev)
 {
+	struct tdx_tsm_link *tlink;
 	int rc;
 
-	struct tdx_tsm_link *tlink __free(kfree) = kzalloc_obj(*tlink);
+	tlink = kzalloc_obj(*tlink);
 	if (!tlink)
 		return NULL;
 
 	rc = pci_tsm_pf0_constructor(pdev, &tlink->pci, tsm_dev);
 	if (rc)
-		return NULL;
+		goto err_free;
 
-	return &no_free_ptr(tlink)->pci.base_tsm;
+	return &tlink->pci.base_tsm;
+
+err_free:
+	kfree(tlink);
+	return NULL;
 }
 
 static void tdx_tsm_link_pf0_remove(struct pci_tsm *tsm)
@@ -80,17 +85,22 @@ static void tdx_tsm_link_pf0_remove(struct pci_tsm *tsm)
 static struct pci_tsm *tdx_tsm_link_fn_probe(struct tsm_dev *tsm_dev,
 					     struct pci_dev *pdev)
 {
+	struct pci_tsm *pci_tsm;
 	int rc;
 
-	struct pci_tsm *pci_tsm __free(kfree) = kzalloc_obj(*pci_tsm);
+	pci_tsm = kzalloc_obj(*pci_tsm);
 	if (!pci_tsm)
 		return NULL;
 
 	rc = pci_tsm_link_constructor(pdev, pci_tsm, tsm_dev);
 	if (rc)
-		return NULL;
+		goto err_free;
 
-	return no_free_ptr(pci_tsm);
+	return pci_tsm;
+
+err_free:
+	kfree(pci_tsm);
+	return NULL;
 }
 
 static struct pci_tsm *tdx_tsm_link_probe(struct tsm_dev *tsm_dev,
Signature · 1 line
2.43.0
Quoted 25 lines
Quoted 25 lines
drivers/virt/coco/tdx-host/tdx-host.c +25 -0
+	if (!tlink)
+		return NULL;
+
+	rc = pci_tsm_pf0_constructor(pdev, &tlink->pci, tsm_dev);
+	if (rc)
+		return NULL;
+
+	return &no_free_ptr(tlink)->pci.base_tsm;
+}
+
+static void tdx_tsm_link_pf0_remove(struct pci_tsm *tsm)
+{
+	struct tdx_tsm_link *tlink = to_tdx_tsm_link(tsm);
+
+	pci_tsm_pf0_destructor(&tlink->pci);
+	kfree(tlink);
+}
+
+static struct pci_tsm *tdx_tsm_link_fn_probe(struct tsm_dev *tsm_dev,
+					     struct pci_dev *pdev)
+{
+	int rc;
+
+	struct pci_tsm *pci_tsm __free(kfree) = kzalloc_obj(*pci_tsm);
+	if (!pci_tsm)
Quoted 8 lines

drivers/virt/coco/tdx-host/tdx-host.c static const struct x86_cpu_id tdx_host_ids[] = { · line 83

Quoted 8 lines
drivers/virt/coco/tdx-host/tdx-host.c +8 -0
+		return NULL;
+
+	rc = pci_tsm_link_constructor(pdev, pci_tsm, tsm_dev);
+	if (rc)
+		return NULL;
+
+	return no_free_ptr(pci_tsm);
+}

Thanks,
baolu

author maintainer
#3 lore raw

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

In reply to Binbin Wu
Quoted 10 lines · Binbin Wu

On 11/12/2026 12:30 PM, Lu Baolu wrote:
[...]

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

Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -0
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs. */

Nit:
It's a bit confusing without the all the background.
Once Rick's patch [1] removes tdx_clflush_page() and its comment, a reader may wonder why this wrapper explains the cache flush but the others don't.

Quoted 3 lines

Is it better to drop the comment and explain it in the change log?

[1]https://lore.kernel.org/all/20261015191710.3632302-2- [email protected]/

Agreed and updated.

Thanks,
baolu

author maintainer
#12 lore raw

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

In reply to Binbin Wu
Quoted 7 lines · Binbin Wu
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.

Nit:
The feature enumerated is TDX Connect, I don't think they are interchangeable. Trusted IOMMU is part of TDX Connect, right?

Yes. I will update it like this:

Add support for reading the global metadata of the trusted IOMMU, which is part of TDX Connect.

TDX Connect support 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.

Thanks,
baolu

author maintainer
#39 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Binbin Wu
Quoted 29 lines · Binbin Wu
Quoted 25 lines · Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

[...]

Quoted 1 line
Quoted 13 lines

drivers/virt/coco/tdx-host/tdx-host.c static int seamldr_init(struct device *dev) · line 323

Quoted 10 lines
  static int tdx_host_probe(struct faux_device *fdev)
  {
drivers/virt/coco/tdx-host/tdx-host.c +7 -1
-	return seamldr_init(&fdev->dev);
+	int ret;
+
+	ret = seamldr_init(&fdev->dev);
+	if (ret)
+		return ret;
+
+	return tdx_tdisp_init(&fdev->dev);

If tdx_tdisp_init() fails, it will also disable TDX module update and the version sysfs attributes. If this is intended, could you add some description in the change log about it?

You are right. That side effect is unnecessary. Module update and the version attributes don't depend on TDISP. How about fixing this by making the TDISP failure non-fatal so tdx-host still provides module update?

Something like:

-static int tdx_tdisp_init(struct device *dev)
+static void tdx_tdisp_init(struct device *dev)

 {
     const struct tdx_sys_info *sysinfo = tdx_get_sysinfo();
     struct tsm_dev *link;

     if (!sysinfo || !tdx_supports_tdisp(sysinfo))

- return 0;
+ return;

     link = tsm_register(dev, &tdx_tsm_link_ops);

- if (IS_ERR(link))
- return dev_err_probe(dev, PTR_ERR(link),
- "failed to register TSM\n");
+ if (IS_ERR(link)) {
+ dev_warn(dev, "failed to register TSM: %pe\n", link);
+ return;
+ }

- return devm_add_action_or_reset(dev, unregister_link_tsm, link);
+ if (devm_add_action_or_reset(dev, unregister_link_tsm, link))
+ dev_warn(dev, "failed to register TSM cleanup\n");
}
@@
static int tdx_host_probe(struct faux_device *fdev)

 {
     int ret;

     ret = seamldr_init(&fdev->dev);
     if (ret)
         return ret;

- return tdx_tdisp_init(&fdev->dev);
+ tdx_tdisp_init(&fdev->dev);
+
+ return 0;
}

Thanks,
baolu

author maintainer
#44 lore raw

Re: [PATCH v3 9/9] coco/tdx-host: Enable trusted DMA on IOMMUs on device connect

In reply to Chao Gao
Quoted 42 lines · Chao Gao
Quoted 32 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. They become usable once TDH.IOMMU.SETUP puts the IOMMU into Secure TDX Mode and the TDX module initializes them. The host keeps the regular translation path.

Issue TDH.IOMMU.SETUP on demand, when the first device behind an IOMMU is connected through the TSM, rather than for every capable IOMMU at probe time. Secure TDX Mode costs host resources: the TDX module's metadata pages and half of the IOMMU's domain ID space. That cost should only be paid for IOMMUs that actually serve such devices.

tdx_iommu_get() and tdx_iommu_put() keep a per-IOMMU refcount. The first get() issues TDH.IOMMU.SETUP and the last put() issues TDH.IOMMU.CLEAR. A single mutex covers both the refcount and the SEAMCALL that follows, so a device cannot join an IOMMU that is being cleared or whose setup has not finished. The refcount lives here rather than in the VT-d driver because tdx-host already owns the per-IOMMU state that must outlive TDH.IOMMU.SETUP: the IOMMU_MT pages and the TDX IOMMU ID.

The IOMMU_MT parameter page lists two contiguous buffers for the trusted invalidation queue, followed by the metadata pages. The TDX module reports how many metadata pages it needs in its global metadata.

With Dynamic PAMT, every 4K page given to the TDX module needs PAMT backing first. Take it for each page of the invalidation queue buffers and the metadata pages before TDH.IOMMU.SETUP, and drop it if setup fails or once TDH.IOMMU.CLEAR succeeds. The parameter page itself only carries the page list into the SEAMCALL and needs no backing.

Quoted 8 lines

If TDH.IOMMU.CLEAR fails, the TDX module may still reference the IOMMU_MT pages, so they are leaked together with their PAMT backing, and the IOMMU is marked unusable. Later get() calls for that IOMMU fail with -EBUSY rather than reuse an IOMMU in an unknown state. tdx-host pins itself while such an IOMMU exists, so unloading can't lose track of it. If the failure happens during unload itself, only the tracking entry is freed; the VT-d driver still has the IOMMU acquired and keeps rejecting it.

Quoted 4 lines

If the pin can fail anyway, what's the point of taking it at all?

If tdx-host is reloaded, we can rely on the VT-d driver or the TDX module to reject transitioning the IOMMU to secure mode.

You are right. VT-d already refuses to reuse an IOMMU whose clear failed, even after tdx-host is reloaded. I will drop this.

Quoted 31 lines
Quoted 17 lines

---

drivers/virt/coco/tdx-host/Kconfig   |   5 +
drivers/virt/coco/tdx-host/Makefile  |   1 +
drivers/virt/coco/tdx-host/private.h |  21 ++
drivers/virt/coco/tdx-host/main.c    |  11 +
drivers/virt/coco/tdx-host/viommu.c  | 401 +++++++++++++++++++++++++++
5 files changed, 439 insertions(+)

create mode 100644 drivers/virt/coco/tdx-host/private.h create mode 100644 drivers/virt/coco/tdx-host/viommu.c

drivers/virt/coco/tdx-host/Kconfig +0 -0
index f8c8cc29d46c..1395929f05bb 100644

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

Quoted 8 lines
drivers/virt/coco/tdx-host/Kconfig +5 -0
@@ -6,3 +6,8 @@ config TDX_HOST_SERVICES
 	select FW_UPLOAD
 	select PCI_TSM
 	default m
+
+config TDX_CONNECT
+	def_bool y
+	depends on TDX_HOST_SERVICES
+	depends on INTEL_IOMMU

Do we need this TDX_CONNECT config? We can just add "depends on INTEL_IOMMU" to TDX_HOST_SERVICES, and then drop the stub functions in private.h. Patch 7 already makes TDX_HOST_SERVICES depend on PCI in the same way.

Quoted 6 lines

The cost is that without INTEL_IOMMU, none of the tdx-host services are available, including TDX module updates. But I doubt anyone would want to launch CoCo VMs while compiling out INTEL_IOMMU entirely.

We can always add this option back if such a configuration turns out to be practical.

I'd like to accept this unless anyone has concerns about the downside.

Quoted 12 lines
Quoted 12 lines
drivers/virt/coco/tdx-host/Kconfig +11 -0
+/*
+ * Serializes the refcounts together with TDH.IOMMU.SETUP and
+ * TDH.IOMMU.CLEAR, so a device can't join an IOMMU that is being
+ * cleared, or one whose setup hasn't finished.
+ */
+static DEFINE_MUTEX(tdx_iommu_lock);
+static LIST_HEAD(tdx_iommu_list);
+
+static void *tdx_iommu_alloc_pages(int node, size_t size)
+{
+	struct folio *folio;
Quoted 8 lines

drivers/virt/coco/tdx-host/Kconfig

Quoted 5 lines
drivers/virt/coco/tdx-host/Kconfig +5 -0
+
+	if (node == NUMA_NO_NODE)
+		node = numa_mem_id();
+
+	folio = __folio_alloc_node(GFP_KERNEL | __GFP_ZERO, get_order(size), node);

Would kzalloc_node() work here? kzalloc_node() handles NUMA_NO_NODE itself, and there's no need to convert between folios and void *.

Yes. kzalloc_node() returns naturally aligned buffers for power-of-two sizes, which covers the 4K and 8K allocations here. Will switch to it in the next version.

Quoted 96 lines
Quoted 96 lines
drivers/virt/coco/tdx-host/Kconfig +95 -0
+	if (!folio)
+		return NULL;
+
+	return folio_address(folio);
+}
+
+static void tdx_iommu_free_pages(void *virt)
+{
+	if (!virt)
+		return;
+
+	folio_put(virt_to_folio(virt));
+}
+
+/* Number of 4K pages backing IOMMU_MT entry @i. */
+static unsigned int mt_entry_nr_pages(unsigned int i)
+{
+	return i < TDX_IOMMU_MT_IQ_NR ? IQ_BUFFER_SIZE >> PAGE_SHIFT : 1;
+}
+
+/*
+ * Drop the PAMT backing of the first @nr 4K pages handed to the TDX
+ * module, in the order mt_pamt_get() took it. Pass UINT_MAX for all.
+ */
+static void mt_pamt_put(struct tdx_iommu_pages *mt, unsigned int nr)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < mt->nr_entries && nr; i++) {
+		unsigned long pfn = PHYS_PFN(__pa(mt->pages[i]));
+
+		for (j = 0; j < mt_entry_nr_pages(i) && nr; j++, nr--)
+			tdx_pamt_put(pfn + j);
+	}
+}
+
+/*
+ * With Dynamic PAMT, every 4K page handed to the TDX module needs PAMT
+ * backing first. This covers the IQ buffers and the metadata pages, not
+ * the root page, which only carries the list of pages into the
+ * SEAMCALL. tdx_pamt_get() is a no-op without Dynamic PAMT.
+ */
+static int mt_pamt_get(struct tdx_iommu_pages *mt)
+{
+	unsigned int i, j, nr = 0;
+	int ret;
+
+	for (i = 0; i < mt->nr_entries; i++) {
+		unsigned long pfn = PHYS_PFN(__pa(mt->pages[i]));
+
+		for (j = 0; j < mt_entry_nr_pages(i); j++) {
+			ret = tdx_pamt_get(pfn + j, NULL);
+			if (ret) {
+				mt_pamt_put(mt, nr);
+				return ret;
+			}
+			nr++;
+		}
+	}
+
+	return 0;
+}
+
+static void free_mt_pages(struct tdx_iommu_pages *array)
+{
+	unsigned int i;
+
+	if (IS_ERR_OR_NULL(array))
+		return;
+
+	mt_pamt_put(array, UINT_MAX);
+
+	for (i = 0; i < array->nr_entries; i++)
+		tdx_iommu_free_pages(array->pages[i]);
+
+	tdx_iommu_free_pages(array->root);
+	kfree(array->pages);
+	kfree(array);
+}
+
+DEFINE_FREE(free_mt_pages, struct tdx_iommu_pages *, free_mt_pages(_T))
+
+static void **alloc_mt_pages(unsigned int nr_entries, int node)
+{
+	unsigned int i;
+	void **pages;
+	void *vaddr;
+
+	pages = kzalloc_objs(*pages, nr_entries);
+	if (!pages)
+		return NULL;
+
+	/* Allocate two contiguous buffers for the invalidation queue. */
+	pages[0] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
+	if (!pages[0])
Quoted 8 lines

drivers/virt/coco/tdx-host/Kconfig

Quoted 5 lines
drivers/virt/coco/tdx-host/Kconfig +5 -0
+		goto free_pages;
+
+	pages[1] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
+	if (!pages[1])
+		goto free_pages;

You can use mt_entry_nr_pages() to fold the two IQ buffer allocations into the loop below.

Good point. I'll fold them into a single loop, using mt_entry_nr_pages() for the size.

    for (i = 0; i < nr_entries; i++) {
        pages[i] = kzalloc_node(mt_entry_nr_pages(i) << PAGE_SHIFT,
                    GFP_KERNEL, node);
        if (!pages[i])
            goto free_pages;
    }
Quoted 11 lines
Quoted 11 lines
drivers/virt/coco/tdx-host/Kconfig +10 -0
+
+	/* Allocate the required number of pages for the IOMMU metadata. */
+	for (i = TDX_IOMMU_MT_IQ_NR; i < nr_entries; i++) {
+		vaddr = tdx_iommu_alloc_pages(node, SZ_4K);
+		if (!vaddr)
+			goto free_pages;
+		pages[i] = vaddr;
+	}
+
+	return pages;
Quoted 8 lines

drivers/virt/coco/tdx-host/Kconfig

Quoted 8 lines
drivers/virt/coco/tdx-host/Kconfig +8 -0
+
+free_pages:
+	for (i = 0; i < nr_entries; i++)
+		tdx_iommu_free_pages(pages[i]);
+	kfree(pages);
+
+	return NULL;
+}

Thanks,
baolu

#4 lore raw

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

In reply to Lu Baolu

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

Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +3 -0
index 4cce77b6a31f..a1f35188a272 100644
@@ -2567,3 +2567,56 @@ void tdx_sys_disable(void)
 	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
 		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
 }
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs. */
+u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,

param is too general, name it iommu_mt, is it better?

Quoted 11 lines
arch/x86/virt/vmx/tdx/tdx.c +11 -0
+		    u64 *tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(param),
+	};
+	u64 ret;
+
+	/*
+	 * Retry only on TDX_INTERRUPTED_RESUMABLE:
+	 *

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

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 *
+	 * The ABI spec also allows retrying on TDX_OPERAND_BUSY, but that
+	 * status doesn't guarantee forward progress, so retrying could spin
+	 * forever. The host serializes all TDH.IOMMU.SETUP and
+	 * TDH.IOMMU.CLEAR calls for an IOMMU, so the TDX module should never
+	 * report the operand as busy.

Many SEAMCALLs return TDX_OPERAND_BUSY, and this is the generally expected handling. I don't think we don't need the comment if nothing special.

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

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+	 */
+	do {
+		ret = seamcall_ret(TDH_IOMMU_SETUP, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	*tdx_iommu_id = args.rcx;

I remember there was an internal debate about whether to only assign the output upon success.

There are several examples in tdx.c that assign the output regardless. But all of them holds error information, which makes total sense to assign on error.

This rcx output however doesn't hold info on error. So I think early exit is the right choice. Also see tdh_phymem_pamt_remove()

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_setup, "tdx-host");
+
+u64 tdh_iommu_clear(u64 tdx_iommu_id)

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

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+{
+	struct tdx_module_args args = {
+		.rcx = tdx_iommu_id,
+	};
+	u64 ret;
+
+	/*
+	 * Don't retry on TDX_OPERAND_BUSY. See the comment in
+	 * tdh_iommu_setup().
+	 */

ditto

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +7 -0
+	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
#16 lore raw

Re: [PATCH v3 3/9] x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host

In reply to Lu Baolu
Quoted 8 lines · Lu Baolu

With Dynamic PAMT, any page handed to the TDX module needs PAMT backing first, via tdx_pamt_get(), and must drop it with tdx_pamt_put() once the TDX module gives the page back. Besides KVM, the tdx-host driver now hands pages to the TDX module: the IOMMU_MT pages for TDH.IOMMU.SETUP.

Export the two helpers to tdx-host as well. A symbol can only be exported once, so replace EXPORT_SYMBOL_FOR_KVM() with an explicit list. Only kvm-intel calls these helpers in KVM.

We are seeking for some simplified intial implementation without DPAMT for TDX huge page feature. So we could also drop DPAMT support for TDX Trusted I/O currently.

As an temporary solution, we can disable TDX Trusted I/O if DPAMT is on.

for example:

static inline bool tdx_supports_trusted_io(const struct tdx_sys_info *sysinfo)

{
	if (tdx_supports_dynamic_pamt(sysinfo))
		return false;

	...
}
#20 lore raw

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

In reply to Lu Baolu
Quoted 6 lines

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?

[...]

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2561

Quoted 10 lines
drivers/iommu/intel/dmar.c +10 -0
+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?

[...]

Quoted 35 lines
drivers/iommu/intel/dmar.c +35 -0
+/*
+ * 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.
+ *

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2656

Quoted 10 lines
drivers/iommu/intel/dmar.c +10 -0
+ * 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

Quoted 52 lines
drivers/iommu/intel/dmar.c +49 -0
+		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");
Signature · 1 line
2.43.0
#36 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Baolu Lu
Quoted 11 lines
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)
+{
+	if (!(sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return false;
+
+	return false; /* To be enabled when kernel is ready */
+}

Patch 2 open-codes the same TDX_FEATURES0_TDXCONNECT check. Can this helper be moved to patch 2 and used there?

Quoted 4 lines

These checks serve different purposes: the metadata patch checks whether the TDX module supports TDX Connect; tdx_supports_tdisp() decides whether the kernel is ready to register the link tsm. The latter may include additional checks, such as quirks, so I would like to keep the

But I don't see the need to distinguish them. Any case the kernel is not ready to handle TDX Trusted I/O, but must read its metadata?

Quoted 1 line

helper for the tsm registration.

#40 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Baolu Lu
Quoted 43 lines · Baolu Lu
Quoted 28 lines · Binbin Wu
Quoted 25 lines · Lu Baolu

Register a Link TSM instance to support host side TSM operations for TDISP, when the TDX Connect support bit is set by TDX Module in tdx_feature0.

This is the main purpose of an independent tdx-host module out of TDX core. Recall that a TEE Security Manager (TSM) is a platform agent that speaks the TEE Device Interface Security Protocol (TDISP) to PCIe devices and manages private memory resources for the platform. An independent tdx-host module allows for device-security enumeration and initialization flows to be deferred from other TDX Module initialization requirements. Crucially, when / if TDX Module init moves earlier in x86 initialization flow this driver is still guaranteed to run after IOMMU and PCI init (i.e. subsys_initcall() vs device_initcall()).

The ability to unload the module, or unbind the driver is also useful for debug and coarse grained transitioning between PCI TSM operation and PCI CMA operation (native kernel PCI device authentication).

For now only check for TDX Connect support in the TDX Module and register the Link TSM. The TSM support is basic boilerplate with operation flows to be added later.

Based on work by Yilun and Dan.

[...]

Quoted 13 lines

drivers/virt/coco/tdx-host/tdx-host.c static int seamldr_init(struct device *dev) · line 323

Quoted 10 lines
  static int tdx_host_probe(struct faux_device *fdev)
  {
drivers/virt/coco/tdx-host/tdx-host.c +7 -1
-	return seamldr_init(&fdev->dev);
+	int ret;
+
+	ret = seamldr_init(&fdev->dev);
+	if (ret)
+		return ret;
+
+	return tdx_tdisp_init(&fdev->dev);

If tdx_tdisp_init() fails, it will also disable TDX module update and the version sysfs attributes. If this is intended, could you add some description in the change log about it?

Quoted 4 lines

You are right. That side effect is unnecessary. Module update and the version attributes don't depend on TDISP. How about fixing this by making the TDISP failure non-fatal so tdx-host still provides module update?

Now we see all features are necessary by default, if any feature fails to enable then the TDX module is broken.

We can have exceptions but that need well justification. Currently I don't see the need to surpress the error.

Quoted 43 lines

Something like:

(quoted diff) +8 -6
-static int tdx_tdisp_init(struct device *dev)
+static void tdx_tdisp_init(struct device *dev)
 {
     const struct tdx_sys_info *sysinfo = tdx_get_sysinfo();
     struct tsm_dev *link;
 
     if (!sysinfo || !tdx_supports_tdisp(sysinfo))
-		return 0;
+		return;
 
     link = tsm_register(dev, &tdx_tsm_link_ops);
-	if (IS_ERR(link))
-		return dev_err_probe(dev, PTR_ERR(link),
-				     "failed to register TSM\n");
+	if (IS_ERR(link)) {
+		dev_warn(dev, "failed to register TSM: %pe\n", link);
+		return;
+	}
 
-	return devm_add_action_or_reset(dev, unregister_link_tsm, link);
+	if (devm_add_action_or_reset(dev, unregister_link_tsm, link))
+		dev_warn(dev, "failed to register TSM cleanup\n");
 }

@@
static int tdx_host_probe(struct faux_device *fdev)

 {
     int ret;

     ret = seamldr_init(&fdev->dev);
     if (ret)
         return ret;
(quoted diff) +3 -1
-	return tdx_tdisp_init(&fdev->dev);
+	tdx_tdisp_init(&fdev->dev);
+
+	return 0;
 }

Thanks,
baolu

#17 lore raw

Re: [PATCH v3 3/9] x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host

In reply to Xu Yilun
Quoted 18 lines · Xu Yilun
Quoted 8 lines · Lu Baolu

With Dynamic PAMT, any page handed to the TDX module needs PAMT backing first, via tdx_pamt_get(), and must drop it with tdx_pamt_put() once the TDX module gives the page back. Besides KVM, the tdx-host driver now hands pages to the TDX module: the IOMMU_MT pages for TDH.IOMMU.SETUP.

Export the two helpers to tdx-host as well. A symbol can only be exported once, so replace EXPORT_SYMBOL_FOR_KVM() with an explicit list. Only kvm-intel calls these helpers in KVM.

We are seeking for some simplified intial implementation without DPAMT for TDX huge page feature. So we could also drop DPAMT support for TDX Trusted I/O currently.

As an temporary solution, we can disable TDX Trusted I/O if DPAMT is on.

for example:

Quoted 7 lines

static inline bool tdx_supports_trusted_io(const struct tdx_sys_info *sysinfo)

{
	if (tdx_supports_dynamic_pamt(sysinfo))
		return false;

	...
}

Yes, let's try to make DPAMT support optional for the initial upstream TDX connect target. We can add support for it in follow on patches and keep the initial code sets smaller. In order to facilitate this, we need to add a kernel param to control enabling DPAMT. I will send a patch soon.

author maintainer
#5 lore raw

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

In reply to Xu Yilun
Quoted 5 lines · Xu Yilun
Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index 4cce77b6a31f..a1f35188a272 100644
@@ -2567,3 +2567,56 @@ void tdx_sys_disable(void)
Quoted 8 lines

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

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +3 -0
  	if (ret && (ret & TDX_SW_ERROR) != TDX_SW_ERROR)
  		pr_err("TDH.SYS.DISABLE failed: 0x%016llx\n", ret);
  }
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs. */
+u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,

param is too general, name it iommu_mt, is it better?

Agreed. In a function whose arguments are all parameters, @param doesn't say anything. I will rename param to iommu_mt.

Quoted 17 lines
Quoted 17 lines
arch/x86/virt/vmx/tdx/tdx.c +16 -0
+		    u64 *tdx_iommu_id)
+{
+	struct tdx_module_args args = {
+		.rcx = reg_base,
+		.rdx = __pa(param),
+	};
+	u64 ret;
+
+	/*
+	 * Retry only on TDX_INTERRUPTED_RESUMABLE:
+	 *
+	 *  - TDX_INTERRUPTED_RESUMABLE guarantees forward progress between
+	 *    calls.
+	 *  - On TDX_INTERRUPTED_RESUMABLE, all registers except RAX remain
+	 *    unchanged.
+	 *
Quoted 8 lines

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

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+	 * The ABI spec also allows retrying on TDX_OPERAND_BUSY, but that
+	 * status doesn't guarantee forward progress, so retrying could spin
+	 * forever. The host serializes all TDH.IOMMU.SETUP and
+	 * TDH.IOMMU.CLEAR calls for an IOMMU, so the TDX module should never
+	 * report the operand as busy.

Many SEAMCALLs return TDX_OPERAND_BUSY, and this is the generally expected handling. I don't think we don't need the comment if nothing special.

I'd like to keep a shorter note. It's the usual handling for TDX folks, but people outside TDX may read this path too.

Quoted 11 lines

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

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+	 */
+	do {
+		ret = seamcall_ret(TDH_IOMMU_SETUP, &args);
+	} while (ret == TDX_INTERRUPTED_RESUMABLE);
+
+	*tdx_iommu_id = args.rcx;

I remember there was an internal debate about whether to only assign the output upon success.

Quoted 3 lines

There are several examples in tdx.c that assign the output regardless. But all of them holds error information, which makes total sense to assign on error.

Oh, I overlooked this.

Quoted 3 lines

This rcx output however doesn't hold info on error. So I think early exit is the right choice. Also see tdh_phymem_pamt_remove()

Agreed. rcx carries nothing useful on failure. Will return early and only set tdx_iommu_id on success.

Quoted 19 lines
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(tdh_iommu_setup, "tdx-host");
+
+u64 tdh_iommu_clear(u64 tdx_iommu_id)

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

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+{
+	struct tdx_module_args args = {
+		.rcx = tdx_iommu_id,
+	};
+	u64 ret;
+
+	/*
+	 * Don't retry on TDX_OPERAND_BUSY. See the comment in
+	 * tdh_iommu_setup().
+	 */

ditto

Quoted 10 lines
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +7 -0
+	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

Thanks,
baolu

author maintainer
#18 lore raw

Re: [PATCH v3 3/9] x86/virt/tdx: Export Dynamic PAMT helpers to tdx-host

In reply to Edgecombe, Rick P
Quoted 24 lines · Rick P
Quoted 23 lines · Xu Yilun
Quoted 8 lines · Lu Baolu

With Dynamic PAMT, any page handed to the TDX module needs PAMT backing first, via tdx_pamt_get(), and must drop it with tdx_pamt_put() once the TDX module gives the page back. Besides KVM, the tdx-host driver now hands pages to the TDX module: the IOMMU_MT pages for TDH.IOMMU.SETUP.

Export the two helpers to tdx-host as well. A symbol can only be exported once, so replace EXPORT_SYMBOL_FOR_KVM() with an explicit list. Only kvm-intel calls these helpers in KVM.

We are seeking for some simplified intial implementation without DPAMT for TDX huge page feature. So we could also drop DPAMT support for TDX Trusted I/O currently.

As an temporary solution, we can disable TDX Trusted I/O if DPAMT is on.

for example:

static inline bool tdx_supports_trusted_io(const struct tdx_sys_info *sysinfo)

{
	if (tdx_supports_dynamic_pamt(sysinfo))
		return false;
Quoted 7 lines
Quoted 2 lines
	...
}

Yes, let's try to make DPAMT support optional for the initial upstream TDX connect target. We can add support for it in follow on patches and keep the initial code sets smaller. In order to facilitate this, we need to add a kernel param to control enabling DPAMT. I will send a patch soon.

Sounds good. I'll temperately drop the DPAMT support in the next version.

Thanks,
baolu

author maintainer
#21 lore raw

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

In reply to Xu Yilun
Quoted 9 lines · Xu Yilun
Quoted 6 lines

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?

I'm open to this. I used the dmar_tdxc_* prefix because the VT-d spec calls this feature "TDX Connect Support". Since these are interfaces for the tdx-host module, I'm fine with renaming them to dmar_tdx_*.

Quoted 57 lines

[...]

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2561

Quoted 10 lines
drivers/iommu/intel/dmar.c +10 -0
+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?

[...]

Quoted 39 lines
drivers/iommu/intel/dmar.c +39 -0
+/*
+ * 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)
Quoted 8 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2656

Quoted 6 lines
drivers/iommu/intel/dmar.c +6 -0
+{
+	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?

The drhd was there so the VT-d driver could check that @pdev and @params match. But you're right that the check only exists because the API takes both. I'll drop drhd from the params and make acquire()/release() take only the pci_dev, so there's nothing to mismatch.

Thanks,
baolu

#13 lore raw

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

In reply to Baolu Lu
Quoted 13 lines

From: Baolu Lu <[email protected]>
Sent: Monday, November 23, 2026 12:10 PM

Quoted 6 lines · Binbin Wu
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.

Nit:
The feature enumerated is TDX Connect, I don't think they are

interchangeable.

Quoted 1 line

Trusted IOMMU is part of TDX Connect, right?

Quoted 9 lines

Yes. I will update it like this:

Add support for reading the global metadata of the trusted IOMMU, which is part of TDX Connect.

TDX Connect support 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.

let's not introduce new terms here. TDX Connect is Intel's branding name for trusted I/O. Intel IOMMU introduces a new TDX mode to support trusted DMA for trusted I/O.

so here we just read metadata of the TDX Connect feature, which includes information to enable the TDX mode in Intel IOMMU, blah blah...

#6 lore raw

RE: [PATCH v3 1/9] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to Lu Baolu
Quoted 9 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to TDs. Each IOMMU has a trusted root-table address register and a trusted invalidation queue, both accessible only from SEAM. The existing untrusted DMA translation path of the IOMMU stays under host control.

Quoted 3 lines

Before trusted DMA can be used through an IOMMU, the TDX module must initialize these resources. TDH.IOMMU.SETUP does this: it puts the IOMMU into Secure TDX Mode, programs the trusted root table and the

s/Secure TDX Mode/TDX mode/, across the series

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

Quoted 6 lines

}

(quoted diff) +2 -0
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs.

*/

arch/x86/virt/vmx/tdx/tdx.c +2 -0
+u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,
+		    u64 *tdx_iommu_id)

No context why talking about cache flush here.

author maintainer
#37 lore raw

Re: [PATCH v3 7/9] coco/tdx-host: Add skeleton to support PCI link encryption

In reply to Xu Yilun
Quoted 11 lines · Xu Yilun
Quoted 11 lines
Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static inline bool tdx_supports_tdisp(const struct tdx_sys_info *sysinfo)
+{
+	if (!(sysinfo->features.tdx_features0 & TDX_FEATURES0_TDXCONNECT))
+		return false;
+
+	return false; /* To be enabled when kernel is ready */
+}

Patch 2 open-codes the same TDX_FEATURES0_TDXCONNECT check. Can this helper be moved to patch 2 and used there?

Quoted 7 lines
Quoted 4 lines

These checks serve different purposes: the metadata patch checks whether the TDX module supports TDX Connect; tdx_supports_tdisp() decides whether the kernel is ready to register the link tsm. The latter may include additional checks, such as quirks, so I would like to keep the

But I don't see the need to distinguish them. Any case the kernel is not ready to handle TDX Trusted I/O, but must read its metadata?

No such case. But I'd rather not have one helper gating both the TDX core and tdx-host. Reading the metadata is harmless, while whether to register the link tsm is a tdx-host-only policy. I'll move tdx_supports_tdisp() into tdx-host as a static helper.

Quoted 1 line
Quoted 1 line

helper for the tsm registration.

Thanks,
baolu

author maintainer
#7 lore raw

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

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

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to TDs. Each IOMMU has a trusted root-table address register and a trusted invalidation queue, both accessible only from SEAM. The existing untrusted DMA translation path of the IOMMU stays under host control.

Quoted 5 lines
Quoted 3 lines

Before trusted DMA can be used through an IOMMU, the TDX module must initialize these resources. TDH.IOMMU.SETUP does this: it puts the IOMMU into Secure TDX Mode, programs the trusted root table and the

s/Secure TDX Mode/TDX mode/, across the series

Secure TDX Mode is the term used in TDX Connect ABI specification.

Quoted 9 lines

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

Quoted 7 lines
  }
(quoted diff) +2 -0
+
+/* No cache flush is needed; hardware keeps caches coherent across KeyIDs.

*/

arch/x86/virt/vmx/tdx/tdx.c +2 -0
+u64 tdh_iommu_setup(u64 reg_base, struct tdx_iommu_mt_param *param,
+		    u64 *tdx_iommu_id)

No context why talking about cache flush here.

The kernel normally flushes the cache when handing pages over to hardware or firmware. That isn't needed here because the hardware keeps caches coherent across KeyIDs. The comment was meant to show that the missing flush is intentional.

Thanks,
baolu

author maintainer
#14 lore raw

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

In reply to Tian, Kevin
Quoted 23 lines · Kevin
Quoted 13 lines

From: Baolu Lu <[email protected]>
Sent: Monday, November 23, 2026 12:10 PM

Quoted 6 lines · Binbin Wu
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.

Nit:
The feature enumerated is TDX Connect, I don't think they are

interchangeable.

Quoted 1 line

Trusted IOMMU is part of TDX Connect, right?

Quoted 9 lines

Yes. I will update it like this:

Add support for reading the global metadata of the trusted IOMMU, which is part of TDX Connect.

TDX Connect support 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.

Quoted 6 lines

let's not introduce new terms here. TDX Connect is Intel's branding name for trusted I/O. Intel IOMMU introduces a new TDX mode to support trusted DMA for trusted I/O.

so here we just read metadata of the TDX Connect feature, which includes information to enable the TDX mode in Intel IOMMU, blah blah...

Okay, sure.

Thanks,
baolu

#8 lore raw

RE: [PATCH v3 1/9] x86/virt/tdx: Add SEAMCALL wrappers for IOMMU setup/clear

In reply to Baolu Lu
Quoted 13 lines

From: Baolu Lu <[email protected]>
Sent: Wednesday, November 25, 2026 10:47 AM

Quoted 9 lines · Kevin
Quoted 9 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to TDs. Each IOMMU has a trusted root-table address register and a trusted invalidation queue, both accessible only from SEAM. The existing untrusted DMA translation path of the IOMMU stays under host control.

Quoted 9 lines
Quoted 1 line
Quoted 1 line

Before trusted DMA can be used through an IOMMU, the TDX module

must

Quoted 4 lines
Quoted 2 lines

initialize these resources. TDH.IOMMU.SETUP does this: it puts the IOMMU into Secure TDX Mode, programs the trusted root table and the

s/Secure TDX Mode/TDX mode/, across the series

Secure TDX Mode is the term used in TDX Connect ABI specification.

Can you double confirm it? I can see two "Secure TDX Mode"s and one "TDX Mode" in the public spec. so it's inconsistent.

Then from the internal ABI spec, still two "Secure TDX Mode"s but a lot of more "TDX mode"s...

#23 lore raw

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

In reply to Lu Baolu
Quoted 37 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

An Intel IOMMU that supports trusted DMA (ECAP.TDXCS) provides a second set of DMA translation resources, managed by the TDX module and used only for devices assigned to TDs. Before they can be used, the tdx-host driver issues TDH.IOMMU.SETUP to put the IOMMU into Secure TDX Mode, and
issues TDH.IOMMU.CLEAR to take it out again.

The VT-d driver owns the hardware state these SEAMCALLs depend on, so it checks that state and keeps its own use of the IOMMU out of the TDX module's way. This is cooperation, not enforcement: the TDX module protects trusted DMA regardless, and a misbehaving host can only cause failures.

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 and returns its register base address, which is how the TDX module identifies an IOMMU, and its NUMA node, so that tdx-host can allocate the TDX module's metadata pages local to that IOMMU.

- dmar_tdxc_acquire() is called right before TDH.IOMMU.SETUP. It checks the preconditions TDH.IOMMU.SETUP requires (DMA translation, queued invalidation and scalable mode enabled) and confines the host to the lower half of the domain ID space, since the TDX module owns the half with the MSB set.

- dmar_tdxc_release() undoes acquire(). tdx-host calls it if TDH.IOMMU.SETUP fails, or after TDH.IOMMU.CLEAR succeeds for the last user. If TDH.IOMMU.CLEAR fails, tdx-host doesn't call it, so the upper half of the domain ID space stays reserved for the TDX module.

Quoted 6 lines

DMAR units advertising ECAP.TDXCS are excluded from DMAR hotplug. No platform currently supports both trusted DMA and DMAR hotplug, so that flow can't be validated on hardware. Hot-removing such a unit is also unsafe, because tdx-host may hold a reference to it and the TDX module may own its trusted DMA resources. This also keeps the unit handle returned by dmar_tdxc_get_params() valid for the lifetime of the system.

Here we used multiple terms (Intel IOMMU, VT-d, DMAR) to talk about a same thing. Intel IOMMU and VT-d are interchangeable, commonly used in many places. But DMAR is more a term in ACPI structure. Better avoid it in description, especially in exported interface names.

drivers/iommu/intel/iommu.h struct intel_iommu { · line 695

Quoted 7 lines
drivers/iommu/intel/iommu.h +2 -0
@@ -689,6 +690,8 @@ 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. */
+	bool		tdxc_acquired;

"for the TDX module" or "for the TDX mode"?

s/tdxc_acquired/tdx_acquired/

include/linux/dmar.h struct dmar_drhd_unit { · line 50

Quoted 5 lines
include/linux/dmar.h +1 -0
@@ -47,9 +47,32 @@ struct dmar_drhd_unit {
 	u8	ignored:1; 		/* ignore drhd		*/
 	u8	include_all:1;
 	u8	gfx_dedicated:1;	/* graphic dedicated	*/
+	struct mutex tdxc_lock;		/* serializes acquire/release */

s/tdxc_lock/tdx_lock/

Quoted 5 lines
include/linux/dmar.h +2 -0
 	struct intel_iommu *iommu;
 };
 
+/*
+ * struct dmar_tdxc_params - IOMMU information for driving
Quoted 8 lines

TDH.IOMMU.*
+ * @reg_base_addr: Register base address of the IOMMU. The TDX module
+ * identifies an IOMMU by it.
+ * @node: NUMA node of the IOMMU, for allocating the TDX module's
+ * metadata pages local to it.
+ * @drhd: VT-d internal handle; the caller must not dereference it. It
+ * stays valid for the lifetime of the system because TDX
+ * Connect-capable units are never hot-removed.

what will happen if the caller deferences it?

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2550

Quoted 4 lines
drivers/iommu/intel/dmar.c +0 -0
@@ -2518,3 +2548,158 @@ bool dmar_platform_optin(void)
 	return ret;
 }
 EXPORT_SYMBOL_GPL(dmar_platform_optin);

code starting here better fits in iommu.c?

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2559

Quoted 9 lines
drivers/iommu/intel/dmar.c +9 -0
+
+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.

hmm now the 4th term for IOMMU...

Quoted 8 lines
drivers/iommu/intel/dmar.c +7 -0
+/*
+ * 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

why defining the caller behavior here? what would happen if the caller just calls acquire()/release() w/o doing any SEAMCALL in between?

Quoted 2 lines

+ * dmar_tdxc_release() if it fails. The caller owns the per-IOMMU refcount
+ * and must serialize all calls for the same IOMMU.

this function already serializes internally. What is the rationale asking for extra serialization in the caller?

Quoted 7 lines

+ *
+ * Return: 0 on success and error code on failure.
+ */

drivers/iommu/intel/dmar.c +4 -0
+int dmar_tdxc_acquire(struct pci_dev *pdev,
+		      const struct dmar_tdxc_params *params)
+{
+	struct dmar_drhd_unit *drhd = params->drhd;

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2662

Quoted 10 lines
drivers/iommu/intel/dmar.c +10 -0
+	struct intel_iommu *iommu = drhd->iommu;
+	int ret;
+
+	if (WARN_ON(dmar_tdxc_find_drhd(pdev) != drhd))
+		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;

what does "tdx-host was reloaded" actually mean?

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2666

Quoted 5 lines
drivers/iommu/intel/dmar.c +3 -0
+
+	/*
+	 * TDH.IOMMU.SETUP requires DMA translation, queued invalidation

and

drivers/iommu/intel/dmar.c +1 -0
+	 * scalable mode to be enabled. Catch these here so the error is

Remove the 1st sentence as it is explained by the code.

Quoted 6 lines
drivers/iommu/intel/dmar.c +3 -0
+	 * -EINVAL rather than an opaque TDX_IOMMU_INVALID_STATE.
+	 */
+	if (!(iommu->gcmd & DMA_GCMD_TE) || !iommu->qi

|| !sm_supported(iommu))

(quoted diff) +2 -0
+		return -EINVAL;
+
#22 lore raw

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

In reply to Baolu Lu
Quoted 10 lines

From: Baolu Lu <[email protected]>
Sent: Tuesday, November 24, 2026 3:47 PM

Quoted 2 lines · Xu Yilun
Quoted 2 lines

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

Quoted 3 lines
Quoted 3 lines

devices use each IOMMU and calls into the VT-d driver only for the first and the last of them:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- 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?

I'm open to this. I used the dmar_tdxc_* prefix because the VT-d spec calls this feature "TDX Connect Support". Since these are interfaces for the tdx-host module, I'm fine with renaming them to dmar_tdx_*.

intel_iommu_tdx_get_params()
intel_iommu_tdx_acquire()
intel_iommu_tdx_release()

?

#28 lore raw

RE: [PATCH v3 5/9] iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module

In reply to Lu Baolu
Quoted 6 lines

From: Lu Baolu <[email protected]>
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?

Quoted 4 lines

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?

Quoted 6 lines

-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.

Quoted 5 lines

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.

Quoted 37 lines

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.

---
 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(-)
drivers/iommu/intel/iommu.h +2 -1
index 18fef91753a0..26135248675d 100644
@@ -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

drivers/iommu/intel/iommu.h struct intel_iommu { · line 697

Quoted 3 lines
drivers/iommu/intel/iommu.h +2 -0
+	 * 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.

Quoted 3 lines

-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.

drivers/iommu/intel/iommu.h

Quoted 11 lines

{

drivers/iommu/intel/iommu.h +6 -2
-	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))

drivers/iommu/intel/iommu.h +1 -0
+		ret = -EBUSY;

here a comment is appreciated, for both why a check on ecmd is required but not for gcmd.

drivers/iommu/intel/iommu.h int dmar_tdxc_acquire(struct pci_dev *pdev, · line 2703

Quoted 10 lines
drivers/iommu/intel/iommu.h +4 -0
@@ -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.

drivers/iommu/intel/iommu.h int ecmd_submit_sync(struct intel_iommu · line 4358

Quoted 7 lines
drivers/iommu/intel/iommu.h +2 -0
@@ -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

drivers/iommu/intel/iommu.h

Quoted 9 lines

atomic

drivers/iommu/intel/iommu.h +8 -0
+	 * 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.

#31 lore raw

RE: [PATCH v3 6/9] iommu/vt-d: Refuse system suspend while acquired by the TDX module

In reply to Lu Baolu
Quoted 18 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

While tdx-host has acquired an IOMMU, the VT-d driver blocks its GCMD writes. TDX disables S3 by clearing acpi_suspend_lowlevel, but that is only checked after the syscore suspend callbacks have run, so an S3 request, e.g. "echo mem > /sys/power/state", still runs iommu_suspend() and then iommu_resume():

suspend_enter()
syscore_suspend()
iommu_suspend() <- runs on the acquired IOMMU
suspend_ops->enter() = acpi_suspend_enter()
if (!acpi_suspend_lowlevel) <- TDX's "S3 disabled" check
return -ENOSYS
syscore_resume()
iommu_resume() <- runs on the acquired IOMMU

Quoted 5 lines

Register a PM notifier for each DMAR unit that supports trusted DMA and refuse PM_SUSPEND_PREPARE with -EBUSY while the unit is acquired. The suspend then fails before tasks are frozen and before syscore callbacks run.

what about directly returning error in iommu_suspend() if any acquired IOMMU exists? at this point all other processes are frozen so no contention exists at this point.

or introduce a new flag or counter (intel_iommu_suspend_disabled) which is set/cleared or incremented/decremented along with acquire/release, then just check this flag/counter in iommu_suspend().

author maintainer
#9 lore raw

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

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

From: Baolu Lu<[email protected]>
Sent: Wednesday, November 25, 2026 10:47 AM

Quoted 10 lines · Kevin
Quoted 10 lines

From: Lu Baolu<[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

Intel VT-d hardware gains a second set of DMA translation structures, managed by the TDX module and used only for devices assigned to TDs. Each IOMMU has a trusted root-table address register and a trusted invalidation queue, both accessible only from SEAM. The existing untrusted DMA translation path of the IOMMU stays under host control.

Before trusted DMA can be used through an IOMMU, the TDX module

must

Quoted 2 lines
Quoted 2 lines

initialize these resources. TDH.IOMMU.SETUP does this: it puts the IOMMU into Secure TDX Mode, programs the trusted root table and the

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

s/Secure TDX Mode/TDX mode/, across the series

Secure TDX Mode is the term used in TDX Connect ABI specification.

Can you double confirm it? I can see two "Secure TDX Mode"s and one "TDX Mode" in the public spec. so it's inconsistent.

Then from the internal ABI spec, still two "Secure TDX Mode"s but a lot of more "TDX mode"s...

You're right, the spec isn't consistent, and "TDX mode" is clear enough. I'll use "TDX mode" across the series.

Thanks,
baolu

author maintainer
#32 lore raw

Re: [PATCH v3 6/9] iommu/vt-d: Refuse system suspend while acquired by the TDX module

In reply to Tian, Kevin
Quoted 24 lines · Kevin
Quoted 18 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

While tdx-host has acquired an IOMMU, the VT-d driver blocks its GCMD writes. TDX disables S3 by clearing acpi_suspend_lowlevel, but that is only checked after the syscore suspend callbacks have run, so an S3 request, e.g. "echo mem > /sys/power/state", still runs iommu_suspend() and then iommu_resume():

suspend_enter()
syscore_suspend()
iommu_suspend() <- runs on the acquired IOMMU
suspend_ops->enter() = acpi_suspend_enter()
if (!acpi_suspend_lowlevel) <- TDX's "S3 disabled" check
return -ENOSYS
syscore_resume()
iommu_resume() <- runs on the acquired IOMMU

Quoted 5 lines

Register a PM notifier for each DMAR unit that supports trusted DMA and refuse PM_SUSPEND_PREPARE with -EBUSY while the unit is acquired. The suspend then fails before tasks are frozen and before syscore callbacks run.

Quoted 7 lines

what about directly returning error in iommu_suspend() if any acquired IOMMU exists? at this point all other processes are frozen so no contention exists at this point.

or introduce a new flag or counter (intel_iommu_suspend_disabled) which is set/cleared or incremented/decremented along with acquire/release, then just check this flag/counter in iommu_suspend().

I'm fine with this. It's actually what I did at first. The only downside is that iommu_suspend() runs very late. By then, every device has already been suspended, and suspending a device that is assigned to a TD may affect its TDISP state. That shouldn't be the IOMMU driver's concern anyway, so let's start with the simpler approach.

Thanks,
baolu

author maintainer
#24 lore raw

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

In reply to Tian, Kevin
Quoted 44 lines · Kevin
Quoted 37 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

An Intel IOMMU that supports trusted DMA (ECAP.TDXCS) provides a second set of DMA translation resources, managed by the TDX module and used only for devices assigned to TDs. Before they can be used, the tdx-host driver issues TDH.IOMMU.SETUP to put the IOMMU into Secure TDX Mode, and
issues TDH.IOMMU.CLEAR to take it out again.

The VT-d driver owns the hardware state these SEAMCALLs depend on, so it checks that state and keeps its own use of the IOMMU out of the TDX module's way. This is cooperation, not enforcement: the TDX module protects trusted DMA regardless, and a misbehaving host can only cause failures.

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 and returns its register base address, which is how the TDX module identifies an IOMMU, and its NUMA node, so that tdx-host can allocate the TDX module's metadata pages local to that IOMMU.

- dmar_tdxc_acquire() is called right before TDH.IOMMU.SETUP. It checks the preconditions TDH.IOMMU.SETUP requires (DMA translation, queued invalidation and scalable mode enabled) and confines the host to the lower half of the domain ID space, since the TDX module owns the half with the MSB set.

- dmar_tdxc_release() undoes acquire(). tdx-host calls it if TDH.IOMMU.SETUP fails, or after TDH.IOMMU.CLEAR succeeds for the last user. If TDH.IOMMU.CLEAR fails, tdx-host doesn't call it, so the upper half of the domain ID space stays reserved for the TDX module.

Quoted 6 lines

DMAR units advertising ECAP.TDXCS are excluded from DMAR hotplug. No platform currently supports both trusted DMA and DMAR hotplug, so that flow can't be validated on hardware. Hot-removing such a unit is also unsafe, because tdx-host may hold a reference to it and the TDX module may own its trusted DMA resources. This also keeps the unit handle returned by dmar_tdxc_get_params() valid for the lifetime of the system.

Quoted 4 lines

Here we used multiple terms (Intel IOMMU, VT-d, DMAR) to talk about a same thing. Intel IOMMU and VT-d are interchangeable, commonly used in many places. But DMAR is more a term in ACPI structure. Better avoid it in description, especially in exported interface names.

Intel IOMMU and VT-d refer to the hardware that the intel-iommu driver drives. DMAR is the ACPI DMA remapping table, and the term is sometimes used for the remapping hardware units it describes.

The interface here is not an intel-iommu driver interface, since those should all go through the iommu core. It's exported from the ACPI/DMAR layer instead, so dmar_tdx_* seems the more fitting name.

Quoted 10 lines

drivers/iommu/intel/iommu.h struct intel_iommu { · line 695

Quoted 8 lines
drivers/iommu/intel/iommu.h +2 -0
@@ -689,6 +690,8 @@ 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. */
+	bool		tdxc_acquired;

"for the TDX module" or "for the TDX mode"?

The TDX mode. Will fix it.

Quoted 2 lines

s/tdxc_acquired/tdx_acquired/

Sure.

Quoted 8 lines

include/linux/dmar.h struct dmar_drhd_unit { · line 50

Quoted 6 lines
include/linux/dmar.h +1 -0
@@ -47,9 +47,32 @@ struct dmar_drhd_unit {
  	u8	ignored:1; 		/* ignore drhd		*/
  	u8	include_all:1;
  	u8	gfx_dedicated:1;	/* graphic dedicated	*/
+	struct mutex tdxc_lock;		/* serializes acquire/release */

s/tdxc_lock/tdx_lock/

Sure.

Quoted 15 lines
Quoted 6 lines
include/linux/dmar.h +2 -0
  	struct intel_iommu *iommu;
  };
 
+/*
+ * struct dmar_tdxc_params - IOMMU information for driving
Quoted 8 lines

TDH.IOMMU.*
+ * @reg_base_addr: Register base address of the IOMMU. The TDX module
+ * identifies an IOMMU by it.
+ * @node: NUMA node of the IOMMU, for allocating the TDX module's
+ * metadata pages local to it.
+ * @drhd: VT-d internal handle; the caller must not dereference it. It
+ * stays valid for the lifetime of the system because TDX
+ * Connect-capable units are never hot-removed.

Quoted 1 line

what will happen if the caller deferences it?

This will be dropped as discussed.

Quoted 7 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2550

Quoted 5 lines
drivers/iommu/intel/dmar.c +0 -0
@@ -2518,3 +2548,158 @@ bool dmar_platform_optin(void)
  	return ret;
  }
  EXPORT_SYMBOL_GPL(dmar_platform_optin);

code starting here better fits in iommu.c?

See above.

Quoted 12 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2559

Quoted 10 lines
drivers/iommu/intel/dmar.c +9 -0
+
+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.

hmm now the 4th term for IOMMU...

This comment is gone as discussed.

Quoted 10 lines
Quoted 9 lines
drivers/iommu/intel/dmar.c +7 -0
+/*
+ * 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

Quoted 2 lines

why defining the caller behavior here? what would happen if the caller just calls acquire()/release() w/o doing any SEAMCALL in between?

Nothing breaks. release() just undoes what acquire() did. You're right that the interface shouldn't define the caller's behavior.

Quoted 6 lines
Quoted 3 lines

+ * dmar_tdxc_release() if it fails. The caller owns the per-IOMMU refcount
+ * and must serialize all calls for the same IOMMU.

this function already serializes internally. What is the rationale asking for extra serialization in the caller?

None, it isn't needed. I'll describe what acquire() and release() do to the IOMMU, and drop the requirements on the caller.

Quoted 12 lines
Quoted 12 lines

+ *
+ * Return: 0 on success and error code on failure.
+ */

drivers/iommu/intel/dmar.c +8 -0
+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))
Quoted 8 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2662

Quoted 6 lines
drivers/iommu/intel/dmar.c +6 -0
+		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;

what does "tdx-host was reloaded" actually mean?

It's stale. After a failed TDH.IOMMU.CLEAR, the flag stays set and the next acquire() fails, whether or not tdx-host is reloaded. I'll reword it to:

	/* Still set if a previous TDH.IOMMU.CLEAR failed. */
Quoted 8 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2666

Quoted 6 lines
drivers/iommu/intel/dmar.c +3 -0
+
+	/*
+	 * TDH.IOMMU.SETUP requires DMA translation, queued invalidation

and

drivers/iommu/intel/dmar.c +1 -0
+	 * scalable mode to be enabled. Catch these here so the error is

Remove the 1st sentence as it is explained by the code.

Will do.

Quoted 6 lines

Review

Quoted 6 lines
drivers/iommu/intel/dmar.c +3 -0
+	 * -EINVAL rather than an opaque TDX_IOMMU_INVALID_STATE.
+	 */
+	if (!(iommu->gcmd & DMA_GCMD_TE) || !iommu->qi

|| !sm_supported(iommu))

(quoted diff) +2 -0
+		return -EINVAL;
+

Thanks,
baolu

author maintainer
#29 lore raw

Re: [PATCH v3 5/9] iommu/vt-d: Block GCMD and ECMD while acquired by the TDX module

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

From: Lu Baolu <[email protected]>
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?

Yes, only the state controlled through GCMD and ECMD interfaces. Once the IOMMU is in TDX mode, the hardware ignores host writes to GCMD and ECMD, so the driver would wait for a status update that never comes.

Quoted 7 lines
Quoted 5 lines

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?

GCMD and ECMD interfaces.

Quoted 9 lines
Quoted 7 lines

-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.

This is stale. They are actually blocked the same way.

ecmd_submit_sync() also holds register_lock until the command completes, and IOMMU_WAIT_OP() panics rather than returning on timeout. So no enhanced command can be in progress once acquire() takes the lock.

The only difference is whether a writer can be reached. GCMD is only accessed at init, hotplug, suspend and shutdown, so they get WARN_ON_ONCE(). ECMD can be reached at runtime through perf, which should be handled specially (see below).

Quoted 7 lines
Quoted 5 lines

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.

Yes. CAP.RWBF is only set on early VT-d hardware. It doesn't apply to a scalable-mode IOMMU hardware that supports TDX Connect. I am not sure about a virtualized VT-d. So a WARN_ON check is still needed.

Quoted 44 lines
Quoted 38 lines

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.

---
  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(-)
drivers/iommu/intel/iommu.h +2 -1
index 18fef91753a0..26135248675d 100644
@@ -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

drivers/iommu/intel/iommu.h

Quoted 3 lines
drivers/iommu/intel/iommu.h +2 -0
+	 * IOMMU. Protected by register_lock.
+	 */
  	bool		tdxc_acquired;

No need to update this comment. It's still for acquiring the tdx mode.

Quoted 1 line

explanation of restrictions in the tdx mode belongs to other places.

Agreed.

Quoted 7 lines
Quoted 4 lines

-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.

Agreed.

Quoted 15 lines

drivers/iommu/intel/iommu.h

Quoted 12 lines
  {
drivers/iommu/intel/iommu.h +6 -2
-	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))

drivers/iommu/intel/iommu.h +1 -0
+		ret = -EBUSY;

here a comment is appreciated, for both why a check on ecmd is required but not for gcmd.

Will fix this.

Quoted 6 lines
Quoted 6 lines
drivers/iommu/intel/iommu.h +0 -0
@@ -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;
 
Quoted 8 lines

drivers/iommu/intel/iommu.h int dmar_tdxc_acquire(struct pci_dev *pdev, · line 2703

Quoted 5 lines
drivers/iommu/intel/iommu.h +4 -0
+	/* 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.

Yes, sure.

Quoted 10 lines

drivers/iommu/intel/iommu.h int ecmd_submit_sync(struct intel_iommu · line 4358

Quoted 8 lines
drivers/iommu/intel/iommu.h +2 -0
@@ -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

Will fix this.

Quoted 14 lines

drivers/iommu/intel/iommu.h

Quoted 10 lines

atomic

drivers/iommu/intel/iommu.h +8 -0
+	 * 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.

Quoted 2 lines

we may need a way to disable iommu perf completely in the acquire function.

Good catch. Returning an error there leaves events that look like they're running but never count, or that can't be stopped.

I'll make perf events and TDX mode mutually exclusive instead:

- The iommu pmu counts its live events under register_lock.
- event_init() fails with -EBUSY while the IOMMU is acquired.
- acquire() fails with -EBUSY while any event exists.

Something like the following(compiled but not tested). Does this work for you?

drivers/iommu/intel/iommu.h +1 -0
index 1bbfb1b6f905..0945e63c901a 100644
@@ -674,6 +674,7 @@ struct iommu_pmu {
 	struct pmu		pmu;
 	DECLARE_BITMAP(used_mask, IOMMU_PMU_IDX_MAX);
 	struct perf_event	*event_list[IOMMU_PMU_IDX_MAX];
+	unsigned int		nr_events;	/* Protected by iommu->register_lock */
 	unsigned char		irq_name[16];
 };
 
drivers/iommu/intel/dmar.c +0 -0
index f0ac8c433198..1ae27198b999 100644
@@ -2588,10 +2588,15 @@ static void dmar_tdxc_unreserve_did(struct intel_iommu *iommu)
 }
 
 /* GCMD and ECMD writers complete under register_lock, so none is in 

flight. */
-static void dmar_tdxc_set_acquired(struct intel_iommu *iommu, bool acquired) +static int dmar_tdxc_set_acquired(struct intel_iommu *iommu, bool acquired)
{
guard(raw_spinlock_irqsave)(&iommu->register_lock);
+ /* Perf issues enhanced commands at runtime and can't handle failure. */
+ if (acquired && iommu->pmu && iommu->pmu->nr_events)
+ return -EBUSY;
+
iommu->tdx_acquired = acquired;
+ return 0;
}

static struct dmar_drhd_unit *dmar_tdxc_find_drhd(struct pci_dev *pdev)
@@ -2677,9 +2682,11 @@ int dmar_tdx_acquire(struct pci_dev *pdev)
if (ret)
return ret;

- dmar_tdxc_set_acquired(iommu, true);
+ ret = dmar_tdxc_set_acquired(iommu, true);
+ if (ret)
+ dmar_tdxc_unreserve_did(iommu);

- return 0;
+ return ret;
}
EXPORT_SYMBOL_FOR_MODULES(dmar_tdx_acquire, "tdx-host");

drivers/iommu/intel/iommu.c +2 -6
index 6dbb421d8099..c810b4776fd7 100644
@@ -4358,12 +4358,8 @@ 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 atomic
-	 * context (e.g. perf pmu::start/stop), so don't sleep; just reject
-	 * the request.
-	 */
-	if (iommu->tdx_acquired) {
+	/* Perf, the only caller, is kept off IOMMUs acquired for TDX mode. */
+	if (WARN_ON_ONCE(iommu->tdx_acquired)) {
 		ret = -EBUSY;
 		goto err;
 	}
drivers/iommu/intel/perfmon.c +24 -1
index e40ed253eaa9..e4df1aee4fcd 100644
@@ -276,9 +276,19 @@ static int iommu_pmu_validate_group(struct perf_event *event)
 	return 0;
 }
 
+static void iommu_pmu_event_destroy(struct perf_event *event)
+{
+	struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
+
+	guard(raw_spinlock_irqsave)(&iommu_pmu->iommu->register_lock);
+	iommu_pmu->nr_events--;
+}
+
 static int iommu_pmu_event_init(struct perf_event *event)
 {
 	struct hw_perf_event *hwc = &event->hw;
+	struct iommu_pmu *iommu_pmu;
+	int ret;
 
 	if (event->attr.type != event->pmu->type)
 		return -ENOENT;
@@ -295,7 +305,20 @@ static int iommu_pmu_event_init(struct perf_event *event)
 
 	hwc->config = iommu_event_config(event);
 
-	return iommu_pmu_validate_group(event);
+	ret = iommu_pmu_validate_group(event);
+	if (ret)
+		return ret;
+
+	iommu_pmu = iommu_event_to_pmu(event);
+	guard(raw_spinlock_irqsave)(&iommu_pmu->iommu->register_lock);
+	/* The enhanced commands that perf relies on are blocked in TDX mode. */
+	if (iommu_pmu->iommu->tdx_acquired)
+		return -EBUSY;
+
+	iommu_pmu->nr_events++;
+	event->destroy = iommu_pmu_event_destroy;
+
+	return 0;
 }
 
 static void iommu_pmu_event_update(struct perf_event *event)
Signature · 4 lines
2.53.0

Thanks,
baolu
#26 lore raw

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

In reply to a message not in the archive
Quoted 53 lines

From: Baolu Lu <[email protected]>
Sent: Wednesday, November 25, 2026 4:18 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, November 12, 2026 12:31 PM

An Intel IOMMU that supports trusted DMA (ECAP.TDXCS) provides a

second

Quoted 3 lines
Quoted 3 lines

set of DMA translation resources, managed by the TDX module and used only for devices assigned to TDs. Before they can be used, the tdx-host driver issues TDH.IOMMU.SETUP to put the IOMMU into Secure TDX

Mode,

Quoted 11 lines
Quoted 11 lines

and
issues TDH.IOMMU.CLEAR to take it out again.

The VT-d driver owns the hardware state these SEAMCALLs depend on, so it checks that state and keeps its own use of the IOMMU out of the TDX module's way. This is cooperation, not enforcement: the TDX module protects trusted DMA regardless, and a misbehaving host can only cause failures.

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

Quoted 10 lines
Quoted 10 lines

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 and returns its register base address, which is how the TDX module identifies an IOMMU, and its NUMA node, so that tdx-host can allocate the TDX module's metadata pages local to that IOMMU.

- dmar_tdxc_acquire() is called right before TDH.IOMMU.SETUP. It checks the preconditions TDH.IOMMU.SETUP requires (DMA translation,

queued

Quoted 6 lines
Quoted 6 lines

invalidation and scalable mode enabled) and confines the host to the lower half of the domain ID space, since the TDX module owns the half with the MSB set.

- dmar_tdxc_release() undoes acquire(). tdx-host calls it if TDH.IOMMU.SETUP fails, or after TDH.IOMMU.CLEAR succeeds for the

last

Quoted 10 lines
Quoted 9 lines

user. If TDH.IOMMU.CLEAR fails, tdx-host doesn't call it, so the upper half of the domain ID space stays reserved for the TDX module.

DMAR units advertising ECAP.TDXCS are excluded from DMAR hotplug. No platform currently supports both trusted DMA and DMAR hotplug, so that flow can't be validated on hardware. Hot-removing such a unit is also unsafe, because tdx-host may hold a reference to it and the TDX module may own its trusted DMA resources. This also keeps the unit handle returned by dmar_tdxc_get_params() valid for the lifetime of the system.

Quoted 8 lines
Quoted 4 lines

Here we used multiple terms (Intel IOMMU, VT-d, DMAR) to talk about a same thing. Intel IOMMU and VT-d are interchangeable, commonly used in many places. But DMAR is more a term in ACPI structure. Better avoid it in description, especially in exported interface names.

Intel IOMMU and VT-d refer to the hardware that the intel-iommu driver drives. DMAR is the ACPI DMA remapping table, and the term is sometimes used for the remapping hardware units it describes.

internally ok, but externally it just adds unnecessary confusion.

Quoted 4 lines

The interface here is not an intel-iommu driver interface, since those should all go through the iommu core. It's exported from the ACPI/DMAR layer instead, so dmar_tdx_* seems the more fitting name.

it's an interface which intel-iommu driver provides to the tsm driver.

it's exported from dmar layer because you chose to put it in dmar.c, but really the logic there is about the general iommu object itself at runtime.

dmar should be used mostly at boot time.

Quoted 7 lines
Quoted 6 lines

drivers/iommu/intel/dmar.c bool dmar_platform_optin(void) · line 2662

Quoted 4 lines
drivers/iommu/intel/dmar.c +4 -0
+	guard(mutex)(&drhd->tdxc_lock);
+	/* Still set if TDH.IOMMU.CLEAR failed and tdx-host was reloaded. */
+	if (iommu->tdxc_acquired)
+		return -EBUSY;

what does "tdx-host was reloaded" actually mean?

Quoted 5 lines

It's stale. After a failed TDH.IOMMU.CLEAR, the flag stays set and the next acquire() fails, whether or not tdx-host is reloaded. I'll reword it to:

	/* Still set if a previous TDH.IOMMU.CLEAR failed. */

I don't think it's necessary. what about the caller only invokes acquire() but forgets to do release()? We don't need to describe all possible reasons why it's left in that state. The code itself is clear enough.

#45 lore raw

Re: [PATCH v3 9/9] coco/tdx-host: Enable trusted DMA on IOMMUs on device connect

In reply to Lu Baolu

drivers/virt/coco/tdx-host/main.c static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm) · line 47

Quoted 10 lines

static int tdx_tsm_link_connect(struct pci_dev *pdev) {

drivers/virt/coco/tdx-host/main.c +8 -0
+	int ret;
+
+	ret = tdx_iommu_get(pdev);
+	if (ret)
+		return ret;
+
+	/* TDISP connect is not implemented yet. */
+	tdx_iommu_put(pdev);

In TDX, usually we prevent the on-going feature implementation from reachable by a stub and finally enable it when full support lands, like 1e3d79135327. So can we do like that?

Oh, we've already done this in Patch 7.

Quoted 2 lines

return -ENXIO;
}

[...]

drivers/virt/coco/tdx-host/viommu.c line 21

Quoted 3 lines
drivers/virt/coco/tdx-host/viommu.c new file +3 -0
+/* These describe the trusted invalidation queue buffers. */
+#define IQ_BUFFER_PAGES		2
+#define IQ_BUFFER_SIZE		SZ_8K

I don't think we have to define 2 instant values, how about?

  #define IQ_BUFFER_SIZE		IQ_BUFFER_PAGES * SZ_4K
Quoted 67 lines
drivers/virt/coco/tdx-host/viommu.c new file +67 -0
+
+/*
+ * 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 {
+	struct tdx_iommu_mt_param *root;
+	void **pages;
+	unsigned int nr_entries;
+};
+
+/**
+ * struct tdx_iommu - Host state of an IOMMU in Secure TDX Mode
+ * @node: Link in tdx_iommu_list
+ * @params: IOMMU information from the VT-d driver
+ * @tdx_iommu_id: Identifier returned by TDH.IOMMU.SETUP
+ * @mt_pages: Pages handed to the TDX module by TDH.IOMMU.SETUP
+ * @users: Number of devices using trusted DMA through this IOMMU
+ * @stuck: TDH.IOMMU.CLEAR failed. The TDX module may still own
+ *	   @mt_pages, so they are leaked and the IOMMU is not reused.
+ */
+struct tdx_iommu {
+	struct list_head node;
+	struct dmar_tdxc_params params;
+	u64 tdx_iommu_id;
+	struct tdx_iommu_pages *mt_pages;
+	unsigned int users;
+	bool stuck;
+};
+
+/*
+ * Serializes the refcounts together with TDH.IOMMU.SETUP and
+ * TDH.IOMMU.CLEAR, so a device can't join an IOMMU that is being
+ * cleared, or one whose setup hasn't finished.
+ */
+static DEFINE_MUTEX(tdx_iommu_lock);
+static LIST_HEAD(tdx_iommu_list);
+
+static void *tdx_iommu_alloc_pages(int node, size_t size)
+{
+	struct folio *folio;
+
+	if (node == NUMA_NO_NODE)
+		node = numa_mem_id();
+
+	folio = __folio_alloc_node(GFP_KERNEL | __GFP_ZERO, get_order(size), node);
+	if (!folio)
+		return NULL;
+
+	return folio_address(folio);
+}
+
+static void tdx_iommu_free_pages(void *virt)
+{
+	if (!virt)
+		return;
+
+	folio_put(virt_to_folio(virt));
+}
+
+/* Number of 4K pages backing IOMMU_MT entry @i. */
+static unsigned int mt_entry_nr_pages(unsigned int i)
+{
+	return i < TDX_IOMMU_MT_IQ_NR ? IQ_BUFFER_SIZE >> PAGE_SHIFT : 1;
+}
+

[...]

drivers/virt/coco/tdx-host/viommu.c line 144

Quoted 13 lines
drivers/virt/coco/tdx-host/viommu.c new file +13 -0
+static void free_mt_pages(struct tdx_iommu_pages *array)
+{
+	unsigned int i;
+
+	if (IS_ERR_OR_NULL(array))
+		return;
+
+	mt_pamt_put(array, UINT_MAX);
+
+	for (i = 0; i < array->nr_entries; i++)
+		tdx_iommu_free_pages(array->pages[i]);
+
+	tdx_iommu_free_pages(array->root);

The root page is just to hold ABI params, doesn't have to be kept until we exit TDX mode.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	kfree(array->pages);
+	kfree(array);

Maybe we don't need so many objects to hold mt page info, see below...

Quoted 20 lines
drivers/virt/coco/tdx-host/viommu.c new file +20 -0
+}
+
+DEFINE_FREE(free_mt_pages, struct tdx_iommu_pages *, free_mt_pages(_T))
+
+static void **alloc_mt_pages(unsigned int nr_entries, int node)
+{
+	unsigned int i;
+	void **pages;
+	void *vaddr;
+
+	pages = kzalloc_objs(*pages, nr_entries);
+	if (!pages)
+		return NULL;
+
+	/* Allocate two contiguous buffers for the invalidation queue. */
+	pages[0] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);
+	if (!pages[0])
+		goto free_pages;
+
+	pages[1] = tdx_iommu_alloc_pages(node, IQ_BUFFER_SIZE);

drivers/virt/coco/tdx-host/viommu.c line 176

Quoted 10 lines
drivers/virt/coco/tdx-host/viommu.c new file +10 -0
+	if (!pages[1])
+		goto free_pages;
+
+	/* Allocate the required number of pages for the IOMMU metadata. */
+	for (i = TDX_IOMMU_MT_IQ_NR; i < nr_entries; i++) {
+		vaddr = tdx_iommu_alloc_pages(node, SZ_4K);
+		if (!vaddr)
+			goto free_pages;
+		pages[i] = vaddr;
+	}

I'm wondering if we could make the page allocation/free/populate simpler, how about allocating a contiguous page bulk, like:

unsigned int nr_pages = sysinfo->tdx_connect.iommu_mt_page_count +

				TDX_IOMMU_MT_IQ_NR * IQ_BUFFER_PAGES;

void *iommu_mt_pages;

  iommu_mt_pages = alloc_pages_exact_nid(node, nr_pages, ...);

Making all pages contiguous is more than needed but it saves a lot of LOC, is it? We don't need to allocate void **pages, maybe we don't even need to define struct tdx_iommu_pages.

Quoted 30 lines
drivers/virt/coco/tdx-host/viommu.c new file +30 -0
+
+	return pages;
+
+free_pages:
+	for (i = 0; i < nr_entries; i++)
+		tdx_iommu_free_pages(pages[i]);
+	kfree(pages);
+
+	return NULL;
+}
+
+static void populate_mt_pages(struct tdx_iommu_pages *array)
+{
+	struct tdx_iommu_mt_param *param = array->root;
+	void **pages = array->pages;
+	unsigned int i;
+
+	for (i = 0; i < TDX_IOMMU_MT_IQ_NR; i++)
+		param->iq[i] = __pa(pages[i]) | IQ_BUFFER_PAGES;
+
+	for (; i < array->nr_entries; i++)
+		param->mt[i - TDX_IOMMU_MT_IQ_NR] = __pa(pages[i]);
+}
+
+/* Allocate the IOMMU_MT pages with PAMT backing. Free with free_mt_pages(). */
+static struct tdx_iommu_pages *alloc_iommu_mt(int node, unsigned int nr_mt_pages)
+{
+	unsigned int nr_entries = nr_mt_pages + TDX_IOMMU_MT_IQ_NR;
+	struct tdx_iommu_pages *array;
+	unsigned int i;

drivers/virt/coco/tdx-host/viommu.c line 216

Quoted 10 lines
drivers/virt/coco/tdx-host/viommu.c new file +10 -0
+	int ret = -ENOMEM;
+
+	if (!nr_mt_pages || nr_mt_pages > TDX_IOMMU_MT_MAX_PAGES)
+		return ERR_PTR(-EINVAL);
+
+	array = kzalloc_obj(*array);
+	if (!array)
+		return ERR_PTR(-ENOMEM);
+
+	array->root = tdx_iommu_alloc_pages(node, sizeof(*array->root));

The root page is just to hold the ABI parameter, doesn't have to allocate per node, is it?

Quoted 141 lines
drivers/virt/coco/tdx-host/viommu.c new file +141 -0
+	if (!array->root)
+		goto free_array;
+
+	array->nr_entries = nr_entries;
+	array->pages = alloc_mt_pages(nr_entries, node);
+	if (!array->pages)
+		goto free_root;
+
+	populate_mt_pages(array);
+
+	ret = mt_pamt_get(array);
+	if (ret)
+		goto free_pages;
+
+	return array;
+
+free_pages:
+	for (i = 0; i < nr_entries; i++)
+		tdx_iommu_free_pages(array->pages[i]);
+	kfree(array->pages);
+free_root:
+	tdx_iommu_free_pages(array->root);
+free_array:
+	kfree(array);
+	return ERR_PTR(ret);
+}
+
+static struct tdx_iommu *tdx_iommu_find(u64 reg_base_addr)
+{
+	struct tdx_iommu *tiommu;
+
+	lockdep_assert_held(&tdx_iommu_lock);
+
+	list_for_each_entry(tiommu, &tdx_iommu_list, node)
+		if (tiommu->params.reg_base_addr == reg_base_addr)
+			return tiommu;
+
+	return NULL;
+}
+
+/* Allocate the IOMMU_MT pages and issue TDH.IOMMU.SETUP. */
+static int tdx_iommu_setup(struct pci_dev *pdev, struct tdx_iommu *tiommu)
+{
+	const struct tdx_sys_info *sysinfo = tdx_get_sysinfo();
+	struct dmar_tdxc_params *params = &tiommu->params;
+	u64 r, tdx_iommu_id;
+	int ret;
+
+	if (!sysinfo)
+		return -ENXIO;
+
+	struct tdx_iommu_pages *mt __free(free_mt_pages) =
+			alloc_iommu_mt(params->node,
+				       sysinfo->tdx_connect.iommu_mt_page_count);
+	if (IS_ERR(mt))
+		return PTR_ERR(mt);
+
+	ret = dmar_tdxc_acquire(pdev, params);
+	if (ret)
+		return ret;
+
+	r = tdh_iommu_setup(params->reg_base_addr, mt->root, &tdx_iommu_id);
+	if (r) {
+		dmar_tdxc_release(pdev, params);
+		pr_err("TDH.IOMMU.SETUP failed, regbase 0x%llx status 0x%llx\n",
+		       params->reg_base_addr, r);
+		return -EIO;
+	}
+
+	tiommu->tdx_iommu_id = tdx_iommu_id;
+	tiommu->mt_pages = no_free_ptr(mt);
+
+	pr_info("Trusted DMA initialized, regbase 0x%llx\n", params->reg_base_addr);
+
+	return 0;
+}
+
+/* Issue TDH.IOMMU.CLEAR and release what TDH.IOMMU.SETUP took. */
+static int tdx_iommu_clear(struct pci_dev *pdev, struct tdx_iommu *tiommu)
+{
+	u64 r;
+
+	r = tdh_iommu_clear(tiommu->tdx_iommu_id);
+	if (r) {
+		pr_err("TDH.IOMMU.CLEAR failed, regbase 0x%llx tdx_iommu_id 0x%llx status 0x%llx\n",
+		       tiommu->params.reg_base_addr, tiommu->tdx_iommu_id, r);
+		/* The pages and their PAMT backing stay with the TDX module. */
+		return -EIO;
+	}
+
+	dmar_tdxc_release(pdev, &tiommu->params);
+	free_mt_pages(tiommu->mt_pages);
+	tiommu->mt_pages = NULL;
+
+	return 0;
+}
+
+/*
+ * tdx_iommu_get() - Enable trusted DMA on the IOMMU translating a device
+ * @pdev: PCI device that will use trusted DMA
+ *
+ * The first device behind an IOMMU transitions that IOMMU into Secure
+ * TDX Mode. Later devices only take a reference. Every successful call
+ * must be balanced by tdx_iommu_put().
+ *
+ * Return: 0 on success and error number on failure.
+ */
+int tdx_iommu_get(struct pci_dev *pdev)
+{
+	struct dmar_tdxc_params params;
+	struct tdx_iommu *tiommu;
+	int ret;
+
+	ret = dmar_tdxc_get_params(pdev, &params);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&tdx_iommu_lock);
+
+	tiommu = tdx_iommu_find(params.reg_base_addr);
+	if (tiommu) {
+		if (tiommu->stuck)
+			return -EBUSY;
+		if (WARN_ON(tiommu->users == UINT_MAX))
+			return -EOVERFLOW;
+		tiommu->users++;
+		return 0;
+	}
+
+	tiommu = kzalloc_obj(*tiommu);
+	if (!tiommu)
+		return -ENOMEM;
+
+	tiommu->params = params;
+	ret = tdx_iommu_setup(pdev, tiommu);
+	if (ret) {
+		kfree(tiommu);
+		return ret;
+	}
+
+	tiommu->users = 1;

drivers/virt/coco/tdx-host/viommu.c line 367

Quoted 10 lines
drivers/virt/coco/tdx-host/viommu.c new file +10 -0
+	list_add(&tiommu->node, &tdx_iommu_list);
+
+	return 0;
+}
+
+/*
+ * tdx_iommu_put() - Drop a reference taken by tdx_iommu_get()
+ * @pdev: Same device passed to tdx_iommu_get()
+ *
+ * The last device behind an IOMMU takes that IOMMU out of Secure TDX

I'm wondering if we really needs to take the IOMMU out of TDX mode when all devices are disconnected. Imagine people connect a device, disconnect and reconnect because of link error, why bother switch TDX mode and reserve/release DomainIDs again and again?

Previously we setup TDX mode for all IOMMUs at tdx-host probe, that's not a bad idea except quite a waste of resouces (e.g. DomainID). There may even be no TSM capable device plugged.

Now we setup TDX mode per IOMMU on-demand, that solves the concern. We don't really need runtime switch off. We don't have to let another device gets a big DID then fail the reconnect.

How about we only exit TDX mode for all IOMMUs on module unload, can that simplify some logic like user count, stuck? and locking? There will be no connect after we unregister TSM.

Thanks

Quoted 37 lines

+ * Mode. If TDH.IOMMU.CLEAR fails, the TDX module may still own the
+ * IOMMU_MT pages, so they are leaked. The IOMMU then stays in Secure TDX
+ * Mode, later tdx_iommu_get() calls for it fail, and the module can no
+ * longer be unloaded.
+ */

drivers/virt/coco/tdx-host/viommu.c new file +29 -0
+void tdx_iommu_put(struct pci_dev *pdev)
+{
+	struct dmar_tdxc_params params;
+	struct tdx_iommu *tiommu;
+
+	if (WARN_ON(dmar_tdxc_get_params(pdev, &params)))
+		return;
+
+	guard(mutex)(&tdx_iommu_lock);
+
+	tiommu = tdx_iommu_find(params.reg_base_addr);
+	if (WARN_ON(!tiommu || tiommu->stuck || !tiommu->users))
+		return;
+
+	if (--tiommu->users)
+		return;
+
+	if (tdx_iommu_clear(pdev, tiommu)) {
+		/* VT-d still sees it acquired; don't let unload forget it. */
+		if (try_module_get(THIS_MODULE)) {
+			tiommu->stuck = true;
+			return;
+		}
+		/* Unloading: free the entry; IOMMU_MT pages stay leaked. */
+	}
+
+	list_del(&tiommu->node);
+	kfree(tiommu);
+}
Signature · 1 line
2.43.0