Skip to contentSkip to conversation

PCI/TSM: Resolve TDISP coherent (CXL) ranges from precommitted HDM decoders

RFC PATCH In review Ankit Agrawal 4 participants 13 messages Nov 5 – Nov 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover PCI/TSM: Resolve TDISP coherent (CXL) ranges from precommitted HDM decoders

=== Background ===

TDISP (TEE Device Interface Security Protocol) lets a Confidential Compute Architecture (CCA) guest establish a trusted, encrypted MMIO mapping to an assigned PCI device. The device's TSM (TEE Security Manager) reports the device interface's MMIO layout back to the guest via a GET_DEVICE_INTERFACE_REPORT response. The guest kernel should parse this report and get the IPA for the regions present in the report. This is required to validate the device regions.

CXL (Compute Express Link) runs over the PCIe physical/electrical layer but adds protocols beyond plain PCIe config/MMIO access. The one relevant here is CXL.mem on Type-2 CXL devices, which lets a device expose device-attached memory (e.g. HBM on a GPU) to the host as regular cacheable, coherent system memory (called CXL region in this series).

A CXL type 2 device exposes this CXL region through one or more HDM (Host-managed Device Memory) decoders - hardware on the device that maps a System Physical Address (SPA) range, as seen by the host/guest, to the corresponding Device Physical Address (DPA) range on the device's own memory. The host (or VMM/firmware) programs an HDM decoder's SPA (IPA) base and size, then "commits" it - after which reads/writes into that range are transparently steered by the decoder to the device's memory. This is conceptually similar to a BAR, as both are ways of mapping device resources into a physical address range that software can access. However, while a BAR is discovered and sized generically via PCI config space, an HDM decoder's mapping is CXL-specific and is not visible in config space or backed by any BAR.

On the systems this series targets, the HDM decoder for the coherent window is "precommitted": platform firmware programs and locks it before any software - including the guest OS - ever runs, and the mapping is fixed and stable across boots (i.e. the guest never needs to program or resize it itself - it only needs to know where the firmware already put it). Because this window has no BAR, the TDISP interface report describes it with a special sentinel range ID instead of a BAR number.

=== Implementation ===

This series does two things: it first introduces the interface-report parsing infrastructure itself, adapted from out-of-tree work [1] originally developed by Dan Williams and Aneesh Kumar and built around the common BAR-based case, and then extends that same infrastructure to also understand firmware-precommitted CXL coherent ranges which cannot be expressed as a BAR. Concretely, in four steps:

1. "PCI/TSM: Create MMIO descriptors via TDISP Report" Introduces the report-parsing infrastructure itself. Only BAR-relative ranges are handled at this point. Cherry-picked and adapted from Dan Williams' posted series [1] (based on Aneesh Kumar K.V [2], co-developed with Xu Yilun), with the evidence-store dependency removed.

2. "PCI/CXL: Populate and insert/remove pdev->coh_resource[]" Snapshots the precommitted HDM decoder's SPA base/size into a new pdev->coh_resource[] array early during CXL enumeration and inserts/removes each entry into/from iomem_resource so the range is reserved like any other device resource, giving later patches a known-good source of truth for the coherent window.

3. "PCI/TSM: Derive the coherent-range IPA from CXL"

    Extends the patch 1 parser to recognize the coherent-range
    sentinel range ID (0xffff) and resolve its address from
    pdev->coh_resource[] instead of a BAR offset.

4. "PCI/TSM: Support multiple coherent ranges via coh_idx"

    Generalizes the spec defined single 0xffff sentinel to a small
    index space so devices with multiple HDM decoders can report
    several coherent windows, indexing into pdev->coh_resource[].

5. "cxl: Invalidate cached coherent range when Normalized Addressing is detected"
Addresses the Normalized Addressing concern raised on v1.

=== Dependencies ===

This series is rebased on top of next-20261103, and requires Srirangan Madhavan's v14 "CXL HDM decoder state caching across PCI reset" series [4] that is also undergoing review.

=== Open design questions ===

- Patch 2 adds pdev->coh_resource[PCI_CXL_MAX_COHERENT_RANGES] (10 struct resource entries, ~640 bytes) unconditionally to struct pci_dev under CONFIG_CXL_RESET regardless of whether a given device is actually CXL-capable. Feedback is welcome on whether this should instead be allocated dynamically.

=== Testing ===

This series has been verified using an adapted version of Dan Williams devsec tool [3], modified to exercise the coherent-range reporting path added here.

=== Tooling ===

An LLM coding assistant (Claude) was used in preparing this series. I have reviewed all of the code.

=== Changes since v1 [5] ===

- Rebased to next-20261103.
- Use a strict "i == mmio->nr" check when parsing the report (Nikolay Borisov, Xu Yilun) - 1/5
- Document that BAR alignment of the obfuscated address relies on the TSM (MRO a multiple of the largest BAR size), citing Arm DEN0137 A9.6.2 (Xu Yilun) - 1/5
- Only record HDM decoders that are enabled and locked and skip zero-sized ones (Richard Cheng) - 2/5
- Rename pci_tsm_coherent_range() to pci_tsm_retrieve_coherent_range() and return a struct range (Dave Jiang, Andy Shevchenko) - 3/5
- Gate the code on a new CONFIG_PCI_TSM_CXL_COHERENT (depends on PCI_TSM && CXL_RESET) and move the ifdef to pci-tsm.h (Dave Jiang). - 3/5
- Use pci_dev_for_each_resource() for BAR containment and fix style issues (Andy Shevchenko). - 3/5
- Handle Normalized Addressing through a new patch 5/5 to invalidate the cached range once it is set (Terry Bowman) - 5/5
- Drop the Assisted-by trailer (Andy Shevchenko)

Ankit Agrawal (5):
PCI/TSM: Create MMIO descriptors via TDISP Report PCI/CXL: Populate and insert/remove pdev->coh_resource[] PCI/TSM: Derive the coherent-range IPA from CXL
PCI/TSM: Support multiple coherent ranges via coh_idx
cxl: Invalidate cached coherent range when Normalized Addressing is
detected

 drivers/cxl/core/atl.c      |   6 +
 drivers/cxl/core/core.h     |   9 ++
 drivers/cxl/core/resource.c |  92 ++++++++++-
 drivers/pci/Kconfig         |  12 ++
 drivers/pci/tsm.c           | 298 ++++++++++++++++++++++++++++++++++++
 include/linux/ioport.h      |   2 +
 include/linux/pci-tsm.h     | 112 ++++++++++++++
 include/linux/pci.h         |  14 ++
 kernel/resource.c           |   8 +
 9 files changed, 552 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
#1 lore raw

RFC PATCH v2 1/5 PCI/TSM: Create MMIO descriptors via TDISP Report

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 address is 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.

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.

---
 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(+)
drivers/pci/tsm.c +239 -0
index 5fdcd7f2e820..d074bfd18d40 100644
@@ -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>
 #include <linux/xarray.h>
 #include "pci.h"
 
@@ -898,3 +903,237 @@ int pci_tsm_doe_transfer(struct pci_dev *pdev, u8 type, const void *req,
 		       resp, resp_sz);
 }
 EXPORT_SYMBOL_GPL(pci_tsm_doe_transfer);
+
+static void mmio_teardown(struct pci_tsm_mmio *mmio, int nr)
+{
+	while (nr--) {
+		struct resource *res = pci_tsm_mmio_resource(mmio, nr);
+
+		/*
+		 * Entries past the point where pci_tsm_mmio_setup() stopped
+		 * (or that were never run through setup() at all, e.g. a
+		 * caller that only wants the resolved range from
+		 * pci_tsm_mmio_alloc()) were never insert_resource()'d, so
+		 * res->parent is NULL. remove_resource() dereferences
+		 * res->parent unconditionally.
+		 */
+		if (res->parent)
+			remove_resource(res);
+	}
+}
+
+/**
+ * pci_tsm_mmio_setup() - mark device MMIO as encrypted in iomem
+ * @pdev: device owner of MMIO resources
+ * @mmio: container of an array of resources to mark encrypted
+ *
+ * Return: 0 on success, -EBUSY if a driver is bound to @pdev, -EINVAL if a
+ * range is invalid, outside the device MMIO or cannot be inserted.
+ */
+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++)
+			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);
+
+void pci_tsm_mmio_teardown(struct pci_tsm_mmio *mmio)
+{
+	mmio_teardown(mmio, mmio->nr);
+}
+EXPORT_SYMBOL_GPL(pci_tsm_mmio_teardown);
+
+/*
+ * PCIe ECN TEE Device Interface Security Protocol (TDISP)
+ *
+ * Device Interface Report data object layout as defined by PCIe r7.0 section
+ * 11.3.11
+ */
+#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_TABLE BIT(0)
+#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_MSIX_PBA BIT(1)
+#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE BIT(2)
+#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_UPDATABLE BIT(3)
+#define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID GENMASK(31, 16)
+
+/* An interface report 'pfn' is 4K in size */
+struct pci_tsm_devif_mmio {
+	__le64 phys;
+	__le32 nr_pfns;
+	__le32 attributes;
+};
+
+struct pci_tsm_devif_report {
+	__le16 interface_info;
+	__le16 reserved;
+	__le16 msi_x_message_control;
+	__le16 lnr_control;
+	__le32 tph_control;
+	__le32 mmio_range_count;
+	struct pci_tsm_devif_mmio mmio[];
+};
+
+/**
+ * pci_tsm_mmio_alloc() - allocate encrypted MMIO range descriptor
+ * @pdev: device owner of MMIO ranges
+ * @report: raw TDISP Device Interface Report bytes (PCIe r7.0 section
+ *	    11.3.11), e.g. as returned by GET_DEVICE_INTERFACE_REPORT
+ * @report_len: length of @report in bytes
+ *
+ * Return: the encrypted MMIO range descriptor on success, NULL on failure
+ *
+ * Unlike upstream, @report is supplied directly by the caller rather than
+ * pulled from a device evidence store, which this tree does not yet have.
+ */
+struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
+					const void *report, size_t report_len)
+{
+	const struct pci_tsm_devif_report *devif_report = report;
+	u64 reporting_bar_base, last_reporting_end;
+	u32 mmio_range_count;
+	int last_bar = -1;
+	int i;
+
+	if (report_len < sizeof(*devif_report))
+		return NULL;
+
+	mmio_range_count = __le32_to_cpu(devif_report->mmio_range_count);
+
+	/* check that the report is self-consistent on mmio entries */
+	if (report_len < struct_size(devif_report, mmio, mmio_range_count))
+		return NULL;
+
+	/* create pci_tsm_mmio descriptors from the report data */
+	struct pci_tsm_mmio *mmio __free(kfree) =
+		kzalloc_flex(*mmio, mmio, mmio_range_count);
+	if (!mmio)
+		return NULL;
+
+	for (i = 0; i < mmio_range_count; i++) {
+		u64 range_off;
+		struct range range;
+		const struct pci_tsm_devif_mmio *mmio_data = &devif_report->mmio[i];
+		struct pci_tsm_mmio_entry *entry =
+			pci_tsm_mmio_entry(mmio, mmio->nr);
+		u64 tsm_offset = __le64_to_cpu(mmio_data->phys);
+		u64 size = (u64)__le32_to_cpu(mmio_data->nr_pfns) * SZ_4K;
+		u32 attr = __le32_to_cpu(mmio_data->attributes);
+		int bar = FIELD_GET(PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID,
+				    attr);
+
+		if (bar >= PCI_STD_NUM_BARS ||
+		    !(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
+		    (pci_resource_flags(pdev, bar) & IORESOURCE_UNSET)) {
+			pci_dbg(pdev, "Invalid reporting bar ID %d\n", bar);
+			return NULL;
+		}
+
+		if (last_bar > bar) {
+			pci_dbg(pdev, "Reporting bar ID not in ascending order\n");
+			return NULL;
+		}
+
+		if (last_bar < bar) {
+			resource_size_t mask = pci_resource_len(pdev, bar) - 1;
+
+			/* Transition to a new bar */
+			last_bar = bar;
+
+			/*
+			 * Determine the obfuscated base of the BAR. BAR
+			 * offsets are never obfuscated.
+			 */
+			reporting_bar_base = tsm_offset & ~mask;
+		} else if (tsm_offset < last_reporting_end) {
+			pci_dbg(pdev, "Reporting ranges within BAR not in ascending order\n");
+			return NULL;
+		}
+
+		/* Per spec the tsm_offset never results in overflow / underflow */
+		last_reporting_end = tsm_offset + size;
+		if (last_reporting_end < tsm_offset) {
+			pci_dbg(pdev, "Reporting range overflow\n");
+			return NULL;
+		}
+
+		range_off = tsm_offset - reporting_bar_base;
+		if (pci_resource_len(pdev, bar) < range_off + size) {
+			pci_dbg(pdev, "Reporting range larger than BAR size\n");
+			return NULL;
+		}
+
+		range.start = pci_resource_start(pdev, bar) + range_off;
+		range.end = range.start + size - 1;
+
+		/* Only record the TEE ranges for later consideration by ioremap() */
+		if (FIELD_GET(PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE,
+			      attr)) {
+			pci_dbg(pdev, "Skipping non-TEE range, BAR%d %pra\n",
+				bar, &range);
+			continue;
+		}
+
+		entry->res.start = range.start;
+		entry->res.end = range.end;
+		entry->res.flags = IORESOURCE_MEM;
+		entry->tsm_offset = tsm_offset;
+		mmio->nr++;
+	}
+
+	return_ptr(mmio);
+}
+EXPORT_SYMBOL_GPL(pci_tsm_mmio_alloc);
+
+/**
+ * pci_tsm_mmio_free() - free a pci_tsm_mmio instance
+ * @pdev: device owner of MMIO ranges
+ * @mmio: instance to free
+ *
+ * Return: 0 if @mmio was idle on entry, -EBUSY otherwise
+ */
+int pci_tsm_mmio_free(struct pci_dev *pdev, struct pci_tsm_mmio *mmio)
+{
+	for (int i = 0; i < mmio->nr; i++) {
+		struct resource *res = pci_tsm_mmio_resource(mmio, i);
+
+		if (dev_WARN_ONCE(&pdev->dev, resource_assigned(res),
+				  "MMIO resource still assigned %pr\n", res))
+			return -EBUSY;
+	}
+	kfree(mmio);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_tsm_mmio_free);
include/linux/ioport.h +2 -0
index f7930b3dfd0a..122f1eefb4b9 100644
@@ -144,6 +144,7 @@ enum {
 	IORES_DESC_RESERVED			= 7,
 	IORES_DESC_SOFT_RESERVED		= 8,
 	IORES_DESC_CXL				= 9,
+	IORES_DESC_ENCRYPTED			= 10,
 };
 
 /*
@@ -240,6 +241,7 @@ struct resource_constraint {
 extern struct resource ioport_resource;
 extern struct resource iomem_resource;
 extern struct resource soft_reserve_resource;
+extern struct resource encrypted_iomem_resource;
 
 extern struct resource *request_resource_conflict(struct resource *root, struct resource *new);
 extern int request_resource(struct resource *root, struct resource *new);
include/linux/pci-tsm.h +33 -0
index a6435aba03f9..e54ad057fa8e 100644
@@ -199,6 +199,34 @@ enum pci_tsm_req_scope {
 	PCI_TSM_REQ_DEBUG_WRITE = 3,
 };
 
+/**
+ * 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;
+};
+
+struct pci_tsm_mmio {
+	int nr;
+	struct pci_tsm_mmio_entry mmio[];
+};
+
+static inline struct pci_tsm_mmio_entry *
+pci_tsm_mmio_entry(struct pci_tsm_mmio *mmio, int idx)
+{
+	return &mmio->mmio[idx];
+}
+
+static inline struct resource *pci_tsm_mmio_resource(struct pci_tsm_mmio *mmio,
+						     int idx)
+{
+	return &mmio->mmio[idx].res;
+}
+
 #ifdef CONFIG_PCI_TSM
 int pci_tsm_register(struct tsm_dev *tsm_dev);
 void pci_tsm_unregister(struct tsm_dev *tsm_dev);
@@ -216,6 +244,11 @@ void pci_tsm_tdi_constructor(struct pci_dev *pdev, struct pci_tdi *tdi,
 ssize_t pci_tsm_guest_req(struct pci_dev *pdev, enum pci_tsm_req_scope scope,
 			  sockptr_t req_in, size_t in_len, sockptr_t req_out,
 			  size_t out_len, u64 *tsm_code);
+int pci_tsm_mmio_setup(struct pci_dev *pdev, struct pci_tsm_mmio *mmio);
+void pci_tsm_mmio_teardown(struct pci_tsm_mmio *mmio);
+struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
+					const void *report, size_t report_len);
+int pci_tsm_mmio_free(struct pci_dev *pdev, struct pci_tsm_mmio *mmio);
 #else
 static inline int pci_tsm_register(struct tsm_dev *tsm_dev)
 {
kernel/resource.c +8 -0
index 0c315c917fbb..7bdf3c2ac141 100644
@@ -56,6 +56,14 @@ struct resource soft_reserve_resource = {
 	.flags	= IORESOURCE_MEM,
 };
 
+struct resource encrypted_iomem_resource = {
+	.name	= "Encrypted MMIO",
+	.start	= 0,
+	.end	= -1,
+	.desc	= IORES_DESC_ENCRYPTED,
+	.flags	= IORESOURCE_MEM,
+};
+
 static DEFINE_RWLOCK(resource_lock);
 
 /*
Signature · 1 line
2.53.0
#2 lore raw

Re: [RFC PATCH v2 1/5] PCI/TSM: Create MMIO descriptors via TDISP Report

Quoted 13 lines · Ankit Agrawal

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.

Quoted 3 lines

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.

Quoted 1 line

address is BAR aligned.

The obfuscated address is not BAR aligned, the MRO should be Bar aligned.

Quoted 10 lines

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 ---

Quoted 27 lines

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.

---
 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(+)
drivers/pci/tsm.c +0 -0
index 5fdcd7f2e820..d074bfd18d40 100644
@@ -9,11 +9,16 @@
 #define dev_fmt(fmt) "PCI/TSM: " fmt
 
 #include <linux/bitfield.h>

drivers/pci/tsm.c line 21

Quoted 10 lines
drivers/pci/tsm.c +5 -0
+#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.

Quoted 2 lines
drivers/pci/tsm.c +0 -0
 #include <linux/xarray.h>
 #include "pci.h"

[...]

Quoted 11 lines
drivers/pci/tsm.c +11 -0
+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;

drivers/pci/tsm.c int pci_tsm_doe_transfer(struct pci_dev *pdev, u8 type, const void *req, · line 953

Quoted 10 lines
drivers/pci/tsm.c +10 -0
+
+		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.

Quoted 20 lines
drivers/pci/tsm.c +20 -0
+			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);
+

[...]

drivers/pci/tsm.c

Quoted 9 lines
drivers/pci/tsm.c +9 -0
+/**
+ * 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/[email protected]/

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

#3 lore raw

RFC PATCH v2 2/5 PCI/CXL: Populate and insert/remove pdev->coh_resource[]

In reply to Ankit Agrawal

On systems with precommitted HDM decoders, the firmware programs the decoders before any software runs. This range also stays constant over the runs.

Snapshot precommitted CXL HDM decoder ranges into a new pdev->coh_resource[] array, so later code has a known-good source for a device coherent (CXL) windows without re-scanning decoder registers.

Only decoders that are enabled and locked are recorded, since an unlocked decoder can be reprogrammed and leave the snapshot stale. Zero-sized decoders are skipped. Decoders found to use Normalized Addressing (a DPA rather than an HPA) are dropped later, once the flag is known in a follow-up patch.

---
 drivers/cxl/core/resource.c | 60 ++++++++++++++++++++++++++++++++++++-
 include/linux/pci.h         | 14 +++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
drivers/cxl/core/resource.c +59 -1
index 3422139ae3ab..d837f8e952a9 100644
@@ -404,6 +404,54 @@ static struct cxl_hdm_info *cxl_pci_hdm_read_info(struct pci_dev *pdev,
 	return ERR_PTR(pcibios_err_to_errno(rc));
 }
 
+/*
+ * Snapshot committed decoders into pdev->coh_resource[] which is called
+ * once per pdev before any driver can bind, so a populated entry is
+ * always firmware-committed and not driver-committed.
+ *
+ * Insert each entry into iomem_resource for /proc/iomem visibility and
+ * conflict detection.
+ */
+static void cxl_populate_coh_resource(struct pci_dev *pdev,
+				      struct cxl_hdm_info *info)
+{
+	int count = min(info->decoder_count, PCI_CXL_MAX_COHERENT_RANGES);
+
+	for (int i = 0; i < count; i++) {
+		struct cxl_decoder_config *config = &info->settings[i].config;
+		struct resource *res = &pdev->coh_resource[i];
+		struct resource *conflict;
+		u64 size = range_len(&config->hpa_range);
+
+		/*
+		 * Only a decoder that is both enabled and locked has a fixed
+		 * mapping that won't change under the snapshot. Also skip a
+		 * zero-sized decoder, whose resource end would wrap.
+		 */
+		if ((config->flags &
+		     (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK)) !=
+			    (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK) ||
+		    !size)
+			continue;
+
+		*res = DEFINE_RES_NAMED_DESC(config->hpa_range.start, size,
+					     "CXL coherent memory",
+					     IORESOURCE_MEM, IORES_DESC_NONE);
+
+		conflict = insert_resource_conflict(&iomem_resource, res);
+		if (conflict) {
+			pci_warn(pdev,
+				 "CXL coherent range: decoder%d %pR conflicts with %s %pR, leaving unpopulated\n",
+				 i, res, conflict->name, conflict);
+			memset(res, 0, sizeof(*res));
+			continue;
+		}
+
+		pci_info(pdev, "CXL coherent range: decoder%d precommitted, populated %pR\n",
+			 i, res);
+	}
+}
+
 static int __pci_cxl_hdm_cache_init(struct pci_dev *pdev)
 {
 	struct cxl_register_map map = { };
@@ -442,8 +490,10 @@ static int __pci_cxl_hdm_cache_init(struct pci_dev *pdev)
 	struct cxl_hdm_info *info __free(kfree) = read_info;
 	guard(rwsem_write)(&cxl_rwsem.dpa);
 	/* Another initializer may have published while we read MMIO. */
-	if (!pdev->hdm)
+	if (!pdev->hdm) {
+		cxl_populate_coh_resource(pdev, info);
 		pdev->hdm = no_free_ptr(info);
+	}
 
 	return 0;
 }
@@ -466,6 +516,14 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
 	info = pdev->hdm;
 	/* Unpublish before freeing so subsequent readers cannot use stale state. */
 	pdev->hdm = NULL;
+
+	for (int i = 0; i < PCI_CXL_MAX_COHERENT_RANGES; i++) {
+		struct resource *res = &pdev->coh_resource[i];
+
+		if (res->parent)
+			remove_resource(res);
+	}
+
 	kfree(info);
 }
 
include/linux/pci.h +14 -0
index 0d24fc65c049..ad7917895f4c 100644
@@ -148,6 +148,13 @@ enum {
 	DEVICE_COUNT_RESOURCE = PCI_NUM_RESOURCES,
 };
 
+/*
+ * Max CXL HDM decoders tracked per endpoint in pci_dev->coh_resource[].
+ * CXL 3.1 spec section 8.2.4.20.1 - CXL HDM Decoder Capability Register,
+ * mention a max of 10 decoders for a CXL device.
+ */
+#define PCI_CXL_MAX_COHERENT_RANGES 10
+
 /**
  * enum pci_interrupt_pin - PCI INTx interrupt values
  * @PCI_INTERRUPT_UNKNOWN: Unknown or unassigned interrupt
@@ -573,6 +580,13 @@ struct pci_dev {
 #endif
 #ifdef CONFIG_CXL_RESET
 	struct cxl_hdm_info *hdm;	/* CXL HDM decoder state */
+	/*
+	 * Precommitted CXL HDM decoder coherent ranges, snapshotted before
+	 * any driver binds. One entry per decoder index. Only written by
+	 * pci_cxl_hdm_cache_init()/_release(), outside any bound driver's
+	 * read window.
+	 */
+	struct resource coh_resource[PCI_CXL_MAX_COHERENT_RANGES];
 #endif
 #ifdef CONFIG_PCI_NPEM
 	struct npem	*npem;		/* Native PCIe Enclosure Management */
Signature · 1 line
2.53.0
#4 lore raw

RFC PATCH v2 3/5 PCI/TSM: Derive the coherent-range IPA from CXL

In reply to Ankit Agrawal

The DevIf report range with range ID 0xffff describes the device's coherent (CXL) memory window and unlike every other reported range carries no BAR number. Recognize PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT and recover that window's address from the CXL side of the device instead of treating it as a BAR-relative offset.

pci_tsm_retrieve_coherent_range() resolves the window from pdev->coh_resource[], snapshotted once with the committed CXL HDM decoders range. The range to IPA conversion is applied onto range_base/range_len using pdev->coh_resource[]. The ascending BAR order checks stay on the BAR path only. The coherent range is required to be the last report entry and is not part of the BAR sequence.

An HDM decoded window lives in the CXL Fixed Memory Window and is not part of a BAR. So by construction, no pci_dev resource contains it. So the function pci_tsm_coh_resource_contains() validates the range against pdev->coh_resource[] instead; the same known-good snapshot pci_tsm_retrieve_coherent_range() already trusts.

---
 drivers/pci/Kconfig     |  12 ++++
 drivers/pci/tsm.c       | 119 ++++++++++++++++++++++++++++------------
 include/linux/pci-tsm.h |  75 +++++++++++++++++++++++++
 3 files changed, 170 insertions(+), 36 deletions(-)
drivers/pci/Kconfig +12 -0
index 16fbd4212e0f..22c416c84a6f 100644
@@ -139,6 +139,18 @@ config PCI_TSM
 
 	  Enable a platform TSM driver to use this capability.
 
+config PCI_TSM_CXL_COHERENT
+	bool "PCI TSM: CXL coherent range support"
+	depends on PCI_TSM && CXL_RESET
+	default y
+	help
+	  Enables helper functions to manage coherent regions on CXL devices
+	  for TSM. The device CXL coherent (CXL.mem) ranges could be tracked
+	  and fetched across subsystems to be reported in a TDISP Device
+	  Interface Report.
+
+	  If unsure, say Y.
+
 config PCI_DOE
 	bool "Enable PCI Data Object Exchange (DOE) support"
 	help
drivers/pci/tsm.c +83 -36

174 lines not on this page · raw message

include/linux/pci-tsm.h +75 -0

104 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

Re: [RFC PATCH v2 3/5] PCI/TSM: Derive the coherent-range IPA from CXL

Quoted 155 lines · Ankit Agrawal

The DevIf report range with range ID 0xffff describes the device's coherent (CXL) memory window and unlike every other reported range carries no BAR number. Recognize PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT and recover that window's address from the CXL side of the device instead of treating it as a BAR-relative offset.

pci_tsm_retrieve_coherent_range() resolves the window from pdev->coh_resource[], snapshotted once with the committed CXL HDM decoders range. The range to IPA conversion is applied onto range_base/range_len using pdev->coh_resource[]. The ascending BAR order checks stay on the BAR path only. The coherent range is required to be the last report entry and is not part of the BAR sequence.

An HDM decoded window lives in the CXL Fixed Memory Window and is not part of a BAR. So by construction, no pci_dev resource contains it. So the function pci_tsm_coh_resource_contains() validates the range against pdev->coh_resource[] instead; the same known-good snapshot pci_tsm_retrieve_coherent_range() already trusts.

---
 drivers/pci/Kconfig     |  12 ++++
 drivers/pci/tsm.c       | 119 ++++++++++++++++++++++++++++------------
 include/linux/pci-tsm.h |  75 +++++++++++++++++++++++++
 3 files changed, 170 insertions(+), 36 deletions(-)
drivers/pci/Kconfig +12 -0
index 16fbd4212e0f..22c416c84a6f 100644
@@ -139,6 +139,18 @@ config PCI_TSM
 
 	  Enable a platform TSM driver to use this capability.
 
+config PCI_TSM_CXL_COHERENT
+	bool "PCI TSM: CXL coherent range support"
+	depends on PCI_TSM && CXL_RESET
+	default y
+	help
+	  Enables helper functions to manage coherent regions on CXL devices
+	  for TSM. The device CXL coherent (CXL.mem) ranges could be tracked
+	  and fetched across subsystems to be reported in a TDISP Device
+	  Interface Report.
+
+	  If unsure, say Y.
+
 config PCI_DOE
 	bool "Enable PCI Data Object Exchange (DOE) support"
 	help
drivers/pci/tsm.c +36 -28
index d074bfd18d40..bc3218c58ff0 100644
@@ -939,8 +939,9 @@ int pci_tsm_mmio_setup(struct pci_dev *pdev, struct pci_tsm_mmio *mmio)
 		return -EBUSY;
 
 	for (i = 0; i < mmio->nr; i++) {
-		struct resource *res = pci_tsm_mmio_resource(mmio, i);
-		int j;
+		struct pci_tsm_mmio_entry *entry = pci_tsm_mmio_entry(mmio, i);
+		struct resource *res = &entry->res;
+		bool coherent = entry->flags & PCI_TSM_MMIO_F_COHERENT;
 
 		if (resource_size(res) == 0 || !(res->flags & IORESOURCE_MEM))
 			break;
@@ -950,13 +951,28 @@ int pci_tsm_mmio_setup(struct pci_dev *pdev, struct pci_tsm_mmio *mmio)
 					     pci_name(pdev), IORESOURCE_MEM,
 					     IORES_DESC_ENCRYPTED);
 
-		for (j = 0; j < PCI_NUM_RESOURCES; j++)
-			if (resource_contains(pci_resource_n(pdev, j), res))
+		/*
+		 * The coherent (CXL.mem) range isn't BAR-backed, so check it
+		 * against pdev->coh_resource[] instead of pci_resource_n().
+		 */
+		if (coherent) {
+			if (!pci_tsm_coh_resource_contains(pdev, res))
 				break;
-
-		/* Request is outside of device MMIO */
-		if (j >= PCI_NUM_RESOURCES)
-			break;
+		} else {
+			struct resource *r;
+			bool contained = false;
+
+			pci_dev_for_each_resource(pdev, r) {
+				if (resource_contains(r, res)) {
+					contained = true;
+					break;
+				}
+			}
+
+			/* Request is outside of device MMIO */
+			if (!contained)
+				break;
+		}
 
 		if (insert_resource(&encrypted_iomem_resource, res) != 0)
 			break;
@@ -988,6 +1004,7 @@ EXPORT_SYMBOL_GPL(pci_tsm_mmio_teardown);
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE BIT(2)
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_UPDATABLE BIT(3)
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID GENMASK(31, 16)
+#define PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT 0xffff
 
 /* An interface report 'pfn' is 4K in size */
 struct pci_tsm_devif_mmio {
@@ -1043,6 +1060,7 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 		return NULL;
 
 	for (i = 0; i < mmio_range_count; i++) {
+		struct range window;
 		u64 range_off;
 		struct range range;
 		const struct pci_tsm_devif_mmio *mmio_data = &devif_report->mmio[i];
@@ -1053,33 +1071,58 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 		u32 attr = __le32_to_cpu(mmio_data->attributes);
 		int bar = FIELD_GET(PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID,
 				    attr);
+		bool coherent =
+			bar == PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT;
 
-		if (bar >= PCI_STD_NUM_BARS ||
-		    !(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
-		    (pci_resource_flags(pdev, bar) & IORESOURCE_UNSET)) {
-			pci_dbg(pdev, "Invalid reporting bar ID %d\n", bar);
-			return NULL;
-		}
-
-		if (last_bar > bar) {
-			pci_dbg(pdev, "Reporting bar ID not in ascending order\n");
-			return NULL;
-		}
-
-		if (last_bar < bar) {
-			resource_size_t mask = pci_resource_len(pdev, bar) - 1;
-
-			/* Transition to a new bar */
-			last_bar = bar;
+		if (coherent) {
+			if (i != mmio_range_count - 1) {
+				pci_dbg(pdev, "Coherent reporting range is not last\n");
+				return NULL;
+			}
 
 			/*
-			 * Determine the obfuscated base of the BAR. BAR
-			 * offsets are never obfuscated.
+			 * No BAR names the coherent range, so fail closed if
+			 * neither a CXL region nor a committed HDM decoder
+			 * resolves its address.
 			 */
-			reporting_bar_base = tsm_offset & ~mask;

drivers/pci/tsm.c struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev, · line 1094

Quoted 10 lines
drivers/pci/tsm.c +7 -3
-		} else if (tsm_offset < last_reporting_end) {
-			pci_dbg(pdev, "Reporting ranges within BAR not in ascending order\n");
-			return NULL;
+			if (pci_tsm_retrieve_coherent_range(pdev, &window)) {
+				pci_dbg(pdev, "No CXL region or committed HDM decoder for coherent reporting range\n");
+				return NULL;
+			}
+
+			reporting_bar_base = tsm_offset &
+					     ~((u64)range_len(&window) - 1);

Hi Ankit,

HDM decoder window can have non-power-of-2 sizes, why do we assume a power-of-2 size for coherent window?

E.g. if we have something like the following
Window start: 4GB
Window size: 768 MB
Reported address: 4GB + 256 MB

Assume MMIO_REPORTING_OFFSET is 0, so no address obfuscation needs to be removed. The reported page is simply 256 MB into the window.

For this size, the mask leaves the reported address unchanged.

0x110000000 & ~(0x30000000 - 1) = 0x110000000

reporting_bar_base becomes 4GB + 256 MB, rather than actual window base of 4 GB, so range_off is 0.

The fianl address will be window.start + range_off = 4GB + 0

A KUnit test against your patch series can reproduce this.
Furthermore, pci_tsm_mmio_setup() accepts the wrong result, because the incorrect address is still insdie the window.

Best regards,
Richard Cheng.

Quoted 184 lines
drivers/pci/tsm.c +40 -5
+		} else {
+			if (bar >= PCI_STD_NUM_BARS ||
+			    !(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
+			    (pci_resource_flags(pdev, bar) & IORESOURCE_UNSET)) {
+				pci_dbg(pdev, "Invalid reporting bar ID %d\n", bar);
+				return NULL;
+			}
+
+			if (last_bar > bar) {
+				pci_dbg(pdev, "Reporting bar ID not in ascending order\n");
+				return NULL;
+			}
+
+			if (last_bar < bar) {
+				resource_size_t mask = pci_resource_len(pdev, bar) - 1;
+
+				/* Transition to a new bar */
+				last_bar = bar;
+
+				/*
+				 * Determine the obfuscated base of the BAR. BAR
+				 * offsets are never obfuscated.
+				 */
+				reporting_bar_base = tsm_offset & ~mask;
+			} else if (tsm_offset < last_reporting_end) {
+				pci_dbg(pdev, "Reporting ranges within BAR not in ascending order\n");
+				return NULL;
+			}
+
+			window.start = pci_resource_start(pdev, bar);
+			window.end = pci_resource_end(pdev, bar);
 		}
 
 		/* Per spec the tsm_offset never results in overflow / underflow */
@@ -1090,19 +1133,21 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 		}
 
 		range_off = tsm_offset - reporting_bar_base;
-		if (pci_resource_len(pdev, bar) < range_off + size) {
-			pci_dbg(pdev, "Reporting range larger than BAR size\n");
+
+		if (range_len(&window) < range_off + size) {
+			pci_dbg(pdev, "Reporting range larger than %s size\n",
+				coherent ? "coherent range" : "BAR");
 			return NULL;
 		}
 
-		range.start = pci_resource_start(pdev, bar) + range_off;
+		range.start = window.start + range_off;
 		range.end = range.start + size - 1;
 
 		/* Only record the TEE ranges for later consideration by ioremap() */
 		if (FIELD_GET(PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE,
 			      attr)) {
-			pci_dbg(pdev, "Skipping non-TEE range, BAR%d %pra\n",
-				bar, &range);
+			pci_dbg(pdev, "Skipping non-TEE range, %s %pra\n",
+				coherent ? "coherent range" : "BAR", &range);
 			continue;
 		}
 
@@ -1110,6 +1155,8 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 		entry->res.end = range.end;
 		entry->res.flags = IORESOURCE_MEM;
 		entry->tsm_offset = tsm_offset;
+		if (coherent)
+			entry->flags |= PCI_TSM_MMIO_F_COHERENT;
 		mmio->nr++;
 	}
 
include/linux/pci-tsm.h +75 -0
index e54ad057fa8e..7c29f10e7795 100644
@@ -1,8 +1,10 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef __PCI_TSM_H
 #define __PCI_TSM_H
+#include <linux/bits.h>
 #include <linux/mutex.h>
 #include <linux/pci.h>
+#include <linux/range.h>
 #include <linux/sockptr.h>
 
 struct pci_tsm;
@@ -199,15 +201,20 @@ enum pci_tsm_req_scope {
 	PCI_TSM_REQ_DEBUG_WRITE = 3,
 };
 
+/* The range is the device's CXL coherent (CXL.mem) window, not BAR-backed */
+#define PCI_TSM_MMIO_F_COHERENT BIT(0)
+
 /**
  * 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.
+ * @flags: PCI_TSM_MMIO_F_* attributes for the range
  */
 struct pci_tsm_mmio_entry {
 	struct resource res;
 	u64 tsm_offset;
+	u32 flags;
 };
 
 struct pci_tsm_mmio {
@@ -227,6 +234,74 @@ static inline struct resource *pci_tsm_mmio_resource(struct pci_tsm_mmio *mmio,
 	return &mmio->mmio[idx].res;
 }
 
+#ifdef CONFIG_PCI_TSM_CXL_COHERENT
+/*
+ * pci_tsm_coh_resource_contains() - check a coherent range against the
+ * PCI-core coh_resource[] snapshot
+ * @pdev: device owner of @res
+ * @res: candidate coherent MMIO range to validate
+ *
+ * The coherent (CXL.mem) range isn't BAR-backed. So pci_resource_n()
+ * containment doesn't apply. Check against pdev->coh_resource[] instead.
+ *
+ * Return: true if @res falls within a populated coh_resource[] entry.
+ */
+static inline bool pci_tsm_coh_resource_contains(struct pci_dev *pdev,
+						 const struct resource *res)
+{
+	for (int i = 0; i < PCI_CXL_MAX_COHERENT_RANGES; i++) {
+		struct resource *coh_res = &pdev->coh_resource[i];
+
+		if (resource_type(coh_res) == IORESOURCE_MEM &&
+		    resource_contains(coh_res, res))
+			return true;
+	}
+
+	return false;
+}
+
+/*
+ * pci_tsm_retrieve_coherent_range() - resolve the device's coherent CXL window
+ * @pdev: device owner of the reported ranges
+ * @out: host physical (guest IPA) range of the coherent window
+ *
+ * The coherent range carries no BAR number, so its address comes from
+ * pdev->coh_resource[], a pre-driver-bind snapshot of committed CXL HDM
+ * decoders.
+ *
+ * Return: 0 with *@out set from the first populated entry, or -ENODEV if no
+ * entry is populated.
+ */
+static inline int pci_tsm_retrieve_coherent_range(struct pci_dev *pdev,
+						  struct range *out)
+{
+	for (int i = 0; i < PCI_CXL_MAX_COHERENT_RANGES; i++) {
+		struct resource *res = &pdev->coh_resource[i];
+
+		if (resource_type(res) != IORESOURCE_MEM)
+			continue;
+
+		out->start = res->start;
+		out->end = res->end;
+		return 0;
+	}
+
+	return -ENODEV;
+}
+#else
+static inline bool pci_tsm_coh_resource_contains(struct pci_dev *pdev,
+						 const struct resource *res)
+{
+	return false;
+}
+
+static inline int pci_tsm_retrieve_coherent_range(struct pci_dev *pdev,
+						  struct range *out)
+{
+	return -ENODEV;
+}
+#endif /* CONFIG_PCI_TSM_CXL_COHERENT */
+
 #ifdef CONFIG_PCI_TSM
 int pci_tsm_register(struct tsm_dev *tsm_dev);
 void pci_tsm_unregister(struct tsm_dev *tsm_dev);
Signature · 1 line
2.53.0
#6 lore raw

Re: [RFC PATCH v2 3/5] PCI/TSM: Derive the coherent-range IPA from CXL

Quoted 8 lines
Quoted 3 lines

+
+                     reporting_bar_base = tsm_offset &
+                                          ~((u64)range_len(&window) - 1);

Hi Ankit,

HDM decoder window can have non-power-of-2 sizes, why do we assume a power-of-2 size for coherent window?

Hi Richard, yeah the region base calculation needs to be updated to correctly determine the CXL region base for the non-powerof2 sizes.

Quoted 23 lines

E.g. if we have something like the following
Window start: 4GB
Window size: 768 MB
Reported address: 4GB + 256 MB

Assume MMIO_REPORTING_OFFSET is 0, so no address obfuscation needs to be removed. The reported page is simply 256 MB into the window.

For this size, the mask leaves the reported address unchanged.

0x110000000 & ~(0x30000000 - 1) = 0x110000000

reporting_bar_base becomes 4GB + 256 MB, rather than actual window base of 4 GB, so range_off is 0.

The fianl address will be window.start + range_off = 4GB + 0

A KUnit test against your patch series can reproduce this.
Furthermore, pci_tsm_mmio_setup() accepts the wrong result, because the incorrect address is still insdie the window.

Best regards,
Richard Cheng.

#7 lore raw

Re: [RFC PATCH v2 3/5] PCI/TSM: Derive the coherent-range IPA from CXL

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

+
+                     reporting_bar_base = tsm_offset &
+                                          ~((u64)range_len(&window) - 1);

Quoted 8 lines
Quoted 4 lines

Hi Ankit,

HDM decoder window can have non-power-of-2 sizes, why do we assume a power-of-2 size for coherent window?

Hi Richard, yeah the region base calculation needs to be updated to correctly determine the CXL region base for the non-powerof2 sizes.

Just to add, IMO determining the base IPA of the CXL region using the interface report does not look straight forward for this case. Trying to figure out a proper solution. Missed to mention this as an open in the cover letter.

maintainer
#8 lore raw

Re: [RFC PATCH v2 3/5] PCI/TSM: Derive the coherent-range IPA from CXL

Quoted 9 lines · Ankit Agrawal
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

+
+                     reporting_bar_base = tsm_offset &
+                                          ~((u64)range_len(&window) - 1);

Hi Ankit,

HDM decoder window can have non-power-of-2 sizes, why do we assume a power-of-2 size for coherent window?

Quoted 8 lines
Quoted 3 lines

Hi Richard, yeah the region base calculation needs to be updated to correctly determine the CXL region base for the non-powerof2 sizes.

Just to add, IMO determining the base IPA of the CXL region using the interface report does not look straight forward for this case. Trying to figure out a proper solution. Missed to mention this as an open in the cover letter.

This is the same old problem, PCI SIG messed up the interface report so it is not usable by the guest.

The pow-2 thing was a work around to avoid that, and it still works fine for CXL.

CXL's non-pow-2 doesn't change anything. The TSM still would compute the pow-2 starting point and use that when applying the offset. Meaning it rounds down the CXL window start to the nearest pow2 alignment, then uses that to set the offset bits.

Linux makes the same calculation to strip the offset bits.

The real fix was to have the interface report include the starting offset, but that didn't happen, and it is too late now, so oh well.

Jason

#9 lore raw

RFC PATCH v2 4/5 PCI/TSM: Support multiple coherent ranges via coh_idx

In reply to Ankit Agrawal

Devices with multiple decoders can report more than one coherent window in a single interface report. Generalize the single 0xffff sentinel into a small id space:
PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_TO_COH_IDX() treats any range id in the top PCI_CXL_MAX_COHERENT_RANGES values (counting down from ~0) as a coherent range index coh_idx. This keeps id 0xffff (~0) backward compatible with the single-range case (coh_idx 0) while allowing ~1, ~2, and so on for additional windows.

Only id 0xffff (~0) is spec-defined: Arm RME System Architecture (DEN0129) section B2.3.5.2 reserves range ID 0xFFFF for a device's region range. ~1, ~2, and so on are this tree's own extension of that idea, invented to cover multi-range reporting.

Use coh_idx through pci_tsm_retrieve_coherent_range() to select the coh_idx entry of pdev->coh_resource[], the precommitted-decoder snapshot and relax pci_tsm_mmio_alloc() to accept more than one coherent region. The coherent-range entries follow all BAR entries in the report and allow multiple trailing coherent ranges.

---
 drivers/pci/tsm.c       | 36 ++++++++++++++++++++++++------------
 include/linux/pci-tsm.h | 34 +++++++++++++++++++---------------
 2 files changed, 43 insertions(+), 27 deletions(-)
drivers/pci/tsm.c +24 -12
index bc3218c58ff0..a2a25bafc93e 100644
@@ -1004,7 +1004,14 @@ EXPORT_SYMBOL_GPL(pci_tsm_mmio_teardown);
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_NON_TEE BIT(2)
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_IS_UPDATABLE BIT(3)
 #define PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID GENMASK(31, 16)
-#define PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT 0xffff
+
+/*
+ * Only id ~0 (0xffff) is spec-defined (Arm RME DEN0129 section B2.3.5.2);
+ * ~1, ~2, etc. extend it for multi-range reporting: id ~N selects coherent
+ * window N, keeping coherent ids disjoint from BAR ids
+ * (0..PCI_STD_NUM_BARS-1) without a separate flag.
+ */
+#define PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_TO_COH_IDX(id) ((u16)~(id))
 
 /* An interface report 'pfn' is 4K in size */
 struct pci_tsm_devif_mmio {
@@ -1042,6 +1049,7 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 	u64 reporting_bar_base, last_reporting_end;
 	u32 mmio_range_count;
 	int last_bar = -1;
+	bool seen_coherent = false;
 	int i;
 
 	if (report_len < sizeof(*devif_report))
@@ -1071,28 +1079,32 @@ struct pci_tsm_mmio *pci_tsm_mmio_alloc(struct pci_dev *pdev,
 		u32 attr = __le32_to_cpu(mmio_data->attributes);
 		int bar = FIELD_GET(PCI_TSM_DEVIF_REPORT_MMIO_ATTR_RANGE_ID,
 				    attr);
-		bool coherent =
-			bar == PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_COHERENT;
+		unsigned int coh_idx =
+			PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_TO_COH_IDX(bar);
+		bool coherent = coh_idx < PCI_CXL_MAX_COHERENT_RANGES;
 
 		if (coherent) {
-			if (i != mmio_range_count - 1) {
-				pci_dbg(pdev, "Coherent reporting range is not last\n");
-				return NULL;
-			}
+			seen_coherent = true;
 
 			/*
-			 * No BAR names the coherent range, so fail closed if
-			 * neither a CXL region nor a committed HDM decoder
-			 * resolves its address.
+			 * Fail closed if pdev->coh_resource[coh_idx] isn't
+			 * populated: nothing else in the report identifies
+			 * the window's address.
 			 */
-			if (pci_tsm_retrieve_coherent_range(pdev, &window)) {
-				pci_dbg(pdev, "No CXL region or committed HDM decoder for coherent reporting range\n");
+			if (pci_tsm_retrieve_coherent_range(pdev, coh_idx, &window)) {
+				pci_dbg(pdev, "No populated coh_resource[] entry for coherent range %u\n",
+					coh_idx);
 				return NULL;
 			}
 
 			reporting_bar_base = tsm_offset &
 					     ~((u64)range_len(&window) - 1);
 		} else {
+			if (seen_coherent) {
+				pci_dbg(pdev, "BAR reporting range follows a coherent range\n");
+				return NULL;
+			}
+
 			if (bar >= PCI_STD_NUM_BARS ||
 			    !(pci_resource_flags(pdev, bar) & IORESOURCE_MEM) ||
 			    (pci_resource_flags(pdev, bar) & IORESOURCE_UNSET)) {
include/linux/pci-tsm.h +19 -15
index 7c29f10e7795..ef4edf3efaf3 100644
@@ -261,32 +261,35 @@ static inline bool pci_tsm_coh_resource_contains(struct pci_dev *pdev,
 }
 
 /*
- * pci_tsm_retrieve_coherent_range() - resolve the device's coherent CXL window
+ * pci_tsm_retrieve_coherent_range() - resolve the device coherent CXL window
  * @pdev: device owner of the reported ranges
+ * @coh_idx: which coherent range to resolve, per
+ *	     %PCI_TSM_DEVIF_REPORT_MMIO_RANGE_ID_TO_COH_IDX()
  * @out: host physical (guest IPA) range of the coherent window
  *
- * The coherent range carries no BAR number, so its address comes from
- * pdev->coh_resource[], a pre-driver-bind snapshot of committed CXL HDM
- * decoders.
+ * Each @coh_idx names one of the device coherent (CXL) windows. It carries
+ * no BAR number and so its address comes from pdev->coh_resource[@coh_idx],
+ * which is a snapshot of committed CXL HDM decoders regions.
  *
- * Return: 0 with *@out set from the first populated entry, or -ENODEV if no
- * entry is populated.
+ * Return: 0 with *@out set, or -ENODEV if @coh_idx is out
+ * of range or has no populated coh_resource[] entry.
  */
 static inline int pci_tsm_retrieve_coherent_range(struct pci_dev *pdev,
+						  unsigned int coh_idx,
 						  struct range *out)
 {
-	for (int i = 0; i < PCI_CXL_MAX_COHERENT_RANGES; i++) {
-		struct resource *res = &pdev->coh_resource[i];
+	struct resource *res;
 
-		if (resource_type(res) != IORESOURCE_MEM)
-			continue;
+	if (coh_idx >= PCI_CXL_MAX_COHERENT_RANGES)
+		return -ENODEV;
 
-		out->start = res->start;
-		out->end = res->end;
-		return 0;
-	}
+	res = &pdev->coh_resource[coh_idx];
+	if (resource_type(res) != IORESOURCE_MEM)
+		return -ENODEV;
 
-	return -ENODEV;
+	out->start = res->start;
+	out->end = res->end;
+	return 0;
 }
 #else
 static inline bool pci_tsm_coh_resource_contains(struct pci_dev *pdev,
@@ -296,6 +299,7 @@ static inline bool pci_tsm_coh_resource_contains(struct pci_dev *pdev,
 }
 
 static inline int pci_tsm_retrieve_coherent_range(struct pci_dev *pdev,
+						  unsigned int coh_idx,
 						  struct range *out)
 {
 	return -ENODEV;
Signature · 1 line
2.53.0
#10 lore raw

RFC PATCH v2 5/5 cxl: Invalidate cached coherent range when Normalized Addressing is detected

In reply to Ankit Agrawal

In case of Normalized addressing, the HDM decoders report DPA instead of HPA. Determine such case and ensure the pdev->coh_resource[] do not cache it.

CXL_DECODER_F_NORMALIZED_ADDRESSING is only set on a decoder once the endpoint is assembled into a region in cxl_prm_setup_root(). This is well after the HDM decoders are snapshotted and populated in pdev->coh_resource[]. So the DPA would be populated in the pdev->coh_resource.

Add pci_cxl_hdm_coh_invalidate() and call it later when the normalized addressing is identified to clear the cached value.

---
 drivers/cxl/core/atl.c      |  6 ++++++
 drivers/cxl/core/core.h     |  9 +++++++++
 drivers/cxl/core/resource.c | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+)
drivers/cxl/core/atl.c +6 -0
index 07ce7c50c47a..2bf4d73fd02f 100644
@@ -175,6 +175,12 @@ static int cxl_prm_setup_root(struct cxl_root *cxl_root, void *data)
 	cxld->config.flags |= CXL_DECODER_F_LOCK;
 	cxld->config.flags |= CXL_DECODER_F_NORMALIZED_ADDRESSING;
 
+	/*
+	 * The PCI-core snapshot of this decoder was taken before the flag was
+	 * known. Invalidate it so a DPA is never exposed as a coherent HPA.
+	 */
+	pci_cxl_hdm_coh_invalidate(pci_dev, cxld->config.id);
+
 	ctx->hpa_range = hpa_range;
 	ctx->interleave_ways = ways;
 	ctx->interleave_granularity = gran;
drivers/cxl/core/core.h +9 -0
index 1aabbbb4ae5e..97d27a27ab67 100644
@@ -139,6 +139,15 @@ struct cxl_rwsem {
 
 extern struct cxl_rwsem cxl_rwsem;
 
+#ifdef CONFIG_CXL_RESET
+void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev, int decoder_id);
+#else
+static inline void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev,
+					      int decoder_id)
+{
+}
+#endif
+
 void cxl_commit_start(void __iomem *hdm, struct cxl_decoder_settings *settings);
 int cxld_await_commit(void __iomem *hdm, int id);
 
drivers/cxl/core/resource.c +32 -0
index d837f8e952a9..d0774926da2d 100644
@@ -527,6 +527,38 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
 	kfree(info);
 }
 
+/*
+ * A decoder is found to use Normalized Addressing only once the endpoint is
+ * assembled into a region, after the snapshot. Record the flag in the cached
+ * settings and drop the coherent range so a DPA is never reported as an HPA.
+ */
+void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev, int decoder_id)
+{
+	struct cxl_hdm_info *info;
+	struct resource *res;
+
+	guard(rwsem_write)(&cxl_rwsem.dpa);
+	info = pdev->hdm;
+	if (!info || decoder_id < 0 || decoder_id >= info->decoder_count)
+		return;
+
+	info->settings[decoder_id].config.flags |=
+		CXL_DECODER_F_NORMALIZED_ADDRESSING;
+
+	if (decoder_id >= PCI_CXL_MAX_COHERENT_RANGES)
+		return;
+
+	res = &pdev->coh_resource[decoder_id];
+	if (!res->flags)
+		return;
+
+	pci_warn(pdev, "CXL coherent range: decoder%d uses Normalized Addressing, dropping %pR\n",
+		 decoder_id, res);
+	if (res->parent)
+		remove_resource(res);
+	memset(res, 0, sizeof(*res));
+}
+
 static void __iomem *cxl_pci_hdm_ioremap_current(struct pci_dev *pdev,
 						 int bar,
 						 resource_size_t offset,
Signature · 1 line
2.53.0
#11 lore raw

Re: [RFC PATCH v2 5/5] cxl: Invalidate cached coherent range when Normalized Addressing is detected

Quoted 103 lines · Ankit Agrawal

In case of Normalized addressing, the HDM decoders report DPA instead of HPA. Determine such case and ensure the pdev->coh_resource[] do not cache it.

CXL_DECODER_F_NORMALIZED_ADDRESSING is only set on a decoder once the endpoint is assembled into a region in cxl_prm_setup_root(). This is well after the HDM decoders are snapshotted and populated in pdev->coh_resource[]. So the DPA would be populated in the pdev->coh_resource.

Add pci_cxl_hdm_coh_invalidate() and call it later when the normalized addressing is identified to clear the cached value.

---
 drivers/cxl/core/atl.c      |  6 ++++++
 drivers/cxl/core/core.h     |  9 +++++++++
 drivers/cxl/core/resource.c | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+)
drivers/cxl/core/atl.c +6 -0
index 07ce7c50c47a..2bf4d73fd02f 100644
@@ -175,6 +175,12 @@ static int cxl_prm_setup_root(struct cxl_root *cxl_root, void *data)
 	cxld->config.flags |= CXL_DECODER_F_LOCK;
 	cxld->config.flags |= CXL_DECODER_F_NORMALIZED_ADDRESSING;
 
+	/*
+	 * The PCI-core snapshot of this decoder was taken before the flag was
+	 * known. Invalidate it so a DPA is never exposed as a coherent HPA.
+	 */
+	pci_cxl_hdm_coh_invalidate(pci_dev, cxld->config.id);
+
 	ctx->hpa_range = hpa_range;
 	ctx->interleave_ways = ways;
 	ctx->interleave_granularity = gran;
drivers/cxl/core/core.h +9 -0
index 1aabbbb4ae5e..97d27a27ab67 100644
@@ -139,6 +139,15 @@ struct cxl_rwsem {
 
 extern struct cxl_rwsem cxl_rwsem;
 
+#ifdef CONFIG_CXL_RESET
+void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev, int decoder_id);
+#else
+static inline void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev,
+					      int decoder_id)
+{
+}
+#endif
+
 void cxl_commit_start(void __iomem *hdm, struct cxl_decoder_settings *settings);
 int cxld_await_commit(void __iomem *hdm, int id);
 
drivers/cxl/core/resource.c +32 -0
index d837f8e952a9..d0774926da2d 100644
@@ -527,6 +527,38 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
 	kfree(info);
 }
 
+/*
+ * A decoder is found to use Normalized Addressing only once the endpoint is
+ * assembled into a region, after the snapshot. Record the flag in the cached
+ * settings and drop the coherent range so a DPA is never reported as an HPA.
+ */
+void pci_cxl_hdm_coh_invalidate(struct pci_dev *pdev, int decoder_id)
+{
+	struct cxl_hdm_info *info;
+	struct resource *res;
+
+	guard(rwsem_write)(&cxl_rwsem.dpa);
+	info = pdev->hdm;
+	if (!info || decoder_id < 0 || decoder_id >= info->decoder_count)
+		return;
+
+	info->settings[decoder_id].config.flags |=
+		CXL_DECODER_F_NORMALIZED_ADDRESSING;
+
+	if (decoder_id >= PCI_CXL_MAX_COHERENT_RANGES)
+		return;
+
+	res = &pdev->coh_resource[decoder_id];
+	if (!res->flags)
+		return;
+
+	pci_warn(pdev, "CXL coherent range: decoder%d uses Normalized Addressing, dropping %pR\n",
+		 decoder_id, res);
+	if (res->parent)
+		remove_resource(res);
+	memset(res, 0, sizeof(*res));
+}
+
 static void __iomem *cxl_pci_hdm_ioremap_current(struct pci_dev *pdev,
 						 int bar,
 						 resource_size_t offset,
Signature · 1 line
2.53.0

Hi Ankit,

Maybe we need to export the function for cxl_core ? with the following x86_64 config, the build fails at modpost:

CONFIG_CXL_BUS=m
CONFIG_CXL_RESET=y
CONFIG_CXL_ATL=y

ERROR: modpost: drivers/cxl/core/cxl_core.ko: symbol 'pci_cxl_hdm_coh_invalidate' undefined!

Maybe we need this "EXPORT_SYMBOL_FOR_MODULES(pci_cxl_hdm_coh_invalidate, "cxl_core");"

Best regards,
Richard Cheng.

#12 lore raw

Re: [RFC PATCH v2 5/5] cxl: Invalidate cached coherent range when Normalized Addressing is detected

Quoted 5 lines

Hi Ankit,

Maybe we need to export the function for cxl_core ? with the following x86_64 config, the build fails at modpost:

Quoted 7 lines

CONFIG_CXL_BUS=m
CONFIG_CXL_RESET=y
CONFIG_CXL_ATL=y

ERROR: modpost: drivers/cxl/core/cxl_core.ko: symbol 'pci_cxl_hdm_coh_invalidate' undefined!

Maybe we need this "EXPORT_SYMBOL_FOR_MODULES(pci_cxl_hdm_coh_invalidate, "cxl_core");"

Yeah, that sounds right. Will fix it.