Re: [RFC PATCH v2 1/5] PCI/TSM: Create MMIO descriptors via TDISP Report
From: Xu Yilun
Date: Fri Nov 13 2026 - 12:49:05 EST
On Thu, Nov 05, 2026 at 03:29:05PM +0000, Ankit Agrawal wrote:
> After pci_tsm_bind() and pci_tsm_lock() the low level TSM driver is
> expected to populate the TDISP GET_DEVICE_INTERFACE_REPORT response
> payload for the device.
>
> Add pci_tsm_mmio_alloc()/pci_tsm_mmio_free() to parse that report into
> a set of encrypted MMIO ranges, and pci_tsm_mmio_setup()/
> pci_tsm_mmio_teardown() to mark those ranges as encrypted in iomem via
> a new encrypted_iomem_resource tree (IORES_DESC_ENCRYPTED). With those
> descriptors the TSM driver can use pci_tsm_mmio_setup() to inform
> ioremap() how to map the device per the device expectations. The VM
> is expected to validate the interface with the relying party before
> accepting the device for operation.
>
> pci_tsm_mmio_alloc() also recovers the obfuscated starting address for
> each encrypted MMIO range, since the VM is never disclosed the HPA
> that correlates to the GPA of the device's MMIO. The obfuscated
I think we need to make clear why we need to parse the obfuscated
addresses like this. IIUC what the guest really needs are the BarID &
the offset within the Bar, so that it knows exactly where the MMIO range
lies. That requires the obfuscation never affects the address bits
for the in-Bar offset. Thus the MRO should be Bar aligned. And since
there is only one MRO across all MMIO ranges, it has to be aligned to the
largest Bar size.
> address is BAR aligned.
The obfuscated address is not BAR aligned, the MRO should be Bar aligned.
>
> BAR alignment is not mandated by the PCI TDISP specification. So this
> relies on the TSM implementation: the MMIO reporting offset (MRO) it
> programs must be a multiple of the largest BAR size so that the
> in-BAR offset of a reported range can be recovered as
> (reported address % BAR size). Arm CCA specifies this in DEN0137
> section A9.6.2 ("This algorithm assumes that the MRO value is a
> multiple of the BAR size") and recommends the RMM pick
> MRO = x * 2^n with 2^n >= the largest BAR size.
>
I think the following doesn't belong to the changelog, could be put
under ---
> Based on an original patch by Aneesh Kumar K.V [1], and cherry-picked
> from Dan Williams' upstream commit [2]. Major functional differences
> from Dan's posting is due to the lack of evidence-store infrastructure
> yet (device_evidence / DEVICE_EVIDENCE_TYPE_REPORT). The
> pci_tsm_mmio_alloc() takes the raw report bytes directly from the caller
> instead of pulling them from a device evidence store.
>
> Link: https://lore.kernel.org/linux-coco/20251117140007.122062-8-aneesh.kumar@xxxxxxxxxx/ [1]
> Link: https://lore.kernel.org/all/20260705220819.2472765-15-djbw@xxxxxxxxxx/ [2]
>
> Signed-off-by: Ankit Agrawal <ankita@xxxxxxxxxx>
> Reviewed-by: Bradley Morgan <brads@xxxxxxxxxxxxxx>
> ---
> drivers/pci/tsm.c | 239 ++++++++++++++++++++++++++++++++++++++++
> include/linux/ioport.h | 2 +
> include/linux/pci-tsm.h | 33 ++++++
> kernel/resource.c | 8 ++
> 4 files changed, 282 insertions(+)
>
> diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
> index 5fdcd7f2e820..d074bfd18d40 100644
> --- a/drivers/pci/tsm.c
> +++ b/drivers/pci/tsm.c
> @@ -9,11 +9,16 @@
> #define dev_fmt(fmt) "PCI/TSM: " fmt
>
> #include <linux/bitfield.h>
> +#include <linux/overflow.h>
> #include <linux/pci.h>
> #include <linux/pci-doe.h>
> #include <linux/pci-tsm.h>
> +#include <linux/range.h>
> +#include <linux/sizes.h>
> +#include <linux/slab.h>
> #include <linux/sysfs.h>
> #include <linux/tsm.h>
> +#include <linux/unaligned.h>
Didn't find it needed.
> #include <linux/xarray.h>
> #include "pci.h"
[...]
> +int pci_tsm_mmio_setup(struct pci_dev *pdev, struct pci_tsm_mmio *mmio)
> +{
> + int i;
> +
> + device_lock_assert(&pdev->dev);
> + if (pdev->dev.driver)
> + return -EBUSY;
> +
> + for (i = 0; i < mmio->nr; i++) {
> + struct resource *res = pci_tsm_mmio_resource(mmio, i);
> + int j;
> +
> + if (resource_size(res) == 0 || !(res->flags & IORESOURCE_MEM))
> + break;
> +
> + /* Only require the caller to set the range, init remainder */
> + *res = DEFINE_RES_NAMED_DESC(res->start, resource_size(res),
> + pci_name(pdev), IORESOURCE_MEM,
> + IORES_DESC_ENCRYPTED);
> +
> + for (j = 0; j < PCI_NUM_RESOURCES; j++)
You've changed to pci_dev_for_each_resource in Patch 3, why not change
it here? Then Patch 3 would be clearer.
> + if (resource_contains(pci_resource_n(pdev, j), res))
> + break;
> +
> + /* Request is outside of device MMIO */
> + if (j >= PCI_NUM_RESOURCES)
> + break;
> +
> + if (insert_resource(&encrypted_iomem_resource, res) != 0)
> + break;
> + }
> +
> + if (i == mmio->nr)
> + return 0;
> +
> + mmio_teardown(mmio, i);
> +
> + return -EINVAL;
> +}
> +EXPORT_SYMBOL_GPL(pci_tsm_mmio_setup);
> +
[...]
> +/**
> + * struct pci_tsm_mmio_entry - an encrypted MMIO range
> + * @res: MMIO address range (typically Guest Physical Address, GPA)
> + * @tsm_offset: Host Physical Address, HPA obfuscation offset added by the TSM.
> + * Translates report addresses to GPA.
> + */
> +struct pci_tsm_mmio_entry {
> + struct resource res;
> + u64 tsm_offset;
As mentioned [1], could you replace tsm_offset by mmio_range_index
according to this patch [2]
[1] https://lore.kernel.org/all/atIY1Joa%2F%2F+hCRcr@yilunxu-OptiPlex-7050/
[2] https://lore.kernel.org/all/20260924041032.1096569-8-zhenzhong.duan@xxxxxxxxx/
Others LGTM.
BTW: Is it possible we merge this patch alone when we collect enough
reviews? This is about PCIe TSM and several other series depend on it.
Thanks,
Yilun