Skip to contentSkip to conversation

coco/tdx-host: Introduce trusted DMA initialization support

PATCH In review Lu Baolu 5 participants 29 messages Nov 12 – Nov 24

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
#2 lore raw

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

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");
author maintainer
#3 lore raw

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

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

#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
author maintainer
#5 lore raw

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

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
#6 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
#7 lore raw

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

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;
 }
author maintainer
#8 lore raw

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

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
#9 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
#10 lore raw

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

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;

	...
}
#11 lore raw

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

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
#12 lore raw

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

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
#13 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
#14 lore raw

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

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
author maintainer
#15 lore raw

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

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

author maintainer
#16 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
#17 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
#18 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
#19 lore raw

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

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);
+}
author maintainer
#20 lore raw

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

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

#21 lore raw

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

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.

#22 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
#23 lore raw

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

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

#24 lore raw

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

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

author maintainer
#25 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
#26 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
#27 lore raw

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

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;
+}
author maintainer
#28 lore raw

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

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