* [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices
@ 2026-12-05 4:14 Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
` (18 more replies)
0 siblings, 19 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Hi folks,
This series adds CXL Reset for Type 2 devices through the existing PCI
reset_method and reset sysfs attributes. It disables CXL.cache, performs
supported cache writeback, initiates CXL Reset with Memory Clear, and
restores cached HDM decoder and CXL Device DVSEC state before PCI use
resumes. Active HDM ranges are reserved and CPU caches invalidated
before and after reset. The method remains limited to eligible
single-function devices with both CXL.cache and CXL.mem enabled. CXL bus
reset restores the same cached state and flushes the host and device
caches around the reset.
The reset sequence follows the CXL 4.0 specification [5], including
CXL.cache disable, optional cache writeback, CXL Reset Memory Clear,
ResetComplete polling, and ResetError reporting. Sections 9.7.2 and
9.7.3 define the completion timeout encodings and reset access
restrictions.
The userspace ABI is the existing PCI reset interface:
/sys/bus/pci/devices/.../reset_method
/sys/bus/pci/devices/.../reset
Userspace selects "cxl_reset" in reset_method and triggers the reset
through reset. Following Dan's v6 feedback, this uses the PCI interface
instead of adding a memdev-specific sysfs ABI.
For sysfs reset, the generic PCI path invokes the driver's
reset_prepare() callback when provided and disables PCI I/O, memory
decoding, and bus mastering. The CXL path reserves affected HDM ranges
exclusively and blocks IOMMU translations where applicable. It does not
offline memory or revoke mappings: userspace is expected to offline
CXL.mem users before reset. The reservation enforces this for claimed
ranges by failing the reset while a conflicting resource is claimed.
Locking changed in v18. The global region lock is no longer held across
the reset. Both reset paths take cxl_rwsem.region only for four short
windows:
prepare: snapshot the cache, validate and reserve the HPA ranges,
set reset_pending
(no region lock) host cache flush, device cache writeback
offline: set regs_offline
(no region lock) reset, Memory Clear wait for cxl_reset
restore: re-read the cached decoders, restore the state,
clear regs_offline
(no region lock) host cache flush
unmark: clear reset_pending
While reset_pending is set, cxl_decoder_commit() returns -EBUSY for that
device, so no region using it can commit in the middle of a reset. While
regs_offline is set, a region teardown only updates the software state
and the cache and leaves the registers alone; the restore then programs
the decoders from the updated cache. Dave's lockdep reproducer [6] (a
region read, a device remove and a slot power-off during the reset) no
longer hangs.
Changes from v17 [1]:
- Rebased onto cxl/for-7.5/cxl-kunit (Alison)
- Reworked the locking as described above, for both cxl_reset and
cxl_bus (Dave, Alison) [6][7]. disable_hdm() now also updates the
cached global control, and the restore re-reads the decoders from the
cache (Dave)
- Fixes from the Sashiko review [10]: the reset preconditions are
checked in the prepare step, so a refused CXL reset no longer falls
back to FLR; DVSEC-decoded memory is judged from the snapshot;
Normalized Addressing survives decoder refreshes; CXL.cache stays off
until the host caches are flushed; a bus reset checks that it can run
before touching the device; function 0 of an ARI device is rejected
when other functions share the bus; all-ones config reads are rejected
when filling the cache
- cxl_reset now refuses, as cxl_bus already did, when CXL.cache is
enabled but the device cannot write back its cache
- Smaller cleanups in patches 2 and 9-12 (Dave, Li Ming). Each patch has
the details.
- Dropped the Assisted-by trailers and added Dave's Reviewed-by to
patches 7 and 8
Still open: Alison's question about resetting before Normalized
Addressing is known [8] and Alejandro's CXL.io/IOMMU question [9]. I'll
follow up in those threads. Earlier history is in the v16 [2] and v15
[3] cover letters.
HDM caching needs the CXL.cache, CXL.mem, CXL Reset and Reset Memory
Clear capabilities, and CXL bus reset restores its state from the same
cache.
CXL Reset also needs a CPU cache clean/invalidate provider, such as the
SMCCC series [4]. Without one, cxl_reset is still listed but fails with
-ENXIO and does not fall back to FLR, unless the test-only
CXL_REGION_INVALIDATION_TEST option is set.
The KUnit suite covers HDM range boundaries, zero-size and uncommitted
decoders, lock and target-type decoding, and 12-way interleave.
AI coding assistants were used to help write and review parts of this
series, and an AI review found two of the v18 fixes. I have reviewed
all AI-assisted changes.
Testing: I ran v18 in QEMU with two cxl-type2 devices behind two CXL
host bridges and lockdep on, for both cxl_reset and cxl_bus, with a
delay added in the prepare step. A region read and a device remove
complete during the delay, and lockdep reports nothing for the CXL
locks. The pciehp slot power-off path still shows the existing
pci_rescan_remove_lock vs reset_lock warning, which also shows up
without this series. Each patch builds with W=1, and Clang 23 context
analysis is clean for the touched files. The CXL KUnit suites, including
cxl_hdm_unpack_kunit, pass.
On hardware, I ran v18 with the SMCCC provider series [4] (provider in
use, CXL_REGION_INVALIDATION_TEST off) on an ARM64 system with four CXL
Type 2 devices and the vendor driver bound. On each device I onlined
the memory as a NUMA node, left dirty CPU cache lines in it, offlined
it, and then issued the driver's CXL reset and CXL bus reset requests
(which select cxl_reset and cxl_bus) once each. All of them succeeded.
Afterwards all four devices still had the CXL DVSEC, CXL.cache and
CXL.mem were still enabled on the device I checked, and a CPU fill and
verify of the device memory matched.
[1] https://lore.kernel.org/linux-cxl/20261105185917.2667656-1-smadhavan@nvidia.com/
[2] https://lore.kernel.org/linux-cxl/20261029145703.2185231-1-smadhavan@nvidia.com/
[3] https://lore.kernel.org/linux-cxl/20261011021422.3428136-1-smadhavan@nvidia.com/
[4] https://lore.kernel.org/linux-arm-kernel/20261021224557.1827361-1-smadhavan@nvidia.com/
[5] https://computeexpresslink.org/wp-content/uploads/2026/02/CXL-Specification_rev4p0_ver1p0_2026February26_clean_evalcopy_v2.pdf
[6] https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
[7] https://lore.kernel.org/linux-cxl/avyOoQjiD7Uh_3rE@aschofie-mobl2.lan/
[8] https://lore.kernel.org/linux-cxl/avyPsvwV1_lc9va5@aschofie-mobl2.lan/
[9] https://lore.kernel.org/linux-cxl/f5fdc266-aed3-47e6-a10f-e8188b58c93f@amd.com/
[10] https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com
Srirangan Madhavan (19):
cxl: Clarify decoder interleave limit
cxl: Share CXL port upstream PCI device lookup
cxl: Move decoder declarations to shared header
cxl: Embed decoder configuration in a standalone structure
cxl: Introduce endpoint HDM decoder settings
cxl: Move HDM decoder helpers to built-in code
cxl: Share HDM decoder register unpacking
cxl: Move cxl_rwsem into built-in code
cxl: Refresh cached PCI HDM decoder settings
cxl: Build register block helpers into the kernel
PCI/CXL: Cache endpoint HDM state during PCI enumeration
PCI/CXL: Add CXL Device Reset sequencing
cxl: Validate and synchronize HDM ranges around reset
PCI/CXL: Reject reset with unsafe function scope
PCI/CXL: Restore CXL state after PCI reset
PCI/CXL: Expose CXL Reset as a PCI reset method
PCI/CXL: Restore CXL state after CXL bus reset
PCI/CXL: Flush host and device caches for CXL bus reset
cxl: Add KUnit checks for HDM decoder unpacking
MAINTAINERS | 1 +
drivers/cxl/Kconfig | 8 +-
drivers/cxl/acpi.c | 26 +-
drivers/cxl/core/Makefile | 2 +-
drivers/cxl/core/atl.c | 4 +-
drivers/cxl/core/core.h | 15 +
drivers/cxl/core/hdm.c | 315 ++++----
drivers/cxl/core/hdm_regs.c | 443 +++++++++++
drivers/cxl/core/hdm_state.c | 499 +++++++++++++
drivers/cxl/core/pci.c | 140 ++--
drivers/cxl/core/port.c | 62 +-
drivers/cxl/core/region.c | 117 +--
drivers/cxl/core/regs.c | 9 +
drivers/cxl/cxl.h | 46 +-
drivers/cxl/pci.c | 2 +-
drivers/cxl/port.c | 4 +-
drivers/cxl/tests/Makefile | 3 +
drivers/cxl/tests/hdm_unpack_kunit.c | 91 +++
drivers/pci/Makefile | 1 +
drivers/pci/bus.c | 1 +
drivers/pci/cxl.c | 1029 ++++++++++++++++++++++++++
drivers/pci/pci.c | 57 +-
drivers/pci/pci.h | 51 ++
drivers/pci/probe.c | 1 +
include/cxl/cxl.h | 57 +-
include/cxl/hdm.h | 119 +++
include/cxl/pci.h | 13 +
include/linux/pci.h | 8 +-
include/uapi/linux/pci_regs.h | 14 +
tools/testing/cxl/Kbuild | 1 -
tools/testing/cxl/test/cxl.c | 158 ++--
31 files changed, 2850 insertions(+), 447 deletions(-)
create mode 100644 drivers/cxl/core/hdm_regs.c
create mode 100644 drivers/cxl/core/hdm_state.c
create mode 100644 drivers/cxl/tests/hdm_unpack_kunit.c
create mode 100644 drivers/pci/cxl.c
create mode 100644 include/cxl/hdm.h
base-commit: 533928808ac1e6a96a7daa74b6ad68c3f7f43a28
--
2.43.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v18 01/19] cxl: Clarify decoder interleave limit
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
` (17 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan, Jonathan Cameron
CXL 2.0 added 16-way memory interleaving, making the comment that the
specification supports only 8-way interleaving stale. Explain that 16 is
the largest interleave a decoder can express, without changing the limit.
Suggested-by: Jonathan Cameron <jic23@kernel.org>
Link: https://lore.kernel.org/linux-cxl/20260912003057.3db9335c@jic23-hlaptop/
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
---
drivers/cxl/cxl.h | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index baa6df2d362a..0a5ab3a00001 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -275,10 +275,7 @@ enum cxl_decoder_type {
CXL_DECODER_HOSTONLYMEM = 3,
};
-/*
- * Current specification goes up to 8, double that seems a reasonable
- * software max for the foreseeable future
- */
+/* 16 is the largest interleave a decoder can express (CXL 4.0 8.2.4.20.7). */
#define CXL_DECODER_MAX_INTERLEAVE 16
#define CXL_QOS_CLASS_INVALID -1
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
` (16 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan, Jonathan Cameron
read_cdat_data() resolves a CXL port's upstream device to its backing PCI
device. HDM cache updates need the same mapping.
Factor the lookup into cxl_port_get_uport_pci_dev() and return a referenced
PCI device to make caller ownership explicit.
Use automatic reference cleanup in read_cdat_data(), as suggested by
Li Ming, so early returns release the PCI device reference.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
---
Changes in v18:
- Restructure read_cdat_data() around a cxl_cdat_read_validate() helper
and __free(pci_dev_put), removing the goto/out layout (Dave Jiang)
---
drivers/cxl/core/core.h | 1 +
drivers/cxl/core/pci.c | 67 ++++++++++++++++++-----------------------
drivers/cxl/core/port.c | 26 ++++++++++++++++
3 files changed, 57 insertions(+), 37 deletions(-)
diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index 79918fde1575..ee639ac2c670 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -157,6 +157,7 @@ long cxl_pci_get_latency(struct pci_dev *pdev);
int cxl_pci_get_bandwidth(struct pci_dev *pdev, struct access_coordinate *c);
int cxl_port_get_switch_dport_bandwidth(struct cxl_port *port,
struct access_coordinate *c);
+struct pci_dev *cxl_port_get_uport_pci_dev(struct cxl_port *port);
static inline struct device *port_to_host(struct cxl_port *port)
{
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 125abb36df7f..1dd16d0f17f6 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -556,6 +556,27 @@ static unsigned char cdat_checksum(void *buf, size_t size)
return sum;
}
+static int cxl_cdat_read_validate(struct device *dev,
+ struct pci_doe_mb *doe_mb,
+ struct cdat_doe_rsp *buf, size_t *length)
+{
+ size_t table_length = *length;
+ int rc;
+
+ rc = cxl_cdat_read_table(dev, doe_mb, buf, length);
+ if (rc)
+ return rc;
+
+ if (table_length != *length)
+ dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
+ table_length, *length);
+
+ if (cdat_checksum(buf->data, *length))
+ return -EIO;
+
+ return 0;
+}
+
/**
* read_cdat_data - Read the CDAT data on this port
* @port: Port to read data from
@@ -564,26 +585,13 @@ static unsigned char cdat_checksum(void *buf, size_t size)
*/
void read_cdat_data(struct cxl_port *port)
{
- struct device *uport = port->uport_dev;
struct device *dev = &port->dev;
struct pci_doe_mb *doe_mb;
- struct pci_dev *pdev = NULL;
- struct cxl_memdev *cxlmd;
struct cdat_doe_rsp *buf;
- size_t table_length, length;
- int rc;
-
- if (is_cxl_memdev(uport)) {
- struct device *host;
-
- cxlmd = to_cxl_memdev(uport);
- host = cxlmd->dev.parent;
- if (dev_is_pci(host))
- pdev = to_pci_dev(host);
- } else if (dev_is_pci(uport)) {
- pdev = to_pci_dev(uport);
- }
+ size_t length;
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
if (!pdev)
return;
@@ -606,30 +614,15 @@ void read_cdat_data(struct cxl_port *port)
* bytes for the DOE header. Table data starts afterwards.
*/
buf = devm_kzalloc(dev, sizeof(*buf) + length, GFP_KERNEL);
- if (!buf)
- goto err;
-
- table_length = length;
-
- rc = cxl_cdat_read_table(dev, doe_mb, buf, &length);
- if (rc)
- goto err;
-
- if (table_length != length)
- dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
- table_length, length);
-
- if (cdat_checksum(buf->data, length))
- goto err;
+ if (!buf || cxl_cdat_read_validate(dev, doe_mb, buf, &length)) {
+ /* Don't leave table data allocated on error */
+ devm_kfree(dev, buf);
+ dev_err(dev, "Failed to read/validate CDAT.\n");
+ return;
+ }
port->cdat.table = buf->data;
port->cdat.length = length;
-
- return;
-err:
- /* Don't leave table data allocated on error */
- devm_kfree(dev, buf);
- dev_err(dev, "Failed to read/validate CDAT.\n");
}
EXPORT_SYMBOL_NS_GPL(read_cdat_data, "CXL");
diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index f3705ea44db7..e4d593a36514 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -34,6 +34,32 @@
static DEFINE_IDA(cxl_port_ida);
static DEFINE_XARRAY(cxl_root_buses);
+/**
+ * cxl_port_get_uport_pci_dev - get the PCI device for a port's upstream device
+ * @port: CXL port to map to a PCI device
+ *
+ * Return: A referenced PCI device, or NULL if the upstream device is not PCI.
+ * The caller must release the returned reference with pci_dev_put().
+ */
+struct pci_dev *cxl_port_get_uport_pci_dev(struct cxl_port *port)
+{
+ struct device *uport = port->uport_dev;
+ struct device *host;
+
+ if (is_cxl_memdev(uport)) {
+ struct cxl_memdev *cxlmd = to_cxl_memdev(uport);
+
+ host = cxlmd->dev.parent;
+ } else {
+ host = uport;
+ }
+
+ if (!host || !dev_is_pci(host))
+ return NULL;
+
+ return pci_dev_get(to_pci_dev(host));
+}
+
/*
* The terminal device in PCI is NULL and @platform_bus
* for platform devices (for cxl_test)
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 03/19] cxl: Move decoder declarations to shared header
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
` (15 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan, Jonathan Cameron
Move the common decoder declarations to include/cxl/cxl.h for use by
shared decoder settings. Preserve their layout and documentation.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
---
drivers/cxl/cxl.h | 38 --------------------------------------
include/cxl/cxl.h | 42 +++++++++++++++++++++++++++++++++++++++++-
2 files changed, 41 insertions(+), 39 deletions(-)
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 0a5ab3a00001..648b4a849ab4 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -270,46 +270,8 @@ int cxl_dport_map_rcd_linkcap(struct pci_dev *pdev, struct cxl_dport *dport);
#define CXL_DECODER_F_NORMALIZED_ADDRESSING BIT(6)
#define CXL_DECODER_F_RESET_MASK (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK)
-enum cxl_decoder_type {
- CXL_DECODER_DEVMEM = 2,
- CXL_DECODER_HOSTONLYMEM = 3,
-};
-
-/* 16 is the largest interleave a decoder can express (CXL 4.0 8.2.4.20.7). */
-#define CXL_DECODER_MAX_INTERLEAVE 16
-
#define CXL_QOS_CLASS_INVALID -1
-/**
- * struct cxl_decoder - Common CXL HDM Decoder Attributes
- * @dev: this decoder's device
- * @id: kernel device name id
- * @hpa_range: Host physical address range mapped by this decoder
- * @interleave_ways: number of cxl_dports in this decode
- * @interleave_granularity: data stride per dport
- * @target_type: accelerator vs expander (type2 vs type3) selector
- * @region: currently assigned region for this decoder
- * @flags: memory type capabilities and locking
- * @target_map: cached copy of hardware port-id list, available at init
- * before all @dport objects have been instantiated. While
- * dport id is 8bit, CFMWS interleave targets are 32bits.
- * @commit: device/decoder-type specific callback to commit settings to hw
- * @reset: device/decoder-type specific callback to reset hw settings
-*/
-struct cxl_decoder {
- struct device dev;
- int id;
- struct range hpa_range;
- int interleave_ways;
- int interleave_granularity;
- enum cxl_decoder_type target_type;
- struct cxl_region *region;
- unsigned long flags;
- u32 target_map[CXL_DECODER_MAX_INTERLEAVE];
- int (*commit)(struct cxl_decoder *cxld);
- void (*reset)(struct cxl_decoder *cxld);
-};
-
/*
* Track whether this decoder is free for userspace provisioning, reserved for
* region autodiscovery, whether it is started connecting (awaiting other
diff --git a/include/cxl/cxl.h b/include/cxl/cxl.h
index 1c04a7823f64..9b4003770aa2 100644
--- a/include/cxl/cxl.h
+++ b/include/cxl/cxl.h
@@ -5,8 +5,10 @@
#ifndef __CXL_CXL_H__
#define __CXL_CXL_H__
+#include <linux/device.h>
#include <linux/node.h>
#include <linux/ioport.h>
+#include <linux/range.h>
#include <cxl/mailbox.h>
/**
@@ -23,7 +25,45 @@ enum cxl_devtype {
CXL_DEVTYPE_CLASSMEM,
};
-struct device;
+struct cxl_region;
+
+enum cxl_decoder_type {
+ CXL_DECODER_DEVMEM = 2,
+ CXL_DECODER_HOSTONLYMEM = 3,
+};
+
+/* 16 is the largest interleave a decoder can express (CXL 4.0 8.2.4.20.7). */
+#define CXL_DECODER_MAX_INTERLEAVE 16
+
+/**
+ * struct cxl_decoder - Common CXL HDM Decoder Attributes
+ * @dev: this decoder's device
+ * @id: kernel device name id
+ * @hpa_range: Host physical address range mapped by this decoder
+ * @interleave_ways: number of cxl_dports in this decode
+ * @interleave_granularity: data stride per dport
+ * @target_type: accelerator vs expander (type2 vs type3) selector
+ * @region: currently assigned region for this decoder
+ * @flags: memory type capabilities and locking
+ * @target_map: cached copy of hardware port-id list, available at init
+ * before all @dport objects have been instantiated. While
+ * dport id is 8bit, CFMWS interleave targets are 32bits.
+ * @commit: device/decoder-type specific callback to commit settings to hw
+ * @reset: device/decoder-type specific callback to reset hw settings
+*/
+struct cxl_decoder {
+ struct device dev;
+ int id;
+ struct range hpa_range;
+ int interleave_ways;
+ int interleave_granularity;
+ enum cxl_decoder_type target_type;
+ struct cxl_region *region;
+ unsigned long flags;
+ u32 target_map[CXL_DECODER_MAX_INTERLEAVE];
+ int (*commit)(struct cxl_decoder *cxld);
+ void (*reset)(struct cxl_decoder *cxld);
+};
/*
* Using struct_group() allows for per register-block-type helper routines,
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (2 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
` (14 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Define cxl_decoder_config independently and embed it in cxl_decoder. Update
decoder field accesses without changing behavior, so reset snapshots can
reuse the configuration type without struct_group_tagged().
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
---
drivers/cxl/acpi.c | 26 +++---
drivers/cxl/core/atl.c | 4 +-
drivers/cxl/core/hdm.c | 118 +++++++++++++-------------
drivers/cxl/core/pci.c | 8 +-
drivers/cxl/core/port.c | 36 ++++----
drivers/cxl/core/region.c | 102 +++++++++++-----------
drivers/cxl/cxl.h | 2 +-
drivers/cxl/pci.c | 2 +-
drivers/cxl/port.c | 4 +-
include/cxl/cxl.h | 29 +++++--
tools/testing/cxl/test/cxl.c | 158 +++++++++++++++++------------------
11 files changed, 248 insertions(+), 241 deletions(-)
diff --git a/drivers/cxl/acpi.c b/drivers/cxl/acpi.c
index b7ce6b59c680..61d0cd9f1546 100644
--- a/drivers/cxl/acpi.c
+++ b/drivers/cxl/acpi.c
@@ -109,16 +109,16 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,
return rc;
/* Does this CXIMS entry apply to the given CXL Window? */
- if (hbig != cxld->interleave_granularity)
+ if (hbig != cxld->config.interleave_granularity)
return 0;
/* IW 1,3 do not use xormaps and skip this parsing entirely */
- if (is_power_of_2(cxld->interleave_ways))
+ if (is_power_of_2(cxld->config.interleave_ways))
/* 2, 4, 8, 16 way */
- nr_maps = ilog2(cxld->interleave_ways);
+ nr_maps = ilog2(cxld->config.interleave_ways);
else
/* 6, 12 way */
- nr_maps = ilog2(cxld->interleave_ways / 3);
+ nr_maps = ilog2(cxld->config.interleave_ways / 3);
if (cxims->nr_xormaps < nr_maps) {
dev_dbg(dev, "CXIMS nr_xormaps[%d] expected[%d]\n",
@@ -369,7 +369,7 @@ static int add_or_reset_cxl_resource(struct resource *parent, struct resource *r
static void cxl_setup_extended_linear_cache(struct cxl_root_decoder *cxlrd)
{
struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
- struct range *hpa = &cxld->hpa_range;
+ struct range *hpa = &cxld->config.hpa_range;
resource_size_t size = range_len(hpa);
resource_size_t start = hpa->start;
resource_size_t cache_size;
@@ -442,13 +442,13 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
return PTR_ERR(cxlrd);
cxld = &cxlrd->cxlsd.cxld;
- cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->hpa_range = (struct range) {
+ cxld->config.flags = cfmws_to_decoder_flags(cfmws->restrictions);
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.hpa_range = (struct range) {
.start = cfmws->base_hpa,
.end = cfmws->base_hpa + cfmws->window_size - 1,
};
- cxld->interleave_ways = ways;
+ cxld->config.interleave_ways = ways;
for (i = 0; i < ways; i++)
cxld->target_map[i] = cfmws->interleave_targets[i];
/*
@@ -457,7 +457,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
*/
if (ways == 1)
ig = CXL_DECODER_MIN_GRANULARITY;
- cxld->interleave_granularity = ig;
+ cxld->config.interleave_granularity = ig;
if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_XOR) {
if (ways != 1 && ways != 3) {
@@ -758,7 +758,7 @@ static int add_root_nvdimm_bridge(struct device *match, void *data)
return 0;
cxld = to_cxl_decoder(match);
- if (!(cxld->flags & CXL_DECODER_F_PMEM))
+ if (!(cxld->config.flags & CXL_DECODER_F_PMEM))
return 0;
cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port);
@@ -875,8 +875,8 @@ static int pair_cxl_resource(struct device *dev, void *data)
struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
struct resource res = {
- .start = cxld->hpa_range.start,
- .end = cxld->hpa_range.end,
+ .start = cxld->config.hpa_range.start,
+ .end = cxld->config.hpa_range.end,
.flags = IORESOURCE_MEM,
};
diff --git a/drivers/cxl/core/atl.c b/drivers/cxl/core/atl.c
index 310668786189..07ce7c50c47a 100644
--- a/drivers/cxl/core/atl.c
+++ b/drivers/cxl/core/atl.c
@@ -172,8 +172,8 @@ static int cxl_prm_setup_root(struct cxl_root *cxl_root, void *data)
*
* Indicate that Normalized Addressing is enabled.
*/
- cxld->flags |= CXL_DECODER_F_LOCK;
- cxld->flags |= CXL_DECODER_F_NORMALIZED_ADDRESSING;
+ cxld->config.flags |= CXL_DECODER_F_LOCK;
+ cxld->config.flags |= CXL_DECODER_F_NORMALIZED_ADDRESSING;
ctx->hpa_range = hpa_range;
ctx->interleave_ways = ways;
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index fdcef20e01ba..01c6455e1581 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -387,11 +387,11 @@ static int __cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
if (cxled->dpa_res) {
dev_dbg(dev, "decoder%d.%d: existing allocation %pr assigned\n",
- port->id, cxled->cxld.id, cxled->dpa_res);
+ port->id, cxled->cxld.config.id, cxled->dpa_res);
return -EBUSY;
}
- if (port->hdm_end + 1 != cxled->cxld.id) {
+ if (port->hdm_end + 1 != cxled->cxld.config.id) {
/*
* Assumes alloc and commit order is always in hardware instance
* order per expectations from 8.2.5.12.20 Committing Decoder
@@ -399,7 +399,7 @@ static int __cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
* decoder[m+1] commit start.
*/
dev_dbg(dev, "decoder%d.%d: expected decoder%d.%d\n", port->id,
- cxled->cxld.id, port->id, port->hdm_end + 1);
+ cxled->cxld.config.id, port->id, port->hdm_end + 1);
return -EBUSY;
}
@@ -412,7 +412,7 @@ static int __cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
dev_name(&cxled->cxld.dev));
if (IS_ERR(res)) {
dev_dbg(dev, "decoder%d.%d: failed to reserve allocation\n",
- port->id, cxled->cxld.id);
+ port->id, cxled->cxld.config.id);
if (skipped)
release_skip(cxlds, base - skipped, skipped);
return PTR_ERR(res);
@@ -435,7 +435,7 @@ static int __cxl_dpa_reserve(struct cxl_endpoint_decoder *cxled,
/* Empty decoders may not be contained by a partition boundary */
if (cxled->part < 0 && resource_size(res))
dev_warn(dev, "decoder%d.%d: %pr does not map any partition\n",
- port->id, cxled->cxld.id, res);
+ port->id, cxled->cxld.config.id, res);
port->hdm_end++;
get_device(&cxled->cxld.dev);
@@ -605,11 +605,11 @@ int cxl_dpa_free(struct cxl_endpoint_decoder *cxled)
dev_name(&cxled->cxld.region->dev));
return -EBUSY;
}
- if (cxled->cxld.flags & CXL_DECODER_F_ENABLE) {
+ if (cxled->cxld.config.flags & CXL_DECODER_F_ENABLE) {
dev_dbg(dev, "decoder enabled\n");
return -EBUSY;
}
- if (cxled->cxld.id != port->hdm_end) {
+ if (cxled->cxld.config.id != port->hdm_end) {
dev_dbg(dev, "expected decoder%d.%d\n", port->id,
port->hdm_end);
return -EBUSY;
@@ -628,7 +628,7 @@ int cxl_dpa_set_part(struct cxl_endpoint_decoder *cxled,
int part;
guard(rwsem_write)(&cxl_rwsem.dpa);
- if (cxled->cxld.flags & CXL_DECODER_F_ENABLE)
+ if (cxled->cxld.config.flags & CXL_DECODER_F_ENABLE)
return -EBUSY;
for (part = 0; part < cxlds->nr_partitions; part++)
@@ -666,7 +666,7 @@ static int __cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, u64 size)
return -EBUSY;
}
- if (cxled->cxld.flags & CXL_DECODER_F_ENABLE) {
+ if (cxled->cxld.config.flags & CXL_DECODER_F_ENABLE) {
dev_dbg(dev, "decoder enabled\n");
return -EBUSY;
}
@@ -741,12 +741,12 @@ static void cxld_set_interleave(struct cxl_decoder *cxld, u32 *ctrl)
* Input validation ensures these warns never fire, but otherwise
* suppress unititalized variable usage warnings.
*/
- if (WARN_ONCE(ways_to_eiw(cxld->interleave_ways, &eiw),
- "invalid interleave_ways: %d\n", cxld->interleave_ways))
+ if (WARN_ONCE(ways_to_eiw(cxld->config.interleave_ways, &eiw),
+ "invalid interleave_ways: %d\n", cxld->config.interleave_ways))
return;
- if (WARN_ONCE(granularity_to_eig(cxld->interleave_granularity, &eig),
+ if (WARN_ONCE(granularity_to_eig(cxld->config.interleave_granularity, &eig),
"invalid interleave_granularity: %d\n",
- cxld->interleave_granularity))
+ cxld->config.interleave_granularity))
return;
u32p_replace_bits(ctrl, eig, CXL_HDM_DECODER0_CTRL_IG_MASK);
@@ -757,14 +757,14 @@ static void cxld_set_interleave(struct cxl_decoder *cxld, u32 *ctrl)
static void cxld_set_type(struct cxl_decoder *cxld, u32 *ctrl)
{
u32p_replace_bits(ctrl,
- !!(cxld->target_type == CXL_DECODER_HOSTONLYMEM),
+ !!(cxld->config.target_type == CXL_DECODER_HOSTONLYMEM),
CXL_HDM_DECODER0_CTRL_HOSTONLY);
}
static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt)
{
struct cxl_dport **t = &cxlsd->target[0];
- int ways = cxlsd->cxld.interleave_ways;
+ int ways = cxlsd->cxld.config.interleave_ways;
*tgt = FIELD_PREP(GENMASK(7, 0), t[0]->port_id);
if (ways > 1)
@@ -811,16 +811,16 @@ static int cxld_await_commit(void __iomem *hdm, int id)
static void setup_hw_decoder(struct cxl_decoder *cxld, void __iomem *hdm)
{
- int id = cxld->id;
+ int id = cxld->config.id;
u64 base, size;
u32 ctrl;
/* common decoder settings */
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->config.id));
cxld_set_interleave(cxld, &ctrl);
cxld_set_type(cxld, &ctrl);
- base = cxld->hpa_range.start;
- size = range_len(&cxld->hpa_range);
+ base = cxld->config.hpa_range.start;
+ size = range_len(&cxld->config.hpa_range);
writel(upper_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
writel(lower_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
@@ -855,9 +855,9 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
void __iomem *hdm = cxlhdm->regs.hdm_decoder;
- int id = cxld->id, rc;
+ int id = cxld->config.id, rc;
- if (cxld->flags & CXL_DECODER_F_ENABLE)
+ if (cxld->config.flags & CXL_DECODER_F_ENABLE)
return 0;
if (cxl_num_decoders_committed(port) != id) {
@@ -890,14 +890,14 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
scoped_guard(rwsem_read, &cxl_rwsem.dpa)
setup_hw_decoder(cxld, hdm);
- rc = cxld_await_commit(hdm, cxld->id);
+ rc = cxld_await_commit(hdm, cxld->config.id);
if (rc) {
dev_dbg(&port->dev, "%s: error %d committing decoder\n",
dev_name(&cxld->dev), rc);
return rc;
}
port->commit_end++;
- cxld->flags |= CXL_DECODER_F_ENABLE;
+ cxld->config.flags |= CXL_DECODER_F_ENABLE;
return 0;
}
@@ -911,8 +911,8 @@ static int commit_reap(struct device *dev, void *data)
return 0;
cxld = to_cxl_decoder(dev);
- if (port->commit_end == cxld->id &&
- ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
+ if (port->commit_end == cxld->config.id &&
+ ((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0)) {
port->commit_end--;
dev_dbg(&port->dev, "reap: %s commit_end: %d\n",
dev_name(&cxld->dev), port->commit_end);
@@ -944,13 +944,13 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
void __iomem *hdm = cxlhdm->regs.hdm_decoder;
- int id = cxld->id;
+ int id = cxld->config.id;
u32 ctrl;
- if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
+ if ((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0)
return;
- if (cxld->flags & CXL_DECODER_F_LOCK)
+ if (cxld->config.flags & CXL_DECODER_F_LOCK)
return;
if (port->commit_end == id)
@@ -969,7 +969,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)
writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
- cxld->flags &= ~CXL_DECODER_F_ENABLE;
+ cxld->config.flags &= ~CXL_DECODER_F_ENABLE;
/* Userspace is now responsible for reconfiguring this decoder */
if (is_endpoint_decoder(&cxld->dev)) {
@@ -996,24 +996,24 @@ static int cxl_setup_hdm_decoder_from_dvsec(
if (!len)
return -ENOENT;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
cxld->commit = NULL;
cxld->reset = NULL;
- cxld->hpa_range = info->dvsec_range[which];
+ cxld->config.hpa_range = info->dvsec_range[which];
/*
* Set the emulated decoder as locked pending additional support to
* change the range registers at run time.
*/
- cxld->flags |= CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
+ cxld->config.flags |= CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
scoped_guard(rwsem_write, &cxl_rwsem.region)
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
rc = devm_cxl_dpa_reserve(cxled, *dpa_base, len, 0);
if (rc) {
dev_err(&port->dev,
"decoder%d.%d: Failed to reserve DPA range %#llx - %#llx: %d\n",
- port->id, cxld->id, *dpa_base, *dpa_base + len - 1, rc);
+ port->id, cxld->config.id, *dpa_base, *dpa_base + len - 1, rc);
return rc;
}
*dpa_base += len;
@@ -1053,36 +1053,36 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
size = 0;
if (base == U64_MAX || size == U64_MAX) {
dev_warn(&port->dev, "decoder%d.%d: Invalid resource range\n",
- port->id, cxld->id);
+ port->id, cxld->config.id);
return -ENXIO;
}
if (info)
cxled = to_cxl_endpoint_decoder(&cxld->dev);
- cxld->hpa_range = (struct range) {
+ cxld->config.hpa_range = (struct range) {
.start = base,
.end = base + size - 1,
};
/* decoders are enabled if committed */
if (committed) {
- cxld->flags |= CXL_DECODER_F_ENABLE;
+ cxld->config.flags |= CXL_DECODER_F_ENABLE;
if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
- cxld->flags |= CXL_DECODER_F_LOCK;
+ cxld->config.flags |= CXL_DECODER_F_LOCK;
if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
else
- cxld->target_type = CXL_DECODER_DEVMEM;
+ cxld->config.target_type = CXL_DECODER_DEVMEM;
guard(rwsem_write)(&cxl_rwsem.region);
- if (cxld->id != cxl_num_decoders_committed(port)) {
+ if (cxld->config.id != cxl_num_decoders_committed(port)) {
dev_warn(&port->dev,
"decoder%d.%d: Committed out of order\n",
- port->id, cxld->id);
+ port->id, cxld->config.id);
return -ENXIO;
}
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
} else {
if (cxled) {
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
@@ -1093,48 +1093,48 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
* more precision.
*/
if (cxlds->type == CXL_DEVTYPE_CLASSMEM)
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
else
- cxld->target_type = CXL_DECODER_DEVMEM;
+ cxld->config.target_type = CXL_DECODER_DEVMEM;
} else {
/* To be overridden by region type at commit time */
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
}
if (!FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl) &&
- cxld->target_type == CXL_DECODER_HOSTONLYMEM) {
+ cxld->config.target_type == CXL_DECODER_HOSTONLYMEM) {
ctrl |= CXL_HDM_DECODER0_CTRL_HOSTONLY;
writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(which));
}
}
rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl),
- &cxld->interleave_ways);
+ &cxld->config.interleave_ways);
if (rc) {
dev_warn(&port->dev,
"decoder%d.%d: Invalid interleave ways (ctrl: %#x)\n",
- port->id, cxld->id, ctrl);
+ port->id, cxld->config.id, ctrl);
return rc;
}
rc = eig_to_granularity(FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl),
- &cxld->interleave_granularity);
+ &cxld->config.interleave_granularity);
if (rc) {
dev_warn(&port->dev,
"decoder%d.%d: Invalid interleave granularity (ctrl: %#x)\n",
- port->id, cxld->id, ctrl);
+ port->id, cxld->config.id, ctrl);
return rc;
}
dev_dbg(&port->dev, "decoder%d.%d: range: %#llx-%#llx iw: %d ig: %d\n",
- port->id, cxld->id, cxld->hpa_range.start, cxld->hpa_range.end,
- cxld->interleave_ways, cxld->interleave_granularity);
+ port->id, cxld->config.id, cxld->config.hpa_range.start, cxld->config.hpa_range.end,
+ cxld->config.interleave_ways, cxld->config.interleave_granularity);
if (!cxled) {
struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(&cxld->dev);
- if (cxld->interleave_ways > cxlsd->nr_targets) {
+ if (cxld->config.interleave_ways > cxlsd->nr_targets) {
dev_err(&port->dev,
"decoder%d.%d: interleave ways: %d exceeds targets: %d\n",
- port->id, cxld->id, cxld->interleave_ways,
+ port->id, cxld->config.id, cxld->config.interleave_ways,
cxlsd->nr_targets);
return -ENXIO;
}
@@ -1143,7 +1143,7 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
&target_id[0]);
put_unaligned_le32(readl(hdm + CXL_HDM_DECODER0_TL_HIGH(which)),
&target_id[4]);
- for (i = 0; i < cxld->interleave_ways; i++)
+ for (i = 0; i < cxld->config.interleave_ways; i++)
cxld->target_map[i] = target_id[i];
return 0;
@@ -1152,11 +1152,11 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
if (!committed)
return 0;
- dpa_size = div_u64_rem(size, cxld->interleave_ways, &remainder);
+ dpa_size = div_u64_rem(size, cxld->config.interleave_ways, &remainder);
if (remainder) {
dev_err(&port->dev,
"decoder%d.%d: invalid committed configuration size: %#llx ways: %d\n",
- port->id, cxld->id, size, cxld->interleave_ways);
+ port->id, cxld->config.id, size, cxld->config.interleave_ways);
return -ENXIO;
}
lo = readl(hdm + CXL_HDM_DECODER0_SKIP_LOW(which));
@@ -1166,7 +1166,7 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
if (rc) {
dev_err(&port->dev,
"decoder%d.%d: Failed to reserve DPA range %#llx - %#llx: %d\n",
- port->id, cxld->id, *dpa_base,
+ port->id, cxld->config.id, *dpa_base,
*dpa_base + dpa_size + skip - 1, rc);
return rc;
}
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 1dd16d0f17f6..63dc3f39b5d8 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -231,10 +231,10 @@ static int dvsec_range_allowed(struct device *dev, const void *arg)
cxld = to_cxl_decoder(dev);
- if (!(cxld->flags & CXL_DECODER_F_RAM))
+ if (!(cxld->config.flags & CXL_DECODER_F_RAM))
return 0;
- return range_contains(&cxld->hpa_range, dev_range);
+ return range_contains(&cxld->config.hpa_range, dev_range);
}
static void disable_hdm(void *_cxlhdm)
@@ -673,7 +673,7 @@ static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data)
return 0;
cxld = to_cxl_decoder(dev);
- if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
+ if ((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0)
return 0;
/*
@@ -687,7 +687,7 @@ static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data)
cxlhdm = dev_get_drvdata(&port->dev);
hdm = cxlhdm->regs.hdm_decoder;
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->config.id));
return !FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl);
}
diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index e4d593a36514..cddbdd441ca3 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -129,7 +129,7 @@ static ssize_t start_show(struct device *dev, struct device_attribute *attr,
{
struct cxl_decoder *cxld = to_cxl_decoder(dev);
- return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start);
+ return sysfs_emit(buf, "%#llx\n", cxld->config.hpa_range.start);
}
static DEVICE_ATTR_ADMIN_RO(start);
@@ -138,7 +138,7 @@ static ssize_t size_show(struct device *dev, struct device_attribute *attr,
{
struct cxl_decoder *cxld = to_cxl_decoder(dev);
- return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range));
+ return sysfs_emit(buf, "%#llx\n", range_len(&cxld->config.hpa_range));
}
static DEVICE_ATTR_RO(size);
@@ -149,7 +149,7 @@ static ssize_t name##_show(struct device *dev, \
struct cxl_decoder *cxld = to_cxl_decoder(dev); \
\
return sysfs_emit(buf, "%s\n", \
- (cxld->flags & (flag)) ? "1" : "0"); \
+ (cxld->config.flags & (flag)) ? "1" : "0"); \
} \
static DEVICE_ATTR_RO(name)
@@ -164,7 +164,7 @@ static ssize_t target_type_show(struct device *dev,
{
struct cxl_decoder *cxld = to_cxl_decoder(dev);
- switch (cxld->target_type) {
+ switch (cxld->config.target_type) {
case CXL_DECODER_DEVMEM:
return sysfs_emit(buf, "accelerator\n");
case CXL_DECODER_HOSTONLYMEM:
@@ -180,7 +180,7 @@ static DEVICE_ATTR_RO(target_type);
*/
static int cxlsd_nr_used_targets(struct cxl_switch_decoder *cxlsd)
{
- return min(cxlsd->cxld.interleave_ways, cxlsd->nr_targets);
+ return min(cxlsd->cxld.config.interleave_ways, cxlsd->nr_targets);
}
static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf)
@@ -323,7 +323,7 @@ static ssize_t interleave_granularity_show(struct device *dev,
{
struct cxl_decoder *cxld = to_cxl_decoder(dev);
- return sysfs_emit(buf, "%d\n", cxld->interleave_granularity);
+ return sysfs_emit(buf, "%d\n", cxld->config.interleave_granularity);
}
static DEVICE_ATTR_RO(interleave_granularity);
@@ -333,7 +333,7 @@ static ssize_t interleave_ways_show(struct device *dev,
{
struct cxl_decoder *cxld = to_cxl_decoder(dev);
- return sysfs_emit(buf, "%d\n", cxld->interleave_ways);
+ return sysfs_emit(buf, "%d\n", cxld->config.interleave_ways);
}
static DEVICE_ATTR_RO(interleave_ways);
@@ -377,14 +377,14 @@ static bool can_create_pmem(struct cxl_root_decoder *cxlrd)
{
unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM;
- return (cxlrd->cxlsd.cxld.flags & flags) == flags;
+ return (cxlrd->cxlsd.cxld.config.flags & flags) == flags;
}
static bool can_create_ram(struct cxl_root_decoder *cxlrd)
{
unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM;
- return (cxlrd->cxlsd.cxld.flags & flags) == flags;
+ return (cxlrd->cxlsd.cxld.config.flags & flags) == flags;
}
static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n)
@@ -459,7 +459,7 @@ static void __cxl_decoder_release(struct cxl_decoder *cxld)
{
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
- ida_free(&port->decoder_ida, cxld->id);
+ ida_free(&port->decoder_ida, cxld->config.id);
put_device(&port->dev);
}
@@ -2071,7 +2071,7 @@ static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
/* need parent to stick around to release the id */
get_device(&port->dev);
- cxld->id = rc;
+ cxld->config.id = rc;
dev = &cxld->dev;
device_initialize(dev);
@@ -2081,10 +2081,10 @@ static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
dev->bus = &cxl_bus_type;
/* Pre initialize an "empty" decoder */
- cxld->interleave_ways = 1;
- cxld->interleave_granularity = PAGE_SIZE;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->hpa_range = (struct range) {
+ cxld->config.interleave_ways = 1;
+ cxld->config.interleave_granularity = PAGE_SIZE;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.hpa_range = (struct range) {
.start = 0,
.end = -1,
};
@@ -2255,7 +2255,7 @@ int cxl_decoder_add_locked(struct cxl_decoder *cxld)
if (WARN_ON_ONCE(IS_ERR(cxld)))
return PTR_ERR(cxld);
- if (cxld->interleave_ways < 1)
+ if (cxld->config.interleave_ways < 1)
return -EINVAL;
dev = &cxld->dev;
@@ -2265,14 +2265,14 @@ int cxl_decoder_add_locked(struct cxl_decoder *cxld)
struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
rc = decoder_populate_targets(cxlsd, port);
- if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
+ if (rc && (cxld->config.flags & CXL_DECODER_F_ENABLE)) {
dev_err(&port->dev,
"Failed to populate active decoder targets\n");
return rc;
}
}
- rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
+ rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->config.id);
if (rc)
return rc;
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index a048bd5c06cb..5bdaeeb30f4a 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -503,8 +503,8 @@ static int set_interleave_ways(struct cxl_region *cxlr, int val)
* Even for x3, x6, and x12 interleaves the region interleave must be a
* power of 2 multiple of the host bridge interleave.
*/
- if (!is_power_of_2(val / cxld->interleave_ways) ||
- (val % cxld->interleave_ways)) {
+ if (!is_power_of_2(val / cxld->config.interleave_ways) ||
+ (val % cxld->config.interleave_ways)) {
dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
return -EINVAL;
}
@@ -581,7 +581,7 @@ static int set_interleave_granularity(struct cxl_region *cxlr, int val)
* with a granularity greater than the root interleave result in invalid
* DPA translations (invalid to support).
*/
- if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
+ if (cxld->config.interleave_ways > 1 && val != cxld->config.interleave_granularity)
return -EINVAL;
lockdep_assert_held_write(&cxl_rwsem.region);
@@ -881,7 +881,7 @@ static int check_commit_order(struct device *dev, void *data)
* order shutdown has occurred, block further allocations until
* that is resolved
*/
- if (((cxld->flags & CXL_DECODER_F_ENABLE) == 0))
+ if (((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0))
return -EBUSY;
return 0;
}
@@ -897,7 +897,7 @@ static int match_free_decoder(struct device *dev, const void *data)
cxld = to_cxl_decoder(dev);
- if (cxld->id != port->commit_end + 1)
+ if (cxld->config.id != port->commit_end + 1)
return 0;
if (cxld->region) {
@@ -943,7 +943,7 @@ static int match_auto_decoder(struct device *dev, const void *data)
return 0;
cxld = to_cxl_decoder(dev);
- r = &cxld->hpa_range;
+ r = &cxld->config.hpa_range;
if (spa_maps_hpa(p, r))
return 1;
@@ -1004,10 +1004,10 @@ static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
* starting address has the lesser id.
*/
dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
- dev_name(&cxld->dev), cxld->id,
- dev_name(&cxld_iter->dev), cxld_iter->id);
+ dev_name(&cxld->dev), cxld->config.id,
+ dev_name(&cxld_iter->dev), cxld_iter->config.id);
- if (cxld_iter->id > cxld->id)
+ if (cxld_iter->config.id > cxld->config.id)
return true;
return false;
@@ -1138,11 +1138,11 @@ static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
*/
dev_WARN_ONCE(&cxlr->dev,
port == cxled_to_port(cxled) &&
- cxld->target_type != cxlr->type,
+ cxld->config.target_type != cxlr->type,
"%s:%s mismatch decoder type %d -> %d\n",
dev_name(&cxled_to_memdev(cxled)->dev),
- dev_name(&cxld->dev), cxld->target_type, cxlr->type);
- cxld->target_type = cxlr->type;
+ dev_name(&cxld->dev), cxld->config.target_type, cxlr->type);
+ cxld->config.target_type = cxlr->type;
cxl_rr->decoder = cxld;
return 0;
}
@@ -1163,12 +1163,12 @@ static void cxl_region_setup_flags(struct cxl_region *cxlr,
set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
}
- if (cxld->flags & CXL_DECODER_F_LOCK) {
+ if (cxld->config.flags & CXL_DECODER_F_LOCK) {
set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
}
- if (cxld->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
+ if (cxld->config.flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
set_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags);
}
@@ -1485,7 +1485,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
distance *= cxl_rr_iter->nr_targets;
iter = to_cxl_port(iter->dev.parent);
} while (!is_cxl_root(iter));
- distance *= cxlrd->cxlsd.cxld.interleave_ways;
+ distance *= cxlrd->cxlsd.cxld.config.interleave_ways;
for (i = 0; i < cxl_rr->nr_targets_set; i++)
if (ep->dport == cxlsd->target[i]) {
@@ -1507,7 +1507,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
* root IG != region IG.
*/
parent_ig = p->interleave_granularity;
- parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
+ parent_iw = cxlrd->cxlsd.cxld.config.interleave_ways;
/*
* For purposes of address bit routing, use power-of-2 math for
* switch ports.
@@ -1520,8 +1520,8 @@ static int cxl_port_setup_targets(struct cxl_port *port,
parent_rr = cxl_rr_load(parent_port, cxlr);
parent_cxld = parent_rr->decoder;
- parent_ig = parent_cxld->interleave_granularity;
- parent_iw = parent_cxld->interleave_ways;
+ parent_ig = parent_cxld->config.interleave_granularity;
+ parent_iw = parent_cxld->config.interleave_ways;
}
rc = granularity_to_eig(parent_ig, &peig);
@@ -1577,10 +1577,10 @@ static int cxl_port_setup_targets(struct cxl_port *port,
}
if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
- if (cxld->interleave_ways != iw ||
- (iw > 1 && cxld->interleave_granularity != ig) ||
- !spa_maps_hpa(p, &cxld->hpa_range) ||
- ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
+ if (cxld->config.interleave_ways != iw ||
+ (iw > 1 && cxld->config.interleave_granularity != ig) ||
+ !spa_maps_hpa(p, &cxld->config.hpa_range) ||
+ ((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0)) {
dev_err(&cxlr->dev,
"%s:%s %s expected iw: %d ig: %d %pr\n",
dev_name(port->uport_dev), dev_name(&port->dev),
@@ -1588,10 +1588,10 @@ static int cxl_port_setup_targets(struct cxl_port *port,
dev_err(&cxlr->dev,
"%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
dev_name(port->uport_dev), dev_name(&port->dev),
- __func__, cxld->interleave_ways,
- cxld->interleave_granularity,
- str_enabled_disabled(cxld->flags & CXL_DECODER_F_ENABLE),
- cxld->hpa_range.start, cxld->hpa_range.end);
+ __func__, cxld->config.interleave_ways,
+ cxld->config.interleave_granularity,
+ str_enabled_disabled(cxld->config.flags & CXL_DECODER_F_ENABLE),
+ cxld->config.hpa_range.start, cxld->config.hpa_range.end);
return -ENXIO;
}
} else {
@@ -1604,9 +1604,9 @@ static int cxl_port_setup_targets(struct cxl_port *port,
return rc;
}
- cxld->interleave_ways = iw;
- cxld->interleave_granularity = ig;
- cxld->hpa_range = (struct range) {
+ cxld->config.interleave_ways = iw;
+ cxld->config.interleave_granularity = ig;
+ cxld->config.hpa_range = (struct range) {
.start = p->res->start,
.end = p->res->end,
};
@@ -1659,7 +1659,7 @@ static void cxl_port_reset_targets(struct cxl_port *port,
cxl_rr->nr_targets_set = 0;
cxld = cxl_rr->decoder;
- cxld->hpa_range = (struct range) {
+ cxld->config.hpa_range = (struct range) {
.start = 0,
.end = -1,
};
@@ -1806,7 +1806,7 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
struct cxl_decoder *cxld = &cxlsd->cxld;
- int iw = cxld->interleave_ways;
+ int iw = cxld->config.interleave_ways;
struct cxl_port *iter;
int rc;
@@ -1904,7 +1904,7 @@ static int match_switch_decoder_by_range(struct device *dev,
return 0;
cxlsd = to_cxl_switch_decoder(dev);
- r1 = &cxlsd->cxld.hpa_range;
+ r1 = &cxlsd->cxld.config.hpa_range;
if (is_root_decoder(dev))
return range_contains(r1, r2);
@@ -1931,7 +1931,7 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
return rc;
}
cxlsd = to_cxl_switch_decoder(dev);
- *ways = cxlsd->cxld.interleave_ways;
+ *ways = cxlsd->cxld.config.interleave_ways;
for (int i = 0; i < *ways; i++) {
if (cxlsd->target[i] == port->parent_dport) {
@@ -2107,10 +2107,10 @@ static int cxl_region_attach(struct cxl_region *cxlr,
return -ENXIO;
}
- if (cxled->cxld.target_type != cxlr->type) {
+ if (cxled->cxld.config.target_type != cxlr->type) {
dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
- cxled->cxld.target_type, cxlr->type);
+ cxled->cxld.config.target_type, cxlr->type);
return -ENXIO;
}
@@ -2196,9 +2196,9 @@ static int cxl_region_attach(struct cxl_region *cxlr,
cxl_region_shared_upstream_bandwidth_update(cxlr);
}
- cxled->cxld.interleave_ways = p->interleave_ways;
- cxled->cxld.interleave_granularity = p->interleave_granularity;
- cxled->cxld.hpa_range = (struct range) {
+ cxled->cxld.config.interleave_ways = p->interleave_ways;
+ cxled->cxld.config.interleave_granularity = p->interleave_granularity;
+ cxled->cxld.config.hpa_range = (struct range) {
.start = p->res->start,
.end = p->res->end,
};
@@ -2323,7 +2323,7 @@ __cxl_decoder_detach(struct cxl_region *cxlr,
}
p->targets[cxled->pos] = NULL;
p->nr_targets--;
- cxled->cxld.hpa_range = (struct range) {
+ cxled->cxld.config.hpa_range = (struct range) {
.start = 0,
.end = -1,
};
@@ -3007,7 +3007,7 @@ static int poison_by_decoder(struct device *dev, void *arg)
}
/* Iterate until commit_end is reached */
- if (cxled->cxld.id == ctx->port->commit_end) {
+ if (cxled->cxld.config.id == ctx->port->commit_end) {
ctx->offset = cxled->dpa_res->end + 1;
ctx->part = cxled->part;
return 1;
@@ -3330,10 +3330,10 @@ static u64 restore_parent(u64 addr, u64 pos, u64 gran, u64 ways)
static u64 unaligned_dpa_to_hpa(struct cxl_decoder *cxld,
struct cxl_region_params *p, int pos, u64 dpa)
{
- int ways_port = p->interleave_ways / cxld->interleave_ways;
+ int ways_port = p->interleave_ways / cxld->config.interleave_ways;
int gran_port = p->interleave_granularity;
- int gran_hb = cxld->interleave_granularity;
- int ways_hb = cxld->interleave_ways;
+ int gran_hb = cxld->config.interleave_granularity;
+ int ways_hb = cxld->config.interleave_ways;
int pos_port, pos_hb, gran_shift;
u64 hpa_port = 0;
@@ -3380,7 +3380,7 @@ static bool region_is_unaligned_mod3(struct cxl_region *cxlr)
struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
struct cxl_region_params *p = &cxlr->params;
- int hbiw = cxld->interleave_ways;
+ int hbiw = cxld->config.interleave_ways;
u64 rem;
if (is_power_of_2(hbiw))
@@ -3495,8 +3495,8 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
* a dpa_offset independent of the target device and then enumerate
* and test that dpa_offset against each candidate endpoint decoder.
*/
- gran = cxld->interleave_granularity;
- interleave_width = gran * cxld->interleave_ways;
+ gran = cxld->config.interleave_granularity;
+ interleave_width = gran * cxld->config.interleave_ways;
interleave_index = div64_u64(offset, interleave_width);
gran_offset = do_div(tmp, gran);
@@ -3596,7 +3596,7 @@ static int match_root_decoder(struct device *dev, const void *data)
return 0;
cxlrd = to_cxl_root_decoder(dev);
- r1 = &cxlrd->cxlsd.cxld.hpa_range;
+ r1 = &cxlrd->cxlsd.cxld.config.hpa_range;
return range_contains(r1, r2);
}
@@ -3801,7 +3801,7 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
do {
cxlr = __create_region(cxlrd, cxlds->part[part].mode,
atomic_read(&cxlrd->region_id),
- cxled->cxld.target_type);
+ cxled->cxld.config.target_type);
} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
if (IS_ERR(cxlr)) {
@@ -3844,9 +3844,9 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
ctx = (struct cxl_region_context) {
.cxled = cxled,
- .hpa_range = cxled->cxld.hpa_range,
- .interleave_ways = cxled->cxld.interleave_ways,
- .interleave_granularity = cxled->cxld.interleave_granularity,
+ .hpa_range = cxled->cxld.config.hpa_range,
+ .interleave_ways = cxled->cxld.config.interleave_ways,
+ .interleave_granularity = cxled->cxld.config.interleave_granularity,
};
struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 648b4a849ab4..9575ecd27ed8 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -287,7 +287,7 @@ enum cxl_decoder_state {
* struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder
* @cxld: base cxl_decoder_object
* @dpa_res: actively claimed DPA span of this decoder
- * @skip: offset into @dpa_res where @cxld.hpa_range maps
+ * @skip: offset into @dpa_res where @cxld.config.hpa_range maps
* @state: autodiscovery state
* @part: partition index this decoder maps
* @pos: interleave position in @cxld.region
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 3ed7c3e70042..adcfff33bd6d 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -1096,7 +1096,7 @@ static int cxl_endpoint_decoder_clear_reset_flags(struct device *dev, void *data
return 0;
cxled = to_cxl_endpoint_decoder(dev);
- cxled->cxld.flags &= ~CXL_DECODER_F_RESET_MASK;
+ cxled->cxld.config.flags &= ~CXL_DECODER_F_RESET_MASK;
return 0;
}
diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
index c12fd0b89883..b369d3e3876d 100644
--- a/drivers/cxl/port.c
+++ b/drivers/cxl/port.c
@@ -40,7 +40,7 @@ static int discover_region(struct device *dev, void *unused)
return 0;
cxled = to_cxl_endpoint_decoder(dev);
- if ((cxled->cxld.flags & CXL_DECODER_F_ENABLE) == 0)
+ if ((cxled->cxld.config.flags & CXL_DECODER_F_ENABLE) == 0)
return 0;
if (cxled->state != CXL_DECODER_STATE_AUTO)
@@ -56,7 +56,7 @@ static int discover_region(struct device *dev, void *unused)
rc = cxl_add_to_region(cxled);
if (rc)
dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
- cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
+ cxled->cxld.config.hpa_range.start, cxled->cxld.config.hpa_range.end);
return 0;
}
diff --git a/include/cxl/cxl.h b/include/cxl/cxl.h
index 9b4003770aa2..713bce162bd0 100644
--- a/include/cxl/cxl.h
+++ b/include/cxl/cxl.h
@@ -36,15 +36,31 @@ enum cxl_decoder_type {
#define CXL_DECODER_MAX_INTERLEAVE 16
/**
- * struct cxl_decoder - Common CXL HDM Decoder Attributes
- * @dev: this decoder's device
+ * struct cxl_decoder_config - common CXL decoder configuration and state
* @id: kernel device name id
* @hpa_range: Host physical address range mapped by this decoder
* @interleave_ways: number of cxl_dports in this decode
* @interleave_granularity: data stride per dport
* @target_type: accelerator vs expander (type2 vs type3) selector
- * @region: currently assigned region for this decoder
* @flags: memory type capabilities and locking
+ *
+ * This is not a complete snapshot of the hardware registers. Switch target
+ * lists and endpoint DPA skip values are not tracked in this common structure.
+ */
+struct cxl_decoder_config {
+ int id;
+ struct range hpa_range;
+ int interleave_ways;
+ int interleave_granularity;
+ enum cxl_decoder_type target_type;
+ unsigned long flags;
+};
+
+/**
+ * struct cxl_decoder - Common CXL HDM Decoder Attributes
+ * @dev: this decoder's device
+ * @config: common decoder configuration and state
+ * @region: currently assigned region for this decoder
* @target_map: cached copy of hardware port-id list, available at init
* before all @dport objects have been instantiated. While
* dport id is 8bit, CFMWS interleave targets are 32bits.
@@ -53,13 +69,8 @@ enum cxl_decoder_type {
*/
struct cxl_decoder {
struct device dev;
- int id;
- struct range hpa_range;
- int interleave_ways;
- int interleave_granularity;
- enum cxl_decoder_type target_type;
+ struct cxl_decoder_config config;
struct cxl_region *region;
- unsigned long flags;
u32 target_map[CXL_DECODER_MAX_INTERLEAVE];
int (*commit)(struct cxl_decoder *cxld);
void (*reset)(struct cxl_decoder *cxld);
diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
index 3eb1051b1ee8..884229869bce 100644
--- a/tools/testing/cxl/test/cxl.c
+++ b/tools/testing/cxl/test/cxl.c
@@ -773,7 +773,7 @@ static int map_targets(struct device *dev, void *data)
* id into the low bits.
*
* The key is formed as:
- * ((unsigned long)port->uport_dev << 4) | cxld->id
+ * ((unsigned long)port->uport_dev << 4) | cxld->config.id
*
* The low bits hold the decoder id (which must fit in 4 bits) while
* the remaining bits identify the upstream port. This key is only used
@@ -783,9 +783,9 @@ static unsigned long cxld_registry_index(struct cxl_decoder *cxld)
{
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
- dev_WARN_ONCE(&port->dev, cxld->id >= 16,
- "decoder id:%d out of range\n", cxld->id);
- return (((unsigned long)port->uport_dev) << 4) | cxld->id;
+ dev_WARN_ONCE(&port->dev, cxld->config.id >= 16,
+ "decoder id:%d out of range\n", cxld->config.id);
+ return (((unsigned long)port->uport_dev) << 4) | cxld->config.id;
}
struct cxl_test_decoder {
@@ -801,14 +801,15 @@ static struct cxl_test_decoder *cxld_registry_find(struct cxl_decoder *cxld)
return xa_load(&decoder_registry, cxld_registry_index(cxld));
}
-#define dbg_cxld(port, msg, cxld) \
- do { \
- struct cxl_decoder *___d = (cxld); \
- dev_dbg((port)->uport_dev, \
+#define dbg_cxld(port, msg, cxld) \
+ do { \
+ struct cxl_decoder *___d = (cxld); \
+ const struct cxl_decoder_config *___c = &___d->config; \
+ dev_dbg((port)->uport_dev, \
"decoder%d: %s range: %#llx-%#llx iw: %d ig: %d flags: %#lx\n", \
- ___d->id, msg, ___d->hpa_range.start, \
- ___d->hpa_range.end + 1, ___d->interleave_ways, \
- ___d->interleave_granularity, ___d->flags); \
+ ___c->id, msg, ___c->hpa_range.start, \
+ ___c->hpa_range.end + 1, ___c->interleave_ways, \
+ ___c->interleave_granularity, ___c->flags); \
} while (0)
static int mock_decoder_commit(struct cxl_decoder *cxld);
@@ -817,12 +818,7 @@ static void init_disabled_mock_decoder(struct cxl_decoder *cxld);
static void cxld_copy(struct cxl_decoder *a, struct cxl_decoder *b)
{
- a->id = b->id;
- a->hpa_range = b->hpa_range;
- a->interleave_ways = b->interleave_ways;
- a->interleave_granularity = b->interleave_granularity;
- a->target_type = b->target_type;
- a->flags = b->flags;
+ a->config = b->config;
a->commit = mock_decoder_commit;
a->reset = mock_decoder_reset;
}
@@ -847,7 +843,7 @@ static int cxld_registry_restore(struct cxl_decoder *cxld,
struct cxl_switch_decoder *cxlsd =
to_cxl_switch_decoder(&cxld->dev);
- if (!(td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE))
+ if (!(td->cxlsd.cxld.config.flags & CXL_DECODER_F_ENABLE))
return 0;
dbg_cxld(port, "restore", &td->cxlsd.cxld);
@@ -860,13 +856,13 @@ static int cxld_registry_restore(struct cxl_decoder *cxld,
cxld->target_map[i] = td->cxlsd.cxld.target_map[i];
}
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
} else {
struct cxl_endpoint_decoder *cxled =
to_cxl_endpoint_decoder(&cxld->dev);
- if (!(td->cxled.cxld.flags & CXL_DECODER_F_ENABLE))
+ if (!(td->cxled.cxld.config.flags & CXL_DECODER_F_ENABLE))
return 0;
dbg_cxld(port, "restore", &td->cxled.cxld);
@@ -881,7 +877,7 @@ static int cxld_registry_restore(struct cxl_decoder *cxld,
init_disabled_mock_decoder(cxld);
return rc;
}
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
}
return 0;
@@ -915,7 +911,7 @@ static void __cxld_registry_save(struct cxl_test_decoder *td,
td->cxled.state = cxled->state;
td->cxled.skip = cxled->skip;
- if (!(cxld->flags & CXL_DECODER_F_ENABLE)) {
+ if (!(cxld->config.flags & CXL_DECODER_F_ENABLE)) {
td->dpa_range.start = 0;
td->dpa_range.end = -1;
} else if (cxled->dpa_res) {
@@ -952,9 +948,9 @@ static void cxld_registry_update(struct cxl_decoder *cxld)
static int mock_decoder_commit(struct cxl_decoder *cxld)
{
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
- int id = cxld->id;
+ int id = cxld->config.id;
- if (cxld->flags & CXL_DECODER_F_ENABLE)
+ if (cxld->config.flags & CXL_DECODER_F_ENABLE)
return 0;
dev_dbg(&port->dev, "%s commit\n", dev_name(&cxld->dev));
@@ -967,7 +963,7 @@ static int mock_decoder_commit(struct cxl_decoder *cxld)
}
port->commit_end++;
- cxld->flags |= CXL_DECODER_F_ENABLE;
+ cxld->config.flags |= CXL_DECODER_F_ENABLE;
if (is_endpoint_decoder(&cxld->dev)) {
struct cxl_endpoint_decoder *cxled =
to_cxl_endpoint_decoder(&cxld->dev);
@@ -982,9 +978,9 @@ static int mock_decoder_commit(struct cxl_decoder *cxld)
static void mock_decoder_reset(struct cxl_decoder *cxld)
{
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
- int id = cxld->id;
+ int id = cxld->config.id;
- if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
+ if ((cxld->config.flags & CXL_DECODER_F_ENABLE) == 0)
return;
dev_dbg(&port->dev, "%s reset\n", dev_name(&cxld->dev));
@@ -994,7 +990,7 @@ static void mock_decoder_reset(struct cxl_decoder *cxld)
dev_dbg(&port->dev,
"%s: out of order reset, expected decoder%d.%d\n",
dev_name(&cxld->dev), port->id, port->commit_end);
- cxld->flags &= ~CXL_DECODER_F_ENABLE;
+ cxld->config.flags &= ~CXL_DECODER_F_ENABLE;
if (is_endpoint_decoder(&cxld->dev)) {
struct cxl_endpoint_decoder *cxled =
@@ -1005,7 +1001,7 @@ static void mock_decoder_reset(struct cxl_decoder *cxld)
}
if (decoder_reset_preserve_registry)
dev_dbg(port->uport_dev, "decoder%d: skip registry update\n",
- cxld->id);
+ cxld->config.id);
else
cxld_registry_update(cxld);
}
@@ -1030,12 +1026,12 @@ static struct cxl_test_decoder *cxld_registry_new(struct cxl_decoder *cxld)
static void init_disabled_mock_decoder(struct cxl_decoder *cxld)
{
- cxld->hpa_range.start = 0;
- cxld->hpa_range.end = -1;
- cxld->interleave_ways = 1;
- cxld->interleave_granularity = 0;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->flags = 0;
+ cxld->config.hpa_range.start = 0;
+ cxld->config.hpa_range.end = -1;
+ cxld->config.interleave_ways = 1;
+ cxld->config.interleave_granularity = 0;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.flags = 0;
cxld->commit = mock_decoder_commit;
cxld->reset = mock_decoder_reset;
@@ -1058,14 +1054,14 @@ static void init_disabled_mock_decoder(struct cxl_decoder *cxld)
static void default_mock_decoder(struct cxl_decoder *cxld)
{
- cxld->hpa_range = (struct range){
+ cxld->config.hpa_range = (struct range){
.start = 0,
.end = -1,
};
- cxld->interleave_ways = 1;
- cxld->interleave_granularity = 256;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.interleave_ways = 1;
+ cxld->config.interleave_granularity = 256;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
cxld->commit = mock_decoder_commit;
cxld->reset = mock_decoder_reset;
@@ -1080,7 +1076,7 @@ static int match_decoder_by_index(struct device *dev, const void *data)
if (!is_switch_decoder(dev))
return 0;
cxld = to_cxl_decoder(dev);
- return cxld->id == target_id;
+ return cxld->config.id == target_id;
}
/*
@@ -1093,11 +1089,11 @@ static void size_zero_mock_decoder_ep(struct cxl_decoder *cxld, u64 base)
{
struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
- cxld->hpa_range = DEFINE_RANGE(base, base - 1);
- cxld->interleave_ways = 2;
- cxld->interleave_granularity = 4096;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->flags = CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
+ cxld->config.hpa_range = DEFINE_RANGE(base, base - 1);
+ cxld->config.interleave_ways = 2;
+ cxld->config.interleave_granularity = 4096;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.flags = CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
cxled->state = CXL_DECODER_STATE_AUTO;
/* decoder[0] reserved [0, size/2), empty decoders sit at that watermark */
devm_cxl_dpa_reserve(cxled, mock_auto_region_size / 2, 0, 0);
@@ -1108,11 +1104,11 @@ static void size_zero_mock_decoder_ep(struct cxl_decoder *cxld, u64 base)
static void size_zero_mock_decoder_sw(struct cxl_decoder *cxld, u64 base,
int level)
{
- cxld->flags = CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->interleave_ways = level == 0 ? 2 : 1;
- cxld->interleave_granularity = 4096;
- cxld->hpa_range = DEFINE_RANGE(base, base - 1);
+ cxld->config.flags = CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.interleave_ways = level == 0 ? 2 : 1;
+ cxld->config.interleave_granularity = 4096;
+ cxld->config.hpa_range = DEFINE_RANGE(base, base - 1);
cxld->commit = mock_decoder_commit;
cxld->reset = mock_decoder_reset;
}
@@ -1147,7 +1143,7 @@ static enum cxld_init_type get_decoder_init_type(struct cxl_decoder *cxld,
* See 'cxl list -BMPu -m cxl_mem.0,cxl_mem.4'
*/
if (!is_endpoint_decoder(&cxld->dev) || !hb0 || pdev->id % 4 ||
- pdev->id > 4 || cxld->id > (mock_zero_size_decoders ? 2 : 0))
+ pdev->id > 4 || cxld->config.id > (mock_zero_size_decoders ? 2 : 0))
return MOCK_DECODER_INIT_DEFAULT;
return type2_test ? MOCK_DECODER_INIT_TYPE2_AUTO :
@@ -1159,9 +1155,9 @@ static bool mock_decoder_handle_saved(struct cxl_decoder *cxld, struct cxl_test_
bool enabled;
if (is_switch_decoder(&cxld->dev))
- enabled = td->cxlsd.cxld.flags & CXL_DECODER_F_ENABLE;
+ enabled = td->cxlsd.cxld.config.flags & CXL_DECODER_F_ENABLE;
else
- enabled = td->cxled.cxld.flags & CXL_DECODER_F_ENABLE;
+ enabled = td->cxled.cxld.config.flags & CXL_DECODER_F_ENABLE;
if (enabled)
return !cxld_registry_restore(cxld, td);
@@ -1183,19 +1179,19 @@ static void mock_init_hdm_type2_cxled(struct cxl_endpoint_decoder *cxled,
int id = 0;
base = window->base_hpa;
- cxld->hpa_range = (struct range) {
+ cxld->config.hpa_range = (struct range) {
.start = base,
.end = base + mock_auto_region_size - 1,
};
- cxld->interleave_ways = 1;
- eig_to_granularity(window->granularity, &cxld->interleave_granularity);
- cxld->target_type = CXL_DECODER_DEVMEM;
- cxld->flags = CXL_DECODER_F_ENABLE;
+ cxld->config.interleave_ways = 1;
+ eig_to_granularity(window->granularity, &cxld->config.interleave_granularity);
+ cxld->config.target_type = CXL_DECODER_DEVMEM;
+ cxld->config.flags = CXL_DECODER_F_ENABLE;
cxled->state = CXL_DECODER_STATE_AUTO;
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
devm_cxl_dpa_reserve(cxled, 0,
- mock_auto_region_size / cxld->interleave_ways, 0);
+ mock_auto_region_size / cxld->config.interleave_ways, 0);
cxld->commit = mock_decoder_commit;
cxld->reset = mock_decoder_reset;
@@ -1216,13 +1212,13 @@ static void mock_init_hdm_type2_cxled(struct cxl_endpoint_decoder *cxled,
cxlsd = to_cxl_switch_decoder(dev);
cxld = &cxlsd->cxld;
- cxld->target_type = CXL_DECODER_DEVMEM;
- cxld->flags = CXL_DECODER_F_ENABLE;
+ cxld->config.target_type = CXL_DECODER_DEVMEM;
+ cxld->config.flags = CXL_DECODER_F_ENABLE;
root_port->commit_end = 0;
- cxld->interleave_ways = 1;
- cxld->interleave_granularity = 4096;
+ cxld->config.interleave_ways = 1;
+ cxld->config.interleave_granularity = 4096;
cxld->target_map[0] = dport->port_id;
- cxld->hpa_range = (struct range) {
+ cxld->config.hpa_range = (struct range) {
.start = base,
.end = base + mock_auto_region_size - 1,
};
@@ -1258,7 +1254,7 @@ static void mock_init_hdm_type3_cxled(struct cxl_endpoint_decoder *cxled,
int i;
/* Simulate missing cxl_mem.4 configuration */
- if (hb0 && pdev->id == 4 && cxld->id == 0 && fail_autoassemble) {
+ if (hb0 && pdev->id == 4 && cxld->config.id == 0 && fail_autoassemble) {
default_mock_decoder(cxld);
return;
}
@@ -1274,25 +1270,25 @@ static void mock_init_hdm_type3_cxled(struct cxl_endpoint_decoder *cxled,
* points at a zero-size decoder, exercising the zero-size
* reservation and poison-by-endpoint code paths.
*/
- if (cxld->id == 1 || cxld->id == 2) {
+ if (cxld->config.id == 1 || cxld->config.id == 2) {
size_zero_mock_decoder_ep(cxld, base);
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
WARN_ON_ONCE(!cxld_registry_new(cxld));
return;
}
- cxld->hpa_range = (struct range) {
+ cxld->config.hpa_range = (struct range) {
.start = base,
.end = base + mock_auto_region_size - 1,
};
- cxld->interleave_ways = 2;
- eig_to_granularity(window->granularity, &cxld->interleave_granularity);
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->flags = CXL_DECODER_F_ENABLE;
+ cxld->config.interleave_ways = 2;
+ eig_to_granularity(window->granularity, &cxld->config.interleave_granularity);
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.flags = CXL_DECODER_F_ENABLE;
cxled->state = CXL_DECODER_STATE_AUTO;
- port->commit_end = cxld->id;
+ port->commit_end = cxld->config.id;
devm_cxl_dpa_reserve(cxled, 0,
- mock_auto_region_size / cxld->interleave_ways, 0);
+ mock_auto_region_size / cxld->config.interleave_ways, 0);
cxld->commit = mock_decoder_commit;
cxld->reset = mock_decoder_reset;
@@ -1326,19 +1322,19 @@ static void mock_init_hdm_type3_cxled(struct cxl_endpoint_decoder *cxled,
cxlsd->cxld.target_map[0] = dport->port_id;
}
cxld = &cxlsd->cxld;
- cxld->target_type = CXL_DECODER_HOSTONLYMEM;
- cxld->flags = CXL_DECODER_F_ENABLE;
+ cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
+ cxld->config.flags = CXL_DECODER_F_ENABLE;
iter->commit_end = 0;
/*
* Switch targets 2 endpoints, while host bridge targets
* one root port
*/
if (i == 0)
- cxld->interleave_ways = 2;
+ cxld->config.interleave_ways = 2;
else
- cxld->interleave_ways = 1;
- cxld->interleave_granularity = 4096;
- cxld->hpa_range = (struct range) {
+ cxld->config.interleave_ways = 1;
+ cxld->config.interleave_granularity = 4096;
+ cxld->config.hpa_range = (struct range) {
.start = base,
.end = base + mock_auto_region_size - 1,
};
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (3 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
` (13 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Keep common decoder configuration in cxl_decoder_config and represent
endpoint DPA skip in cxl_endpoint_decoder_settings. Share range
programming, but keep endpoint skip and switch target-list writes in
their respective commit paths.
Separate commit initiation from completion waiting so reset restoration
can reuse register programming without changing normal DPA-lock policy.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
---
drivers/cxl/core/core.h | 8 ++++
drivers/cxl/core/hdm.c | 95 ++++++++++++++++++++++++++++-------------
include/cxl/hdm.h | 19 +++++++++
3 files changed, 92 insertions(+), 30 deletions(-)
create mode 100644 include/cxl/hdm.h
diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index ee639ac2c670..da40521171ca 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -7,6 +7,8 @@
#include <cxl/mailbox.h>
#include <linux/rwsem.h>
+struct cxl_endpoint_decoder_settings;
+
extern const struct device_type cxl_nvdimm_bridge_type;
extern const struct device_type cxl_nvdimm_type;
extern const struct device_type cxl_pmu_type;
@@ -139,6 +141,12 @@ struct cxl_rwsem {
extern struct cxl_rwsem cxl_rwsem;
+void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm);
+void cxl_commit_start_switch(const struct cxl_decoder_config *config,
+ u64 targets, void __iomem *hdm);
+int cxld_await_commit(void __iomem *hdm, int id);
+
int cxl_memdev_init(void);
void cxl_memdev_exit(void);
void cxl_mbox_init(void);
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 01c6455e1581..89261ec3595f 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -4,6 +4,7 @@
#include <linux/unaligned.h>
#include <linux/device.h>
#include <linux/delay.h>
+#include <cxl/hdm.h>
#include "cxlmem.h"
#include "core.h"
@@ -732,7 +733,7 @@ int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, u64 size)
return devm_add_action_or_reset(&port->dev, cxl_dpa_release, cxled);
}
-static void cxld_set_interleave(struct cxl_decoder *cxld, u32 *ctrl)
+static void cxld_set_interleave(const struct cxl_decoder_config *config, u32 *ctrl)
{
u16 eig;
u8 eiw;
@@ -741,12 +742,12 @@ static void cxld_set_interleave(struct cxl_decoder *cxld, u32 *ctrl)
* Input validation ensures these warns never fire, but otherwise
* suppress unititalized variable usage warnings.
*/
- if (WARN_ONCE(ways_to_eiw(cxld->config.interleave_ways, &eiw),
- "invalid interleave_ways: %d\n", cxld->config.interleave_ways))
+ if (WARN_ONCE(ways_to_eiw(config->interleave_ways, &eiw),
+ "invalid interleave_ways: %d\n", config->interleave_ways))
return;
- if (WARN_ONCE(granularity_to_eig(cxld->config.interleave_granularity, &eig),
+ if (WARN_ONCE(granularity_to_eig(config->interleave_granularity, &eig),
"invalid interleave_granularity: %d\n",
- cxld->config.interleave_granularity))
+ config->interleave_granularity))
return;
u32p_replace_bits(ctrl, eig, CXL_HDM_DECODER0_CTRL_IG_MASK);
@@ -754,10 +755,10 @@ static void cxld_set_interleave(struct cxl_decoder *cxld, u32 *ctrl)
*ctrl |= CXL_HDM_DECODER0_CTRL_COMMIT;
}
-static void cxld_set_type(struct cxl_decoder *cxld, u32 *ctrl)
+static void cxld_set_type(const struct cxl_decoder_config *config, u32 *ctrl)
{
u32p_replace_bits(ctrl,
- !!(cxld->config.target_type == CXL_DECODER_HOSTONLYMEM),
+ !!(config->target_type == CXL_DECODER_HOSTONLYMEM),
CXL_HDM_DECODER0_CTRL_HOSTONLY);
}
@@ -789,7 +790,7 @@ static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt)
* clock skew and other marginal behavior
*/
#define COMMIT_TIMEOUT_MS 20
-static int cxld_await_commit(void __iomem *hdm, int id)
+int cxld_await_commit(void __iomem *hdm, int id)
{
u32 ctrl;
int i;
@@ -809,45 +810,79 @@ static int cxld_await_commit(void __iomem *hdm, int id)
return -ETIMEDOUT;
}
-static void setup_hw_decoder(struct cxl_decoder *cxld, void __iomem *hdm)
+static u32 cxl_hdm_write_range(void __iomem *hdm,
+ const struct cxl_decoder_config *config)
{
- int id = cxld->config.id;
- u64 base, size;
+ u64 size = range_len(&config->hpa_range);
+ u64 base = config->hpa_range.start;
+ int id = config->id;
u32 ctrl;
- /* common decoder settings */
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->config.id));
- cxld_set_interleave(cxld, &ctrl);
- cxld_set_type(cxld, &ctrl);
- base = cxld->config.hpa_range.start;
- size = range_len(&cxld->config.hpa_range);
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ cxld_set_interleave(config, &ctrl);
+ cxld_set_type(config, &ctrl);
writel(upper_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
writel(lower_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
writel(upper_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
writel(lower_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
+ return ctrl;
+}
+
+void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm)
+{
+ int id = settings->config.id;
+ u32 ctrl;
+
+ lockdep_assert_held(&cxl_rwsem.dpa);
+ ctrl = cxl_hdm_write_range(hdm, &settings->config);
+ writel(upper_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_HIGH(id));
+ writel(lower_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_LOW(id));
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+}
+
+void cxl_commit_start_switch(const struct cxl_decoder_config *config,
+ u64 targets, void __iomem *hdm)
+{
+ int id = config->id;
+ u32 ctrl;
+
+ lockdep_assert_held(&cxl_rwsem.dpa);
+ ctrl = cxl_hdm_write_range(hdm, config);
+ writel(upper_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_HIGH(id));
+ writel(lower_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_LOW(id));
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+}
+
+static void
+cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled,
+ struct cxl_endpoint_decoder_settings *settings)
+{
+ lockdep_assert_held(&cxl_rwsem.dpa);
+
+ *settings = (struct cxl_endpoint_decoder_settings) {
+ .config = cxled->cxld.config,
+ .skip = cxled->skip,
+ };
+}
+
+static void setup_hw_decoder(struct cxl_decoder *cxld, void __iomem *hdm)
+{
+ struct cxl_endpoint_decoder_settings settings;
if (is_switch_decoder(&cxld->dev)) {
struct cxl_switch_decoder *cxlsd =
to_cxl_switch_decoder(&cxld->dev);
- void __iomem *tl_hi = hdm + CXL_HDM_DECODER0_TL_HIGH(id);
- void __iomem *tl_lo = hdm + CXL_HDM_DECODER0_TL_LOW(id);
u64 targets;
cxlsd_set_targets(cxlsd, &targets);
- writel(upper_32_bits(targets), tl_hi);
- writel(lower_32_bits(targets), tl_lo);
+ cxl_commit_start_switch(&cxld->config, targets, hdm);
} else {
- struct cxl_endpoint_decoder *cxled =
- to_cxl_endpoint_decoder(&cxld->dev);
- void __iomem *sk_hi = hdm + CXL_HDM_DECODER0_SKIP_HIGH(id);
- void __iomem *sk_lo = hdm + CXL_HDM_DECODER0_SKIP_LOW(id);
-
- writel(upper_32_bits(cxled->skip), sk_hi);
- writel(lower_32_bits(cxled->skip), sk_lo);
+ cxl_endpoint_decoder_snapshot(to_cxl_endpoint_decoder(&cxld->dev),
+ &settings);
+ cxl_commit_start_endpoint(&settings, hdm);
}
-
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
}
static int cxl_decoder_commit(struct cxl_decoder *cxld)
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
new file mode 100644
index 000000000000..219b9bb67440
--- /dev/null
+++ b/include/cxl/hdm.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+#ifndef __CXL_HDM_H__
+#define __CXL_HDM_H__
+
+#include <linux/types.h>
+#include <cxl/cxl.h>
+
+/**
+ * struct cxl_endpoint_decoder_settings - endpoint HDM decoder programming
+ * @config: common decoder configuration
+ * @skip: length of DPA space skipped before this decoder's DPA mapping
+ */
+struct cxl_endpoint_decoder_settings {
+ struct cxl_decoder_config config;
+ u64 skip;
+};
+
+#endif /* __CXL_HDM_H__ */
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (4 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
` (12 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
The PCI core will restore HDM decoders after a reset, possibly while no
CXL driver is loaded, so it cannot call into cxl_core.
Move the HDM register programming helpers from hdm.c to hdm_regs.c and
build that file into the kernel whenever CXL_BUS is enabled, including
when CXL_BUS is modular. Export only the symbols that cxl_core uses.
Keep the DPA lock assertion in the normal decoder programming caller so
the register helpers do not depend on cxl_rwsem. Leave cxl_rwsem in
cxl_core until built-in cache management needs it.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Li Ming <ming.li@zohomail.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
---
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=e0b476ea99567fb5aa416eac0cf61bca20724880
---
drivers/cxl/core/Makefile | 1 +
drivers/cxl/core/hdm.c | 102 +------------------------------
drivers/cxl/core/hdm_regs.c | 118 ++++++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+), 100 deletions(-)
create mode 100644 drivers/cxl/core/hdm_regs.c
diff --git a/drivers/cxl/core/Makefile b/drivers/cxl/core/Makefile
index b2627933b7fb..4236908a1095 100644
--- a/drivers/cxl/core/Makefile
+++ b/drivers/cxl/core/Makefile
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_CXL_BUS) += cxl_core.o
+obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o
obj-$(CONFIG_CXL_SUSPEND) += suspend.o
obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 89261ec3595f..c243a0f5fa08 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -733,35 +733,6 @@ int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, u64 size)
return devm_add_action_or_reset(&port->dev, cxl_dpa_release, cxled);
}
-static void cxld_set_interleave(const struct cxl_decoder_config *config, u32 *ctrl)
-{
- u16 eig;
- u8 eiw;
-
- /*
- * Input validation ensures these warns never fire, but otherwise
- * suppress unititalized variable usage warnings.
- */
- if (WARN_ONCE(ways_to_eiw(config->interleave_ways, &eiw),
- "invalid interleave_ways: %d\n", config->interleave_ways))
- return;
- if (WARN_ONCE(granularity_to_eig(config->interleave_granularity, &eig),
- "invalid interleave_granularity: %d\n",
- config->interleave_granularity))
- return;
-
- u32p_replace_bits(ctrl, eig, CXL_HDM_DECODER0_CTRL_IG_MASK);
- u32p_replace_bits(ctrl, eiw, CXL_HDM_DECODER0_CTRL_IW_MASK);
- *ctrl |= CXL_HDM_DECODER0_CTRL_COMMIT;
-}
-
-static void cxld_set_type(const struct cxl_decoder_config *config, u32 *ctrl)
-{
- u32p_replace_bits(ctrl,
- !!(config->target_type == CXL_DECODER_HOSTONLYMEM),
- CXL_HDM_DECODER0_CTRL_HOSTONLY);
-}
-
static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt)
{
struct cxl_dport **t = &cxlsd->target[0];
@@ -784,77 +755,6 @@ static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt)
*tgt |= FIELD_PREP(GENMASK_ULL(63, 56), t[7]->port_id);
}
-/*
- * Per CXL 2.0 8.2.5.12.20 Committing Decoder Programming, hardware must set
- * committed or error within 10ms, but just be generous with 20ms to account for
- * clock skew and other marginal behavior
- */
-#define COMMIT_TIMEOUT_MS 20
-int cxld_await_commit(void __iomem *hdm, int id)
-{
- u32 ctrl;
- int i;
-
- for (i = 0; i < COMMIT_TIMEOUT_MS; i++) {
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
- if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMIT_ERROR, ctrl)) {
- ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
- return -EIO;
- }
- if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
- return 0;
- fsleep(1000);
- }
-
- return -ETIMEDOUT;
-}
-
-static u32 cxl_hdm_write_range(void __iomem *hdm,
- const struct cxl_decoder_config *config)
-{
- u64 size = range_len(&config->hpa_range);
- u64 base = config->hpa_range.start;
- int id = config->id;
- u32 ctrl;
-
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
- cxld_set_interleave(config, &ctrl);
- cxld_set_type(config, &ctrl);
-
- writel(upper_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
- writel(lower_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
- writel(upper_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
- writel(lower_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
- return ctrl;
-}
-
-void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
- void __iomem *hdm)
-{
- int id = settings->config.id;
- u32 ctrl;
-
- lockdep_assert_held(&cxl_rwsem.dpa);
- ctrl = cxl_hdm_write_range(hdm, &settings->config);
- writel(upper_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_HIGH(id));
- writel(lower_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_LOW(id));
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
-}
-
-void cxl_commit_start_switch(const struct cxl_decoder_config *config,
- u64 targets, void __iomem *hdm)
-{
- int id = config->id;
- u32 ctrl;
-
- lockdep_assert_held(&cxl_rwsem.dpa);
- ctrl = cxl_hdm_write_range(hdm, config);
- writel(upper_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_HIGH(id));
- writel(lower_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_LOW(id));
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
-}
-
static void
cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled,
struct cxl_endpoint_decoder_settings *settings)
@@ -871,6 +771,8 @@ static void setup_hw_decoder(struct cxl_decoder *cxld, void __iomem *hdm)
{
struct cxl_endpoint_decoder_settings settings;
+ lockdep_assert_held(&cxl_rwsem.dpa);
+
if (is_switch_decoder(&cxld->dev)) {
struct cxl_switch_decoder *cxlsd =
to_cxl_switch_decoder(&cxld->dev);
diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
new file mode 100644
index 000000000000..53d2f236e49c
--- /dev/null
+++ b/drivers/cxl/core/hdm_regs.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+#include <linux/delay.h>
+#include <linux/bug.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <linux/kernel.h>
+
+#include <cxl/hdm.h>
+
+#include "cxl.h"
+#include "core.h"
+
+/*
+ * HDM decoder register helpers are built in whenever CXL_BUS is enabled,
+ * including when cxl_core is modular. Callers provide any required locking.
+ */
+
+static void cxld_set_interleave(const struct cxl_decoder_config *config, u32 *ctrl)
+{
+ u16 eig;
+ u8 eiw;
+
+ /*
+ * Input validation ensures these warns never fire, but otherwise
+ * suppress unititalized variable usage warnings.
+ */
+ if (WARN_ONCE(ways_to_eiw(config->interleave_ways, &eiw),
+ "invalid interleave_ways: %d\n", config->interleave_ways))
+ return;
+ if (WARN_ONCE(granularity_to_eig(config->interleave_granularity, &eig),
+ "invalid interleave_granularity: %d\n",
+ config->interleave_granularity))
+ return;
+
+ u32p_replace_bits(ctrl, eig, CXL_HDM_DECODER0_CTRL_IG_MASK);
+ u32p_replace_bits(ctrl, eiw, CXL_HDM_DECODER0_CTRL_IW_MASK);
+ *ctrl |= CXL_HDM_DECODER0_CTRL_COMMIT;
+}
+
+static void cxld_set_type(const struct cxl_decoder_config *config, u32 *ctrl)
+{
+ u32p_replace_bits(ctrl,
+ !!(config->target_type == CXL_DECODER_HOSTONLYMEM),
+ CXL_HDM_DECODER0_CTRL_HOSTONLY);
+}
+
+/*
+ * Per CXL 2.0 8.2.5.12.20 Committing Decoder Programming, hardware must set
+ * committed or error within 10ms, but just be generous with 20ms to account for
+ * clock skew and other marginal behavior
+ */
+#define COMMIT_TIMEOUT_MS 20
+int cxld_await_commit(void __iomem *hdm, int id)
+{
+ u32 ctrl;
+ int i;
+
+ for (i = 0; i < COMMIT_TIMEOUT_MS; i++) {
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMIT_ERROR, ctrl)) {
+ ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ return -EIO;
+ }
+ if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+ return 0;
+ fsleep(1000);
+ }
+
+ return -ETIMEDOUT;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxld_await_commit, "cxl_core");
+
+static u32 cxl_hdm_write_range(void __iomem *hdm,
+ const struct cxl_decoder_config *config)
+{
+ u64 size = range_len(&config->hpa_range);
+ u64 base = config->hpa_range.start;
+ int id = config->id;
+ u32 ctrl;
+
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ cxld_set_interleave(config, &ctrl);
+ cxld_set_type(config, &ctrl);
+
+ writel(upper_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
+ writel(lower_32_bits(base), hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
+ writel(upper_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
+ writel(lower_32_bits(size), hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
+ return ctrl;
+}
+
+void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm)
+{
+ int id = settings->config.id;
+ u32 ctrl;
+
+ ctrl = cxl_hdm_write_range(hdm, &settings->config);
+ writel(upper_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_HIGH(id));
+ writel(lower_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_LOW(id));
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_commit_start_endpoint, "cxl_core");
+
+void cxl_commit_start_switch(const struct cxl_decoder_config *config,
+ u64 targets, void __iomem *hdm)
+{
+ int id = config->id;
+ u32 ctrl;
+
+ ctrl = cxl_hdm_write_range(hdm, config);
+ writel(upper_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_HIGH(id));
+ writel(lower_32_bits(targets), hdm + CXL_HDM_DECODER0_TL_LOW(id));
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_commit_start_switch, "cxl_core");
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 07/19] cxl: Share HDM decoder register unpacking
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (5 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
` (11 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Factor common HDM register parsing into cxl_hdm_unpack_decoder().
Validate range and interleave parameters in locals before publishing the
decoder configuration, including enable and lock flags. Reject nonzero
ranges whose end overflows the address space while preserving the
zero-size range representation.
Read decoder base and size registers low then high through a shared
helper used by enumeration and the reset snapshot.
Read switch target lists and endpoint DPA skip in their respective
branches of init_hdm_decoder().
Preserve the decoder ID allocated by cxl_decoder_init() when publishing
the parsed configuration.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
---
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=7d6ed7faf7d8e14abce8ae23902d5323738868d2
---
drivers/cxl/core/core.h | 5 +++
drivers/cxl/core/hdm.c | 63 ++++++++-----------------------
drivers/cxl/core/hdm_regs.c | 74 +++++++++++++++++++++++++++++++++++++
3 files changed, 94 insertions(+), 48 deletions(-)
diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index da40521171ca..7af8d380c8ac 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -147,6 +147,11 @@ void cxl_commit_start_switch(const struct cxl_decoder_config *config,
u64 targets, void __iomem *hdm);
int cxld_await_commit(void __iomem *hdm, int id);
+int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,
+ u32 ctrl, u64 base, u64 size);
+int cxl_hdm_read_decoder(struct cxl_decoder_config *config, void __iomem *hdm,
+ int id, u32 ctrl);
+
int cxl_memdev_init(void);
void cxl_memdev_exit(void);
void cxl_mbox_init(void);
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index c243a0f5fa08..00ea70f1c88e 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -965,8 +965,8 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
{
struct cxl_endpoint_decoder *cxled = NULL;
u8 target_id[CXL_HDM_DECODER0_TL_TARGETS];
- u64 size, base, skip, dpa_size, lo, hi;
- bool committed;
+ u64 size, skip, dpa_size, lo, hi;
+ struct cxl_decoder_config config;
u32 remainder;
int i, rc;
u32 ctrl;
@@ -976,41 +976,25 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
which, info);
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(which));
- lo = readl(hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(which));
- hi = readl(hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(which));
- base = (hi << 32) + lo;
- lo = readl(hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(which));
- hi = readl(hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(which));
- size = (hi << 32) + lo;
- committed = !!(ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED);
+ rc = cxl_hdm_read_decoder(&config, hdm, which, ctrl);
+ if (rc) {
+ dev_warn(&port->dev,
+ "decoder%d.%d: Invalid decoder configuration (ctrl: %#x): %d\n",
+ port->id, cxld->config.id, ctrl, rc);
+ return rc;
+ }
+
+ config.id = cxld->config.id;
cxld->commit = cxl_decoder_commit;
cxld->reset = cxl_decoder_reset;
-
- if (!committed)
- size = 0;
- if (base == U64_MAX || size == U64_MAX) {
- dev_warn(&port->dev, "decoder%d.%d: Invalid resource range\n",
- port->id, cxld->config.id);
- return -ENXIO;
- }
+ cxld->config = config;
+ size = range_len(&cxld->config.hpa_range);
if (info)
cxled = to_cxl_endpoint_decoder(&cxld->dev);
- cxld->config.hpa_range = (struct range) {
- .start = base,
- .end = base + size - 1,
- };
/* decoders are enabled if committed */
- if (committed) {
- cxld->config.flags |= CXL_DECODER_F_ENABLE;
- if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
- cxld->config.flags |= CXL_DECODER_F_LOCK;
- if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
- cxld->config.target_type = CXL_DECODER_HOSTONLYMEM;
- else
- cxld->config.target_type = CXL_DECODER_DEVMEM;
-
+ if (cxld->config.flags & CXL_DECODER_F_ENABLE) {
guard(rwsem_write)(&cxl_rwsem.region);
if (cxld->config.id != cxl_num_decoders_committed(port)) {
dev_warn(&port->dev,
@@ -1044,23 +1028,6 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(which));
}
}
- rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl),
- &cxld->config.interleave_ways);
- if (rc) {
- dev_warn(&port->dev,
- "decoder%d.%d: Invalid interleave ways (ctrl: %#x)\n",
- port->id, cxld->config.id, ctrl);
- return rc;
- }
- rc = eig_to_granularity(FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl),
- &cxld->config.interleave_granularity);
- if (rc) {
- dev_warn(&port->dev,
- "decoder%d.%d: Invalid interleave granularity (ctrl: %#x)\n",
- port->id, cxld->config.id, ctrl);
- return rc;
- }
-
dev_dbg(&port->dev, "decoder%d.%d: range: %#llx-%#llx iw: %d ig: %d\n",
port->id, cxld->config.id, cxld->config.hpa_range.start, cxld->config.hpa_range.end,
cxld->config.interleave_ways, cxld->config.interleave_granularity);
@@ -1086,7 +1053,7 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
return 0;
}
- if (!committed)
+ if (!(cxld->config.flags & CXL_DECODER_F_ENABLE))
return 0;
dpa_size = div_u64_rem(size, cxld->config.interleave_ways, &remainder);
diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
index 53d2f236e49c..cc5d77c4abf5 100644
--- a/drivers/cxl/core/hdm_regs.c
+++ b/drivers/cxl/core/hdm_regs.c
@@ -116,3 +116,77 @@ void cxl_commit_start_switch(const struct cxl_decoder_config *config,
writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
}
EXPORT_SYMBOL_FOR_MODULES(cxl_commit_start_switch, "cxl_core");
+
+int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,
+ u32 ctrl, u64 base, u64 size)
+{
+ bool committed = FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl);
+ enum cxl_decoder_type target_type = 0;
+ int interleave_granularity;
+ unsigned long flags = 0;
+ struct range hpa_range;
+ int interleave_ways;
+ u64 end;
+ int rc;
+
+ if (!committed)
+ size = 0;
+ if (base == U64_MAX || size == U64_MAX)
+ return -ENXIO;
+ /* A zero-size decoder maps no HPA; its range is empty at @base. */
+ end = base - 1;
+ if (size && check_add_overflow(base, size - 1, &end))
+ return -ENXIO;
+
+ hpa_range = (struct range) {
+ .start = base,
+ .end = end,
+ };
+
+ if (committed) {
+ flags |= CXL_DECODER_F_ENABLE;
+ if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK)
+ flags |= CXL_DECODER_F_LOCK;
+ if (FIELD_GET(CXL_HDM_DECODER0_CTRL_HOSTONLY, ctrl))
+ target_type = CXL_DECODER_HOSTONLYMEM;
+ else
+ target_type = CXL_DECODER_DEVMEM;
+ }
+
+ rc = eiw_to_ways(FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl),
+ &interleave_ways);
+ if (rc)
+ return rc;
+ rc = eig_to_granularity(FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl),
+ &interleave_granularity);
+ if (rc)
+ return rc;
+
+ *config = (struct cxl_decoder_config) {
+ .id = id,
+ .hpa_range = hpa_range,
+ .interleave_ways = interleave_ways,
+ .interleave_granularity = interleave_granularity,
+ .target_type = target_type,
+ .flags = flags,
+ };
+
+ return 0;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_unpack_decoder, "cxl_core");
+
+int cxl_hdm_read_decoder(struct cxl_decoder_config *config, void __iomem *hdm,
+ int id, u32 ctrl)
+{
+ u64 lo, hi, base, size;
+
+ lo = readl(hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
+ hi = readl(hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
+ base = (hi << 32) | lo;
+ lo = readl(hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
+ hi = readl(hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
+ size = (hi << 32) | lo;
+
+ return cxl_hdm_unpack_decoder(config, id, ctrl, base, size);
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_read_decoder, "cxl_core");
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (6 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
` (10 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Keep the region and DPA locks available to built-in CXL state management
when cxl_core is modular.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
---
drivers/cxl/core/Makefile | 2 +-
drivers/cxl/core/hdm.c | 5 -----
drivers/cxl/core/hdm_state.c | 15 +++++++++++++++
3 files changed, 16 insertions(+), 6 deletions(-)
create mode 100644 drivers/cxl/core/hdm_state.c
diff --git a/drivers/cxl/core/Makefile b/drivers/cxl/core/Makefile
index 4236908a1095..d2814c65f431 100644
--- a/drivers/cxl/core/Makefile
+++ b/drivers/cxl/core/Makefile
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_CXL_BUS) += cxl_core.o
-obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o
+obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o
obj-$(CONFIG_CXL_SUSPEND) += suspend.o
obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 00ea70f1c88e..1fabbd4a63a3 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -18,11 +18,6 @@
* for enumerating these registers and capabilities.
*/
-struct cxl_rwsem cxl_rwsem = {
- .region = __RWSEM_INITIALIZER(cxl_rwsem.region),
- .dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa),
-};
-
static int add_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld)
{
int rc;
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
new file mode 100644
index 000000000000..c9cc019902ef
--- /dev/null
+++ b/drivers/cxl/core/hdm_state.c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+#include <linux/export.h>
+#include <linux/rwsem.h>
+
+#include <cxl/hdm.h>
+
+#include "cxl.h"
+#include "core.h"
+
+struct cxl_rwsem cxl_rwsem = {
+ .region = __RWSEM_INITIALIZER(cxl_rwsem.region),
+ .dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa),
+};
+EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core");
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (7 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
` (9 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Early PCI discovery creates the HDM cache, while later CXL enumeration and
decoder operations provide updated programming state.
Refresh the PCI snapshot when decoders are enumerated, committed, or reset
so reset recovery need not walk the CXL topology. Keep cache access and
locking in the built-in HDM state helpers. Ignore updates when no cache
exists and reject decoder-count mismatches.
Use CXL_RESET for the PCI HDM cache. Shared HDM register helpers do not
require it.
Skip cache checks and updates for DVSEC-emulated decoders, which do not
use HDM registers. Keep cached global control when MMIO reads return all
ones.
Non-PCI mock ports have no upstream PCI device, so the cache helpers
ignore them. This avoids test wrappers and a circular dependency between
cxl_core and cxl_mock.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
---
Changes in v18:
- Declare the pci_dev reference at its initializer, add kernel-doc for the
exported cache helpers, make the decoder-count check return bool as
cxl_hdm_cache_decoder_count_match(), and drop an unused forward
declaration (Dave Jiang)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=50a88f5194e5df38590d413f925d8d5afa8a8488
---
drivers/cxl/Kconfig | 5 +++
drivers/cxl/core/core.h | 1 +
drivers/cxl/core/hdm.c | 30 ++++++++++++++
drivers/cxl/core/hdm_state.c | 78 ++++++++++++++++++++++++++++++++++++
drivers/cxl/core/pci.c | 27 +++++++++++++
include/cxl/hdm.h | 32 +++++++++++++++
include/linux/pci.h | 6 +++
7 files changed, 179 insertions(+)
diff --git a/drivers/cxl/Kconfig b/drivers/cxl/Kconfig
index fdd57a4118db..37bfa6689b04 100644
--- a/drivers/cxl/Kconfig
+++ b/drivers/cxl/Kconfig
@@ -6,6 +6,7 @@ menuconfig CXL_BUS
select FW_UPLOAD
select PCI_DOE
select FIRMWARE_TABLE
+ select CXL_RESET
select NUMA_KEEP_MEMINFO if NUMA_MEMBLKS
select FWCTL if CXL_FEATURES
help
@@ -260,3 +261,7 @@ config CXL_KUNIT_TEST
If unsure, say N.
endif
+
+# CXL_BUS requires the PCI HDM cache and built-in state helpers, even when modular.
+config CXL_RESET
+ bool
diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index 7af8d380c8ac..715097894144 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -220,6 +220,7 @@ static inline void devm_cxl_dport_ras_setup(struct cxl_dport *dport) { }
int cxl_gpf_port_setup(struct cxl_dport *dport);
struct cxl_hdm;
+int cxl_hdm_check_decoder_count(struct cxl_hdm *cxlhdm);
int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
struct cxl_endpoint_dvsec_info *info);
int cxl_port_get_possible_dports(struct cxl_port *port);
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 1fabbd4a63a3..f88418177582 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -750,6 +750,24 @@ static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt)
*tgt |= FIELD_PREP(GENMASK_ULL(63, 56), t[7]->port_id);
}
+static void cxl_hdm_refresh_decoder(struct cxl_hdm *cxlhdm,
+ struct cxl_decoder *cxld)
+{
+ void __iomem *hdm = cxlhdm->regs.hdm_decoder;
+
+ if (!hdm)
+ return;
+ if (!is_endpoint_decoder(&cxld->dev))
+ return;
+
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(cxlhdm->port);
+ if (!pdev)
+ return;
+ cxl_hdm_cache_refresh_decoder(pdev, hdm,
+ to_cxl_endpoint_decoder(&cxld->dev));
+}
+
static void
cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled,
struct cxl_endpoint_decoder_settings *settings)
@@ -830,6 +848,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
}
port->commit_end++;
cxld->config.flags |= CXL_DECODER_F_ENABLE;
+ cxl_hdm_refresh_decoder(cxlhdm, cxld);
return 0;
}
@@ -902,6 +921,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)
writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
cxld->config.flags &= ~CXL_DECODER_F_ENABLE;
+ cxl_hdm_refresh_decoder(cxlhdm, cxld);
/* Userspace is now responsible for reconfiguring this decoder */
if (is_endpoint_decoder(&cxld->dev)) {
@@ -1121,6 +1141,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
{
void __iomem *hdm = cxlhdm->regs.hdm_decoder;
struct cxl_port *port = cxlhdm->port;
+ /* DVSEC-emulated decoders do not describe the cached HDM registers. */
+ bool emulated = should_emulate_decoders(info);
u64 dpa_base = 0;
int i, rc;
@@ -1136,6 +1158,12 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
return -ENXIO;
}
+ if (is_cxl_endpoint(port) && hdm && !emulated) {
+ rc = cxl_hdm_check_decoder_count(cxlhdm);
+ if (rc)
+ return rc;
+ }
+
cxl_settle_decoders(cxlhdm);
/*
@@ -1182,6 +1210,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
put_device(&cxld->dev);
return rc;
}
+ if (!emulated)
+ cxl_hdm_refresh_decoder(cxlhdm, cxld);
rc = add_hdm_decoder(port, cxld);
if (rc) {
dev_warn(&port->dev,
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
index c9cc019902ef..ec335ac21ad4 100644
--- a/drivers/cxl/core/hdm_state.c
+++ b/drivers/cxl/core/hdm_state.c
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
#include <linux/export.h>
+#include <linux/io.h>
+#include <linux/pci.h>
#include <linux/rwsem.h>
#include <cxl/hdm.h>
@@ -13,3 +15,79 @@ struct cxl_rwsem cxl_rwsem = {
.dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa),
};
EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core");
+
+/**
+ * cxl_hdm_cache_refresh_decoder() - update the PCI HDM cache from a decoder
+ * @pdev: PCI device that owns the HDM cache
+ * @hdm: mapped HDM decoder register block, for the global control register
+ * @cxled: endpoint decoder whose state is recorded
+ *
+ * Record the decoder's current configuration, or just its id when it is
+ * disabled, and refresh the cached global control register unless the read
+ * returns all ones. Does nothing if @pdev has no cache or the decoder id is
+ * outside the cached count. Takes cxl_rwsem.dpa for write.
+ */
+void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
+ struct cxl_endpoint_decoder *cxled)
+{
+ struct cxl_endpoint_decoder_settings *settings;
+ struct cxl_decoder *cxld = &cxled->cxld;
+ struct cxl_hdm_info *info;
+ u32 global_ctrl;
+ int id;
+
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ id = cxld->config.id;
+ info = pdev->hdm;
+ if (!info)
+ return;
+ if (id < 0 || id >= info->decoder_count) {
+ pci_warn(pdev, "CXL HDM decoder %d exceeds cached count %d\n",
+ id, info->decoder_count);
+ return;
+ }
+
+ /* Preserve the last good value when the device does not respond. */
+ global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+ if (global_ctrl != U32_MAX)
+ info->global_ctrl = global_ctrl;
+ settings = &info->settings[id];
+
+ /*
+ * A disabled decoder's software object may retain its old range and
+ * skip state. Leave only the decoder id in the cached settings so stale
+ * state is not restored as an enabled decode.
+ */
+ *settings = (struct cxl_endpoint_decoder_settings) {
+ .config.id = id,
+ };
+ if (cxld->config.flags & CXL_DECODER_F_ENABLE) {
+ settings->config = cxld->config;
+ settings->skip = cxled->skip;
+ }
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_refresh_decoder, "cxl_core");
+
+/**
+ * cxl_hdm_cache_decoder_count_match() - compare a decoder count to the cache
+ * @pdev: PCI device that owns the HDM cache
+ * @count: decoder count found during CXL enumeration
+ *
+ * Takes cxl_rwsem.dpa for read. Warns when the counts differ.
+ *
+ * Return: true if @pdev has no cache or the cached decoder count equals
+ * @count, false otherwise.
+ */
+bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count)
+{
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ if (pdev->hdm && pdev->hdm->decoder_count != count) {
+ pci_warn(pdev,
+ "CXL HDM cache decoder count mismatch: cached=%d hdm=%d\n",
+ pdev->hdm->decoder_count, count);
+ return false;
+ }
+
+ return true;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_decoder_count_match, "cxl_core");
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 63dc3f39b5d8..d0da38763b4b 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -6,6 +6,7 @@
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/pci-doe.h>
+#include <cxl/hdm.h>
#include <cxl/pci.h>
#include <linux/aer.h>
#include <cxlpci.h>
@@ -180,6 +181,32 @@ int cxl_await_media_ready(struct cxl_dev_state *cxlds)
}
EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, "CXL");
+/**
+ * cxl_hdm_check_decoder_count() - check enumeration against the PCI HDM cache
+ * @cxlhdm: HDM capability containing the decoder count to check
+ *
+ * Take cxl_rwsem.dpa for read while comparing the cached count. Call only
+ * for hardware HDM decoders, since DVSEC-emulated ranges do not describe
+ * the HDM registers represented by the cache.
+ *
+ * Return: 0 if the counts match or no upstream PCI device or cache exists,
+ * or -ENXIO if the cached and enumerated decoder counts differ.
+ */
+int cxl_hdm_check_decoder_count(struct cxl_hdm *cxlhdm)
+{
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(cxlhdm->port);
+
+ if (!pdev)
+ return 0;
+
+ if (!cxl_hdm_cache_decoder_count_match(pdev, cxlhdm->decoder_count))
+ return -ENXIO;
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_hdm_check_decoder_count, "CXL");
+
static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
{
struct pci_dev *pdev = to_pci_dev(cxlds->dev);
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index 219b9bb67440..6c5b369ce490 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -6,6 +6,9 @@
#include <linux/types.h>
#include <cxl/cxl.h>
+struct pci_dev;
+struct cxl_endpoint_decoder;
+
/**
* struct cxl_endpoint_decoder_settings - endpoint HDM decoder programming
* @config: common decoder configuration
@@ -16,4 +19,33 @@ struct cxl_endpoint_decoder_settings {
u64 skip;
};
+/**
+ * struct cxl_hdm_info - cached CXL HDM state for a PCI device
+ * @decoder_count: number of entries in @settings
+ * @hdm_bar: PCI BAR containing the HDM decoder capability
+ * @hdm_offset: offset of the HDM decoder capability in @hdm_bar
+ * @hdm_size: size of the HDM decoder register block
+ * @global_ctrl: HDM decoder global control register
+ * @dvsec_ctrl: CXL DVSEC control register
+ * @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state
+ * @settings: per-endpoint-decoder programming state
+ *
+ * Publication through pci_dev.hdm and all mutable fields are protected by
+ * cxl_rwsem.dpa.
+ */
+struct cxl_hdm_info {
+ int decoder_count;
+ int hdm_bar;
+ resource_size_t hdm_offset;
+ resource_size_t hdm_size;
+ u32 global_ctrl;
+ u16 dvsec_ctrl;
+ bool dvsec_ctrl_valid;
+ struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count);
+};
+
+void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
+ struct cxl_endpoint_decoder *cxled);
+bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count);
+
#endif /* __CXL_HDM_H__ */
diff --git a/include/linux/pci.h b/include/linux/pci.h
index aa3765a20a96..f62aab652770 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -339,6 +339,9 @@ struct pcie_link_state;
struct pci_sriov;
struct pci_p2pdma;
struct rcec_ea;
+#ifdef CONFIG_CXL_RESET
+struct cxl_hdm_info;
+#endif
/* struct pci_dev - describes a PCI device
*
@@ -567,6 +570,9 @@ struct pci_dev {
#ifdef CONFIG_PCI_DOE
struct xarray doe_mbs; /* Data Object Exchange mailboxes */
#endif
+#ifdef CONFIG_CXL_RESET
+ struct cxl_hdm_info *hdm; /* CXL HDM decoder state */
+#endif
#ifdef CONFIG_PCI_NPEM
struct npem *npem; /* Native PCIe Enclosure Management */
#endif
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 10/19] cxl: Build register block helpers into the kernel
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (8 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
` (8 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Build register discovery with the built-in HDM helpers so PCI enumeration
can use it even when cxl_core is modular. Export the interfaces used by
cxl_core.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
---
Changes in v18:
- Keep only the built-in register helper changes here; the BAR/offset
fields and cxl_dvsec_hdm_count_valid() move to the patch that uses them,
and the exports use EXPORT_SYMBOL_FOR_MODULES() (Dave Jiang)
---
drivers/cxl/core/Makefile | 3 +--
drivers/cxl/core/regs.c | 4 ++++
drivers/cxl/cxl.h | 3 ---
include/cxl/pci.h | 3 +++
tools/testing/cxl/Kbuild | 1 -
5 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/drivers/cxl/core/Makefile b/drivers/cxl/core/Makefile
index d2814c65f431..c48a1d67b8bf 100644
--- a/drivers/cxl/core/Makefile
+++ b/drivers/cxl/core/Makefile
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_CXL_BUS) += cxl_core.o
-obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o
+obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o regs.o
obj-$(CONFIG_CXL_SUSPEND) += suspend.o
obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
@@ -9,7 +9,6 @@ CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src)
cxl_core-y := port.o
cxl_core-y += pmem.o
-cxl_core-y += regs.o
cxl_core-y += memdev.o
cxl_core-y += mbox.o
cxl_core-y += pci.o
diff --git a/drivers/cxl/core/regs.c b/drivers/cxl/core/regs.c
index 41416fa2ce4a..ef7c236b846c 100644
--- a/drivers/cxl/core/regs.c
+++ b/drivers/cxl/core/regs.c
@@ -200,6 +200,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
return ret_val;
}
+EXPORT_SYMBOL_FOR_MODULES(devm_cxl_iomap_block, "cxl_core");
int cxl_map_component_regs(const struct cxl_register_map *map,
struct cxl_component_regs *regs,
@@ -532,6 +533,7 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb)
return offset;
}
+EXPORT_SYMBOL_FOR_MODULES(cxl_rcrb_to_aer, "cxl_core");
static resource_size_t cxl_rcrb_to_linkcap(struct device *dev, struct cxl_dport *dport)
{
@@ -648,6 +650,7 @@ resource_size_t __rcrb_to_component(struct device *dev, struct cxl_rcrb_info *ri
return component_reg_phys;
}
+EXPORT_SYMBOL_FOR_MODULES(__rcrb_to_component, "cxl_core");
resource_size_t cxl_rcd_component_reg_phys(struct device *dev,
struct cxl_dport *dport)
@@ -656,3 +659,4 @@ resource_size_t cxl_rcd_component_reg_phys(struct device *dev,
return CXL_RESOURCE_NONE;
return __rcrb_to_component(dev, &dport->rcrb, CXL_RCRB_UPSTREAM);
}
+EXPORT_SYMBOL_FOR_MODULES(cxl_rcd_component_reg_phys, "cxl_core");
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 9575ecd27ed8..9f8eb2cb4fcc 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -245,9 +245,6 @@ enum cxl_regloc_type;
int cxl_count_regblock(struct pci_dev *pdev, enum cxl_regloc_type type);
int cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_type type,
struct cxl_register_map *map, unsigned int index);
-int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
- struct cxl_register_map *map);
-int cxl_setup_regs(struct cxl_register_map *map);
struct cxl_dport;
int cxl_dport_map_rcd_linkcap(struct pci_dev *pdev, struct cxl_dport *dport);
diff --git a/include/cxl/pci.h b/include/cxl/pci.h
index 3e0000015871..ba7a4ab9c458 100644
--- a/include/cxl/pci.h
+++ b/include/cxl/pci.h
@@ -19,4 +19,7 @@ struct pci_dev;
int cxl_pci_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
struct cxl_register_map *map);
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+ struct cxl_register_map *map);
+int cxl_setup_regs(struct cxl_register_map *map);
#endif
diff --git a/tools/testing/cxl/Kbuild b/tools/testing/cxl/Kbuild
index e49ce644b4b1..446e571cf6d7 100644
--- a/tools/testing/cxl/Kbuild
+++ b/tools/testing/cxl/Kbuild
@@ -59,7 +59,6 @@ obj-m += cxl_core.o
cxl_core-y := $(CXL_CORE_SRC)/port.o
cxl_core-y += $(CXL_CORE_SRC)/pmem.o
-cxl_core-y += $(CXL_CORE_SRC)/regs.o
cxl_core-y += $(CXL_CORE_SRC)/memdev.o
cxl_core-y += $(CXL_CORE_SRC)/mbox.o
cxl_core-y += $(CXL_CORE_SRC)/pci.o
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (9 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
` (7 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Cache HDM state in pci_dev->hdm after BAR resource setup and before driver
binding, so reset can restore it without a bound CXL driver.
Limit caching to devices that advertise CXL.cache, CXL.mem, CXL Reset,
and Reset Memory Clear. Save their HDM register location, decoder
settings, and Device DVSEC state.
Validate capability reads and decoder counts, and restore PCI_COMMAND
after temporary MMIO access, reporting restoration failures. Publish the
cache under the PCI device lock.
Update the cached global control when the CXL driver disables HDM on
unbind, so a later reset does not restore a stale enable bit.
Preserve the register block BAR and offset in struct cxl_register_map, so
the cache can map the HDM registers again after a reset.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Drop the cxl_rwsem.dpa locking from the cache helpers here, since no
reset path reads the cache yet; it returns with the readers in the
reset locking patch. Free the cache directly on release, simplify
__cxl_pci_hdm_read_info() and move the publish comment above
guard(pci_dev) (Li Ming)
https://lore.kernel.org/linux-cxl/e8084759-ce40-4a74-b47f-cf62080be7d5@zohomail.com/
- Reject all-ones reads of PCI_COMMAND, the DVSEC Control register, the
global HDM decoder control and the decoder skip registers when filling
the cache, so an unresponsive device is not cached as valid state
(Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=11
- Move the register block BAR and offset fields and
cxl_dvsec_hdm_count_valid() here from the register helper patch, where
they are first used (Dave)
https://lore.kernel.org/linux-cxl/8f06ddfa-1c8d-4421-a830-8429b9e5ee88@intel.com/
- Update the cached global control when the CXL driver disables HDM on
unbind, so a later reset does not restore a stale enable bit (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=11
- Add cxl_pci_read_config_word(), which maps a failed or all-ones config
read to an errno, and use it for the reads checked here and in the
later reset patches (Li Ming)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=b12baa1105f33cc1a10f9c9c0c6376194397d9f3
---
MAINTAINERS | 1 +
drivers/cxl/core/hdm_regs.c | 102 +++++++++++++++
drivers/cxl/core/hdm_state.c | 46 ++++++-
drivers/cxl/core/pci.c | 38 ++++--
drivers/cxl/core/regs.c | 5 +
drivers/pci/Makefile | 1 +
drivers/pci/bus.c | 1 +
drivers/pci/cxl.c | 226 ++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 8 ++
drivers/pci/probe.c | 1 +
include/cxl/cxl.h | 4 +
include/cxl/hdm.h | 14 ++-
include/cxl/pci.h | 10 ++
include/uapi/linux/pci_regs.h | 2 +
14 files changed, 445 insertions(+), 14 deletions(-)
create mode 100644 drivers/pci/cxl.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..48d16dc4be46 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6735,6 +6735,7 @@ S: Maintained
F: Documentation/driver-api/cxl
F: Documentation/userspace-api/fwctl/fwctl-cxl.rst
F: drivers/cxl/
+F: drivers/pci/cxl.c
F: include/cxl/
F: include/uapi/linux/cxl_mem.h
F: tools/testing/cxl/
diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
index cc5d77c4abf5..cc99ae83d1cd 100644
--- a/drivers/cxl/core/hdm_regs.c
+++ b/drivers/cxl/core/hdm_regs.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
#include <linux/delay.h>
#include <linux/bug.h>
+#include <linux/device.h>
#include <linux/errno.h>
#include <linux/export.h>
#include <linux/kernel.h>
@@ -190,3 +191,104 @@ int cxl_hdm_read_decoder(struct cxl_decoder_config *config, void __iomem *hdm,
return cxl_hdm_unpack_decoder(config, id, ctrl, base, size);
}
EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_read_decoder, "cxl_core");
+
+/**
+ * cxl_hdm_decoder_count_mapped() - read the decoder count of a mapped block
+ * @dev: device for error reporting
+ * @hdm: mapped HDM decoder capability
+ * @size: size of the mapping
+ *
+ * Return: the decoder count, or a negative errno if the count is reserved or
+ * the mapping cannot hold every decoder.
+ */
+int cxl_hdm_decoder_count_mapped(struct device *dev, void __iomem *hdm,
+ resource_size_t size)
+{
+ int count;
+
+ count = cxl_hdm_decoder_count(readl(hdm + CXL_HDM_DECODER_CAP_OFFSET));
+ if (count < 0)
+ return count;
+
+ /* The last decoder's target-list high register must fit in the mapping. */
+ if (CXL_HDM_DECODER0_TL_HIGH(count - 1) + sizeof(u32) > size) {
+ dev_err(dev, "CXL HDM decoder count exceeds mapped register block\n");
+ return -ENXIO;
+ }
+
+ return count;
+}
+
+/* Read one endpoint decoder; a disabled decoder keeps only its id. */
+static int cxl_hdm_read_decoder_settings(struct device *dev,
+ struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm, int id)
+{
+ u64 lo, hi;
+ u32 ctrl;
+ int rc;
+
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ rc = cxl_hdm_read_decoder(&settings->config, hdm, id, ctrl);
+ if (rc) {
+ dev_err(dev,
+ "CXL HDM decoder %d has invalid configuration: %d\n",
+ id, rc);
+ return rc;
+ }
+
+ if (!(settings->config.flags & CXL_DECODER_F_ENABLE)) {
+ *settings = (struct cxl_endpoint_decoder_settings) {
+ .config.id = id,
+ };
+ } else {
+ lo = readl(hdm + CXL_HDM_DECODER0_SKIP_LOW(id));
+ hi = readl(hdm + CXL_HDM_DECODER0_SKIP_HIGH(id));
+ settings->skip = (hi << 32) | lo;
+ if (settings->skip == U64_MAX)
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+/**
+ * cxl_hdm_read_state() - read endpoint HDM decoder state
+ * @dev: device for error messages
+ * @hdm: mapped HDM decoder capability
+ * @info: state to fill; @info->decoder_count must be set
+ *
+ * Return: 0 on success, or a negative errno if a decoder holds an invalid
+ * configuration, the decoder count changed while reading, or the device
+ * returned all-ones.
+ */
+int cxl_hdm_read_state(struct device *dev, void __iomem *hdm,
+ struct cxl_hdm_info *info)
+{
+ int count;
+ int rc;
+
+ info->global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+ if (info->global_ctrl == U32_MAX)
+ return -ENODEV;
+
+ for (int i = 0; i < info->decoder_count; i++) {
+ rc = cxl_hdm_read_decoder_settings(dev, &info->settings[i],
+ hdm, i);
+ if (rc)
+ return rc;
+ }
+
+ /*
+ * A changed decoder count means the snapshot no longer describes the
+ * complete hardware state. Reject it rather than publish a partial cache.
+ */
+ count = cxl_hdm_decoder_count(readl(hdm + CXL_HDM_DECODER_CAP_OFFSET));
+ if (count != info->decoder_count) {
+ dev_err(dev, "CXL HDM decoder count changed from %d to %d\n",
+ info->decoder_count, count);
+ return -ENXIO;
+ }
+
+ return 0;
+}
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
index ec335ac21ad4..ddb69411de1c 100644
--- a/drivers/cxl/core/hdm_state.c
+++ b/drivers/cxl/core/hdm_state.c
@@ -1,21 +1,52 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+#include <linux/cleanup.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/pci.h>
#include <linux/rwsem.h>
+#include <linux/slab.h>
#include <cxl/hdm.h>
-
#include "cxl.h"
#include "core.h"
+/*
+ * CXL state that must stay available while no CXL driver is bound: the CXL
+ * locks and the per-device HDM cache filled during PCI enumeration.
+ */
struct cxl_rwsem cxl_rwsem = {
.region = __RWSEM_INITIALIZER(cxl_rwsem.region),
.dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa),
};
EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core");
+void cxl_hdm_cache_set_dvsec_ctrl(struct pci_dev *pdev, u16 ctrl)
+{
+ if (!pdev->hdm)
+ return;
+
+ pdev->hdm->dvsec_ctrl = ctrl;
+ pdev->hdm->dvsec_ctrl_valid = true;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_dvsec_ctrl, "cxl_core");
+
+/**
+ * cxl_hdm_cache_set_global_ctrl() - record a global control write in the cache
+ * @pdev: endpoint that owns the cache
+ * @ctrl: value just written to the HDM decoder global control register
+ *
+ * Keep the cache in step with writes made outside decoder commit and teardown,
+ * such as enabling HDM when the driver binds and disabling it when it unbinds,
+ * so a later reset does not restore a stale enable bit.
+ */
+void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl)
+{
+ if (pdev->hdm)
+ pdev->hdm->global_ctrl = ctrl;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_global_ctrl, "cxl_core");
+
/**
* cxl_hdm_cache_refresh_decoder() - update the PCI HDM cache from a decoder
* @pdev: PCI device that owns the HDM cache
@@ -91,3 +122,16 @@ bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count)
return true;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_decoder_count_match, "cxl_core");
+
+/**
+ * cxl_hdm_cache_release() - free cached HDM state
+ * @slot: cache pointer owned by the device
+ *
+ * Called while the PCI device is being released, so no driver or reset path
+ * can still reach the cache and no locking is needed.
+ */
+void cxl_hdm_cache_release(struct cxl_hdm_info **slot)
+{
+ kfree(*slot);
+ *slot = NULL;
+}
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index d0da38763b4b..a9d33357d095 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -212,22 +212,31 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
struct pci_dev *pdev = to_pci_dev(cxlds->dev);
int d = cxlds->cxl_dvsec;
u16 ctrl;
- int rc;
+ int rc, ret = 1;
rc = pci_read_config_word(pdev, d + PCI_DVSEC_CXL_CTRL, &ctrl);
if (rc)
return pcibios_err_to_errno(rc);
+ /* An all-ones read must not be written back or cached. */
+ if (PCI_POSSIBLE_ERROR(ctrl))
+ return -ENODEV;
- if ((ctrl & PCI_DVSEC_CXL_MEM_ENABLE) == val)
- return 1;
- ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE;
- ctrl |= val;
+ if ((ctrl & PCI_DVSEC_CXL_MEM_ENABLE) != val) {
+ ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE;
+ ctrl |= val;
- rc = pci_write_config_word(pdev, d + PCI_DVSEC_CXL_CTRL, ctrl);
- if (rc)
- return pcibios_err_to_errno(rc);
+ rc = pci_write_config_word(pdev, d + PCI_DVSEC_CXL_CTRL, ctrl);
+ if (rc)
+ return pcibios_err_to_errno(rc);
+ ret = 0;
+ }
- return 0;
+ /*
+ * Refresh the PCI-core cache from the live register even when
+ * MEM_ENABLE already matched: a reset restores the cached value.
+ */
+ cxl_hdm_cache_set_dvsec_ctrl(pdev, ctrl);
+ return ret;
}
static void clear_mem_enable(void *cxlds)
@@ -266,13 +275,20 @@ static int dvsec_range_allowed(struct device *dev, const void *arg)
static void disable_hdm(void *_cxlhdm)
{
- u32 global_ctrl;
struct cxl_hdm *cxlhdm = _cxlhdm;
void __iomem *hdm = cxlhdm->regs.hdm_decoder;
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(cxlhdm->port);
+ u32 global_ctrl;
global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+
+ /* Keep the reset cache up to date. */
+ if (pdev && global_ctrl != U32_MAX)
+ cxl_hdm_cache_set_global_ctrl(pdev,
+ global_ctrl & ~CXL_HDM_DECODER_ENABLE);
}
static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)
@@ -317,7 +333,7 @@ int cxl_dvsec_rr_decode(struct cxl_dev_state *cxlds,
* capable, there is no point in continuing to enable CXL.mem.
*/
hdm_count = FIELD_GET(PCI_DVSEC_CXL_HDM_COUNT, cap);
- if (!hdm_count || hdm_count > 2)
+ if (!cxl_dvsec_hdm_count_valid(hdm_count))
return -EINVAL;
/*
diff --git a/drivers/cxl/core/regs.c b/drivers/cxl/core/regs.c
index ef7c236b846c..a20cea3cf760 100644
--- a/drivers/cxl/core/regs.c
+++ b/drivers/cxl/core/regs.c
@@ -300,6 +300,8 @@ static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi,
}
map->reg_type = reg_type;
+ map->bar = bar;
+ map->bar_offset = offset;
map->resource = pci_resource_start(pdev, bar) + offset;
map->max_size = pci_resource_len(pdev, bar) - offset;
return true;
@@ -324,6 +326,7 @@ static int __cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_ty
*map = (struct cxl_register_map) {
.host = &pdev->dev,
.resource = CXL_RESOURCE_NONE,
+ .bar = -1,
};
regloc = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
@@ -354,6 +357,8 @@ static int __cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_ty
}
map->resource = CXL_RESOURCE_NONE;
+ map->bar = -1;
+ map->bar_offset = 0;
if (index == CXL_INSTANCES_COUNT)
return instance;
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 41ebc3b9a518..7a5d9f7297e9 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -34,6 +34,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o
obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
obj-$(CONFIG_VGA_ARB) += vgaarb.o
obj-$(CONFIG_PCI_DOE) += doe.o
+obj-$(CONFIG_CXL_RESET) += cxl.o
obj-$(CONFIG_PCI_IDE) += ide.o
obj-$(CONFIG_PCI_TSM) += tsm.o
obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 655ed53436d3..aad567fb7c56 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -359,6 +359,7 @@ void pci_bus_add_device(struct pci_dev *dev)
/* Save config space for error recoverability */
pci_save_state(dev);
+ pci_cxl_hdm_cache_init(dev);
/*
* Enable runtime PM, which potentially allows the device to
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
new file mode 100644
index 000000000000..56119e3fd03a
--- /dev/null
+++ b/drivers/pci/cxl.c
@@ -0,0 +1,226 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+/*
+ * CXL reset support in the PCI core. Cache the HDM decoder state of a CXL
+ * memory device at enumeration, so it can be restored after a reset without
+ * a CXL driver. HDM register formats and programming live in drivers/cxl.
+ */
+#include <linux/bitfield.h>
+#include <linux/cleanup.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/pci.h>
+#include <linux/slab.h>
+
+#include <cxl/hdm.h>
+#include <cxl/pci.h>
+
+#include "pci.h"
+
+/*
+ * Read a config word. A failed read or an all-ones value means the device is
+ * not responding, so return an errno rather than a value to be interpreted.
+ */
+static int cxl_pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
+{
+ int rc;
+
+ rc = pci_read_config_word(pdev, where, val);
+ if (rc)
+ return pcibios_err_to_errno(rc);
+ if (PCI_POSSIBLE_ERROR(*val))
+ return -ENODEV;
+
+ return 0;
+}
+
+/* Find the CXL Device DVSEC and read its capability register. */
+static int cxl_pci_device_dvsec(struct pci_dev *pdev, u16 *dvsec, u16 *cap)
+{
+ *dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
+ PCI_DVSEC_CXL_DEVICE);
+ if (!*dvsec)
+ return -ENOTTY;
+
+ return cxl_pci_read_config_word(pdev, *dvsec + PCI_DVSEC_CXL_CAP, cap);
+}
+
+static bool cxl_pci_bar_assigned(struct pci_dev *pdev, int bar)
+{
+ struct resource *res = &pdev->resource[bar];
+
+ return pci_resource_len(pdev, bar) &&
+ !(res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) &&
+ resource_type(res) == IORESOURCE_MEM && res->start && res->end;
+}
+
+static void __iomem *cxl_pci_hdm_map_regs(struct pci_dev *pdev,
+ struct cxl_register_map *map,
+ resource_size_t *hdm_offset,
+ int *decoder_count)
+{
+ struct cxl_reg_map *hdm_map = &map->component_map.hdm_decoder;
+ void __iomem *hdm;
+ int rc;
+
+ /* cxl_setup_regs() populates hdm_map, including its valid bit. */
+ rc = cxl_setup_regs(map);
+ if (rc)
+ return ERR_PTR(rc);
+ if (!hdm_map->valid)
+ return ERR_PTR(-ENODEV);
+ if (hdm_map->offset > map->max_size ||
+ hdm_map->size > map->max_size - hdm_map->offset)
+ return ERR_PTR(-ENXIO);
+ if (check_add_overflow(map->bar_offset,
+ (resource_size_t)hdm_map->offset, hdm_offset))
+ return ERR_PTR(-EOVERFLOW);
+
+ hdm = ioremap(map->resource + hdm_map->offset, hdm_map->size);
+ if (!hdm)
+ return ERR_PTR(-ENOMEM);
+
+ *decoder_count = cxl_hdm_decoder_count_mapped(&pdev->dev, hdm,
+ hdm_map->size);
+ if (*decoder_count < 0) {
+ iounmap(hdm);
+ return ERR_PTR(*decoder_count);
+ }
+
+ return hdm;
+}
+
+static struct cxl_hdm_info *__cxl_pci_hdm_read_info(struct pci_dev *pdev,
+ struct cxl_register_map *map,
+ int dvsec)
+{
+ struct cxl_reg_map *hdm_map = &map->component_map.hdm_decoder;
+ struct cxl_hdm_info *info = NULL;
+ resource_size_t hdm_offset;
+ void __iomem *hdm;
+ int decoder_count;
+ u16 dvsec_ctrl;
+ int rc;
+
+ hdm = cxl_pci_hdm_map_regs(pdev, map, &hdm_offset, &decoder_count);
+ if (IS_ERR(hdm))
+ return ERR_CAST(hdm);
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL,
+ &dvsec_ctrl);
+ if (rc)
+ goto out;
+
+ info = kzalloc_flex(*info, settings, decoder_count, GFP_KERNEL);
+ if (!info) {
+ rc = -ENOMEM;
+ goto out;
+ }
+
+ info->decoder_count = decoder_count;
+ info->hdm_bar = map->bar;
+ info->hdm_offset = hdm_offset;
+ info->hdm_size = hdm_map->size;
+ info->dvsec = dvsec;
+ info->dvsec_ctrl = dvsec_ctrl;
+ info->dvsec_ctrl_valid = true;
+
+ rc = cxl_hdm_read_state(&pdev->dev, hdm, info);
+out:
+ if (rc)
+ kfree(info);
+ iounmap(hdm);
+ return rc ? ERR_PTR(rc) : info;
+}
+
+static struct cxl_hdm_info *cxl_pci_hdm_read_info(struct pci_dev *pdev,
+ struct cxl_register_map *map,
+ int dvsec)
+{
+ struct cxl_hdm_info *info;
+ u16 command;
+ int rc;
+
+ rc = cxl_pci_read_config_word(pdev, PCI_COMMAND, &command);
+ if (rc)
+ return ERR_PTR(rc);
+
+ if (!(command & PCI_COMMAND_MEMORY)) {
+ rc = pci_write_config_word(pdev, PCI_COMMAND,
+ command | PCI_COMMAND_MEMORY);
+ if (rc)
+ return ERR_PTR(pcibios_err_to_errno(rc));
+ }
+
+ info = __cxl_pci_hdm_read_info(pdev, map, dvsec);
+ if (!(command & PCI_COMMAND_MEMORY)) {
+ rc = pci_write_config_word(pdev, PCI_COMMAND, command);
+ if (rc)
+ pci_err(pdev,
+ "failed to restore PCI_COMMAND after CXL HDM cache init: %d\n",
+ pcibios_err_to_errno(rc));
+ }
+ return info;
+}
+
+static int __pci_cxl_hdm_cache_init(struct pci_dev *pdev)
+{
+ struct cxl_register_map map = { };
+ struct cxl_hdm_info *read_info;
+ u16 dvsec, cap;
+ int rc;
+
+ /*
+ * The PCI device lock excludes other initializers. CXL drivers bind
+ * only after enumeration, so no cache mutator can run yet and
+ * cxl_rwsem.dpa is not needed to publish.
+ */
+ guard(pci_dev)(pdev);
+
+ /* Avoid duplicate MMIO reads when the cache is already present. */
+ if (pdev->hdm)
+ return 0;
+
+ rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
+ if (rc == -ENOTTY)
+ return 0;
+ if (rc)
+ return rc;
+ if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE) ||
+ !cxl_dvsec_hdm_count_valid(FIELD_GET(PCI_DVSEC_CXL_HDM_COUNT, cap)))
+ return 0;
+
+ rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+ if (rc)
+ return rc;
+ if (map.bar < 0 || map.bar >= PCI_STD_NUM_BARS ||
+ !cxl_pci_bar_assigned(pdev, map.bar))
+ return -ENODEV;
+
+ read_info = cxl_pci_hdm_read_info(pdev, &map, dvsec);
+ if (IS_ERR(read_info))
+ return PTR_ERR(read_info);
+
+ pdev->hdm = read_info;
+
+ return 0;
+}
+
+void pci_cxl_hdm_cache_init(struct pci_dev *pdev)
+{
+ int rc;
+
+ rc = __pci_cxl_hdm_cache_init(pdev);
+ if (rc)
+ pci_dbg(pdev, "CXL HDM cache init failed: %d\n", rc);
+}
+
+void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
+{
+ cxl_hdm_cache_release(&pdev->hdm);
+}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960..ea5126e86f4a 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -736,6 +736,14 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }
static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
#endif
+#ifdef CONFIG_CXL_RESET
+void pci_cxl_hdm_cache_init(struct pci_dev *pdev);
+void pci_cxl_hdm_cache_release(struct pci_dev *pdev);
+#else
+static inline void pci_cxl_hdm_cache_init(struct pci_dev *pdev) { }
+static inline void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { }
+#endif
+
#ifdef CONFIG_PCI_NPEM
void pci_npem_create(struct pci_dev *dev);
void pci_npem_remove(struct pci_dev *dev);
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..f4ab10994908 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -2491,6 +2491,7 @@ static void pci_release_dev(struct device *dev)
struct pci_dev *pci_dev;
pci_dev = to_pci_dev(dev);
+ pci_cxl_hdm_cache_release(pci_dev);
pci_release_capabilities(pci_dev);
pci_release_of_node(pci_dev);
pcibios_release_device(pci_dev);
diff --git a/include/cxl/cxl.h b/include/cxl/cxl.h
index 713bce162bd0..d9e9b3ccdd4a 100644
--- a/include/cxl/cxl.h
+++ b/include/cxl/cxl.h
@@ -149,6 +149,8 @@ struct cxl_pmu_reg_map {
* @base: virtual base of the register-block-BAR + @block_offset
* @resource: physical resource base of the register block
* @max_size: maximum mapping size to perform register search
+ * @bar: PCI BAR containing the register block
+ * @bar_offset: register block offset relative to @bar
* @reg_type: see enum cxl_regloc_type
* @component_map: cxl_reg_map for component registers
* @device_map: cxl_reg_maps for device registers
@@ -159,6 +161,8 @@ struct cxl_register_map {
void __iomem *base;
resource_size_t resource;
resource_size_t max_size;
+ int bar;
+ resource_size_t bar_offset;
u8 reg_type;
union {
struct cxl_component_reg_map component_map;
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index 6c5b369ce490..e424ccfcf152 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -26,12 +26,12 @@ struct cxl_endpoint_decoder_settings {
* @hdm_offset: offset of the HDM decoder capability in @hdm_bar
* @hdm_size: size of the HDM decoder register block
* @global_ctrl: HDM decoder global control register
+ * @dvsec: config space offset of the CXL Device DVSEC
* @dvsec_ctrl: CXL DVSEC control register
* @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state
* @settings: per-endpoint-decoder programming state
*
- * Publication through pci_dev.hdm and all mutable fields are protected by
- * cxl_rwsem.dpa.
+ * Publication through pci_dev.hdm is serialized by the PCI device lock.
*/
struct cxl_hdm_info {
int decoder_count;
@@ -39,13 +39,23 @@ struct cxl_hdm_info {
resource_size_t hdm_offset;
resource_size_t hdm_size;
u32 global_ctrl;
+ u16 dvsec;
u16 dvsec_ctrl;
bool dvsec_ctrl_valid;
struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count);
};
+struct device;
+
+int cxl_hdm_decoder_count_mapped(struct device *dev, void __iomem *hdm,
+ resource_size_t size);
+int cxl_hdm_read_state(struct device *dev, void __iomem *hdm,
+ struct cxl_hdm_info *info);
+void cxl_hdm_cache_set_dvsec_ctrl(struct pci_dev *pdev, u16 ctrl);
+void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl);
void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
struct cxl_endpoint_decoder *cxled);
bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count);
+void cxl_hdm_cache_release(struct cxl_hdm_info **slot);
#endif /* __CXL_HDM_H__ */
diff --git a/include/cxl/pci.h b/include/cxl/pci.h
index ba7a4ab9c458..b15d7e415e27 100644
--- a/include/cxl/pci.h
+++ b/include/cxl/pci.h
@@ -4,6 +4,8 @@
#ifndef __CXL_CXL_PCI_H__
#define __CXL_CXL_PCI_H__
+#include <linux/types.h>
+
/* Register Block Identifier (RBI) */
enum cxl_regloc_type {
CXL_REGLOC_RBI_EMPTY = 0,
@@ -22,4 +24,12 @@ int cxl_pci_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
struct cxl_register_map *map);
int cxl_setup_regs(struct cxl_register_map *map);
+/*
+ * CXL r4.0 sec 8.1.3.1: a CXL.mem capable device reports one or two HDM
+ * ranges. 00b is illegal when Mem_Capable is set and 11b is reserved.
+ */
+static inline bool cxl_dvsec_hdm_count_valid(unsigned int hdm_count)
+{
+ return hdm_count == 1 || hdm_count == 2;
+}
#endif
diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
index facaa324bd86..ca3e77b72d87 100644
--- a/include/uapi/linux/pci_regs.h
+++ b/include/uapi/linux/pci_regs.h
@@ -1352,6 +1352,8 @@
#define PCI_DVSEC_CXL_CACHE_CAPABLE _BITUL(0)
#define PCI_DVSEC_CXL_MEM_CAPABLE _BITUL(2)
#define PCI_DVSEC_CXL_HDM_COUNT __GENMASK(5, 4)
+#define PCI_DVSEC_CXL_RST_CAPABLE _BITUL(7)
+#define PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE _BITUL(11)
#define PCI_DVSEC_CXL_CTRL 0xC
#define PCI_DVSEC_CXL_MEM_ENABLE _BITUL(2)
#define PCI_DVSEC_CXL_RANGE_SIZE_HIGH(i) (0x18 + (i * 0x10))
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (10 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
` (6 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Implement CXL Device Reset for eligible Type 2 devices: disable CXL.cache,
perform supported writeback, drain transactions, prepare the IOMMU,
initiate reset with Memory Clear enabled, and wait for completion.
Observe the 100 ms post-reset quiet period before polling Status2, and
treat an all-ones read as not ready. Wait for the timeout the device
advertises, up to 100 seconds, and access only Status2 until reset
completion, error or expiry, as CXL 4.0 section 9.7.3 requires. Control2
cleanup runs only afterwards. Treat Control2 command bits as
transient state, restore cache policy on exit, preserve the primary error,
and abort when IOMMU preparation fails.
Reuse Device DVSEC discovery from cache initialization and keep the
reset entry point declaration private to PCI core.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Reword the reset wait message to "Waiting for CXL reset to complete" and
log an error when the wait times out (Li Ming)
https://lore.kernel.org/linux-cxl/568d7c66-eb67-4369-8b15-5ee923471881@zohomail.com/
- Use cxl_pci_read_config_word() for the Control, Control2 and Status2
reads that reject an all-ones value, instead of open-coding the check
(Li Ming)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=04f8f84816d724f8b47f4d47a77916ac37c63a49
---
drivers/pci/cxl.c | 241 +++++++++++++++++++++++++++++++++-
drivers/pci/pci.h | 5 +
include/uapi/linux/pci_regs.h | 12 ++
3 files changed, 257 insertions(+), 1 deletion(-)
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 56119e3fd03a..4d6cfabab2c0 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -3,13 +3,17 @@
/*
* CXL reset support in the PCI core. Cache the HDM decoder state of a CXL
* memory device at enumeration, so it can be restored after a reset without
- * a CXL driver. HDM register formats and programming live in drivers/cxl.
+ * a CXL driver, and implement CXL Device Reset. HDM register formats and
+ * programming live in drivers/cxl.
*/
#include <linux/bitfield.h>
#include <linux/cleanup.h>
+#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/io.h>
#include <linux/ioport.h>
+#include <linux/iommu.h>
+#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/slab.h>
@@ -224,3 +228,238 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
{
cxl_hdm_cache_release(&pdev->hdm);
}
+
+/*
+ * CXL r4.0 sec 9.7.2 defines the reset completion timeout encodings.
+ * Sec 9.7.3 leaves config-space access behavior undefined for 100 ms after
+ * initiating CXL Reset, then limits software to CXL Status2 access until
+ * reset completion, timeout, or error. Wait for the full advertised timeout
+ * so that Control2 is not accessed while the device may still be resetting.
+ */
+#define CXL_RESET_RRS_WAIT_MS 100
+#define CXL_RESET_STATUS_POLL_MS 20
+static const u32 cxl_reset_timeout_ms[] = {
+ 10, 100, 1000, 10000, 100000,
+};
+
+/*
+ * CXL 4.0 section 9.6 permits using the device cache size to determine
+ * a timeout. Use a conservative fixed estimate of 100 ms.
+ */
+#define CXL_CACHE_WBI_TIMEOUT_US 100000
+#define CXL_CACHE_WBI_POLL_US 100
+
+#define CXL_RESET_CTRL2_CMD_MASK \
+ (PCI_DVSEC_CXL_INIT_CACHE_WBI | PCI_DVSEC_CXL_INIT_CXL_RST)
+
+static int cxl_reset_update_ctrl2(struct pci_dev *pdev, int dvsec,
+ u16 set, u16 clear)
+{
+ u16 ctrl2;
+ int rc;
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, &ctrl2);
+ if (rc)
+ return rc;
+
+ /*
+ * INIT_CACHE_WBI and INIT_CXL_RST are commands. Do not replay a command
+ * that remains set in the value read back while updating persistent
+ * Control2 state.
+ */
+ ctrl2 &= ~CXL_RESET_CTRL2_CMD_MASK;
+ ctrl2 &= ~clear;
+ ctrl2 |= set;
+
+ rc = pci_write_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2, ctrl2);
+ if (rc)
+ return pcibios_err_to_errno(rc);
+
+ return 0;
+}
+
+static int cxl_reset_wait_cache_wbi(struct pci_dev *pdev, int dvsec)
+{
+ unsigned long deadline;
+ u16 status2;
+ int rc;
+
+ rc = cxl_reset_update_ctrl2(pdev, dvsec,
+ PCI_DVSEC_CXL_INIT_CACHE_WBI, 0);
+ if (rc)
+ return rc;
+
+ deadline = jiffies + usecs_to_jiffies(CXL_CACHE_WBI_TIMEOUT_US);
+ while (time_before(jiffies, deadline)) {
+ usleep_range(CXL_CACHE_WBI_POLL_US,
+ CXL_CACHE_WBI_POLL_US * 2);
+
+ rc = cxl_pci_read_config_word(pdev,
+ dvsec + PCI_DVSEC_CXL_STATUS2,
+ &status2);
+ if (rc)
+ return rc;
+ if (status2 & PCI_DVSEC_CXL_CACHE_INV)
+ return 0;
+ }
+
+ pci_warn(pdev, "timed out waiting for CXL cache writeback and invalidate\n");
+ return -ETIMEDOUT;
+}
+
+static int cxl_reset_disable_cache(struct pci_dev *pdev, int dvsec, u16 cap)
+{
+ int rc;
+
+ rc = cxl_reset_update_ctrl2(pdev, dvsec,
+ PCI_DVSEC_CXL_DISABLE_CACHING, 0);
+ if (rc || !(cap & PCI_DVSEC_CXL_CACHE_WBI_CAPABLE))
+ return rc;
+
+ return cxl_reset_wait_cache_wbi(pdev, dvsec);
+}
+
+static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec,
+ u16 saved_ctrl2)
+{
+ int rc;
+
+ rc = cxl_reset_update_ctrl2(pdev, dvsec,
+ saved_ctrl2 & PCI_DVSEC_CXL_DISABLE_CACHING,
+ PCI_DVSEC_CXL_DISABLE_CACHING);
+ if (rc)
+ pci_err(pdev, "failed to restore CXL cache policy: %d\n", rc);
+
+ return rc;
+}
+
+static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap)
+{
+ unsigned long deadline;
+ u32 timeout_ms;
+ u16 status2;
+ int idx, rc;
+
+ idx = FIELD_GET(PCI_DVSEC_CXL_RST_TIMEOUT, cap);
+ if (idx >= ARRAY_SIZE(cxl_reset_timeout_ms)) {
+ idx = ARRAY_SIZE(cxl_reset_timeout_ms) - 1;
+ pci_warn(pdev,
+ "unknown CXL reset timeout encoding; using %u ms\n",
+ cxl_reset_timeout_ms[idx]);
+ }
+
+ /* Allow one retry after the mandatory 100 ms quiet period. */
+ timeout_ms = max_t(u32, cxl_reset_timeout_ms[idx],
+ CXL_RESET_RRS_WAIT_MS + CXL_RESET_STATUS_POLL_MS);
+ pci_info(pdev, "Waiting for CXL reset to complete (max wait: %u ms)\n",
+ timeout_ms);
+ msleep(CXL_RESET_RRS_WAIT_MS);
+ deadline = jiffies + msecs_to_jiffies(timeout_ms -
+ CXL_RESET_RRS_WAIT_MS);
+
+ for (;;) {
+ rc = pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_STATUS2,
+ &status2);
+ if (rc)
+ return pcibios_err_to_errno(rc);
+ if (status2 != U16_MAX) {
+ if (status2 & PCI_DVSEC_CXL_RST_ERR)
+ return -EIO;
+ if (status2 & PCI_DVSEC_CXL_RST_DONE)
+ return 0;
+ }
+
+ if (time_after_eq(jiffies, deadline)) {
+ pci_err(pdev, "timed out waiting for CXL reset to complete\n");
+ return -ETIMEDOUT;
+ }
+
+ msleep(CXL_RESET_STATUS_POLL_MS);
+ }
+}
+
+static int cxl_clear_memory(struct pci_dev *pdev, int dvsec, bool initiate)
+{
+ if (initiate)
+ return cxl_reset_update_ctrl2(pdev, dvsec,
+ PCI_DVSEC_CXL_INIT_CXL_RST |
+ PCI_DVSEC_CXL_RST_MEM_CLR_EN,
+ 0);
+
+ return cxl_reset_update_ctrl2(pdev, dvsec, 0,
+ PCI_DVSEC_CXL_RST_MEM_CLR_EN);
+}
+
+static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
+{
+ int rc, rc2;
+
+ if (!pci_wait_for_pending_transaction(pdev)) {
+ pci_err(pdev, "timed out waiting for pending transactions\n");
+ return -ETIMEDOUT;
+ }
+
+ rc = pci_dev_reset_iommu_prepare(pdev);
+ if (rc) {
+ pci_err(pdev, "failed to stop IOMMU for CXL reset: %d\n", rc);
+ return rc;
+ }
+
+ rc = cxl_clear_memory(pdev, dvsec, true);
+ if (!rc)
+ rc = cxl_reset_wait_done(pdev, dvsec, cap);
+
+ /* Always clear Memory Clear; successful cleanup must not hide reset failure. */
+ rc2 = cxl_clear_memory(pdev, dvsec, false);
+ if (rc2)
+ pci_err(pdev, "failed to clear CXL Reset Memory Clear: %d\n", rc2);
+ rc = rc ?: rc2;
+
+ pci_dev_reset_iommu_done(pdev);
+ return rc;
+}
+
+static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
+{
+ u16 saved_ctrl2;
+ int rc, rc2;
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
+ &saved_ctrl2);
+ if (rc)
+ return rc;
+
+ rc = cxl_reset_disable_cache(pdev, dvsec, cap);
+ if (!rc)
+ rc = __cxl_reset_execute(pdev, dvsec, cap);
+ /* Restore cache policy after any attempt to disable caching. */
+ rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, saved_ctrl2);
+ return rc ?: rc2;
+}
+
+int cxl_reset_function(struct pci_dev *pdev, bool probe)
+{
+ u16 dvsec, cap, ctrl;
+ int rc;
+
+ rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
+ if (rc)
+ return rc;
+ if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
+ !(cap & PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE))
+ return -ENOTTY;
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL, &ctrl);
+ if (rc)
+ return rc;
+ if (!(ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) ||
+ !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))
+ return -ENOTTY;
+
+ if (probe)
+ return 0;
+
+ return cxl_reset_execute(pdev, dvsec, cap);
+}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index ea5126e86f4a..5bfa77fd1d37 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -739,9 +739,14 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
#ifdef CONFIG_CXL_RESET
void pci_cxl_hdm_cache_init(struct pci_dev *pdev);
void pci_cxl_hdm_cache_release(struct pci_dev *pdev);
+int cxl_reset_function(struct pci_dev *pdev, bool probe);
#else
static inline void pci_cxl_hdm_cache_init(struct pci_dev *pdev) { }
static inline void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { }
+static inline int cxl_reset_function(struct pci_dev *pdev, bool probe)
+{
+ return -ENOTTY;
+}
#endif
#ifdef CONFIG_PCI_NPEM
diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
index ca3e77b72d87..7224857c7b30 100644
--- a/include/uapi/linux/pci_regs.h
+++ b/include/uapi/linux/pci_regs.h
@@ -1352,10 +1352,22 @@
#define PCI_DVSEC_CXL_CACHE_CAPABLE _BITUL(0)
#define PCI_DVSEC_CXL_MEM_CAPABLE _BITUL(2)
#define PCI_DVSEC_CXL_HDM_COUNT __GENMASK(5, 4)
+#define PCI_DVSEC_CXL_CACHE_WBI_CAPABLE _BITUL(6)
#define PCI_DVSEC_CXL_RST_CAPABLE _BITUL(7)
+#define PCI_DVSEC_CXL_RST_TIMEOUT __GENMASK(10, 8)
#define PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE _BITUL(11)
#define PCI_DVSEC_CXL_CTRL 0xC
+#define PCI_DVSEC_CXL_CACHE_ENABLE _BITUL(0)
#define PCI_DVSEC_CXL_MEM_ENABLE _BITUL(2)
+#define PCI_DVSEC_CXL_CTRL2 0x10
+#define PCI_DVSEC_CXL_DISABLE_CACHING _BITUL(0)
+#define PCI_DVSEC_CXL_INIT_CACHE_WBI _BITUL(1)
+#define PCI_DVSEC_CXL_INIT_CXL_RST _BITUL(2)
+#define PCI_DVSEC_CXL_RST_MEM_CLR_EN _BITUL(3)
+#define PCI_DVSEC_CXL_STATUS2 0x12
+#define PCI_DVSEC_CXL_CACHE_INV _BITUL(0)
+#define PCI_DVSEC_CXL_RST_DONE _BITUL(1)
+#define PCI_DVSEC_CXL_RST_ERR _BITUL(2)
#define PCI_DVSEC_CXL_RANGE_SIZE_HIGH(i) (0x18 + (i * 0x10))
#define PCI_DVSEC_CXL_RANGE_SIZE_LOW(i) (0x1C + (i * 0x10))
#define PCI_DVSEC_CXL_MEM_INFO_VALID _BITUL(0)
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (11 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
` (5 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Refuse reset unless enabled system-physical HDM ranges can be reserved
exclusively and CPU-cache invalidation is available. Invalidate before
reset and again before ending IOMMU exclusion, holding range reservations
until the second invalidation completes. A later patch places state
restoration before the second invalidation.
Userspace is expected to offline CXL.mem users before reset. The
reservation enforces this for claimed ranges: the reset fails while a
conflicting resource is claimed, and no one can claim the range during
the reset. It cannot see accesses by code that uses the range without
claiming it, which the offline expectation also covers.
Reject normalized-addressing decoders because their cached ranges are not
system physical addresses. Record Normalized Addressing in the cache when
region discovery sets it, since the cache is filled earlier. A reset that
takes its snapshot between endpoint probe and that update can still
miss it.
Ignore zero-size decoders because they map no address range. Refuse reset
when the device decodes memory through its DVSEC ranges instead of HDM
decoders, since the cache holds no range to reserve for that memory, and
when CXL.cache is enabled but the device cannot write back and invalidate
its cache, since the reset would discard dirty device-cache lines.
Hold the global cxl_rwsem.region write lock only for short windows: to
take the snapshot and reserve the ranges, to mark the HDM registers
offline right before the reset starts, and to clear both marks once the
second invalidation is done. In between, the reserved ranges stay held
and a per-device reset_pending mark makes cxl_decoder_commit() fail for
the device's endpoint decoders, so no new decode can come online during
the reset. A region torn down while the registers are offline updates
only the software state and the cache. The Memory Clear wait, which lasts
up to the timeout the device advertises (at most 100 seconds), and the
cache flushes run without the lock.
Allow the existing CXL_REGION_INVALIDATION_TEST option to bypass missing
CPU cache invalidation for test environments without a provider.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Check the CXL Reset preconditions in the prepare step, under the lock,
instead of early in cxl_reset_function(), and refuse with errors other
than -ENOTTY, so a refused reset no longer falls back to FLR or PM
reset (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=13
- Record Normalized Addressing even when the root decoder lookup fails,
and keep it when a decoder refresh on rebind rebuilds the cached
settings (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=13
- Take cxl_rwsem.dpa in the cache setters here, where reset first reads
the cache concurrently with endpoint probe (Li Ming)
https://lore.kernel.org/linux-cxl/a2226978-8200-41f9-9763-7916b946899b@zohomail.com/
- Do not hold the global cxl_rwsem.region lock across the reset. Take it
only to snapshot the cache and reserve the ranges, to mark the
registers offline right before the reset, and to clear the marks after
the second invalidation; while the new per-device reset_pending mark is
set, cxl_decoder_commit() fails for the device's endpoint decoders. The
Memory Clear wait and the cache flushes run without the lock (Dave)
https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
- Refuse reset when CXL.cache is enabled but the device cannot write back
and invalidate its cache, in both the CXL Reset and bus reset paths,
since reset would discard dirty device-cache lines (AI review)
- Do not access the HDM decoder registers while the device is in reset
when a region is torn down; update only the software state and the
cache. A separate regs_offline mark covers just that window, so a
teardown outside it still writes the registers (AI review)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=5944c6e61f14c0dd28d54537be15edb2cadb0e8b
---
drivers/cxl/Kconfig | 3 +-
drivers/cxl/core/hdm.c | 52 +++++-
drivers/cxl/core/hdm_state.c | 341 ++++++++++++++++++++++++++++++++++-
drivers/cxl/core/region.c | 15 ++
drivers/pci/cxl.c | 159 ++++++++++++++--
include/cxl/hdm.h | 56 +++++-
6 files changed, 600 insertions(+), 26 deletions(-)
diff --git a/drivers/cxl/Kconfig b/drivers/cxl/Kconfig
index 37bfa6689b04..2211f295e1ed 100644
--- a/drivers/cxl/Kconfig
+++ b/drivers/cxl/Kconfig
@@ -226,7 +226,8 @@ config CXL_REGION_INVALIDATION_TEST
invalidation mechanism. For example usage of wbinvd is restricted to
bare metal x86. However, for testing purposes toggling this option
can disable that data integrity safety and proceed with enabling
- regions when there might be conflicting contents in the CPU cache.
+ regions or performing CXL reset when there might be conflicting
+ contents in the CPU cache.
If unsure, or if this kernel is meant for production environments,
say N.
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index f88418177582..ebc9995eec1b 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -764,10 +764,31 @@ static void cxl_hdm_refresh_decoder(struct cxl_hdm *cxlhdm,
cxl_port_get_uport_pci_dev(cxlhdm->port);
if (!pdev)
return;
- cxl_hdm_cache_refresh_decoder(pdev, hdm,
+ /* Do not read the device while it is in reset. */
+ cxl_hdm_cache_refresh_decoder(pdev,
+ cxl_hdm_cache_is_regs_offline(pdev) ?
+ NULL : hdm,
to_cxl_endpoint_decoder(&cxld->dev));
}
+/*
+ * While the device is in CXL Reset or behind a secondary bus reset its
+ * registers must not be accessed. The reset clears the decoders anyway, and
+ * the restore uncommits any decoder the cache shows as disabled.
+ */
+static bool cxl_decoder_regs_offline(struct cxl_decoder *cxld)
+{
+ struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+
+ if (!is_endpoint_decoder(&cxld->dev))
+ return false;
+
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
+
+ return pdev && cxl_hdm_cache_is_regs_offline(pdev);
+}
+
static void
cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled,
struct cxl_endpoint_decoder_settings *settings)
@@ -818,6 +839,19 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
return -EBUSY;
}
+ /* Don't commit a new decoder between reset prepare and restore. */
+ if (is_endpoint_decoder(&cxld->dev)) {
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
+
+ if (pdev && cxl_hdm_cache_is_reset_pending(pdev)) {
+ dev_dbg(&port->dev,
+ "attempted to commit %s during reset\n",
+ dev_name(&cxld->dev));
+ return -EBUSY;
+ }
+ }
+
/*
* For endpoint decoders hosted on CXL memory devices that
* support the sanitize operation, make sure sanitize is not in-flight.
@@ -911,14 +945,16 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)
"%s: out of order reset, expected decoder%d.%d\n",
dev_name(&cxld->dev), port->id, port->commit_end);
- ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
- ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ if (!cxl_decoder_regs_offline(cxld)) {
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
- writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
- writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
- writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
- writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
+ writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
+ writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
+ writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
+ writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
+ }
cxld->config.flags &= ~CXL_DECODER_F_ENABLE;
cxl_hdm_refresh_decoder(cxlhdm, cxld);
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
index ddb69411de1c..497709bffff2 100644
--- a/drivers/cxl/core/hdm_state.c
+++ b/drivers/cxl/core/hdm_state.c
@@ -1,8 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/errno.h>
#include <linux/export.h>
#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/list.h>
+#include <linux/memregion.h>
+#include <linux/overflow.h>
#include <linux/pci.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
@@ -13,7 +19,8 @@
/*
* CXL state that must stay available while no CXL driver is bound: the CXL
- * locks and the per-device HDM cache filled during PCI enumeration.
+ * locks, the per-device HDM cache filled during PCI enumeration, and the
+ * region quiesce used around a reset.
*/
struct cxl_rwsem cxl_rwsem = {
.region = __RWSEM_INITIALIZER(cxl_rwsem.region),
@@ -23,6 +30,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core");
void cxl_hdm_cache_set_dvsec_ctrl(struct pci_dev *pdev, u16 ctrl)
{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
if (!pdev->hdm)
return;
@@ -42,6 +50,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_dvsec_ctrl, "cxl_core");
*/
void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl)
{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
if (pdev->hdm)
pdev->hdm->global_ctrl = ctrl;
}
@@ -50,7 +59,8 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_global_ctrl, "cxl_core");
/**
* cxl_hdm_cache_refresh_decoder() - update the PCI HDM cache from a decoder
* @pdev: PCI device that owns the HDM cache
- * @hdm: mapped HDM decoder register block, for the global control register
+ * @hdm: mapped HDM decoder register block, for the global control register,
+ * or NULL to leave the cached global control alone (the device is in reset)
* @cxled: endpoint decoder whose state is recorded
*
* Record the decoder's current configuration, or just its id when it is
@@ -64,6 +74,7 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
struct cxl_endpoint_decoder_settings *settings;
struct cxl_decoder *cxld = &cxled->cxld;
struct cxl_hdm_info *info;
+ unsigned long normalized;
u32 global_ctrl;
int id;
@@ -79,10 +90,13 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
}
/* Preserve the last good value when the device does not respond. */
- global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
- if (global_ctrl != U32_MAX)
- info->global_ctrl = global_ctrl;
+ if (hdm) {
+ global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+ if (global_ctrl != U32_MAX)
+ info->global_ctrl = global_ctrl;
+ }
settings = &info->settings[id];
+ normalized = settings->config.flags & CXL_DECODER_F_NORMALIZED_ADDRESSING;
/*
* A disabled decoder's software object may retain its old range and
@@ -94,6 +108,8 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
};
if (cxld->config.flags & CXL_DECODER_F_ENABLE) {
settings->config = cxld->config;
+ /* Region discovery sets this; a refresh on rebind must keep it. */
+ settings->config.flags |= normalized;
settings->skip = cxled->skip;
}
}
@@ -123,6 +139,93 @@ bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count)
}
EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_decoder_count_match, "cxl_core");
+/**
+ * cxl_hdm_cache_set_normalized() - record Normalized Addressing in the cache
+ * @pdev: endpoint that owns the cache
+ * @id: endpoint decoder id
+ *
+ * Normalized Addressing is determined during region discovery, after the
+ * cache was filled, so the cached decoder lacks it. Record only that flag: the
+ * decoder's software lock must not become a hardware Lock On Commit when the
+ * cache is restored.
+ */
+void cxl_hdm_cache_set_normalized(struct pci_dev *pdev, int id)
+{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (!pdev->hdm || id < 0 || id >= pdev->hdm->decoder_count)
+ return;
+
+ pdev->hdm->settings[id].config.flags |=
+ CXL_DECODER_F_NORMALIZED_ADDRESSING;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_normalized, "cxl_core");
+
+/**
+ * cxl_hdm_cache_set_reset_pending() - mark a device as between reset prepare
+ * and restore
+ * @pdev: endpoint that owns the cache
+ * @pending: set or clear the mark
+ *
+ * Callers hold cxl_rwsem.region for write, which also serializes this against
+ * cxl_decoder_commit(). While the mark is set, commits of the device's
+ * decoders fail, so no new decode can come online during the reset.
+ */
+void cxl_hdm_cache_set_reset_pending(struct pci_dev *pdev, bool pending)
+{
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (pdev->hdm)
+ pdev->hdm->reset_pending = pending;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_reset_pending, "cxl_core");
+
+/**
+ * cxl_hdm_cache_is_reset_pending() - check whether a reset of @pdev is pending
+ * @pdev: endpoint that owns the cache
+ *
+ * Return: true between cxl_hdm_cache_set_reset_pending(pdev, true) and the
+ * matching clear.
+ */
+bool cxl_hdm_cache_is_reset_pending(struct pci_dev *pdev)
+{
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ return pdev->hdm && pdev->hdm->reset_pending;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_is_reset_pending, "cxl_core");
+
+/**
+ * cxl_hdm_cache_set_regs_offline() - mark the HDM registers of a device unusable
+ * @pdev: endpoint that owns the cache
+ * @offline: set or clear the mark
+ *
+ * Set only while the device is in reset, which is shorter than the pending
+ * mark. Callers hold cxl_rwsem.region for write. While the mark is set, a
+ * decoder teardown updates the software state and the cache but leaves the
+ * registers alone; the restore then uncommits what the cache shows disabled.
+ */
+void cxl_hdm_cache_set_regs_offline(struct pci_dev *pdev, bool offline)
+{
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (pdev->hdm)
+ pdev->hdm->regs_offline = offline;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_regs_offline, "cxl_core");
+
+/**
+ * cxl_hdm_cache_is_regs_offline() - check whether the HDM registers are unusable
+ * @pdev: endpoint that owns the cache
+ *
+ * Return: true between cxl_hdm_cache_set_regs_offline(pdev, true) and the
+ * matching clear.
+ */
+bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev)
+{
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ return pdev->hdm && pdev->hdm->regs_offline;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_is_regs_offline, "cxl_core");
+
/**
* cxl_hdm_cache_release() - free cached HDM state
* @slot: cache pointer owned by the device
@@ -135,3 +238,231 @@ void cxl_hdm_cache_release(struct cxl_hdm_info **slot)
kfree(*slot);
*slot = NULL;
}
+
+/**
+ * cxl_hdm_snapshot_is_dvsec_decoded() - check a snapshot for DVSEC-decoded memory
+ * @info: snapshot from cxl_hdm_cache_snapshot()
+ *
+ * Judge the snapshot itself, not the live cache, which endpoint probe and
+ * unbind can change after the snapshot was taken.
+ *
+ * Return: true if CXL.mem is enabled while HDM decoders are globally disabled.
+ */
+bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info)
+{
+ return (info->dvsec_ctrl & PCI_DVSEC_CXL_MEM_ENABLE) &&
+ !(info->global_ctrl & CXL_HDM_DECODER_ENABLE);
+}
+
+/**
+ * cxl_hdm_cache_snapshot() - copy the published HDM state
+ * @slot: cache pointer owned by the device
+ *
+ * Return: a copy the caller frees with kfree(), ERR_PTR(-ENXIO) when nothing
+ * is published, or another ERR_PTR() on failure.
+ */
+struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot)
+{
+ struct cxl_hdm_info *info, *copy;
+ size_t size;
+
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ info = *slot;
+ if (!info)
+ return ERR_PTR(-ENXIO);
+ if (info->decoder_count <= 0)
+ return ERR_PTR(-EINVAL);
+
+ size = struct_size(info, settings, info->decoder_count);
+ if (size == SIZE_MAX)
+ return ERR_PTR(-EOVERFLOW);
+
+ copy = kmemdup(info, size, GFP_KERNEL);
+ if (!copy)
+ return ERR_PTR(-ENOMEM);
+
+ return copy;
+}
+
+void cxl_region_quiesce_lock(void)
+ __acquires(cxl_region_quiesce)
+ __context_unsafe(token maps to cxl_rwsem.region)
+{
+ down_write(&cxl_rwsem.region);
+ __acquire(cxl_region_quiesce);
+}
+
+void cxl_region_quiesce_unlock(void)
+ __releases(cxl_region_quiesce)
+ __context_unsafe(token maps to cxl_rwsem.region)
+{
+ __release(cxl_region_quiesce);
+ up_write(&cxl_rwsem.region);
+}
+
+/*
+ * Bound the range twice: request_mem_region() takes resource_size_t while
+ * cpu_cache_invalidate_memregion() takes size_t, and the two differ on
+ * 32-bit builds with CONFIG_PHYS_ADDR_T_64BIT. range_len() can also reach
+ * RESOURCE_SIZE_MAX + 1 for a full-width range, and wraps to zero when
+ * resource_size_t is 64-bit, which the !len test catches.
+ */
+static int cxl_hdm_range_validate(const struct range *hpa_range)
+{
+ u64 len = range_len(hpa_range);
+
+ if (!len)
+ return -EINVAL;
+ if (hpa_range->end > RESOURCE_SIZE_MAX || len > RESOURCE_SIZE_MAX ||
+ len > SIZE_MAX)
+ return -EOVERFLOW;
+
+ return 0;
+}
+
+static int cxl_hdm_range_add(struct cxl_hdm_quiesce *q,
+ const struct range *hpa_range)
+{
+ struct cxl_hdm_range *range, *next, *new_range;
+ int rc;
+
+ rc = cxl_hdm_range_validate(hpa_range);
+ if (rc)
+ return rc;
+
+ list_for_each_entry(range, &q->ranges, list)
+ if (range_contains(&range->hpa_range, hpa_range))
+ return 0;
+
+ new_range = kzalloc_obj(*new_range);
+ if (!new_range)
+ return -ENOMEM;
+
+ new_range->hpa_range = *hpa_range;
+ new_range->len = range_len(hpa_range);
+
+ list_for_each_entry_safe(range, next, &q->ranges, list) {
+ if (range_contains(hpa_range, &range->hpa_range)) {
+ list_del(&range->list);
+ kfree(range);
+ }
+ }
+ list_add_tail(&new_range->list, &q->ranges);
+
+ return 0;
+}
+
+static int cxl_hdm_ranges_collect(struct cxl_hdm_quiesce *q,
+ const struct cxl_hdm_info *snapshot)
+{
+ int rc;
+
+ for (int i = 0; i < snapshot->decoder_count; i++) {
+ const struct cxl_decoder_config *config =
+ &snapshot->settings[i].config;
+
+ /* A committed zero-size decoder maps no HPA. */
+ if (!(config->flags & CXL_DECODER_F_ENABLE) ||
+ !range_len(&config->hpa_range))
+ continue;
+
+ if (config->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
+ return -EOPNOTSUPP;
+
+ rc = cxl_hdm_range_add(q, &config->hpa_range);
+ if (rc)
+ return rc;
+ }
+
+ return 0;
+}
+
+static int cxl_hdm_ranges_request(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range;
+
+ lockdep_assert_held_write(&cxl_rwsem.region);
+
+ list_for_each_entry(range, &q->ranges, list) {
+ const struct range *hpa_range = &range->hpa_range;
+
+ range->res = request_mem_region(hpa_range->start, range->len,
+ "cxl_reset");
+ if (!range->res)
+ return -EBUSY;
+ }
+
+ return 0;
+}
+
+static int cxl_hdm_ranges_invalidate(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range;
+ int rc = 0;
+
+ if (!cpu_cache_has_invalidate_memregion() &&
+ IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
+ pr_info_once("CXL reset: bypassing CPU cache invalidation for testing\n");
+ return 0;
+ }
+
+ list_for_each_entry(range, &q->ranges, list) {
+ const struct range *hpa_range = &range->hpa_range;
+ int rc2;
+
+ rc2 = cpu_cache_invalidate_memregion(hpa_range->start, range->len);
+ rc = rc ?: rc2;
+ }
+
+ return rc;
+}
+
+/**
+ * cxl_hdm_quiesce_prepare() - take the HPA ranges of a device out of use
+ * @q: quiesce state to initialize
+ * @snapshot: HDM state from cxl_hdm_cache_snapshot()
+ *
+ * Reserve every enabled system-physical range in @snapshot exclusively. The
+ * caller then drops cxl_rwsem.region and calls cxl_hdm_quiesce_flush() to
+ * write back and invalidate CPU caches for the ranges. Call
+ * cxl_hdm_quiesce_destroy() afterwards whether or not this succeeds.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int cxl_hdm_quiesce_prepare(struct cxl_hdm_quiesce *q,
+ const struct cxl_hdm_info *snapshot)
+{
+ int rc;
+
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ INIT_LIST_HEAD(&q->ranges);
+
+ if (!cpu_cache_has_invalidate_memregion() &&
+ !IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST))
+ return -ENXIO;
+
+ rc = cxl_hdm_ranges_collect(q, snapshot);
+ if (rc)
+ return rc;
+
+ return cxl_hdm_ranges_request(q);
+}
+
+/* Evict lines fetched during reset before DMA exclusion ends. */
+int cxl_hdm_quiesce_flush(struct cxl_hdm_quiesce *q)
+{
+ return cxl_hdm_ranges_invalidate(q);
+}
+
+void cxl_hdm_quiesce_destroy(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range, *next;
+
+ list_for_each_entry_safe(range, next, &q->ranges, list) {
+ list_del(&range->list);
+ if (range->res)
+ release_mem_region(range->hpa_range.start,
+ resource_size(range->res));
+ kfree(range);
+ }
+}
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 5bdaeeb30f4a..314347d1911e 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -13,6 +13,7 @@
#include <linux/idr.h>
#include <linux/memory-tiers.h>
#include <linux/string_choices.h>
+#include <cxl/hdm.h>
#include <cxlmem.h>
#include <cxl.h>
#include "core.h"
@@ -3852,6 +3853,20 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
get_cxl_root_decoder(cxled, &ctx);
+ /*
+ * Translation setup may have marked the decoder as Normalized
+ * Addressing, after the PCI cache was filled. Record it for reset,
+ * even when the root decoder lookup failed.
+ */
+ if (cxled->cxld.config.flags & CXL_DECODER_F_NORMALIZED_ADDRESSING) {
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(cxled_to_port(cxled));
+
+ if (pdev)
+ cxl_hdm_cache_set_normalized(pdev,
+ cxled->cxld.config.id);
+ }
+
if (IS_ERR(cxlrd))
return PTR_ERR(cxlrd);
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 4d6cfabab2c0..c90715c5a6d5 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -15,6 +15,7 @@
#include <linux/iommu.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
+#include <linux/memregion.h>
#include <linux/pci.h>
#include <linux/slab.h>
@@ -23,6 +24,13 @@
#include "pci.h"
+struct cxl_pci_cache {
+ struct cxl_hdm_quiesce quiesce;
+ u16 saved_ctrl2;
+ /* A cache-policy write was attempted and may need undoing. */
+ bool cache_disabled;
+};
+
/*
* Read a config word. A failed read or an all-ones value means the device is
* not responding, so return an errno rather than a value to be interpreted.
@@ -229,6 +237,93 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
cxl_hdm_cache_release(&pdev->hdm);
}
+/*
+ * Prepare window. Hold the region lock only long enough to capture the HDM
+ * state, reserve its ranges and mark the device as resetting. The caller does
+ * the host and device cache maintenance after this returns, without the lock.
+ *
+ * Return: snapshot to be freed with kfree(), or an ERR_PTR() with the lock
+ * released, no reservation held and nothing marked.
+ */
+static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
+ int dvsec, u16 cap,
+ struct cxl_pci_cache *cache)
+{
+ struct cxl_hdm_info *snapshot;
+ int rc;
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
+ &cache->saved_ctrl2);
+ if (rc)
+ return ERR_PTR(rc);
+
+ cxl_region_quiesce_lock();
+ snapshot = cxl_hdm_cache_snapshot(&pdev->hdm);
+ if (IS_ERR(snapshot)) {
+ pci_err(pdev, "CXL HDM decoder state unavailable: %ld\n",
+ PTR_ERR(snapshot));
+ cxl_region_quiesce_unlock();
+ return snapshot;
+ }
+ /* Judge the snapshot: the live cache can change after it was taken. */
+ if (cxl_hdm_snapshot_is_dvsec_decoded(snapshot)) {
+ rc = -EOPNOTSUPP;
+ goto free_snapshot;
+ }
+ /*
+ * With CXL.cache enabled the device cache may hold dirty lines that only
+ * Cache Writeback and Invalidate can push back to memory. Reset would
+ * discard them, so refuse when the device cannot do it.
+ */
+ if ((snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) &&
+ !(cap & PCI_DVSEC_CXL_CACHE_WBI_CAPABLE)) {
+ pci_err(pdev, "CXL reset refused: CXL.cache is enabled but the device cannot write back and invalidate its cache\n");
+ rc = -EOPNOTSUPP;
+ goto free_snapshot;
+ }
+
+ rc = cxl_hdm_quiesce_prepare(&cache->quiesce, snapshot);
+ if (rc) {
+ pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc);
+ cxl_hdm_quiesce_destroy(&cache->quiesce);
+ goto free_snapshot;
+ }
+
+ /* From here until restore, no decoder of this device can commit. */
+ cxl_hdm_cache_set_reset_pending(pdev, true);
+ cxl_region_quiesce_unlock();
+ return snapshot;
+
+free_snapshot:
+ kfree(snapshot);
+ cxl_region_quiesce_unlock();
+ return ERR_PTR(rc);
+}
+
+/*
+ * Offline window. Called right before the device is reset, so that a decoder
+ * teardown from here on does not access the registers of a device in reset. A
+ * teardown before this point still writes them.
+ */
+static void cxl_reset_regs_offline(struct pci_dev *pdev)
+{
+ cxl_region_quiesce_lock();
+ cxl_hdm_cache_set_regs_offline(pdev, true);
+ cxl_region_quiesce_unlock();
+}
+
+/*
+ * Unmark window. Once the host cache flush is done, or the reset is abandoned,
+ * decoders of this device may commit again.
+ */
+static void cxl_reset_hdm_unmark(struct pci_dev *pdev)
+{
+ cxl_region_quiesce_lock();
+ cxl_hdm_cache_set_regs_offline(pdev, false);
+ cxl_hdm_cache_set_reset_pending(pdev, false);
+ cxl_region_quiesce_unlock();
+}
+
/*
* CXL r4.0 sec 9.7.2 defines the reset completion timeout encodings.
* Sec 9.7.3 leaves config-space access behavior undefined for 100 ms after
@@ -333,6 +428,21 @@ static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec,
return rc;
}
+/* Shared host invalidation and device cache writeback, without the region lock. */
+static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap,
+ struct cxl_pci_cache *cache)
+{
+ int rc;
+
+ rc = cxl_hdm_quiesce_flush(&cache->quiesce);
+ if (rc)
+ return rc;
+
+ /* Track an attempted change so partial failures still restore policy. */
+ cache->cache_disabled = true;
+ return cxl_reset_disable_cache(pdev, dvsec, cap);
+}
+
static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap)
{
unsigned long deadline;
@@ -390,21 +500,25 @@ static int cxl_clear_memory(struct pci_dev *pdev, int dvsec, bool initiate)
PCI_DVSEC_CXL_RST_MEM_CLR_EN);
}
-static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
+static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap,
+ struct cxl_hdm_quiesce *q)
{
int rc, rc2;
if (!pci_wait_for_pending_transaction(pdev)) {
pci_err(pdev, "timed out waiting for pending transactions\n");
+ cxl_reset_hdm_unmark(pdev);
return -ETIMEDOUT;
}
rc = pci_dev_reset_iommu_prepare(pdev);
if (rc) {
pci_err(pdev, "failed to stop IOMMU for CXL reset: %d\n", rc);
+ cxl_reset_hdm_unmark(pdev);
return rc;
}
+ cxl_reset_regs_offline(pdev);
rc = cxl_clear_memory(pdev, dvsec, true);
if (!rc)
rc = cxl_reset_wait_done(pdev, dvsec, cap);
@@ -415,25 +529,48 @@ static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
pci_err(pdev, "failed to clear CXL Reset Memory Clear: %d\n", rc2);
rc = rc ?: rc2;
+ rc2 = cxl_hdm_quiesce_flush(q);
+ if (rc2)
+ pci_err(pdev, "failed to invalidate CPU cache after CXL reset: %d\n", rc2);
+ rc = rc ?: rc2;
+ cxl_reset_hdm_unmark(pdev);
+
pci_dev_reset_iommu_done(pdev);
return rc;
}
+/*
+ * Three short region-lock windows surround the reset: cxl_reset_hdm_prepare()
+ * captures the state and reserves the ranges, cxl_reset_regs_offline() keeps
+ * region teardown off the registers just before the reset starts, and
+ * cxl_reset_hdm_unmark() lets decoders commit again after the host flush.
+ * The wait for Memory Clear, up to 100 s, and the flush run without the lock.
+ */
static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
{
- u16 saved_ctrl2;
+ struct cxl_pci_cache cache = {};
int rc, rc2;
- rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
- &saved_ctrl2);
- if (rc)
- return rc;
-
- rc = cxl_reset_disable_cache(pdev, dvsec, cap);
- if (!rc)
- rc = __cxl_reset_execute(pdev, dvsec, cap);
+ struct cxl_hdm_info *snapshot __free(kfree) =
+ cxl_reset_hdm_prepare(pdev, dvsec, cap, &cache);
+ if (IS_ERR(snapshot))
+ return PTR_ERR(snapshot);
+
+ rc = cxl_reset_cache_prepare(pdev, dvsec, cap, &cache);
+ if (!rc) {
+ rc = __cxl_reset_execute(pdev, dvsec, cap, &cache.quiesce);
+ } else {
+ /* Nothing was reset: clear the mark and drop the reservation. */
+ cxl_reset_hdm_unmark(pdev);
+ if (!cache.cache_disabled) {
+ pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc);
+ cxl_hdm_quiesce_destroy(&cache.quiesce);
+ return rc;
+ }
+ }
/* Restore cache policy after any attempt to disable caching. */
- rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, saved_ctrl2);
+ rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2);
+ cxl_hdm_quiesce_destroy(&cache.quiesce);
return rc ?: rc2;
}
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index e424ccfcf152..eac5600d2849 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -3,10 +3,14 @@
#ifndef __CXL_HDM_H__
#define __CXL_HDM_H__
+#include <linux/list.h>
#include <linux/types.h>
+
#include <cxl/cxl.h>
+struct device;
struct pci_dev;
+struct resource;
struct cxl_endpoint_decoder;
/**
@@ -29,9 +33,12 @@ struct cxl_endpoint_decoder_settings {
* @dvsec: config space offset of the CXL Device DVSEC
* @dvsec_ctrl: CXL DVSEC control register
* @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state
+ * @reset_pending: a reset of the device is between prepare and unmark
+ * @regs_offline: the device is being reset; do not access its HDM registers
* @settings: per-endpoint-decoder programming state
*
- * Publication through pci_dev.hdm is serialized by the PCI device lock.
+ * Publication through pci_dev.hdm is serialized by the PCI device lock. All
+ * mutable fields are protected by cxl_rwsem.dpa.
*/
struct cxl_hdm_info {
int decoder_count;
@@ -42,6 +49,8 @@ struct cxl_hdm_info {
u16 dvsec;
u16 dvsec_ctrl;
bool dvsec_ctrl_valid;
+ bool reset_pending;
+ bool regs_offline;
struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count);
};
@@ -56,6 +65,51 @@ void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl);
void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
struct cxl_endpoint_decoder *cxled);
bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count);
+void cxl_hdm_cache_set_normalized(struct pci_dev *pdev, int id);
+void cxl_hdm_cache_set_reset_pending(struct pci_dev *pdev, bool pending);
+bool cxl_hdm_cache_is_reset_pending(struct pci_dev *pdev);
+void cxl_hdm_cache_set_regs_offline(struct pci_dev *pdev, bool offline);
+bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev);
void cxl_hdm_cache_release(struct cxl_hdm_info **slot);
+bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info);
+struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot);
+
+/*
+ * Region quiesce blocks CXL region commit and teardown, and with them decoder
+ * reprogramming, while reset handling captures or restores HDM decoder state.
+ */
+token_context_lock(cxl_region_quiesce);
+
+/**
+ * struct cxl_hdm_range - HPA range held exclusively across a reset
+ * @list: entry in &cxl_hdm_quiesce.ranges
+ * @hpa_range: system physical address range decoded by the device
+ * @len: length of @hpa_range
+ * @res: reservation of @hpa_range, once requested
+ */
+struct cxl_hdm_range {
+ struct list_head list;
+ struct range hpa_range;
+ u64 len;
+ struct resource *res;
+};
+
+/**
+ * struct cxl_hdm_quiesce - HPA ranges held exclusively across a reset
+ * @ranges: list of &struct cxl_hdm_range, see cxl_hdm_quiesce_prepare()
+ */
+struct cxl_hdm_quiesce {
+ struct list_head ranges;
+};
+
+void cxl_region_quiesce_lock(void)
+ __acquires(cxl_region_quiesce);
+void cxl_region_quiesce_unlock(void)
+ __releases(cxl_region_quiesce);
+int cxl_hdm_quiesce_prepare(struct cxl_hdm_quiesce *q,
+ const struct cxl_hdm_info *snapshot)
+ __must_hold(cxl_region_quiesce);
+int cxl_hdm_quiesce_flush(struct cxl_hdm_quiesce *q);
+void cxl_hdm_quiesce_destroy(struct cxl_hdm_quiesce *q);
#endif /* __CXL_HDM_H__ */
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (12 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
` (4 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
The per-device PCI reset interface must not reset sibling functions.
Reject cxl_reset and cxl_bus for VFs, multifunction devices, including
function 0 of an ARI device that has other functions, and PFs with enabled
VFs.
Suggested-by: Alex Williamson <alwilliamson@nvidia.com>
Link: https://lore.kernel.org/linux-cxl/20260626160846.426e71b6@nvidia.com/
Link: https://lore.kernel.org/linux-cxl/6a50441e2bb5e_3cabcb1003e@djbw-dev.notmuch/
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
---
Changes in v18:
- Also reject function 0 of an ARI device when other functions share the
bus, since ->multifunction is not set for it (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=14
- Check for ARI siblings only when the reset executes. While reset methods
are probed, the device is not on its bus list yet and its siblings are not
enumerated, so the check could not tell whether siblings exist and rejected
cxl_reset and cxl_bus for the first device on an ARI-forwarding bus. Found
while testing on an ARM64 system with a CXL Type 2 device.
- Keep Bjorn's Acked-by and Richard's Reviewed-by, since only the point
where the sibling check runs changed
---
drivers/pci/cxl.c | 10 ++++++++++
drivers/pci/pci.c | 13 +++++++++++++
2 files changed, 23 insertions(+)
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index c90715c5a6d5..0c60f6d7bb4f 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -579,6 +579,16 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
u16 dvsec, cap, ctrl;
int rc;
+ /*
+ * ->multifunction is not set for function 0 of an ARI device, so look
+ * for siblings on the bus. Skip this when probing: @pdev is not on the
+ * bus list yet and its siblings are not enumerated.
+ */
+ if (pdev->is_virtfn || pdev->multifunction || pci_num_vf(pdev) ||
+ (!probe && pci_ari_enabled(pdev->bus) &&
+ !list_is_singular(&pdev->bus->devices)))
+ return -ENOTTY;
+
rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
if (rc)
return rc;
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7c6f2aba8144..5a8338340d23 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5064,6 +5064,19 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
u16 dvsec, reg, val;
int rc;
+ /*
+ * Reject VFs and multifunction devices before walking the bridge's
+ * capabilities: every VF behind the same bridge would repeat the walk.
+ *
+ * ->multifunction is not set for function 0 of an ARI device, so look
+ * for siblings on the bus. Skip this when probing: @dev is not on the
+ * bus list yet and its siblings are not enumerated.
+ */
+ if (dev->is_virtfn || dev->multifunction || pci_num_vf(dev) ||
+ (!probe && pci_ari_enabled(dev->bus) &&
+ !list_is_singular(&dev->bus->devices)))
+ return -ENOTTY;
+
bridge = pci_upstream_bridge(dev);
if (!bridge)
return -ENOTTY;
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (13 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
` (3 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Restore the PCI configuration needed for HDM access, followed by cached
HDM decoder and CXL Device DVSEC state. Set Lock On Commit before
recommitting a cleared locked decoder; leave surviving locked state
unchanged.
Stop at the first restoration failure and preserve a disabled PCI command
snapshot. Keep IOMMU exclusion active through restoration and post-reset
cache invalidation.
Program the decoders from built-in CXL code that takes only a mapped
register block, and keep PCI state handling in drivers/pci/cxl.c.
The restore helper structure and diagnostics follow Dave Jiang's
reference implementation on his cxl-type2-reset branch.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Restore the state in a short region-lock window of its own,
cxl_reset_hdm_restore(), now that the lock is no longer held across
the reset, and let a region teardown write the registers again there
(Dave)
https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
- Follow the bus reset rules when a CXL Reset fails: save the disabled
state if the cache policy cannot be restored after an aborted reset,
and keep caching off after a reset that was started and failed
(Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=15
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18
- Restore the DVSEC Control with CXL.cache off and turn it on only after
the host caches are flushed and the cache policy is restored, so the
device does not cache again before the flush (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=81fff5893a3140361cac625b09ada798afb92e03
---
drivers/cxl/core/core.h | 2 +-
drivers/cxl/core/hdm.c | 2 +-
drivers/cxl/core/hdm_regs.c | 177 +++++++++++++++++++++++--
drivers/pci/cxl.c | 249 ++++++++++++++++++++++++++++++++++--
include/cxl/hdm.h | 2 +
5 files changed, 403 insertions(+), 29 deletions(-)
diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index 715097894144..7aaa4e305a4b 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -145,7 +145,7 @@ void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *setti
void __iomem *hdm);
void cxl_commit_start_switch(const struct cxl_decoder_config *config,
u64 targets, void __iomem *hdm);
-int cxld_await_commit(void __iomem *hdm, int id);
+int cxld_await_commit(void __iomem *hdm, int id, bool committed);
int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,
u32 ctrl, u64 base, u64 size);
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index ebc9995eec1b..15ad06c6b5c5 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -874,7 +874,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
scoped_guard(rwsem_read, &cxl_rwsem.dpa)
setup_hw_decoder(cxld, hdm);
- rc = cxld_await_commit(hdm, cxld->config.id);
+ rc = cxld_await_commit(hdm, cxld->config.id, true);
if (rc) {
dev_dbg(&port->dev, "%s: error %d committing decoder\n",
dev_name(&cxld->dev), rc);
diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
index cc99ae83d1cd..358e2ba7495d 100644
--- a/drivers/cxl/core/hdm_regs.c
+++ b/drivers/cxl/core/hdm_regs.c
@@ -52,7 +52,7 @@ static void cxld_set_type(const struct cxl_decoder_config *config, u32 *ctrl)
* clock skew and other marginal behavior
*/
#define COMMIT_TIMEOUT_MS 20
-int cxld_await_commit(void __iomem *hdm, int id)
+int cxld_await_commit(void __iomem *hdm, int id, bool committed)
{
u32 ctrl;
int i;
@@ -64,7 +64,7 @@ int cxld_await_commit(void __iomem *hdm, int id)
writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
return -EIO;
}
- if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+ if (!!(ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED) == committed)
return 0;
fsleep(1000);
}
@@ -92,8 +92,12 @@ static u32 cxl_hdm_write_range(void __iomem *hdm,
return ctrl;
}
-void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
- void __iomem *hdm)
+/*
+ * Program every endpoint decoder register except Commit and Lock On Commit.
+ * Return the control value to write; Commit is not set in it.
+ */
+static u32 cxl_hdm_program_endpoint(const struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm)
{
int id = settings->config.id;
u32 ctrl;
@@ -101,7 +105,17 @@ void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *setti
ctrl = cxl_hdm_write_range(hdm, &settings->config);
writel(upper_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_HIGH(id));
writel(lower_32_bits(settings->skip), hdm + CXL_HDM_DECODER0_SKIP_LOW(id));
- writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+
+ return ctrl & ~CXL_HDM_DECODER0_CTRL_COMMIT;
+}
+
+void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *settings,
+ void __iomem *hdm)
+{
+ u32 ctrl = cxl_hdm_program_endpoint(settings, hdm);
+
+ writel(ctrl | CXL_HDM_DECODER0_CTRL_COMMIT,
+ hdm + CXL_HDM_DECODER0_CTRL_OFFSET(settings->config.id));
}
EXPORT_SYMBOL_FOR_MODULES(cxl_commit_start_endpoint, "cxl_core");
@@ -220,8 +234,7 @@ int cxl_hdm_decoder_count_mapped(struct device *dev, void __iomem *hdm,
}
/* Read one endpoint decoder; a disabled decoder keeps only its id. */
-static int cxl_hdm_read_decoder_settings(struct device *dev,
- struct cxl_endpoint_decoder_settings *settings,
+static int cxl_hdm_read_decoder_settings(struct cxl_endpoint_decoder_settings *settings,
void __iomem *hdm, int id)
{
u64 lo, hi;
@@ -230,12 +243,8 @@ static int cxl_hdm_read_decoder_settings(struct device *dev,
ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
rc = cxl_hdm_read_decoder(&settings->config, hdm, id, ctrl);
- if (rc) {
- dev_err(dev,
- "CXL HDM decoder %d has invalid configuration: %d\n",
- id, rc);
+ if (rc)
return rc;
- }
if (!(settings->config.flags & CXL_DECODER_F_ENABLE)) {
*settings = (struct cxl_endpoint_decoder_settings) {
@@ -273,10 +282,13 @@ int cxl_hdm_read_state(struct device *dev, void __iomem *hdm,
return -ENODEV;
for (int i = 0; i < info->decoder_count; i++) {
- rc = cxl_hdm_read_decoder_settings(dev, &info->settings[i],
+ rc = cxl_hdm_read_decoder_settings(&info->settings[i],
hdm, i);
- if (rc)
+ if (rc) {
+ dev_err(dev, "CXL HDM decoder %d has invalid configuration: %d\n",
+ i, rc);
return rc;
+ }
}
/*
@@ -292,3 +304,140 @@ int cxl_hdm_read_state(struct device *dev, void __iomem *hdm,
return 0;
}
+
+static int cxl_hdm_decoder_uncommit(void __iomem *hdm, int id, bool *retained)
+{
+ u32 ctrl;
+
+ *retained = false;
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+ if (ctrl == U32_MAX)
+ return -EIO;
+ if (ctrl & CXL_HDM_DECODER0_CTRL_COMMIT_ERROR)
+ return -EIO;
+
+ if (ctrl & CXL_HDM_DECODER0_CTRL_LOCK) {
+ if (ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED) {
+ *retained = true;
+ return 0;
+ }
+
+ return -EIO;
+ }
+
+ if (!(ctrl & CXL_HDM_DECODER0_CTRL_COMMITTED))
+ return 0;
+
+ ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
+
+ return cxld_await_commit(hdm, id, false);
+}
+
+static bool
+cxl_hdm_decoder_settings_match(const struct cxl_endpoint_decoder_settings *saved,
+ const struct cxl_endpoint_decoder_settings *active)
+{
+ const struct cxl_decoder_config *a = &saved->config;
+ const struct cxl_decoder_config *b = &active->config;
+
+ return a->id == b->id &&
+ a->hpa_range.start == b->hpa_range.start &&
+ a->hpa_range.end == b->hpa_range.end &&
+ a->interleave_ways == b->interleave_ways &&
+ a->interleave_granularity == b->interleave_granularity &&
+ a->target_type == b->target_type &&
+ (a->flags & CXL_DECODER_F_RESET_MASK) ==
+ (b->flags & CXL_DECODER_F_RESET_MASK) &&
+ saved->skip == active->skip;
+}
+
+static int
+cxl_hdm_restore_decoder(void __iomem *hdm,
+ const struct cxl_endpoint_decoder_settings *settings)
+{
+ const struct cxl_decoder_config *config = &settings->config;
+ bool retained;
+ u32 ctrl;
+ int rc;
+
+ rc = cxl_hdm_decoder_uncommit(hdm, config->id, &retained);
+ if (rc)
+ return rc;
+
+ if (retained) {
+ struct cxl_endpoint_decoder_settings active;
+
+ rc = cxl_hdm_read_decoder_settings(&active, hdm, config->id);
+ if (rc)
+ return rc;
+ if (!cxl_hdm_decoder_settings_match(settings, &active))
+ return -EIO;
+ return 0;
+ }
+
+ if (!(config->flags & CXL_DECODER_F_ENABLE))
+ return 0;
+
+ /*
+ * Configure every decoder register, then set Lock On Commit, then set
+ * Commit, as CXL describes. Setting Lock On Commit before the base and
+ * size are written can make a device drop those writes.
+ */
+ ctrl = cxl_hdm_program_endpoint(settings, hdm);
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(config->id));
+ if (config->flags & CXL_DECODER_F_LOCK) {
+ ctrl |= CXL_HDM_DECODER0_CTRL_LOCK;
+ writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(config->id));
+ }
+ writel(ctrl | CXL_HDM_DECODER0_CTRL_COMMIT,
+ hdm + CXL_HDM_DECODER0_CTRL_OFFSET(config->id));
+ rc = cxld_await_commit(hdm, config->id, true);
+ if (rc)
+ return rc;
+
+ if (!(config->flags & CXL_DECODER_F_LOCK))
+ return 0;
+
+ ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(config->id));
+ if (ctrl == U32_MAX || !(ctrl & CXL_HDM_DECODER0_CTRL_LOCK))
+ return -EIO;
+
+ return 0;
+}
+
+/**
+ * cxl_hdm_restore_state() - reprogram endpoint HDM decoders from a snapshot
+ * @hdm: mapped HDM decoder capability
+ * @snapshot: state from cxl_hdm_cache_snapshot()
+ *
+ * Restore global control, then recommit each enabled decoder in order. Leave
+ * a locked decoder that kept its programming across the reset unchanged,
+ * after checking that it still matches @snapshot.
+ *
+ * Return: 0 on success, or the first failure.
+ */
+int cxl_hdm_restore_state(void __iomem *hdm,
+ const struct cxl_hdm_info *snapshot)
+{
+ int count;
+ int rc;
+
+ count = cxl_hdm_decoder_count(readl(hdm + CXL_HDM_DECODER_CAP_OFFSET));
+ if (count != snapshot->decoder_count)
+ return -ENXIO;
+
+ /* Restore global HDM control before committing individual decoders. */
+ writel(snapshot->global_ctrl, hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+
+ for (int i = 0; i < snapshot->decoder_count; i++) {
+ rc = cxl_hdm_restore_decoder(hdm, &snapshot->settings[i]);
+ if (rc)
+ return rc;
+ }
+
+ /* Flush posted HDM writes before restoring protocol enables. */
+ readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+
+ return 0;
+}
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 0c60f6d7bb4f..1053aa2f6607 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
/*
- * CXL reset support in the PCI core. Cache the HDM decoder state of a CXL
- * memory device at enumeration, so it can be restored after a reset without
- * a CXL driver, and implement CXL Device Reset. HDM register formats and
+ * CXL reset support in the PCI core: cache the HDM decoder state of a CXL
+ * memory device at enumeration, implement CXL Device Reset, and restore the
+ * cached state afterwards without a CXL driver. HDM register formats and
* programming live in drivers/cxl.
*/
#include <linux/bitfield.h>
@@ -16,6 +16,7 @@
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/memregion.h>
+#include <linux/overflow.h>
#include <linux/pci.h>
#include <linux/slab.h>
@@ -237,6 +238,183 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
cxl_hdm_cache_release(&pdev->hdm);
}
+static void __iomem *cxl_pci_hdm_ioremap_current(struct pci_dev *pdev,
+ int bar,
+ resource_size_t offset,
+ resource_size_t size)
+{
+ struct resource *res;
+ resource_size_t hdm_start;
+ void __iomem *hdm;
+
+ if (bar < 0 || bar >= PCI_STD_NUM_BARS || !size)
+ return IOMEM_ERR_PTR(-EINVAL);
+ if (!cxl_pci_bar_assigned(pdev, bar))
+ return IOMEM_ERR_PTR(-ENODEV);
+
+ res = &pdev->resource[bar];
+ if (offset > resource_size(res) || size > resource_size(res) - offset)
+ return IOMEM_ERR_PTR(-ENODEV);
+ if (check_add_overflow(res->start, offset, &hdm_start))
+ return IOMEM_ERR_PTR(-EOVERFLOW);
+
+ hdm = ioremap(hdm_start, size);
+ if (!hdm) {
+ pci_err(pdev, "failed to remap CXL HDM decoder registers\n");
+ return IOMEM_ERR_PTR(-ENOMEM);
+ }
+
+ return hdm;
+}
+
+static void cxl_restore_pci_state_for_hdm_restore(struct pci_dev *pdev,
+ u16 *command)
+{
+ u32 saved_config = pdev->saved_config_space[PCI_COMMAND / 4];
+ u32 restore_config;
+
+ device_lock_assert(&pdev->dev);
+ *command = saved_config & 0xffff;
+
+ /*
+ * Restore BARs and the related PCI state needed to reach HDM MMIO.
+ * Temporarily alter the saved command value so that
+ * pci_restore_state() enables MSE but cannot enable BME or INTx before
+ * CXL state restoration completes. Preserve the original saved value
+ * for the outer PCI reset path.
+ */
+ restore_config = saved_config | PCI_COMMAND_MEMORY |
+ PCI_COMMAND_INTX_DISABLE;
+ restore_config &= ~PCI_COMMAND_MASTER;
+
+ pdev->saved_config_space[PCI_COMMAND / 4] = restore_config;
+ pci_restore_state(pdev);
+ pdev->saved_config_space[PCI_COMMAND / 4] = saved_config;
+}
+
+static int cxl_restore_dvsec_ctrl(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot)
+{
+ int rc;
+
+ if (!snapshot->dvsec_ctrl_valid) {
+ pci_err(pdev, "CXL DVSEC control snapshot is invalid\n");
+ return -ENXIO;
+ }
+
+ /* Leave CXL.cache off until the host caches are flushed. */
+ rc = pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL,
+ snapshot->dvsec_ctrl &
+ ~PCI_DVSEC_CXL_CACHE_ENABLE);
+ if (rc) {
+ rc = pcibios_err_to_errno(rc);
+ pci_err(pdev, "failed to restore CXL DVSEC control: %d\n", rc);
+ }
+
+ return rc;
+}
+
+/* Turn CXL.cache back on after the host caches are flushed. */
+static int cxl_reset_enable_cache(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot)
+{
+ int rc;
+
+ if (!(snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE))
+ return 0;
+
+ rc = pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL,
+ snapshot->dvsec_ctrl);
+ if (rc) {
+ rc = pcibios_err_to_errno(rc);
+ pci_err(pdev, "failed to enable CXL.cache after reset: %d\n", rc);
+ }
+
+ return rc;
+}
+
+static int cxl_restore_state(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot)
+{
+ void __iomem *hdm;
+ int rc;
+
+ hdm = cxl_pci_hdm_ioremap_current(pdev, snapshot->hdm_bar,
+ snapshot->hdm_offset,
+ snapshot->hdm_size);
+ if (IS_ERR(hdm)) {
+ pci_err(pdev, "failed to map CXL HDM state for restore: %ld\n",
+ PTR_ERR(hdm));
+ return PTR_ERR(hdm);
+ }
+
+ rc = cxl_hdm_restore_state(hdm, snapshot);
+ iounmap(hdm);
+ if (rc) {
+ pci_err(pdev, "failed to restore CXL HDM state: %d\n", rc);
+ return rc;
+ }
+
+ return cxl_restore_dvsec_ctrl(pdev, snapshot);
+}
+
+static void cxl_reset_save_disabled_state(struct pci_dev *pdev)
+{
+ u32 *saved_command = &pdev->saved_config_space[PCI_COMMAND / 4];
+ int rc;
+
+ rc = pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
+ if (rc)
+ pci_err(pdev,
+ "failed to disable device after CXL reset failure: %d\n",
+ pcibios_err_to_errno(rc));
+
+ /* Preserve the pre-reset snapshot if PCI state has not been restored. */
+ if (!pdev->state_saved) {
+ rc = pci_save_state(pdev);
+ if (rc)
+ pci_err(pdev,
+ "failed to save disabled state after CXL reset failure: %d\n",
+ rc);
+ }
+
+ /* Ensure the outer PCI restore cannot re-enable MSE or BME. */
+ *saved_command &= GENMASK(31, 16);
+ *saved_command |= PCI_COMMAND_INTX_DISABLE;
+}
+
+static int cxl_reset_save_restored_state(struct pci_dev *pdev, u16 command)
+{
+ int rc;
+
+ rc = pci_write_config_word(pdev, PCI_COMMAND, command);
+ if (rc) {
+ rc = pcibios_err_to_errno(rc);
+ pci_err(pdev, "failed to restore PCI command state: %d\n", rc);
+ cxl_reset_save_disabled_state(pdev);
+ return rc;
+ }
+
+ rc = pci_save_state(pdev);
+ if (rc) {
+ pci_err(pdev, "failed to save restored state after CXL reset: %d\n",
+ rc);
+ cxl_reset_save_disabled_state(pdev);
+ }
+
+ return rc;
+}
+
+/* Caller holds the region lock and keeps device traffic isolated. */
+static int pci_cxl_restore_state(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot, u16 *command)
+ __must_hold(cxl_region_quiesce)
+{
+ device_lock_assert(&pdev->dev);
+ cxl_restore_pci_state_for_hdm_restore(pdev, command);
+ return cxl_restore_state(pdev, snapshot);
+}
+
/*
* Prepare window. Hold the region lock only long enough to capture the HDM
* state, reserve its ranges and mark the device as resetting. The caller does
@@ -300,6 +478,25 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
return ERR_PTR(rc);
}
+/*
+ * Restore window. Take the region lock again to write the HDM state back. The
+ * pending mark stays set: the caller does the host cache flush without the
+ * lock and then calls cxl_reset_hdm_unmark().
+ */
+static int cxl_reset_hdm_restore(struct pci_dev *pdev,
+ struct cxl_hdm_info *snapshot, u16 *command)
+{
+ int rc;
+
+ cxl_region_quiesce_lock();
+ rc = pci_cxl_restore_state(pdev, snapshot, command);
+ /* The registers are usable again; later teardowns write them. */
+ cxl_hdm_cache_set_regs_offline(pdev, false);
+ cxl_region_quiesce_unlock();
+
+ return rc;
+}
+
/*
* Offline window. Called right before the device is reset, so that a decoder
* teardown from here on does not access the registers of a device in reset. A
@@ -501,7 +698,9 @@ static int cxl_clear_memory(struct pci_dev *pdev, int dvsec, bool initiate)
}
static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap,
- struct cxl_hdm_quiesce *q)
+ struct cxl_hdm_info *snapshot,
+ struct cxl_hdm_quiesce *q,
+ u16 *command, bool *attempted)
{
int rc, rc2;
@@ -518,6 +717,7 @@ static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap,
return rc;
}
+ *attempted = true;
cxl_reset_regs_offline(pdev);
rc = cxl_clear_memory(pdev, dvsec, true);
if (!rc)
@@ -529,27 +729,37 @@ static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap,
pci_err(pdev, "failed to clear CXL Reset Memory Clear: %d\n", rc2);
rc = rc ?: rc2;
+ /* Restore window; skipped after a failed reset. */
+ if (!rc)
+ rc = cxl_reset_hdm_restore(pdev, snapshot, command);
+
rc2 = cxl_hdm_quiesce_flush(q);
if (rc2)
pci_err(pdev, "failed to invalidate CPU cache after CXL reset: %d\n", rc2);
rc = rc ?: rc2;
cxl_reset_hdm_unmark(pdev);
+ if (rc)
+ cxl_reset_save_disabled_state(pdev);
pci_dev_reset_iommu_done(pdev);
return rc;
}
/*
- * Three short region-lock windows surround the reset: cxl_reset_hdm_prepare()
+ * Four short region-lock windows surround the reset: cxl_reset_hdm_prepare()
* captures the state and reserves the ranges, cxl_reset_regs_offline() keeps
- * region teardown off the registers just before the reset starts, and
- * cxl_reset_hdm_unmark() lets decoders commit again after the host flush.
- * The wait for Memory Clear, up to 100 s, and the flush run without the lock.
+ * region teardown off the registers just before the reset starts,
+ * cxl_reset_hdm_restore() restores the state, and cxl_reset_hdm_unmark() lets
+ * decoders commit again after the host flush. The wait for Memory Clear, up
+ * to 100 s, and the flush run without the lock.
*/
static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
{
struct cxl_pci_cache cache = {};
- int rc, rc2;
+ bool attempted = false;
+ bool keep_caching_off;
+ int rc, rc2 = 0;
+ u16 command;
struct cxl_hdm_info *snapshot __free(kfree) =
cxl_reset_hdm_prepare(pdev, dvsec, cap, &cache);
@@ -558,7 +768,8 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
rc = cxl_reset_cache_prepare(pdev, dvsec, cap, &cache);
if (!rc) {
- rc = __cxl_reset_execute(pdev, dvsec, cap, &cache.quiesce);
+ rc = __cxl_reset_execute(pdev, dvsec, cap, snapshot,
+ &cache.quiesce, &command, &attempted);
} else {
/* Nothing was reset: clear the mark and drop the reservation. */
cxl_reset_hdm_unmark(pdev);
@@ -568,10 +779,22 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
return rc;
}
}
- /* Restore cache policy after any attempt to disable caching. */
- rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2);
+ keep_caching_off = attempted && rc;
+ if (cache.cache_disabled && !keep_caching_off) {
+ rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2);
+ if (!rc2 && attempted)
+ rc2 = cxl_reset_enable_cache(pdev, snapshot);
+ if (rc2)
+ cxl_reset_save_disabled_state(pdev);
+ }
cxl_hdm_quiesce_destroy(&cache.quiesce);
- return rc ?: rc2;
+
+ if (rc)
+ return rc;
+ if (rc2)
+ return rc2;
+
+ return cxl_reset_save_restored_state(pdev, command);
}
int cxl_reset_function(struct pci_dev *pdev, bool probe)
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index eac5600d2849..a7e342bf645e 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -60,6 +60,8 @@ int cxl_hdm_decoder_count_mapped(struct device *dev, void __iomem *hdm,
resource_size_t size);
int cxl_hdm_read_state(struct device *dev, void __iomem *hdm,
struct cxl_hdm_info *info);
+int cxl_hdm_restore_state(void __iomem *hdm,
+ const struct cxl_hdm_info *snapshot);
void cxl_hdm_cache_set_dvsec_ctrl(struct pci_dev *pdev, u16 ctrl);
void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl);
void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (14 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
` (2 subsequent siblings)
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Register cxl_reset with the PCI reset-method interface and place it before
FLR, which does not reset CXL.cache or CXL.mem protocol state.
Return -ENOTTY when device capabilities or function scope do not permit a
reset, so that the next method is tried. Refuse with another error when
cached HDM state or required cache synchronization cannot make the reset
safe, since FLR would not reset the CXL state.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Reviewed-by: Richard Cheng <icheng@nvidia.com>
---
Changes in v18:
- Return -ENOTTY only when the device or its function scope does not
permit CXL Reset. Refuse with another error when cached HDM state or
cache synchronization cannot make it safe, so a refused reset no longer
falls back to FLR, which would not reset the CXL state (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=13
---
drivers/pci/cxl.c | 10 ++++++++--
drivers/pci/pci.c | 1 +
include/linux/pci.h | 2 +-
3 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 1053aa2f6607..cc2d5a4f67df 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -814,7 +814,7 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
if (rc)
- return rc;
+ return probe ? -ENOTTY : rc;
if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) ||
!(cap & PCI_DVSEC_CXL_MEM_CAPABLE) ||
!(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
@@ -823,7 +823,7 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL, &ctrl);
if (rc)
- return rc;
+ return probe ? -ENOTTY : rc;
if (!(ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) ||
!(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))
return -ENOTTY;
@@ -831,5 +831,11 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
if (probe)
return 0;
+ /* Raw reset callers may not have saved the PCI state needed for restore. */
+ if (!pdev->state_saved) {
+ pci_err(pdev, "CXL reset requires saved PCI state\n");
+ return -EINVAL;
+ }
+
return cxl_reset_execute(pdev, dvsec, cap);
}
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 5a8338340d23..9ab647719c9f 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5204,6 +5204,7 @@ const struct pci_reset_fn_method pci_reset_fn_methods[] = {
{ },
{ pci_dev_specific_reset, .name = "device_specific" },
{ pci_dev_acpi_reset, .name = "acpi" },
+ { cxl_reset_function, .name = "cxl_reset" },
{ pcie_reset_flr, .name = "flr" },
{ pci_af_flr, .name = "af_flr" },
{ pci_pm_reset, .name = "pm" },
diff --git a/include/linux/pci.h b/include/linux/pci.h
index f62aab652770..6dba622430f4 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -52,7 +52,7 @@
PCI_STATUS_PARITY)
/* Number of reset methods used in pci_reset_fn_methods array in pci.c */
-#define PCI_NUM_RESET_METHODS 8
+#define PCI_NUM_RESET_METHODS 9
#define PCI_RESET_PROBE true
#define PCI_RESET_DO_RESET false
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (15 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
CXL bus reset can clear HDM decoder programming and CXL Device DVSEC
control state. Restore the cached state after a successful cxl_bus reset
while IOMMU exclusion remains active.
Restore PCI configuration first so HDM MMIO is accessible, and preserve a
disabled state if restoration fails.
Copy the cached state before the reset, then refresh its decoder settings
from the cache under the CXL region quiesce once the reset completes. A
region decommit during the reset updates the cache, and restoring the
earlier decoders would leave the endpoint decoding an HPA range that the
CXL core considers free. Keep global and DVSEC control from before the
reset, since a refresh during the reset can read them as all-ones.
The prepare/restore interface and decoder-refresh structure follow Dave
Jiang's cxl-type2-reset reference branch.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com> # pci/pci.c
---
Changes in v18:
- Refresh the decoders from the cache in the shared restore window, so
the CXL Reset path also restores the decoders as the CXL core last left
them, now that a region can be torn down during the reset (Dave)
https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
- Turn CXL.cache back on in cxl_restore_command_after_pci_reset(), after
the DVSEC Control is restored with it off, so the device does not cache
again before the host caches are flushed (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18
- Declare and initialize the __free() snapshot in cxl_reset_bus_function()
in one statement (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=17
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=b93f0c8e67b198a63786fd3721b7177ab9df60be
---
drivers/cxl/core/hdm_state.c | 31 +++++++++++
drivers/pci/cxl.c | 104 +++++++++++++++++++++++++++++++++--
drivers/pci/pci.c | 14 +++++
drivers/pci/pci.h | 28 ++++++++++
include/cxl/hdm.h | 2 +
5 files changed, 175 insertions(+), 4 deletions(-)
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
index 497709bffff2..139a141c0eac 100644
--- a/drivers/cxl/core/hdm_state.c
+++ b/drivers/cxl/core/hdm_state.c
@@ -284,6 +284,37 @@ struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot)
return copy;
}
+/**
+ * cxl_hdm_cache_copy_decoders() - refresh the decoders of a snapshot
+ * @slot: cache pointer owned by the device
+ * @dst: snapshot of the same device from cxl_hdm_cache_snapshot()
+ *
+ * Copy only the per-decoder settings, which region commit and teardown keep
+ * current. Leave the rest of @dst, including global and DVSEC control, as
+ * captured: a refresh during a reset may have read those registers from a
+ * device that returned all-ones.
+ *
+ * Return: 0 on success, -ENXIO when nothing is published, or -EINVAL when
+ * @dst holds a different number of decoders.
+ */
+int cxl_hdm_cache_copy_decoders(struct cxl_hdm_info *const *slot,
+ struct cxl_hdm_info *dst)
+{
+ struct cxl_hdm_info *info;
+
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ info = *slot;
+ if (!info)
+ return -ENXIO;
+ if (info->decoder_count != dst->decoder_count)
+ return -EINVAL;
+
+ memcpy(dst->settings, info->settings,
+ flex_array_size(info, settings, info->decoder_count));
+ return 0;
+}
+
void cxl_region_quiesce_lock(void)
__acquires(cxl_region_quiesce)
__context_unsafe(token maps to cxl_rwsem.region)
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index cc2d5a4f67df..0d5225e8141b 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -479,9 +479,14 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
}
/*
- * Restore window. Take the region lock again to write the HDM state back. The
- * pending mark stays set: the caller does the host cache flush without the
- * lock and then calls cxl_reset_hdm_unmark().
+ * Restore window. Take the region lock to refresh the decoder settings in
+ * @snapshot from the cache and write the HDM state back. Leave @pdev disabled
+ * on failure.
+ *
+ * Refresh the decoders now rather than rely on the copy taken before the
+ * reset: a region commit or teardown during the reset has updated the cache,
+ * and restoring older decoders would leave the device decoding ranges the
+ * CXL core considers free.
*/
static int cxl_reset_hdm_restore(struct pci_dev *pdev,
struct cxl_hdm_info *snapshot, u16 *command)
@@ -489,7 +494,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev,
int rc;
cxl_region_quiesce_lock();
- rc = pci_cxl_restore_state(pdev, snapshot, command);
+ rc = cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot);
+ if (!rc)
+ rc = pci_cxl_restore_state(pdev, snapshot, command);
/* The registers are usable again; later teardowns write them. */
cxl_hdm_cache_set_regs_offline(pdev, false);
cxl_region_quiesce_unlock();
@@ -521,6 +528,95 @@ static void cxl_reset_hdm_unmark(struct pci_dev *pdev)
cxl_region_quiesce_unlock();
}
+/**
+ * pci_cxl_reset_prepare() - allocate HDM state storage ahead of a bus reset
+ * @pdev: device about to be reset
+ *
+ * Copy the cached HDM state before the reset, so that restoring cannot fail
+ * for lack of memory. The decoder settings are refreshed when the state is
+ * restored.
+ *
+ * Return: storage for cxl_restore_state_after_pci_reset(), to be freed with
+ * kfree(); NULL when @pdev has no cached HDM state; or an ERR_PTR() on
+ * failure.
+ */
+struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev)
+{
+ struct cxl_hdm_info *snapshot;
+
+ device_lock_assert(&pdev->dev);
+ snapshot = cxl_hdm_cache_snapshot(&pdev->hdm);
+ if (IS_ERR(snapshot))
+ return PTR_ERR(snapshot) == -ENXIO ? NULL : snapshot;
+
+ /* Raw reset callers may not have saved the PCI state needed for restore. */
+ if (!pdev->state_saved) {
+ pci_err(pdev, "CXL bus reset requires saved PCI state\n");
+ kfree(snapshot);
+ return ERR_PTR(-EINVAL);
+ }
+
+ return snapshot;
+}
+
+/**
+ * cxl_restore_state_after_pci_reset() - restore CXL state after a bus reset
+ * @pdev: device that was reset
+ * @snapshot: storage from pci_cxl_reset_prepare()
+ * @command: returns the saved PCI_COMMAND value to apply once the IOMMU is
+ * restored
+ *
+ * Under the region quiesce, refresh the decoder settings in @snapshot from the
+ * cache and restore: first the PCI configuration needed to reach HDM MMIO,
+ * then the HDM decoder and CXL Device DVSEC state. Leave @pdev disabled on
+ * failure.
+ *
+ * Bus mastering stays off, because the caller has not yet restored IOMMU
+ * translation and a DMA request now would hit the blocking domain. The caller
+ * calls cxl_restore_command_after_pci_reset() once the IOMMU is restored.
+ */
+int cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
+ struct cxl_hdm_info *snapshot,
+ u16 *command)
+{
+ int rc;
+
+ device_lock_assert(&pdev->dev);
+ rc = cxl_reset_hdm_restore(pdev, snapshot, command);
+ if (rc)
+ cxl_reset_save_disabled_state(pdev);
+
+ return rc;
+}
+
+/**
+ * cxl_restore_command_after_pci_reset() - re-enable the device after a bus reset
+ * @pdev: device that was reset
+ * @snapshot: HDM state from pci_cxl_reset_prepare()
+ * @command: PCI_COMMAND value from cxl_restore_state_after_pci_reset()
+ *
+ * Call after the IOMMU is restored: this can enable bus mastering. Save the
+ * restored PCI state, then turn CXL.cache back on, now that the host caches
+ * are flushed. Leave @pdev disabled on failure.
+ */
+int cxl_restore_command_after_pci_reset(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot,
+ u16 command)
+{
+ int rc;
+
+ device_lock_assert(&pdev->dev);
+ rc = cxl_reset_save_restored_state(pdev, command);
+ if (rc)
+ return rc;
+
+ rc = cxl_reset_enable_cache(pdev, snapshot);
+ if (rc)
+ cxl_reset_save_disabled_state(pdev);
+
+ return rc;
+}
+
/*
* CXL r4.0 sec 9.7.2 defines the reset completion timeout encodings.
* Sec 9.7.3 leaves config-space access behavior undefined for 100 ms after
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 9ab647719c9f..3d9647c110dd 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5062,6 +5062,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
{
struct pci_dev *bridge;
u16 dvsec, reg, val;
+ u16 command;
int rc;
/*
@@ -5092,6 +5093,11 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
if (rc)
return -ENOTTY;
+ struct cxl_hdm_info *snapshot __free(kfree) =
+ pci_cxl_reset_prepare(dev);
+ if (IS_ERR(snapshot))
+ return PTR_ERR(snapshot);
+
rc = pci_dev_reset_iommu_prepare(dev);
if (rc) {
pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc);
@@ -5112,7 +5118,15 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL,
reg);
+ if (!rc && snapshot)
+ rc = cxl_restore_state_after_pci_reset(dev, snapshot, &command);
+
pci_dev_reset_iommu_done(dev);
+
+ /* Enable bus mastering only once the IOMMU translates again. */
+ if (!rc && snapshot)
+ rc = cxl_restore_command_after_pci_reset(dev, snapshot, command);
+
return rc;
}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 5bfa77fd1d37..cdbe4a8b4050 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -736,10 +736,18 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }
static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
#endif
+struct cxl_hdm_info;
#ifdef CONFIG_CXL_RESET
void pci_cxl_hdm_cache_init(struct pci_dev *pdev);
void pci_cxl_hdm_cache_release(struct pci_dev *pdev);
int cxl_reset_function(struct pci_dev *pdev, bool probe);
+struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev);
+int cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
+ struct cxl_hdm_info *snapshot,
+ u16 *command);
+int cxl_restore_command_after_pci_reset(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot,
+ u16 command);
#else
static inline void pci_cxl_hdm_cache_init(struct pci_dev *pdev) { }
static inline void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { }
@@ -747,6 +755,26 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe)
{
return -ENOTTY;
}
+
+static inline struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev)
+{
+ return NULL;
+}
+
+static inline int
+cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
+ struct cxl_hdm_info *snapshot, u16 *command)
+{
+ return 0;
+}
+
+static inline int
+cxl_restore_command_after_pci_reset(struct pci_dev *pdev,
+ const struct cxl_hdm_info *snapshot,
+ u16 command)
+{
+ return 0;
+}
#endif
#ifdef CONFIG_PCI_NPEM
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index a7e342bf645e..594ee8da2c11 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -75,6 +75,8 @@ bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev);
void cxl_hdm_cache_release(struct cxl_hdm_info **slot);
bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info);
struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot);
+int cxl_hdm_cache_copy_decoders(struct cxl_hdm_info *const *slot,
+ struct cxl_hdm_info *dst);
/*
* Region quiesce blocks CXL region commit and teardown, and with them decoder
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 18/19] PCI/CXL: Flush host and device caches for CXL bus reset
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (16 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
CXL bus reset can reset a Type 2 device while its host and device caches
still contain data for the device's HDM ranges. Flush the host caches,
disable device caching, and trigger device cache writeback and
invalidation when supported before reset. Invalidate the host caches
again before ending IOMMU exclusion.
Hold the CXL region lock only to take the snapshot and reserve the
affected HPA ranges, to mark the HDM registers offline right before the
reset, to restore the state after the reset, and to clear the marks once
the second invalidation is done. Do the host cache flushes and the bus
reset without the lock. The reserved ranges and the pending mark keep new
decode from coming online in between, and a region torn down during the
reset does not access the device's registers. Preserve the device's cache
policy across the operation.
If reset was not attempted, undo preparation without changing the saved
PCI command unless cleanup fails. After an attempted reset, keep the
device disabled on recovery failure and do not re-enable caching against
unrestored HDM state. Enable bus mastering only after the IOMMU is
restored, so that no DMA request reaches the blocking domain.
If no cached HDM state exists, retain the existing CXL bus reset path.
For a device with cached state, reject the reset if its cache-maintenance
state cannot be safely prepared or restored. An invalidation provider is
required unless the existing test-only CXL_REGION_INVALIDATION_TEST bypass
is enabled.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Do not hold the region lock across the bus reset. Share the prepare,
offline, restore and unmark windows of the CXL Reset path, and do the
host cache flushes and the bus reset without the lock (Dave)
https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
- Check that a slot or parent bus reset can run before touching the
device, and that the SBR unmask took effect before resetting, so a bus
reset that cannot run no longer leaves the device disabled with caching
off. Any result from the bus reset itself still counts as an attempted
reset (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=18
---
drivers/pci/cxl.c | 164 ++++++++++++++++++++++++++++++++++++----------
drivers/pci/pci.c | 35 ++++++++--
drivers/pci/pci.h | 20 ++++--
3 files changed, 173 insertions(+), 46 deletions(-)
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 0d5225e8141b..a86d56b5879b 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -415,6 +415,11 @@ static int pci_cxl_restore_state(struct pci_dev *pdev,
return cxl_restore_state(pdev, snapshot);
}
+static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec,
+ u16 saved_ctrl2);
+static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap,
+ struct cxl_pci_cache *cache);
+
/*
* Prepare window. Hold the region lock only long enough to capture the HDM
* state, reserve its ranges and mark the device as resetting. The caller does
@@ -430,10 +435,12 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
struct cxl_hdm_info *snapshot;
int rc;
- rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
- &cache->saved_ctrl2);
- if (rc)
- return ERR_PTR(rc);
+ if (cap & PCI_DVSEC_CXL_CACHE_CAPABLE) {
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
+ &cache->saved_ctrl2);
+ if (rc)
+ return ERR_PTR(rc);
+ }
cxl_region_quiesce_lock();
snapshot = cxl_hdm_cache_snapshot(&pdev->hdm);
@@ -443,6 +450,10 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
cxl_region_quiesce_unlock();
return snapshot;
}
+ if (!snapshot->dvsec_ctrl_valid) {
+ rc = -ENXIO;
+ goto free_snapshot;
+ }
/* Judge the snapshot: the live cache can change after it was taken. */
if (cxl_hdm_snapshot_is_dvsec_decoded(snapshot)) {
rc = -EOPNOTSUPP;
@@ -479,14 +490,13 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
}
/*
- * Restore window. Take the region lock to refresh the decoder settings in
- * @snapshot from the cache and write the HDM state back. Leave @pdev disabled
- * on failure.
+ * Restore window. Take the region lock again to bring the cached decoders up
+ * to date and write the HDM state back. The pending mark stays set: the caller
+ * does the host cache flush without the lock and then calls
+ * cxl_reset_hdm_unmark().
*
- * Refresh the decoders now rather than rely on the copy taken before the
- * reset: a region commit or teardown during the reset has updated the cache,
- * and restoring older decoders would leave the device decoding ranges the
- * CXL core considers free.
+ * A region torn down during the reset has already refreshed the cache, so the
+ * decoders are re-read here instead of restoring the snapshot's stale copy.
*/
static int cxl_reset_hdm_restore(struct pci_dev *pdev,
struct cxl_hdm_info *snapshot, u16 *command)
@@ -495,7 +505,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev,
cxl_region_quiesce_lock();
rc = cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot);
- if (!rc)
+ if (rc)
+ pci_err(pdev, "failed to refresh cached HDM decoders: %d\n", rc);
+ else
rc = pci_cxl_restore_state(pdev, snapshot, command);
/* The registers are usable again; later teardowns write them. */
cxl_hdm_cache_set_regs_offline(pdev, false);
@@ -509,7 +521,7 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev,
* teardown from here on does not access the registers of a device in reset. A
* teardown before this point still writes them.
*/
-static void cxl_reset_regs_offline(struct pci_dev *pdev)
+void cxl_reset_regs_offline(struct pci_dev *pdev)
{
cxl_region_quiesce_lock();
cxl_hdm_cache_set_regs_offline(pdev, true);
@@ -529,63 +541,143 @@ static void cxl_reset_hdm_unmark(struct pci_dev *pdev)
}
/**
- * pci_cxl_reset_prepare() - allocate HDM state storage ahead of a bus reset
+ * pci_cxl_reset_prepare() - prepare HDM state and caches before a bus reset
* @pdev: device about to be reset
+ * @cachep: receives the cache state to pass to
+ * cxl_restore_state_after_pci_reset()
*
- * Copy the cached HDM state before the reset, so that restoring cannot fail
- * for lack of memory. The decoder settings are refreshed when the state is
- * restored.
+ * Capture HDM state and reserve its ranges under the region lock, then drop
+ * the lock and complete host and device cache maintenance. No region lock is
+ * held across the reset: the device's decoders cannot commit until
+ * cxl_restore_state_after_pci_reset() clears the pending mark.
*
- * Return: storage for cxl_restore_state_after_pci_reset(), to be freed with
- * kfree(); NULL when @pdev has no cached HDM state; or an ERR_PTR() on
- * failure.
+ * Return: snapshot to be freed with kfree(), NULL when no cached HDM state is
+ * present, or an ERR_PTR() on failure. A cached device without a valid DVSEC
+ * snapshot is rejected because its cache-maintenance scope is unknown.
+ * Only a successful non-NULL snapshot returns with *@cachep set. The caller
+ * must pass both to cxl_restore_state_after_pci_reset() even if reset is never
+ * attempted. The snapshot remains caller-owned. All other returns leave
+ * *@cachep NULL and nothing reserved or marked.
*/
-struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev)
+struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev,
+ struct cxl_pci_cache **cachep)
{
struct cxl_hdm_info *snapshot;
+ struct cxl_pci_cache *cache;
+ u16 dvsec, cap, command;
+ int rc;
device_lock_assert(&pdev->dev);
- snapshot = cxl_hdm_cache_snapshot(&pdev->hdm);
- if (IS_ERR(snapshot))
- return PTR_ERR(snapshot) == -ENXIO ? NULL : snapshot;
+ *cachep = NULL;
+ if (!pdev->hdm)
+ return NULL;
+ rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
+ if (rc == -ENOTTY)
+ return ERR_PTR(-ENODEV);
+ if (rc)
+ return ERR_PTR(rc);
/* Raw reset callers may not have saved the PCI state needed for restore. */
if (!pdev->state_saved) {
pci_err(pdev, "CXL bus reset requires saved PCI state\n");
- kfree(snapshot);
return ERR_PTR(-EINVAL);
}
+ cache = kzalloc_obj(*cache);
+ if (!cache)
+ return ERR_PTR(-ENOMEM);
+
+ snapshot = cxl_reset_hdm_prepare(pdev, dvsec, cap, cache);
+ if (IS_ERR(snapshot)) {
+ kfree(cache);
+ return snapshot;
+ }
+
+ rc = cxl_reset_cache_prepare(pdev, dvsec, cap, cache);
+ if (!rc && !pci_wait_for_pending_transaction(pdev))
+ rc = -ETIMEDOUT;
+ if (rc) {
+ rc = cxl_restore_state_after_pci_reset(pdev, snapshot, cache, false,
+ rc, &command);
+ kfree(snapshot);
+ return ERR_PTR(rc);
+ }
+
+ *cachep = cache;
return snapshot;
}
/**
- * cxl_restore_state_after_pci_reset() - restore CXL state after a bus reset
+ * cxl_restore_state_after_pci_reset() - finish a prepared reset and release its quiesce
* @pdev: device that was reset
* @snapshot: storage from pci_cxl_reset_prepare()
+ * @cache: quiesce state from pci_cxl_reset_prepare()
+ * @reset_attempted: reset helper was invoked and may have affected hardware
+ * @rc: preparation or PCI reset result
* @command: returns the saved PCI_COMMAND value to apply once the IOMMU is
* restored
*
- * Under the region quiesce, refresh the decoder settings in @snapshot from the
- * cache and restore: first the PCI configuration needed to reach HDM MMIO,
- * then the HDM decoder and CXL Device DVSEC state. Leave @pdev disabled on
- * failure.
+ * Restore state through pci_cxl_restore_state(), complete host invalidation,
+ * and restore the cache policy changed by planned-reset preparation. When
+ * @reset_attempted is false, undo preparation without changing the saved PCI
+ * command, unless cleanup itself fails. After an attempted reset, keep caching
+ * and the device disabled if state restoration or host invalidation fails.
+ * A reset-helper error does not prove that hardware was untouched.
+ * Unplanned recovery uses pci_cxl_restore_state() directly and owns its own
+ * quiesce and cache-maintenance sequencing.
*
* Bus mastering stays off, because the caller has not yet restored IOMMU
* translation and a DMA request now would hit the blocking domain. The caller
* calls cxl_restore_command_after_pci_reset() once the IOMMU is restored.
+ *
+ * The region lock is taken only to restore the HDM state and, after the host
+ * cache invalidation, to clear the pending mark that blocks decoder commits.
+ * Complete host cache invalidation before PCI operation resumes, then release
+ * the reserved ranges and free @cache on every exit. @snapshot remains
+ * caller-owned.
*/
int cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
struct cxl_hdm_info *snapshot,
- u16 *command)
+ struct cxl_pci_cache *cache,
+ bool reset_attempted, int rc, u16 *command)
{
- int rc;
+ int rc2 = 0, policy_rc = 0;
+ *command = 0;
+ if (!cache)
+ return rc;
device_lock_assert(&pdev->dev);
- rc = cxl_reset_hdm_restore(pdev, snapshot, command);
- if (rc)
- cxl_reset_save_disabled_state(pdev);
+ /* The restore result matters only after an attempted, uncorrupted reset. */
+ if (reset_attempted && !rc)
+ rc = cxl_reset_hdm_restore(pdev, snapshot, command);
+
+ /* Complete host invalidation before ending IOMMU exclusion. */
+ if (reset_attempted || cache->cache_disabled) {
+ rc2 = cxl_hdm_quiesce_flush(&cache->quiesce);
+ if (rc2)
+ pci_err(pdev,
+ "failed to invalidate CPU cache after CXL bus reset: %d\n",
+ rc2);
+ }
+ cxl_reset_hdm_unmark(pdev);
+ /* Undo preparation on abort; enable caching after reset only if safe. */
+ if (cache->cache_disabled && (!reset_attempted || (!rc && !rc2)))
+ policy_rc = cxl_reset_restore_cache_policy(pdev, snapshot->dvsec,
+ cache->saved_ctrl2);
+ if (reset_attempted) {
+ rc = rc ?: rc2 ?: policy_rc;
+ if (rc)
+ cxl_reset_save_disabled_state(pdev);
+ } else {
+ /* An aborted reset alone must not poison the saved PCI command. */
+ if (rc2 || policy_rc)
+ cxl_reset_save_disabled_state(pdev);
+ rc = rc ?: rc2 ?: policy_rc;
+ }
+
+ cxl_hdm_quiesce_destroy(&cache->quiesce);
+ kfree(cache);
return rc;
}
@@ -728,7 +820,7 @@ static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap,
int rc;
rc = cxl_hdm_quiesce_flush(&cache->quiesce);
- if (rc)
+ if (rc || !(cap & PCI_DVSEC_CXL_CACHE_CAPABLE))
return rc;
/* Track an attempted change so partial failures still restore policy. */
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 3d9647c110dd..9fd15fdb3872 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5060,9 +5060,11 @@ static int pci_reset_bus_function(struct pci_dev *dev, bool probe)
static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
{
+ struct cxl_pci_cache *cache;
struct pci_dev *bridge;
u16 dvsec, reg, val;
u16 command;
+ bool attempted = false;
int rc;
/*
@@ -5089,19 +5091,30 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
if (probe)
return 0;
+ /*
+ * Check that a bus reset can run before touching device state, so that
+ * a later -ENOTTY cannot mean "nothing was reset".
+ */
+ rc = pci_dev_reset_slot_function(dev, PCI_RESET_PROBE);
+ if (rc == -ENOTTY)
+ rc = pci_parent_bus_reset(dev, PCI_RESET_PROBE);
+ if (rc)
+ return rc;
+
rc = pci_read_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, ®);
if (rc)
return -ENOTTY;
struct cxl_hdm_info *snapshot __free(kfree) =
- pci_cxl_reset_prepare(dev);
+ pci_cxl_reset_prepare(dev, &cache);
if (IS_ERR(snapshot))
return PTR_ERR(snapshot);
rc = pci_dev_reset_iommu_prepare(dev);
if (rc) {
pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc);
- return rc;
+ return cxl_restore_state_after_pci_reset(dev, snapshot, cache,
+ false, rc, &command);
}
if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) {
@@ -5112,14 +5125,26 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
val);
}
- rc = pci_reset_bus_function(dev, probe);
+ if (cxl_sbr_masked(bridge)) {
+ /* The unmask did not take effect, so SBR would do nothing. */
+ rc = -ENOTTY;
+ } else {
+ if (snapshot)
+ cxl_reset_regs_offline(dev);
+ rc = pci_reset_bus_function(dev, probe);
+ /*
+ * A bus reset method was verified above, so any result, even
+ * -ENOTTY from the wait for the bus, may follow a reset.
+ */
+ attempted = true;
+ }
if (reg != val)
pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL,
reg);
- if (!rc && snapshot)
- rc = cxl_restore_state_after_pci_reset(dev, snapshot, &command);
+ rc = cxl_restore_state_after_pci_reset(dev, snapshot, cache, attempted,
+ rc, &command);
pci_dev_reset_iommu_done(dev);
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index cdbe4a8b4050..546911ad8af4 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -737,14 +737,18 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { }
#endif
struct cxl_hdm_info;
+struct cxl_pci_cache;
#ifdef CONFIG_CXL_RESET
void pci_cxl_hdm_cache_init(struct pci_dev *pdev);
void pci_cxl_hdm_cache_release(struct pci_dev *pdev);
int cxl_reset_function(struct pci_dev *pdev, bool probe);
-struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev);
+struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev,
+ struct cxl_pci_cache **cachep);
+void cxl_reset_regs_offline(struct pci_dev *pdev);
int cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
struct cxl_hdm_info *snapshot,
- u16 *command);
+ struct cxl_pci_cache *cache,
+ bool reset_attempted, int rc, u16 *command);
int cxl_restore_command_after_pci_reset(struct pci_dev *pdev,
const struct cxl_hdm_info *snapshot,
u16 command);
@@ -756,16 +760,22 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe)
return -ENOTTY;
}
-static inline struct cxl_hdm_info *pci_cxl_reset_prepare(struct pci_dev *pdev)
+static inline struct cxl_hdm_info *
+pci_cxl_reset_prepare(struct pci_dev *pdev, struct cxl_pci_cache **cachep)
{
+ *cachep = NULL;
return NULL;
}
+static inline void cxl_reset_regs_offline(struct pci_dev *pdev) { }
+
static inline int
cxl_restore_state_after_pci_reset(struct pci_dev *pdev,
- struct cxl_hdm_info *snapshot, u16 *command)
+ struct cxl_hdm_info *snapshot,
+ struct cxl_pci_cache *cache,
+ bool reset_attempted, int rc, u16 *command)
{
- return 0;
+ return rc;
}
static inline int
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
` (17 preceding siblings ...)
2026-12-05 4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
@ 2026-12-05 4:14 ` Srirangan Madhavan
18 siblings, 0 replies; 20+ messages in thread
From: Srirangan Madhavan @ 2026-12-05 4:14 UTC (permalink / raw)
To: Alison Schofield, Bjorn Helgaas, Dave Jiang, Davidlohr Bueso,
Ira Weiny, Jonathan Cameron, Vishal Verma, linux-cxl, linux-pci,
linux-kernel
Cc: Li Ming, Joshua Machauer, Alex Williamson, vsethi, alwilliamson,
Sai Yashwanth Reddy Kancherla, Vishal Aslot, Manish Honap,
Jiandi An, Richard Cheng, linux-tegra, Alejandro Lucero Palau,
Srirangan Madhavan
Test cxl_hdm_unpack_decoder() without a PCI device or HDM MMIO. Cover
range boundaries and overflow, an all-ones base, zero-size and uncommitted
decoders, and a locked host-only decoder with 12-way interleave.
Run the checks as a KUnit suite rather than during cxl_test module loading,
so a failure does not prevent unrelated CXL mock tests from running. An
uncommitted decoder has no meaningful target type, so do not check it.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Rebase onto the KUnit support in the base tree: drop the Kconfig and
Makefile pieces it now provides, and allow the KUnit module to use the
cxl_hdm_unpack_decoder() export
---
drivers/cxl/core/hdm_regs.c | 2 +-
drivers/cxl/tests/Makefile | 3 +
drivers/cxl/tests/hdm_unpack_kunit.c | 91 ++++++++++++++++++++++++++++
3 files changed, 95 insertions(+), 1 deletion(-)
create mode 100644 drivers/cxl/tests/hdm_unpack_kunit.c
diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
index 358e2ba7495d..0c43956c3c86 100644
--- a/drivers/cxl/core/hdm_regs.c
+++ b/drivers/cxl/core/hdm_regs.c
@@ -188,7 +188,7 @@ int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,
return 0;
}
-EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_unpack_decoder, "cxl_core");
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_unpack_decoder, "cxl_core,cxl_hdm_unpack_kunit");
int cxl_hdm_read_decoder(struct cxl_decoder_config *config, void __iomem *hdm,
int id, u32 ctrl)
diff --git a/drivers/cxl/tests/Makefile b/drivers/cxl/tests/Makefile
index aa5e4f8e3638..1ade68d8b002 100644
--- a/drivers/cxl/tests/Makefile
+++ b/drivers/cxl/tests/Makefile
@@ -3,3 +3,6 @@ ccflags-y += -I$(srctree)/drivers/cxl
obj-$(CONFIG_CXL_KUNIT_TEST) += cxl_hdm_encoding_kunit.o
cxl_hdm_encoding_kunit-y := hdm_encoding_kunit.o
+
+obj-$(CONFIG_CXL_KUNIT_TEST) += cxl_hdm_unpack_kunit.o
+cxl_hdm_unpack_kunit-y := hdm_unpack_kunit.o
diff --git a/drivers/cxl/tests/hdm_unpack_kunit.c b/drivers/cxl/tests/hdm_unpack_kunit.c
new file mode 100644
index 000000000000..c1131e145757
--- /dev/null
+++ b/drivers/cxl/tests/hdm_unpack_kunit.c
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
+#include <kunit/test.h>
+#include <linux/bitfield.h>
+#include <linux/sizes.h>
+
+#include "cxl.h"
+#include "../core/core.h"
+
+static void cxl_hdm_unpack_test(struct kunit *test)
+{
+ static const struct {
+ const char *name;
+ u32 ctrl;
+ u64 base;
+ u64 size;
+ int rc;
+ unsigned long flags;
+ int ways;
+ int granularity;
+ enum cxl_decoder_type target_type;
+ } cases[] = {
+ { "valid", CXL_HDM_DECODER0_CTRL_COMMITTED,
+ SZ_1G, SZ_256M, 0, CXL_DECODER_F_ENABLE,
+ 1, 256, CXL_DECODER_DEVMEM },
+ { "overflow", CXL_HDM_DECODER0_CTRL_COMMITTED,
+ U64_MAX - 127, 256, -ENXIO },
+ { "all-ones base", CXL_HDM_DECODER0_CTRL_COMMITTED,
+ U64_MAX, SZ_256M, -ENXIO },
+ { "last address", CXL_HDM_DECODER0_CTRL_COMMITTED,
+ U64_MAX - 255, 256, 0, CXL_DECODER_F_ENABLE,
+ 1, 256, CXL_DECODER_DEVMEM },
+ { "zero size", CXL_HDM_DECODER0_CTRL_COMMITTED,
+ SZ_1G, 0, 0, CXL_DECODER_F_ENABLE,
+ 1, 256, CXL_DECODER_DEVMEM },
+ { "uncommitted stale size", 0,
+ SZ_1G, SZ_256M, 0, 0, 1, 256 },
+ { "locked host-only interleave",
+ CXL_HDM_DECODER0_CTRL_COMMITTED | CXL_HDM_DECODER0_CTRL_LOCK |
+ CXL_HDM_DECODER0_CTRL_HOSTONLY |
+ FIELD_PREP_CONST(CXL_HDM_DECODER0_CTRL_IW_MASK, 10) |
+ FIELD_PREP_CONST(CXL_HDM_DECODER0_CTRL_IG_MASK, 2),
+ SZ_1G, SZ_256M, 0, CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK,
+ 12, SZ_1K, CXL_DECODER_HOSTONLYMEM },
+ };
+
+ for (int i = 0; i < ARRAY_SIZE(cases); i++) {
+ struct cxl_decoder_config config;
+ u64 size;
+ int rc;
+
+ rc = cxl_hdm_unpack_decoder(&config, 3, cases[i].ctrl,
+ cases[i].base, cases[i].size);
+ KUNIT_EXPECT_EQ_MSG(test, rc, cases[i].rc, "%s", cases[i].name);
+ if (rc || rc != cases[i].rc)
+ continue;
+ size = cases[i].size;
+ if (!(cases[i].flags & CXL_DECODER_F_ENABLE))
+ size = 0;
+ KUNIT_EXPECT_EQ_MSG(test, config.id, 3, "%s", cases[i].name);
+ KUNIT_EXPECT_EQ_MSG(test, config.hpa_range.start, cases[i].base,
+ "%s", cases[i].name);
+ KUNIT_EXPECT_EQ_MSG(test, range_len(&config.hpa_range), size,
+ "%s", cases[i].name);
+ KUNIT_EXPECT_EQ_MSG(test, config.flags, cases[i].flags,
+ "%s", cases[i].name);
+ KUNIT_EXPECT_EQ_MSG(test, config.interleave_ways, cases[i].ways,
+ "%s", cases[i].name);
+ KUNIT_EXPECT_EQ_MSG(test, config.interleave_granularity,
+ cases[i].granularity, "%s", cases[i].name);
+ /* An uncommitted decoder has no meaningful target type. */
+ if (cases[i].flags & CXL_DECODER_F_ENABLE)
+ KUNIT_EXPECT_EQ_MSG(test, config.target_type,
+ cases[i].target_type, "%s", cases[i].name);
+ }
+}
+
+static struct kunit_case cxl_hdm_test_cases[] = {
+ KUNIT_CASE(cxl_hdm_unpack_test),
+ {}
+};
+
+static struct kunit_suite cxl_hdm_test_suite = {
+ .name = "cxl-hdm-unpack",
+ .test_cases = cxl_hdm_test_cases,
+};
+
+kunit_test_suite(cxl_hdm_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for CXL HDM decoder unpacking");
+MODULE_LICENSE("GPL");
--
2.43.0
^ permalink raw reply related [flat|nested] 20+ messages in thread
end of thread, other threads:[~2026-12-05 4:16 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox