mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O
@ 2026-11-16 11:10 Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session Chao Gao
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: x86, linux-coco, kvm, linux-kernel, linux-pci
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Kiryl Shutsemau, Dave Hansen, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, H. Peter Anvin, Bjorn Helgaas

Hi all,

This series adds PCIe link encryption on TDX hosts. Yilun previously
posted one large series with all the changes needed for this [1]. That
series has been split up, and the parts are being posted separately.
This RFC is posted for early review. It is not yet seeking review from
the x86 maintainers.

The SEAMCALL wrappers this series uses are in a separate series, not
yet posted. So, their names and parameters may still change.

Today, userspace sets up link encryption by writing to the PCI/TSM
"connect" sysfs attribute. A proposal under discussion [2] adds CTLV
attestation commands [3] to this flow, so that userspace can select and
verify the device's certificates before the IDE stream is set up. This
series uses only the "connect" attribute. I will convert it to the new
flow once the discussion settles.

== Background ==

With shared I/O, devices cannot access a confidential VM's private
memory, so the VM bounces I/O data through memory it shares with the
host. With trusted I/O, the VM establishes trust in a device and then
lets it access private memory directly, which avoids the bouncing.

The device's MMIO and DMA traffic then carries private data across PCIe
links and switches. A physical attacker on a link, or a malicious
switch, could read, modify or replay that traffic. PCIe link encryption
protects the traffic between the root port and the device against these
attacks, which keeps switches out of the VM's TCB.

Link encryption builds on:

  - Integrity and Data Encryption (IDE) [4], the PCIe feature that
    encrypts and integrity-protects data exchanged between the root port
    (part of the CPU's root complex) and the device.

  - The Security Protocol and Data Model (SPDM) [5], a protocol for
    authenticating a device and setting up a secure session with it.

  - Data Object Exchange (DOE) [6], a PCIe feature that provides a
    mailbox in the device's config space.

At a high level, setting up link encryption means opening an SPDM
session with the device through its DOE mailbox, configuring an IDE
stream on both the root port and the device, and programming the
stream's keys into the root port and sending them to the device over
the SPDM session.

The TEE Security Manager (TSM) is a trusted platform component, such as
the TDX module or AMD's Secure Processor firmware. The work is split
between the TSM and the host kernel. The TSM runs the SPDM session and
handles the keys. It doesn't own the device, so the host kernel configures
the device's IDE settings and relays SPDM messages between the TSM and
the device.

== This series ==

This series adds a TDX-TSM driver to the TDX host services module
(tdx-host.ko). The driver registers the TDX module as a TSM with the
PCI/TSM framework [7] and implements its connect and disconnect
operations. PCI/TSM calls them when userspace writes to a device's
"connect" and "disconnect" sysfs attributes.

Connect sets up the SPDM session, the IDE stream and its keys through
SEAMCALLs, and disconnect tears them down.

During some SEAMCALLs, the TDX module may need to exchange several SPDM
messages with the device. The kernel calls such a SEAMCALL in a loop.
Each time it returns an SPDM message with a dedicated status code, the
kernel sends the message through the device's DOE mailbox and calls the
SEAMCALL again with the device's reply, until the SEAMCALL completes.

== Points for discussion ==

1. Raw DOE frames (See patch 3)

The TDX module produces and consumes whole DOE objects, header included,
but the PCI DOE API takes payloads only. The TDX-TSM driver therefore
strips and rebuilds the DOE header itself. The code is small, but it
puts DOE format details in a TSM driver, while the DOE core already
handles them. A PCI DOE API that takes whole objects would keep them in
the core, but the TDX-TSM driver would be its only user. Is such an API
worth adding, or can we live with the DOE format handling in the
TDX-TSM driver?

2. Default IDE Stream (See patch 5)

TDISP requires each device to have one default IDE stream [8]. The PCI
core does not set Default Stream, so each TSM driver sets it itself, by
looking up the device's IDE settings with pci_ide_to_settings() and
setting default_stream. AMD's driver already does this, and the TDX-TSM
driver does the same. Since the requirement is shared, the PCI core
could instead set Default Stream on the device's TC0 stream, which is
what TDISP expects. Should this move into the PCI core now, or can it be
a later cleanup?

== Limitations ==

The TDX module configures the root port's IDE settings and accepts
only one address range for the stream. The root port therefore encrypts
only traffic to the device's prefetchable MMIO range, and a device with
no prefetchable range cannot be connected. We are evaluating whether
this can be lifted.

Link encryption needs the root complex to enter a TDX mode, in which
only the TDX module can configure the root port's IDE settings. The
kernel opts in to this mode with TDH.IOMMU.SETUP. As its name suggests,
this SEAMCALL also puts the IOMMU into TDX mode, which changes the
IOMMU's behavior. Unfortunately, the two cannot be enabled separately,
even though link encryption does not need IOMMU TDX mode.

== Dependencies ==

The series depends on the following series:

  - TDX module extensions [9]
  - IOMMU TDX mode [10]
  - SEAMCALL wrappers for PCIe Link Encryption [not yet posted]

The full stack is available at:

  https://github.com/gaochaointel/linux-dev/tree/link-encryption-rfc-v1

== Testing ==

Tested that link encryption can be enabled and disabled through PCI/TSM:

  # echo tsm0 > /sys/bus/pci/devices/$DEVICE/tsm/connect
  # echo tsm0 > /sys/bus/pci/devices/$DEVICE/tsm/disconnect

Note that I disabled DPAMT for this testing as this series does not yet
install DPAMT for the SPDM metadata, which will be added later.

== Changes since Yilun's version ==
  - Rebased onto the latest tip/x86/tdx branch.
  - Rewrote the cover letter and commit messages
  - Took over the patches from Zhenzhong and Yilun, and credited them
    in the changelogs.
  - Dropped the Reviewed-by tags collected as patches are changed
    significantly.
  - Allocated struct spdm_config_info_t with kzalloc_obj() and added a
    static_assert() on its size, which removes the casts on allocation
    and free.
  - Fixed a leak of the SPDM message buffers (tlink->in_msg and
    tlink->out_msg), and moved their allocation into tdx_spdm_create(),
    next to the SPDM metadata.
  - Dropped the SPDM session key update patch.
  - Made TDX_HOST_SERVICES depend on PCI and select PCI_TSM directly.
    Otherwise tdx-host fails to build with CONFIG_PCI=n. A TDX host
    without PCI support is not a realistic configuration
  - Replaced the bit operations and endian conversions in the DOE
    header handling with struct pci_doe_header and struct pci_doe_msg.
  - Returned -EIO instead of -EFAULT when TDH.IDE.STREAM.CREATE fails.
  - Reworded changelog to describe the TDX module's limit on concurrent
    TDH.SPDM.CONNECT. i.e., why tdx_spdm_lock.
  - Clarified that having an IDE default stream is required by TDISP spec.
  - Fixed checkpatch warnings

== AI Usage ==

AI was used under supervision to review code, refine logs and explore ways
to improve readability. I wrote the first draft of this cover letter, which
then changed significantly over several rounds of review with AI and was
trimmed a lot for brevity. The idea for simplifying the DOE format
handling in patch 3 came from AI.

== References ==

[1]: https://lore.kernel.org/all/20260327160132.2946114-1-yilun.xu@linux.intel.com/
[2]: https://lore.kernel.org/all/20261028152618.1356860-1-aneesh.kumar@kernel.org/
[3]: https://lore.kernel.org/all/20261015145053.3664509-1-jiri@resnulli.us/
[4]: PCIe Specification r7.0, section 6.33 "Integrity & Data Encryption (IDE)"
[5]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0274_1.3.0.pdf
[6]: PCIe Specification r7.0, section 6.30 "Data Object Exchange (DOE)"
[7]: https://lwn.net/Articles/1043262/
[8]: Intel TDX Connect Architecture Specification
     https://cdrdv2-public.intel.com/773614/intel-tdx-connect-architecture-specification.pdf
[9]: https://lore.kernel.org/all/20261013-tdx-module-ext-v4-0-de5ffac65470@linux.intel.com/
[10]: https://lore.kernel.org/kvm/20261112043054.2664857-1-baolu.lu@linux.intel.com/

Chao Gao (7):
  coco/tdx-host: Create metadata for an SPDM session
  coco/tdx-host: Establish an SPDM session with the device
  coco/tdx-host: Exchange SPDM messages through DOE
  coco/tdx-host: Pass a device info buffer to tdh_spdm_connect()
  coco/tdx-host: Configure an IDE stream
  coco/tdx-host: Program IDE keys and enable the IDE stream
  x86/virt/tdx: Enable PCIe link encryption

 arch/x86/include/asm/shared/tdx_errno.h     |   1 +
 arch/x86/include/asm/tdx.h                  |   2 +-
 arch/x86/include/asm/tdx_global_metadata.h  |   2 +
 arch/x86/virt/vmx/tdx/tdx.c                 |   2 +-
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c |  12 +
 drivers/pci/ide.c                           |   5 +-
 drivers/virt/coco/tdx-host/main.c           | 496 +++++++++++++++++++-
 include/linux/pci-ide.h                     |   2 +
 8 files changed, 516 insertions(+), 6 deletions(-)

-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device Chao Gao
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: linux-kernel, linux-coco, kvm
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Kiryl Shutsemau

The TDX module talks to the device over a Security Protocol and Data
Model (SPDM) session. It needs host memory for each session's metadata,
and reports the number of pages required in its global metadata.

When setting up an SPDM session, allocate the memory and hand it to the
TDX module with TDH.SPDM.CREATE. Keep the returned session ID for later
SEAMCALLs. When tearing the session down, reclaim the memory with
TDH.SPDM.DELETE. If that fails, leak the memory, as the TDX module may
still be using it.

Based on work by Yilun and Zhenzhong.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 arch/x86/include/asm/tdx_global_metadata.h  |   1 +
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c |   6 ++
 drivers/virt/coco/tdx-host/main.c           | 104 +++++++++++++++++++-
 3 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h
index e5e54159ad36..537e872eeb0e 100644
--- a/arch/x86/include/asm/tdx_global_metadata.h
+++ b/arch/x86/include/asm/tdx_global_metadata.h
@@ -52,6 +52,7 @@ struct tdx_sys_info_ext {
 };
 
 struct tdx_sys_info_connect {
+	u16 spdm_mt_page_count;
 	u16 iommu_mt_page_count;
 };
 
diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
index 9b46f34cb469..f20e029f240b 100644
--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
@@ -144,6 +144,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_
 	int ret;
 	u64 val;
 
+	ret = read_sys_metadata_field(0x3000000100000002, &val);
+	if (ret)
+		return ret;
+
+	sysinfo_connect->spdm_mt_page_count = val;
+
 	ret = read_sys_metadata_field(0x3000000100000003, &val);
 	if (ret)
 		return ret;
diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index a815dbe5127a..919e77ae2326 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2025 Intel Corporation
  */
 
+#include <linux/bitfield.h>
 #include <linux/device/faux.h>
 #include <linux/firmware.h>
 #include <linux/module.h>
@@ -26,8 +27,30 @@ static const struct x86_cpu_id tdx_host_ids[] = {
 };
 MODULE_DEVICE_TABLE(x86cpu, tdx_host_ids);
 
+#define TDISP_FUNC_ID		GENMASK(15, 0)
+#define TDISP_FUNC_ID_SEGMENT		GENMASK(23, 16)
+#define TDISP_FUNC_ID_SEG_VALID		BIT(24)
+
+static inline u32 tdisp_func_id(struct pci_dev *pdev)
+{
+	u32 func_id;
+
+	func_id = FIELD_PREP(TDISP_FUNC_ID_SEGMENT, pci_domain_nr(pdev->bus));
+	if (func_id)
+		func_id |= TDISP_FUNC_ID_SEG_VALID;
+	func_id |= FIELD_PREP(TDISP_FUNC_ID,
+			      PCI_DEVID(pdev->bus->number, pdev->devfn));
+
+	return func_id;
+}
+
 struct tdx_tsm_link {
 	struct pci_tsm_pf0 pci;
+	u32 func_id;
+
+	u64 spdm_id;
+	unsigned int spdm_mt_nr_pages;
+	void *spdm_mt;
 };
 
 static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
@@ -35,21 +58,97 @@ 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_spdm_create(struct tdx_tsm_link *tlink)
+{
+	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+	struct tdx_hpa_list_info info;
+	unsigned int nr_pages;
+	u64 spdm_id, sret;
+	void *spdm_mt;
+	int ret;
+
+	if (!tdx_sysinfo)
+		return -ENXIO;
+
+	nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count;
+
+	spdm_mt = alloc_pages_exact(nr_pages * PAGE_SIZE,
+				    GFP_KERNEL | __GFP_ZERO);
+	if (!spdm_mt)
+		return -ENOMEM;
+
+	ret = tdx_hpa_list_info_setup(&info, spdm_mt, nr_pages);
+	if (ret)
+		goto out_free_spdm_mt;
+
+	sret = tdh_spdm_create(tlink->func_id, &info, &spdm_id);
+
+	tdx_hpa_list_info_free(&info);
+
+	if (sret) {
+		ret = -EIO;
+		goto out_free_spdm_mt;
+	}
+
+	tlink->spdm_id = spdm_id;
+	tlink->spdm_mt = spdm_mt;
+	tlink->spdm_mt_nr_pages = nr_pages;
+	return 0;
+
+out_free_spdm_mt:
+	free_pages_exact(spdm_mt, nr_pages * PAGE_SIZE);
+	return ret;
+}
+
+static void tdx_spdm_delete(struct tdx_tsm_link *tlink)
+{
+	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
+	u64 sret;
+
+	sret = tdh_spdm_delete(tlink->spdm_id);
+	if (sret) {
+		/* leak the metadata pages */
+		pci_err(pdev, "fail to delete spdm 0x%llx\n", sret);
+		return;
+	}
+
+	free_pages_exact(tlink->spdm_mt, tlink->spdm_mt_nr_pages * PAGE_SIZE);
+}
+
+static int tdx_spdm_session_setup(struct tdx_tsm_link *tlink)
+{
+	return tdx_spdm_create(tlink);
+}
+
+static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
+{
+	tdx_spdm_delete(tlink);
+}
+
 static int tdx_tsm_link_connect(struct pci_dev *pdev)
 {
+	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
 	int ret;
 
 	ret = tdx_iommu_get(pdev);
 	if (ret)
 		return ret;
 
-	/* TDISP connect is not implemented yet. */
+	ret = tdx_spdm_session_setup(tlink);
+	if (ret)
+		goto out_iommu_put;
+	return 0;
+
+out_iommu_put:
 	tdx_iommu_put(pdev);
-	return -ENXIO;
+	return ret;
 }
 
 static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
 {
+	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
+
+	tdx_spdm_session_teardown(tlink);
 	tdx_iommu_put(pdev);
 }
 
@@ -66,6 +165,7 @@ static struct pci_tsm *tdx_tsm_link_pf0_probe(struct tsm_dev *tsm_dev,
 	if (rc)
 		return NULL;
 
+	tlink->func_id = tdisp_func_id(pdev);
 	return &no_free_ptr(tlink)->pci.base_tsm;
 }
 
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE Chao Gao
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: linux-kernel, linux-coco, kvm
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Kiryl Shutsemau

Setting up PCIe link encryption requires a secure channel to the device
to exchange keys. This channel is an SPDM session over the device's PCI
DOE mailbox.

The TDX module cannot access the DOE mailbox but needs to exchange SPDM
messages with the device, so the host relays SPDM messages between the
two.

TDH.SPDM.CONNECT establishes the session and TDH.SPDM.DISCONNECT tears
it down. Both may need several message exchanges with the device.
Whenever the TDX module has a message for the device, the SEAMCALL
returns with a dedicated status code. The host sends the message to the
device and calls the SEAMCALL again with the device's response, until
the SEAMCALL completes.

Call both SEAMCALLs in such a loop. The DOE transfer itself is a stub
for now.

Pass the messages through two pages per device, one for each direction.
One pair is enough because PCI/TSM serializes operations on a device.

The TDX module limits how many TDH.SPDM.CONNECT and TDH.SPDM.DISCONNECT
calls can run at the same time. The limit is implementation-specific,
and a call over the limit fails with TDX_OPERAND_BUSY. For simplicity,
serialize them across all devices with a mutex.

Based on work by Yilun.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 arch/x86/include/asm/shared/tdx_errno.h |   1 +
 drivers/virt/coco/tdx-host/main.c       | 131 +++++++++++++++++++++++-
 2 files changed, 130 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/shared/tdx_errno.h b/arch/x86/include/asm/shared/tdx_errno.h
index ee411b360e20..6581cec7e097 100644
--- a/arch/x86/include/asm/shared/tdx_errno.h
+++ b/arch/x86/include/asm/shared/tdx_errno.h
@@ -27,6 +27,7 @@
 #define TDX_EPT_WALK_FAILED			0xC0000B0000000000ULL
 #define TDX_EPT_ENTRY_STATE_INCORRECT		0xC0000B0D00000000ULL
 #define TDX_METADATA_FIELD_NOT_READABLE		0xC0000C0200000000ULL
+#define TDX_SPDM_REQUEST			0x00000F5700000000ULL
 
 /*
  * TDX module operand ID, appears in 31:0 part of error code as
diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index 919e77ae2326..32755d70dac5 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -44,11 +44,26 @@ static inline u32 tdisp_func_id(struct pci_dev *pdev)
 	return func_id;
 }
 
+#define SPDM_CAP_HBEAT		BIT(13)
+#define SPDM_CAP_KEY_UPD	BIT(14)
+
+struct spdm_config_info_t {
+	u32 vmm_spdm_cap;
+	u8 spdm_session_policy;
+	u8 certificate_slot_mask;
+	u8 raw_bitstream_requested;
+	u8 reserved[4089];
+} __packed;
+
+static_assert(sizeof(struct spdm_config_info_t) == 4096);
+
 struct tdx_tsm_link {
 	struct pci_tsm_pf0 pci;
 	u32 func_id;
 
 	u64 spdm_id;
+	struct page *in_msg;
+	struct page *out_msg;
 	unsigned int spdm_mt_nr_pages;
 	void *spdm_mt;
 };
@@ -58,9 +73,89 @@ 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_spdm_msg_exchange(struct tdx_tsm_link *tlink)
+{
+	/* TODO: implement the PCI DOE transfer */
+	return 0;
+}
+
+/*
+ * When serving SEAMCALLs, the TDX module extensions work like service thread
+ * pools. The number of service threads is limited per service type (or
+ * "per extension" in terms of TDX). When the host issues a request via
+ * SEAMCALL but the TDX module can't find an available thread, it returns
+ * TDX_OPERAND_BUSY. Currently the thread number limit for the TDISP extension
+ * is 1. So a mutex could work to prevent SEAMCALL busy returns.
+ */
+static DEFINE_MUTEX(tdx_spdm_lock);
+
+static int tdx_spdm_xfer_handler(struct tdx_tsm_link *tlink, u64 sret,
+				 u64 out_msg_sz)
+{
+	int ret;
+
+	if (sret == TDX_SPDM_REQUEST) {
+		ret = tdx_spdm_msg_exchange(tlink);
+		if (ret < 0)
+			return ret;
+
+		return -EAGAIN;
+	}
+
+	if (sret == TDX_SUCCESS)
+		return 0;
+
+	return -EIO;
+}
+
+static int tdx_spdm_session_connect(struct tdx_tsm_link *tlink)
+{
+	struct spdm_config_info_t *spdm_conf;
+	u64 sret, out_msg_sz;
+	int ret;
+
+	/* create & fill SPDM configuration buffer - a single page */
+	spdm_conf = kzalloc_obj(*spdm_conf);
+	if (!spdm_conf)
+		return -ENOMEM;
+
+	spdm_conf->certificate_slot_mask = 0xff;
+
+	do {
+		mutex_lock(&tdx_spdm_lock);
+		sret = tdh_spdm_connect(tlink->spdm_id,
+					virt_to_page(spdm_conf),
+					tlink->in_msg, tlink->out_msg,
+					NULL, &out_msg_sz);
+		mutex_unlock(&tdx_spdm_lock);
+		ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
+	} while (ret == -EAGAIN);
+
+	kfree(spdm_conf);
+
+	return ret;
+}
+
+static void tdx_spdm_session_disconnect(struct tdx_tsm_link *tlink)
+{
+	u64 sret, out_msg_sz;
+	int ret;
+
+	do {
+		mutex_lock(&tdx_spdm_lock);
+		sret = tdh_spdm_disconnect(tlink->spdm_id, tlink->in_msg,
+					   tlink->out_msg, &out_msg_sz);
+		mutex_unlock(&tdx_spdm_lock);
+		ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
+	} while (ret == -EAGAIN);
+
+	WARN_ON(ret);
+}
+
 static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 {
 	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
+	struct page *in_msg_page, *out_msg_page;
 	struct tdx_hpa_list_info info;
 	unsigned int nr_pages;
 	u64 spdm_id, sret;
@@ -70,12 +165,21 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 	if (!tdx_sysinfo)
 		return -ENXIO;
 
+	ret = -ENOMEM;
+	in_msg_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!in_msg_page)
+		return ret;
+
+	out_msg_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!out_msg_page)
+		goto out_free_in_msg;
+
 	nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count;
 
 	spdm_mt = alloc_pages_exact(nr_pages * PAGE_SIZE,
 				    GFP_KERNEL | __GFP_ZERO);
 	if (!spdm_mt)
-		return -ENOMEM;
+		goto out_free_out_msg;
 
 	ret = tdx_hpa_list_info_setup(&info, spdm_mt, nr_pages);
 	if (ret)
@@ -92,11 +196,17 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 
 	tlink->spdm_id = spdm_id;
 	tlink->spdm_mt = spdm_mt;
+	tlink->in_msg = in_msg_page;
+	tlink->out_msg = out_msg_page;
 	tlink->spdm_mt_nr_pages = nr_pages;
 	return 0;
 
 out_free_spdm_mt:
 	free_pages_exact(spdm_mt, nr_pages * PAGE_SIZE);
+out_free_out_msg:
+	__free_page(out_msg_page);
+out_free_in_msg:
+	__free_page(in_msg_page);
 	return ret;
 }
 
@@ -112,16 +222,33 @@ static void tdx_spdm_delete(struct tdx_tsm_link *tlink)
 		return;
 	}
 
+	__free_page(tlink->in_msg);
+	__free_page(tlink->out_msg);
 	free_pages_exact(tlink->spdm_mt, tlink->spdm_mt_nr_pages * PAGE_SIZE);
 }
 
 static int tdx_spdm_session_setup(struct tdx_tsm_link *tlink)
 {
-	return tdx_spdm_create(tlink);
+	int ret;
+
+	ret = tdx_spdm_create(tlink);
+	if (ret)
+		return ret;
+
+	ret = tdx_spdm_session_connect(tlink);
+	if (ret)
+		goto out_spdm_delete;
+
+	return 0;
+
+out_spdm_delete:
+	tdx_spdm_delete(tlink);
+	return ret;
 }
 
 static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
 {
+	tdx_spdm_session_disconnect(tlink);
 	tdx_spdm_delete(tlink);
 }
 
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 4/7] coco/tdx-host: Pass a device info buffer to tdh_spdm_connect() Chao Gao
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: x86, linux-coco, kvm, linux-kernel
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Kiryl Shutsemau, Dave Hansen

