* [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
@ 2026-11-16 11:10 ` Chao Gao
2026-11-21 1:35 ` [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O Edgecombe, Rick P
1 sibling, 0 replies; 3+ 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, ®s);
+ 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 related [flat|nested] 3+ messages in thread* Re: [RFC PATCH 0/7] TDX: Enable PCIe link encryption for trusted I/O
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 5/7] coco/tdx-host: Configure an IDE stream Chao Gao
@ 2026-11-21 1:35 ` Edgecombe, Rick P
1 sibling, 0 replies; 3+ messages in thread
From: Edgecombe, Rick P @ 2026-11-21 1:35 UTC (permalink / raw)
To: kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org, Gao, Chao, x86@kernel.org,
linux-pci@vger.kernel.org
Cc: Xu, Yilun, dave.hansen@linux.intel.com, baolu.lu@linux.intel.com,
Peng, Chao P, bp@alien8.de, kas@kernel.org, mingo@redhat.com,
hpa@zytor.com, tglx@kernel.org, Tian, Kevin, bhelgaas@google.com
On Mon, 2026-11-16 at 03:10 -0800, Chao Gao wrote:
> == 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?
Why can't the TDX module return what we need? I guess AMD's firmware must
already return just the payload. Any idea what ARM will do?
>
> 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?
Not very sure on this point. Do you think the TSM drivers would never want to
pick a different default stream? If they did is there a point to moving it?
Might be better to wait to see how it shakes out.
>
> == 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.
Can you describe how it impacts the initial support here? Is it about enabling
more devices, or simplifying the initial code?
>
> 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.
Hmm, usually TDX is not easy to enable on a system. So users are not likely to
accidentally hit the existing limitations. So I'm thinking we just do the same
here. Document the limits and not add a switch. But the limits should be cleanly
handled and understood. Which is a WIP.
^ permalink raw reply [flat|nested] 3+ messages in thread