Re: [RFC PATCH 1/7] coco/tdx-host: Create metadata for an SPDM session
From: Edgecombe, Rick P
Date: Fri Nov 20 2026 - 21:06:18 EST
On Mon, 2026-11-16 at 03:10 -0800, Chao Gao wrote:
> 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.
Since we usually talk about metadata and mean the global metadata, we will
probably need to explain what session metadata is. Is it just data?
>
> 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.
>
This is getting to be a pretty familiar concept. TDX needs per-something scoped
memory. Give it to the TDX module per some seamcall. Can we make it sounds
especially ordinary and then just call out any thing that is different?
> 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.
When would it fail?
>
> Based on work by Yilun and Zhenzhong.
>
> Signed-off-by: Chao Gao <chao.gao@xxxxxxxxx>
> ---
> 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;
> +
We'll hopefully see this hunk get updated once the metadata cleanup series
lands.
> 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;
> +}
Is this TDISP specific encoding or a TDX-TDISP specific encoding? I think TDISP.
There isn't a more general place to put it?
> +
> 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);
Why 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);
Hmm, its not clear from this patch why we would need so many levels of wrapper
for the setup/teardown seamcall. But it grows in later patches. But it looks
weird in this patch and will probably throw off the tip review.
> +}
> +
> +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);
Why is this here instead of tdx_spdm_create()?
> return &no_free_ptr(tlink)->pci.base_tsm;
> }
>