The TDX module cannot access a device's DOE mailbox, so the kernel
relays SPDM messages between the TDX module and the device. The TDX
module produces and consumes whole DOE data objects, header included,
but pci_tsm_doe_transfer() takes only the payload and builds the header
itself.

Define struct pci_doe_header and struct pci_doe_msg to describe a DOE
data object. Strip the header from each request before passing the
payload to pci_tsm_doe_transfer(), and rebuild the header around the
device's response before handing it back to the TDX module.

An alternative is to add a PCI DOE API that takes whole data objects.
This would avoid the conversion, but the TDX-TSM driver would be its
only user.

Based on work by Yilun.

AI suggested defining the DOE header and object structures in place of
the bit operations, and dropping the endian conversions.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 drivers/virt/coco/tdx-host/main.c | 49 ++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index 32755d70dac5..86299658ae45 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -11,6 +11,7 @@
 #include <linux/module.h>
 #include <linux/sysfs.h>
 #include <linux/pci.h>
+#include <linux/pci-doe.h>
 #include <linux/pci-tsm.h>
 #include <linux/tsm.h>
 
@@ -73,9 +74,55 @@ static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
 	return container_of(tsm, struct tdx_tsm_link, pci.base_tsm);
 }
 
+struct pci_doe_header {
+	u16 vendor;
+	u8 type;
+	u8 rsvd0;
+	u32 length: 18; /* in DWORDs */
+	u32 rsvd1: 14;
+} __packed;
+
+struct pci_doe_msg {
+	struct pci_doe_header header;
+	u8 payload[];
+} __packed;
+
+/*
+ * The TDX module produces and consumes complete DOE data objects, but
+ * pci_doe() takes only the payload and builds the header itself. Strip
+ * the header from the request and rebuild it for the response.
+ */
 static int tdx_spdm_msg_exchange(struct tdx_tsm_link *tlink)
 {
-	/* TODO: implement the PCI DOE transfer */
+	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
+	struct pci_doe_msg *req  = page_address(tlink->out_msg);
+	struct pci_doe_msg *resp = page_address(tlink->in_msg);
+
+	size_t req_sz		= req->header.length * sizeof(u32);
+	size_t resp_sz		= PAGE_SIZE;
+
+	size_t req_payload_sz	= req_sz  - sizeof(struct pci_doe_header);
+	size_t resp_payload_sz	= resp_sz - sizeof(struct pci_doe_header);
+
+	void *resp_payload_addr	= resp->payload;
+	void *req_payload_addr	= req->payload;
+
+	u8 type = req->header.type;
+
+	int ret;
+
+	ret = pci_tsm_doe_transfer(pdev, type,
+				   req_payload_addr, req_payload_sz,
+				   resp_payload_addr, resp_payload_sz);
+	if (ret < 0) {
+		pci_err(pdev, "spdm msg exchange fail %d\n", ret);
+		return ret;
+	}
+
+	resp->header.vendor = PCI_VENDOR_ID_PCI_SIG;
+	resp->header.type = type;
+	resp->header.length = (ret + sizeof(struct pci_doe_header)) / sizeof(u32);
+
 	return 0;
 }
 
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 4/7] coco/tdx-host: Pass a device info buffer to tdh_spdm_connect()
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
                   ` (2 preceding siblings ...)
  2026-11-16 11:10 ` [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream Chao Gao
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: linux-kernel, linux-coco, kvm
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Kiryl Shutsemau

TDH.SPDM.CONNECT requires a host-provided buffer, into which the TDX
module writes information about the device.

TDs will use the information to attest the device, which is not
supported yet. The kernel doesn't use it for now, so treat it as opaque
data.

Take the buffer size from the TDX global metadata, and keep the buffer
for the lifetime of the SPDM session.

Based on work by Yilun.

[This patch will be reworked when the series moves to the CTLV
  attestation flow.]

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 arch/x86/include/asm/tdx_global_metadata.h  |  1 +
 arch/x86/virt/vmx/tdx/tdx_global_metadata.c |  6 +++++
 drivers/virt/coco/tdx-host/main.c           | 27 +++++++++++++++++++--
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h
index 537e872eeb0e..30f3ca006e88 100644
--- a/arch/x86/include/asm/tdx_global_metadata.h
+++ b/arch/x86/include/asm/tdx_global_metadata.h
@@ -54,6 +54,7 @@ struct tdx_sys_info_ext {
 struct tdx_sys_info_connect {
 	u16 spdm_mt_page_count;
 	u16 iommu_mt_page_count;
+	u16 spdm_max_dev_info_pages;
 };
 
 struct tdx_sys_info {
diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
index f20e029f240b..cf747b0dc945 100644
--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
@@ -156,6 +156,12 @@ static __init int get_tdx_sys_info_connect(struct tdx_sys_info_connect *sysinfo_
 
 	sysinfo_connect->iommu_mt_page_count = val;
 
+	ret = read_sys_metadata_field(0x3000000100000007, &val);
+	if (ret)
+		return ret;
+
+	sysinfo_connect->spdm_max_dev_info_pages = val;
+
 	return 0;
 }
 
diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index 86299658ae45..da56178eac2e 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -67,6 +67,8 @@ struct tdx_tsm_link {
 	struct page *out_msg;
 	unsigned int spdm_mt_nr_pages;
 	void *spdm_mt;
+	unsigned int dev_info_nr_pages;
+	void *dev_info;
 };
 
 static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
@@ -158,6 +160,7 @@ static int tdx_spdm_xfer_handler(struct tdx_tsm_link *tlink, u64 sret,
 static int tdx_spdm_session_connect(struct tdx_tsm_link *tlink)
 {
 	struct spdm_config_info_t *spdm_conf;
+	struct tdx_hpa_list_info info;
 	u64 sret, out_msg_sz;
 	int ret;
 
@@ -168,16 +171,23 @@ static int tdx_spdm_session_connect(struct tdx_tsm_link *tlink)
 
 	spdm_conf->certificate_slot_mask = 0xff;
 
+	ret = tdx_hpa_list_info_setup(&info, tlink->dev_info, tlink->dev_info_nr_pages);
+	if (ret)
+		goto out_free_spdm_conf;
+
 	do {
 		mutex_lock(&tdx_spdm_lock);
 		sret = tdh_spdm_connect(tlink->spdm_id,
 					virt_to_page(spdm_conf),
 					tlink->in_msg, tlink->out_msg,
-					NULL, &out_msg_sz);
+					&info, &out_msg_sz);
 		mutex_unlock(&tdx_spdm_lock);
 		ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
 	} while (ret == -EAGAIN);
 
+	tdx_hpa_list_info_free(&info);
+
+out_free_spdm_conf:
 	kfree(spdm_conf);
 
 	return ret;
@@ -203,9 +213,11 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 {
 	const struct tdx_sys_info *tdx_sysinfo = tdx_get_sysinfo();
 	struct page *in_msg_page, *out_msg_page;
+	unsigned int dev_info_nr_pages;
 	struct tdx_hpa_list_info info;
 	unsigned int nr_pages;
 	u64 spdm_id, sret;
+	void *dev_info;
 	void *spdm_mt;
 	int ret;
 
@@ -222,11 +234,17 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 		goto out_free_in_msg;
 
 	nr_pages = tdx_sysinfo->tdx_connect.spdm_mt_page_count;
+	dev_info_nr_pages = tdx_sysinfo->tdx_connect.spdm_max_dev_info_pages;
+
+	dev_info = alloc_pages_exact(dev_info_nr_pages * PAGE_SIZE,
+				     GFP_KERNEL | __GFP_ZERO);
+	if (!dev_info)
+		goto out_free_out_msg;
 
 	spdm_mt = alloc_pages_exact(nr_pages * PAGE_SIZE,
 				    GFP_KERNEL | __GFP_ZERO);
 	if (!spdm_mt)
-		goto out_free_out_msg;
+		goto out_free_dev_info;
 
 	ret = tdx_hpa_list_info_setup(&info, spdm_mt, nr_pages);
 	if (ret)
@@ -246,10 +264,14 @@ static int tdx_spdm_create(struct tdx_tsm_link *tlink)
 	tlink->in_msg = in_msg_page;
 	tlink->out_msg = out_msg_page;
 	tlink->spdm_mt_nr_pages = nr_pages;
+	tlink->dev_info = dev_info;
+	tlink->dev_info_nr_pages = dev_info_nr_pages;
 	return 0;
 
 out_free_spdm_mt:
 	free_pages_exact(spdm_mt, nr_pages * PAGE_SIZE);
+out_free_dev_info:
+	free_pages_exact(dev_info, dev_info_nr_pages * PAGE_SIZE);
 out_free_out_msg:
 	__free_page(out_msg_page);
 out_free_in_msg:
@@ -272,6 +294,7 @@ static void tdx_spdm_delete(struct tdx_tsm_link *tlink)
 	__free_page(tlink->in_msg);
 	__free_page(tlink->out_msg);
 	free_pages_exact(tlink->spdm_mt, tlink->spdm_mt_nr_pages * PAGE_SIZE);
+	free_pages_exact(tlink->dev_info, tlink->dev_info_nr_pages * PAGE_SIZE);
 }
 
 static int tdx_spdm_session_setup(struct tdx_tsm_link *tlink)
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
                   ` (3 preceding siblings ...)
  2026-11-16 11:10 ` [RFC PATCH 4/7] coco/tdx-host: Pass a device info buffer to tdh_spdm_connect() Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 6/7] coco/tdx-host: Program IDE keys and enable the " Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 7/7] x86/virt/tdx: Enable PCIe link encryption Chao Gao
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: linux-pci, linux-kernel, x86, linux-coco, kvm
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Bjorn Helgaas, Kiryl Shutsemau, Dave Hansen

Establishing a PCIe Integrity and Data Encryption (IDE) stream has two
parts. The stream is first provisioned in the config space of both
Partner Ports (the root port and the device), and the keys are then
programmed via IDE Key Management (IDE_KM).

Commit 1e4d2ff3ae45 ("PCI/IDE: Add IDE establishment helpers") added
the pci_ide_stream_*() helpers for provisioning. They split it into
fine-grained allocate, register and setup steps to accommodate
differences between SEV-TIO and TDX Connect, such as who programs the
root port and who allocates the stream ID.

Provision the stream with these helpers. IDE_KM and enabling the stream
are left to later changes.

The TDX module owns the root port's IDE registers and provides
TDH.IDE.STREAM.CREATE for the host to program them. Configure the root
port with the SEAMCALL, passing the register values as parameters, and
write only the device's IDE registers from the host.

An IDE stream can be associated with address ranges, so that traffic
to different ranges, such as those of different devices, goes through
different streams with different keys. A stream can be associated with
multiple ranges, but TDX supports only one. Keep the prefetchable range
and drop the non-prefetchable one. Export pci_ide_stream_to_regs() to
generate the root port's register values from the adjusted settings.

TDH.IDE.STREAM.CREATE also returns the stream ID that the TDX module
assigns. Configure the device with the same ID.

Based on work by Yilun.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 drivers/pci/ide.c                 |   5 +-
 drivers/virt/coco/tdx-host/main.c | 147 ++++++++++++++++++++++++++++++
 include/linux/pci-ide.h           |   2 +
 3 files changed, 152 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/ide.c b/drivers/pci/ide.c
index beb67b8fb5c5..aced5ef923a9 100644
--- a/drivers/pci/ide.c
+++ b/drivers/pci/ide.c
@@ -550,8 +550,8 @@ static void mem_assoc_to_regs(struct pci_bus_region *region,
  * @ide: registered IDE settings descriptor
  * @regs: output register values
  */
-static void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide,
-				   struct pci_ide_regs *regs)
+void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide,
+			    struct pci_ide_regs *regs)
 {
 	struct pci_ide_partner *settings = pci_ide_to_settings(pdev, ide);
 	int assoc_idx = 0;
@@ -580,6 +580,7 @@ static void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide,
 
 	regs->nr_addr = assoc_idx;
 }
+EXPORT_SYMBOL_GPL(pci_ide_stream_to_regs);
 
 /**
  * pci_ide_stream_setup() - program settings to Selective IDE Stream registers
diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index da56178eac2e..63209a712c28 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -12,6 +12,7 @@
 #include <linux/sysfs.h>
 #include <linux/pci.h>
 #include <linux/pci-doe.h>
+#include <linux/pci-ide.h>
 #include <linux/pci-tsm.h>
 #include <linux/tsm.h>
 
@@ -69,6 +70,10 @@ struct tdx_tsm_link {
 	void *spdm_mt;
 	unsigned int dev_info_nr_pages;
 	void *dev_info;
+
+	struct pci_ide *ide;
+	struct page *stream_mt;
+	unsigned int stream_id;
 };
 
 static struct tdx_tsm_link *to_tdx_tsm_link(struct pci_tsm *tsm)
@@ -322,6 +327,140 @@ static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
 	tdx_spdm_delete(tlink);
 }
 
+static void sel_stream_block_regs(struct pci_dev *pdev, struct pci_ide *ide,
+				  struct pci_ide_regs *regs)
+{
+	struct pci_dev *rp = pcie_find_root_port(pdev);
+	struct pci_ide_partner *setting = pci_ide_to_settings(rp, ide);
+
+	/* only support address association for prefetchable memory */
+	setting->mem_assoc = (struct pci_bus_region) { 0, -1 };
+	pci_ide_stream_to_regs(rp, ide, regs);
+}
+
+#define STREAM_INFO_RP_DEVFN		GENMASK_ULL(7, 0)
+#define STREAM_INFO_TYPE		BIT_ULL(8)
+#define  STREAM_INFO_TYPE_LINK		0
+#define  STREAM_INFO_TYPE_SEL		1
+
+static int tdx_ide_stream_create(struct tdx_tsm_link *tlink,
+				 struct pci_ide *ide)
+{
+	u64 stream_info, stream_ctrl, stream_id;
+	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
+	struct pci_dev *rp = pcie_find_root_port(pdev);
+	struct pci_ide_regs regs;
+	struct page *stream_mt;
+	u64 sret;
+	int ret;
+
+	stream_mt = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!stream_mt)
+		return -ENOMEM;
+
+	stream_info = FIELD_PREP(STREAM_INFO_RP_DEVFN, rp->devfn);
+	stream_info |= FIELD_PREP(STREAM_INFO_TYPE, STREAM_INFO_TYPE_SEL);
+	/* Keep optional stream features disabled for now. */
+	stream_ctrl = 0;
+
+	sel_stream_block_regs(pdev, ide, &regs);
+	if (regs.nr_addr != 1) {
+		ret = -EFAULT;
+		goto out_free_stream_mt;
+	}
+
+	sret = tdh_ide_stream_create(stream_info, tlink->spdm_id,
+				     stream_mt, stream_ctrl,
+				     regs.rid1, regs.rid2, regs.addr[0].assoc1,
+				     regs.addr[0].assoc2, regs.addr[0].assoc3,
+				     &stream_id);
+	if (sret) {
+		ret = -EIO;
+		goto out_free_stream_mt;
+	}
+
+	tlink->stream_id = stream_id;
+	tlink->stream_mt = stream_mt;
+
+	return 0;
+
+out_free_stream_mt:
+	__free_page(stream_mt);
+
+	return ret;
+}
+
+static void tdx_ide_stream_delete(struct tdx_tsm_link *tlink)
+{
+	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
+	u64 sret;
+
+	sret = tdh_ide_stream_block(tlink->spdm_id, tlink->stream_id);
+	if (sret) {
+		/* leak the metadata pages */
+		pci_err(pdev, "ide stream block fail 0x%llx\n", sret);
+		return;
+	}
+
+	sret = tdh_ide_stream_delete(tlink->spdm_id, tlink->stream_id);
+	if (sret) {
+		pci_err(pdev, "ide stream delete fail 0x%llx\n", sret);
+		return;
+	}
+
+	__free_page(tlink->stream_mt);
+}
+
+static int tdx_ide_stream_setup(struct tdx_tsm_link *tlink)
+{
+	struct pci_dev *pdev = tlink->pci.base_tsm.pdev;
+	struct pci_ide *ide;
+	int ret;
+
+	ide = pci_ide_stream_alloc(pdev);
+	if (!ide)
+		return -ENOMEM;
+
+	/* Configure IDE capability for RP & get stream_id */
+	ret = tdx_ide_stream_create(tlink, ide);
+	if (ret)
+		goto out_pci_ide_stream_free;
+
+	ide->stream_id = tlink->stream_id;
+	ret = pci_ide_stream_register(ide);
+	if (ret)
+		goto out_tdx_ide_stream_delete;
+
+	/*
+	 * Configure stream settings for the device. TDISP requires one
+	 * stream to be the default stream, and the PCI core does not set
+	 * one, so make the only stream the default.
+	 */
+	ide->partner[PCI_IDE_EP].default_stream = 1;
+	pci_ide_stream_setup(pdev, ide);
+
+	tlink->ide = ide;
+
+	return 0;
+
+out_tdx_ide_stream_delete:
+	tdx_ide_stream_delete(tlink);
+out_pci_ide_stream_free:
+	pci_ide_stream_free(ide);
+
+	return ret;
+}
+
+static void tdx_ide_stream_teardown(struct tdx_tsm_link *tlink)
+{
+	struct pci_ide *ide = tlink->ide;
+
+	pci_ide_stream_teardown(ide->pdev, ide);
+	pci_ide_stream_unregister(ide);
+	tdx_ide_stream_delete(tlink);
+	pci_ide_stream_free(ide);
+}
+
 static int tdx_tsm_link_connect(struct pci_dev *pdev)
 {
 	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
@@ -334,8 +473,15 @@ static int tdx_tsm_link_connect(struct pci_dev *pdev)
 	ret = tdx_spdm_session_setup(tlink);
 	if (ret)
 		goto out_iommu_put;
+
+	ret = tdx_ide_stream_setup(tlink);
+	if (ret)
+		goto out_spdm_teardown;
+
 	return 0;
 
+out_spdm_teardown:
+	tdx_spdm_session_teardown(tlink);
 out_iommu_put:
 	tdx_iommu_put(pdev);
 	return ret;
@@ -345,6 +491,7 @@ static void tdx_tsm_link_disconnect(struct pci_dev *pdev)
 {
 	struct tdx_tsm_link *tlink = to_tdx_tsm_link(pdev->tsm);
 
+	tdx_ide_stream_teardown(tlink);
 	tdx_spdm_session_teardown(tlink);
 	tdx_iommu_put(pdev);
 }
diff --git a/include/linux/pci-ide.h b/include/linux/pci-ide.h
index ae07d9f699c0..f8074865d2cd 100644
--- a/include/linux/pci-ide.h
+++ b/include/linux/pci-ide.h
@@ -104,6 +104,8 @@ struct pci_ide {
 void pci_ide_set_nr_streams(struct pci_host_bridge *hb, u16 nr);
 struct pci_ide_partner *pci_ide_to_settings(struct pci_dev *pdev,
 					    struct pci_ide *ide);
+void pci_ide_stream_to_regs(struct pci_dev *pdev, struct pci_ide *ide,
+			    struct pci_ide_regs *regs);
 struct pci_ide *pci_ide_stream_alloc(struct pci_dev *pdev);
 void pci_ide_stream_free(struct pci_ide *ide);
 int  pci_ide_stream_register(struct pci_ide *ide);
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 6/7] coco/tdx-host: Program IDE keys and enable the IDE stream
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
                   ` (4 preceding siblings ...)
  2026-11-16 11:10 ` [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  2026-11-16 11:10 ` [RFC PATCH 7/7] x86/virt/tdx: Enable PCIe link encryption Chao Gao
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: x86, linux-coco, kvm, linux-kernel
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Kiryl Shutsemau, Dave Hansen

With the IDE stream provisioned on root ports and the device, the stream
keys are next programmed via IDE_KM, and then the stream is enabled. The
trusted firmware (TSM) programs the keys into the root port, and sends
them to the device as IDE_KM messages over the SPDM session.

On TDX, TDH.IDE.STREAM.KM does both, and also enables the stream on the
root port. Like TDH.SPDM.CONNECT, it returns to the host whenever it
has an IDE_KM message for the device. Call it in the same loop that
relays the messages through the DOE mailbox.

Unlike TDH.SPDM.CONNECT, TDH.IDE.STREAM.KM is not subject to the TDX
module's concurrency limit, so don't serialize it with the mutex.

After the stream gets enabled on the root port, enable the stream on
the device from the host.

Teardown reverses these steps.

Based on work by Yilun.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 drivers/virt/coco/tdx-host/main.c | 48 +++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/virt/coco/tdx-host/main.c b/drivers/virt/coco/tdx-host/main.c
index 63209a712c28..040ceea45f8d 100644
--- a/drivers/virt/coco/tdx-host/main.c
+++ b/drivers/virt/coco/tdx-host/main.c
@@ -327,6 +327,37 @@ static void tdx_spdm_session_teardown(struct tdx_tsm_link *tlink)
 	tdx_spdm_delete(tlink);
 }
 
+enum tdx_ide_stream_km_op {
+	TDX_IDE_STREAM_KM_SETUP	= 0,
+	TDX_IDE_STREAM_KM_STOP	= 2,
+};
+
+static int tdx_ide_stream_km(struct tdx_tsm_link *tlink,
+			     enum tdx_ide_stream_km_op op)
+{
+	u64 sret, out_msg_sz;
+	int ret;
+
+	do {
+		sret = tdh_ide_stream_km(tlink->spdm_id, tlink->stream_id, op,
+					 tlink->in_msg, tlink->out_msg,
+					 &out_msg_sz);
+		ret = tdx_spdm_xfer_handler(tlink, sret, out_msg_sz);
+	} while (ret == -EAGAIN);
+
+	return ret;
+}
+
+static int tdx_ide_stream_key_program(struct tdx_tsm_link *tlink)
+{
+	return tdx_ide_stream_km(tlink, TDX_IDE_STREAM_KM_SETUP);
+}
+
+static void tdx_ide_stream_key_stop(struct tdx_tsm_link *tlink)
+{
+	tdx_ide_stream_km(tlink, TDX_IDE_STREAM_KM_STOP);
+}
+
 static void sel_stream_block_regs(struct pci_dev *pdev, struct pci_ide *ide,
 				  struct pci_ide_regs *regs)
 {
@@ -439,10 +470,25 @@ static int tdx_ide_stream_setup(struct tdx_tsm_link *tlink)
 	ide->partner[PCI_IDE_EP].default_stream = 1;
 	pci_ide_stream_setup(pdev, ide);
 
+	/* Key Programming for RP & target device, enable IDE stream for RP */
+	ret = tdx_ide_stream_key_program(tlink);
+	if (ret)
+		goto out_pci_ide_stream_teardown;
+
+	/* Enable IDE stream for target device */
+	ret = pci_ide_stream_enable(pdev, ide);
+	if (ret)
+		goto out_tdx_ide_stream_key_stop;
+
 	tlink->ide = ide;
 
 	return 0;
 
+out_tdx_ide_stream_key_stop:
+	tdx_ide_stream_key_stop(tlink);
+out_pci_ide_stream_teardown:
+	pci_ide_stream_teardown(pdev, ide);
+	pci_ide_stream_unregister(ide);
 out_tdx_ide_stream_delete:
 	tdx_ide_stream_delete(tlink);
 out_pci_ide_stream_free:
@@ -455,6 +501,8 @@ static void tdx_ide_stream_teardown(struct tdx_tsm_link *tlink)
 {
 	struct pci_ide *ide = tlink->ide;
 
+	pci_ide_stream_disable(ide->pdev, ide);
+	tdx_ide_stream_key_stop(tlink);
 	pci_ide_stream_teardown(ide->pdev, ide);
 	pci_ide_stream_unregister(ide);
 	tdx_ide_stream_delete(tlink);
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 7/7] x86/virt/tdx: Enable PCIe link encryption
  2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
                   ` (5 preceding siblings ...)
  2026-11-16 11:10 ` [RFC PATCH 6/7] coco/tdx-host: Program IDE keys and enable the " Chao Gao
@ 2026-11-16 11:10 ` Chao Gao
  6 siblings, 0 replies; 8+ messages in thread
From: Chao Gao @ 2026-11-16 11:10 UTC (permalink / raw)
  To: linux-kernel, linux-coco, kvm
  Cc: rick.p.edgecombe, kevin.tian, yilun.xu, chao.p.peng, baolu.lu,
	chao.gao, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Kiryl Shutsemau

The basic SPDM session and IDE functionalities are all implemented,
configure the TDX Module to enable the TDISP feature and in turn enable
PCIe link encryption.

Based on work by Yilun.

Signed-off-by: Chao Gao <chao.gao@intel.com>
---
 arch/x86/include/asm/tdx.h  | 2 +-
 arch/x86/virt/vmx/tdx/tdx.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index add83e7b9c36..c051f2ae82e6 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -147,7 +147,7 @@ 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 */
+	return true;
 }
 
 int tdx_guest_keyid_alloc(void);
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index d661de8230cf..9e87484283b8 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -1043,7 +1043,7 @@ struct tdmr_info_pa_array {
 };
 
 /* List all kernel-supported add-on features0 bits here */
-#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0	(0)
+#define TDX_KERNEL_SUPPORTED_ADDON_FEATURES0	(TDX_FEATURES0_TDXCONNECT)
 
 static __init u64 get_tdx_usable_addon_features0(void)
 {
-- 
2.52.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-16 11:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 11:10 [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Chao Gao
2026-11-16 11:10 ` [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session Chao Gao
2026-11-16 11:10 ` [RFC PATCH 2/7] coco/tdx-host: Establish an SPDM session with the device Chao Gao
2026-11-16 11:10 ` [RFC PATCH 3/7] coco/tdx-host: Exchange SPDM messages through DOE Chao Gao
2026-11-16 11:10 ` [RFC PATCH 4/7] coco/tdx-host: Pass a device info buffer to tdh_spdm_connect() Chao Gao
2026-11-16 11:10 ` [RFC PATCH 5/7] coco/tdx-host: Configure an IDE stream Chao Gao
2026-11-16 11:10 ` [RFC PATCH 6/7] coco/tdx-host: Program IDE keys and enable the " Chao Gao
2026-11-16 11:10 ` [RFC PATCH 7/7] x86/virt/tdx: Enable PCIe link encryption Chao Gao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®