From: Chao Gao <chao.gao@intel.com>
To: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev,
kvm@vger.kernel.org
Cc: rick.p.edgecombe@intel.com, kevin.tian@intel.com,
yilun.xu@intel.com, chao.p.peng@intel.com,
baolu.lu@linux.intel.com, chao.gao@intel.com,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Kiryl Shutsemau <kas@kernel.org>
Subject: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
Date: Mon, 16 Nov 2026 03:10:19 -0800 [thread overview]
Message-ID: <20261116111037.3478844-2-chao.gao@intel.com> (raw)
In-Reply-To: <20261116111037.3478844-1-chao.gao@intel.com>
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
next prev parent reply other threads:[~2026-11-16 11:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116111037.3478844-2-chao.gao@intel.com \
--to=chao.gao@intel.com \
--cc=baolu.lu@linux.intel.com \
--cc=bp@alien8.de \
--cc=chao.p.peng@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kas@kernel.org \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=yilun.xu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®