PCI/CXL: Add CXL reset support for Type 2 devices
Subsystems (3)
v13 cover PCI/CXL: Add CXL reset support for Type 2 devices
Hi folks!
This series adds CXL Reset support for CXL Type 2 devices through the existing PCI reset_method ABI. The reset sequence follows the CXL 4.0 specification [1], including CXL.cache disable, optional cache writeback, CXL Reset Memory Clear, ResetComplete polling, and ResetError reporting.
The userspace ABI is the existing PCI reset interface:
/sys/bus/pci/devices/.../reset_method
/sys/bus/pci/devices/.../resetUserspace can select "cxl_reset" in reset_method and then trigger reset through the existing reset attribute.
CXL Reset is exposed conservatively. The target must be a single-function CXL Type 2 device with no enabled VFs, CXL.cache and CXL.mem must be enabled, and CXL Reset Memory Clear must be supported. The kernel must also have cached HDM state sufficient to validate affected ranges and restore decoder programming.
For reset through the sysfs reset attribute, the generic PCI path calls the driver's reset_prepare() callback when provided and disables PCI I/O, memory decoding, and bus mastering. The CXL reset path then temporarily reserves each affected HDM range exclusively for the duration of the reset, synchronizes CPU and device caches, and blocks IOMMU translations where applicable. It restores HDM decoder state and the CXL Device DVSEC Control register before PCI operation resumes. The path does not offline memory or revoke mappings; it relies on CXL.mem users retaining conflicting iomem resources while access remains active.
Following Dan's v6 feedback, this uses the existing PCI reset_method interface instead of adding a memdev-specific sysfs ABI.
v13 changes from v12 [2]:
- Split HDM helper movement from semantic changes. Patches 1--7 now separate the stale-comment cleanup, shared port lookup, pure code movement, declaration movement, settings abstraction, build changes, and register unpacking.
- Make cxl_commit_start() and the low-level settings writer void, and keep fallible commit polling separate.
- Rename the shared register parser to cxl_hdm_unpack_decoder(), build all values in locals, and publish a complete settings object only after validation succeeds. Committed and locked state are represented with the other decoder flags.
- Store exactly the detected number of decoder settings in a flexible array. Discover and validate the Register Locator BAR and offset directly, avoiding the resource-to-BAR round trip.
- Move CXL_RESET out of the CXL_BUS dependency block so built-in PCI reset support remains valid when cxl_core is modular.
- Simplify DVSEC Control2 command handling, clear Memory Clear on every post-programming exit, and perform a final WBI status read at the timeout boundary.
- Abort when IOMMU reset preparation fails. Require CPU cache invalidation for active HDM ranges and invalidate both before reset and after state restoration while the ranges remain reserved.
- Make restoration fail fast. Stop at the first mapping, decoder, DVSEC, PCI command, or state-save failure and retain a disabled PCI command state on post-reset failure.
- Restore the complete cached CXL Device DVSEC Control register rather than only Cache_Enable and Mem_Enable.
- Drop the method-specific reset_method ABI text; the existing ABI file does not document individual PCI reset methods.
- Retain the v12 Type 2 eligibility policy. The questions about CXL.mem-only devices and Type 2 devices with CXL.cache disabled are left for follow-up discussion rather than expanding this series.
- Retain the existing bus/cxl_bus method priority and carry forward the v12 CXL state restoration after cxl_bus reset.
Patch overview:
Patch 1 removes the stale interleave-limit comment in isolation.
Patch 2 factors the referenced upstream PCI-device lookup out of the existing CDAT path before the HDM cache becomes its second user.
Patch 3 mechanically moves HDM programming helpers to resource.c.
Patch 4 mechanically moves shared decoder declarations to include/cxl/.
Patch 5 introduces the reusable decoder-settings snapshot and separates infallible programming from fallible commit polling.
Patch 6 makes reset helpers available to built-in PCI code with modular cxl_core configurations.
Patch 7 shares transactional HDM register unpacking between normal CXL enumeration and early PCI discovery.
Patch 8 refreshes cached PCI HDM settings as CXL decoder state changes.
Patch 9 caches endpoint HDM and DVSEC state during PCI enumeration using direct BAR-relative Register Locator metadata.
Patch 10 implements the CXL Device Reset sequence and its Control2 cleanup and timeout handling.
Patch 11 reserves active HDM ranges and performs mandatory pre-reset and post-restore CPU cache invalidation.
Patch 12 rejects reset where function scope is unsafe, including multifunction devices and PFs with enabled VFs.
Patch 13 restores cached global and per-decoder HDM state, decoder locks, and the complete CXL Device DVSEC Control register with fail-closed error handling.
Patch 14 exposes cxl_reset through the PCI reset-method table ahead of FLR.
Patch 15 restores cached CXL state after cxl_bus reset while its existing IOMMU exclusion remains active.
The CXL.cache/CXL.mem eligibility requirements are intentionally unchanged from v12. I would appreciate clarification on whether support for CXL.mem-only devices, or Type 2 devices whose CXL.cache protocol is disabled by platform policy, should be handled as a separate follow-up.
Testing:
- Built vmlinux and modules with W=1 on the for-7.4/cxl-zero-size-decoder topic tip.
- The corresponding v13 reset paths were built and boot-tested on an arm64 NVIDIA system, including CXL Reset and cxl_bus restoration.
References:
[1] https://computeexpresslink.org/wp-content/uploads/2026/02/CXL-Specification_rev4p0_ver1p0_2026February26_clean_evalcopy_v2.pdf
[2] https://lore.kernel.org/linux-cxl/[email protected]/
Srirangan Madhavan (15):
cxl: Drop stale decoder interleave limit comment
cxl: Share CXL port upstream PCI device lookup
cxl: Move HDM decoder programming helpers
cxl: Move decoder declarations to shared header
cxl: Introduce reusable HDM decoder settings
cxl: Make HDM reset helpers available to built-in PCI code
cxl: Share HDM decoder register unpacking
cxl: Refresh cached PCI HDM decoder settings
cxl: Cache endpoint HDM state during PCI enumeration
cxl: Add CXL Device Reset sequencing
cxl: Validate and synchronize HDM ranges around reset PCI/CXL: Reject reset with unsafe function scope
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
drivers/cxl/Kconfig | 4 + drivers/cxl/core/Makefile | 2 +- drivers/cxl/core/core.h | 8 + drivers/cxl/core/hdm.c | 241 ++++---- drivers/cxl/core/pci.c | 38 +- drivers/cxl/core/port.c | 26 + drivers/cxl/core/regs.c | 9 + drivers/cxl/core/resource.c | 1280 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxl.h | 41 -- drivers/pci/bus.c | 2 + drivers/pci/pci.c | 13 + drivers/pci/probe.c | 2 + include/cxl/cxl.h | 121 +++- include/linux/pci.h | 8 +- include/uapi/linux/pci_regs.h | 14 + tools/testing/cxl/Kbuild | 1 - 16 files changed, 1614 insertions(+), 196 deletions(-) create mode 100644 drivers/cxl/core/resource.c
base-commit: fef22d37d47043b63e4007f2f5fe762fa21fae0c
Signature · 1 line
2.43.0
PATCH v13 1/15 cxl: Drop stale decoder interleave limit comment
CXL 2.0 added 16-way memory interleaving, making the comment that the specification supports only 8-way interleaving stale. Remove the comment without changing CXL_DECODER_MAX_INTERLEAVE.
--- drivers/cxl/cxl.h | 4 ---- 1 file changed, 4 deletions(-)
drivers/cxl/cxl.h +0 -4
@@ -269,10 +269,6 @@ 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 -*/ #define CXL_DECODER_MAX_INTERLEAVE 16#define CXL_QOS_CLASS_INVALID -1
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 01/15] cxl: Drop stale decoder interleave limit comment
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
CXL 2.0 added 16-way memory interleaving, making the comment that the specification supports only 8-way interleaving stale. Remove the comment without changing
CXL_DECODER_MAX_INTERLEAVE.
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v13 01/15] cxl: Drop stale decoder interleave limit comment
In reply to Srirangan MadhavanQuoted 7 lines · On Tue, 22 Sep 2026 08:39:10 +0000 Srirangan Madhavan <[email protected]>…
CXL 2.0 added 16-way memory interleaving, making the comment that the specification supports only 8-way interleaving stale. Remove the comment without changing
CXL_DECODER_MAX_INTERLEAVE.
Re: [PATCH v13 01/15] cxl: Drop stale decoder interleave limit comment
In reply to Srirangan MadhavanQuoted 7 lines · Srirangan Madhavan
CXL 2.0 added 16-way memory interleaving, making the comment that the specification supports only 8-way interleaving stale. Remove the comment without changing
CXL_DECODER_MAX_INTERLEAVE.
Quoted 19 lines
--- drivers/cxl/cxl.h | 4 ---- 1 file changed, 4 deletions(-)drivers/cxl/cxl.h +0
-4@@ -269,10 +269,6 @@ 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 -*/ #define CXL_DECODER_MAX_INTERLEAVE 16#define CXL_QOS_CLASS_INVALID -1
PATCH v13 2/15 cxl: Share CXL port upstream PCI device lookup
In reply to Srirangan Madhavanread_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.
--- drivers/cxl/core/core.h | 1 + drivers/cxl/core/pci.c | 23 ++++++----------------- drivers/cxl/core/port.c | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-)
drivers/cxl/core/core.h +1 -0
@@ -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) {
drivers/cxl/core/pci.c +6 -17
@@ -564,26 +564,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 pci_dev *pdev =cxl_port_get_uport_pci_dev (port);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); - } -if (!pdev)return ;
@@ -591,14 +578,14 @@ void read_cdat_data(struct cxl_port *port) CXL_DOE_PROTOCOL_TABLE_ACCESS);if (!doe_mb) {dev_dbg (dev,"No CDAT mailbox\n" ); -return ; + goto out; } port->cdat_available = true;if (cxl_cdat_get_length (dev, doe_mb, &length)) {dev_dbg (dev,"No CDAT length\n" ); -return ; + goto out; }/*
@@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out; err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev); }EXPORT_SYMBOL_NS_GPL (read_cdat_data,"CXL" );
drivers/cxl/core/port.c +26 -0
@@ -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)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
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.Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
In reply to Srirangan MadhavanQuoted 8 lines · Srirangan Madhavan
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.Signed-off-by Srirangan Madhavan <[email protected]>---
Notes on the actual changes below, but is read_cdat_data() the only place that could use this helper? If not, then I would like for you to do the same for other locations; or at least explain why only this location is being refactored.
Quoted 68 lines
drivers/cxl/core/core.h | 1 + drivers/cxl/core/pci.c | 23 ++++++----------------- drivers/cxl/core/port.c | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-)drivers/cxl/core/core.h +1
-0@@ -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) {drivers/cxl/core/pci.c +3
-16@@ -564,26 +564,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 pci_dev *pdev =cxl_port_get_uport_pci_dev (port);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); - } -if (!pdev)return ;@@ -591,14 +578,14 @@ void read_cdat_data(struct cxl_port *port) CXL_DOE_PROTOCOL_TABLE_ACCESS);if (!doe_mb) {dev_dbg (dev,"No CDAT mailbox\n" ); -return ; + goto out; } port->cdat_available = true;if (cxl_cdat_get_length (dev, doe_mb, &length)) {dev_dbg (dev,"No CDAT length\n" ); -return ; + goto out; }/* @@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data;
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 621
Quoted 10 lines
drivers/cxl/core/pci.c +3
-1port->cdat.length = length; -return ; + goto out; err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev);
I'd use __free() instead of introducing another goto, especially since this is the ideal use case for it. There should be a free action defined for this, but I can't remember what it's called.
Quoted 40 lines
drivers/cxl/core/pci.c +0
-0}EXPORT_SYMBOL_NS_GPL (read_cdat_data,"CXL" );drivers/cxl/core/port.c +26
-0@@ -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)
Jonathan Cameron
maintainerRe: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
Quoted 9 lines
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 615
Quoted 9 lines
drivers/cxl/core/pci.c +1
-1/* @@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out;
Confusing flow, just duplicate the pci_dev_put()
Quoted 10 lines
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 621
Quoted 6 lines
drivers/cxl/core/pci.c +2
-0err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev);I'd use
__free()instead of introducing another goto, especially since this is the ideal use case for it. There should be a free action defined for this, but I can't remember what it's called.
Don't do that. See the guidance on where __free should be used in cleanup.h. There are places where it 'could' be used but we don't because it adds fragility under potential code refactoring.
Jonathan
Dave Jiang
maintainerRe: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
Quoted 23 lines · Jonathan Cameron
Quoted 9 lines
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 615
Quoted 9 lines
drivers/cxl/core/pci.c +1
-1/* @@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out;Confusing flow, just duplicate the
pci_dev_put()Quoted 10 lines
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 621
Quoted 6 lines
drivers/cxl/core/pci.c +2
-0err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev);I'd use
__free()instead of introducing another goto, especially since this is the ideal use case for it. There should be a free action defined for this, but I can't remember what it's called.
Quoted 3 lines
Don't do that. See the guidance on where
__freeshould be used incleanup.h. There are places where it 'could' be used but we don't because it adds fragility under potential code refactoring.
Ah yeah because the devm_kzalloc() forcing the goto. Essentially don't mix goto and __cleanup().
Quoted 7 lines
Jonathan
Re: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
In reply to Jonathan CameronQuoted 15 lines · Jonathan Cameron
Quoted 7 lines
drivers/cxl/core/pci.c void read_cdat_data(struct cxl_port *port) · line 615
Quoted 7 lines
/*drivers/cxl/core/pci.c +1
-1@@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out;Confusing flow, just duplicate the
pci_dev_put()Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/pci.c +2
-0err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev);
Quoted 8 lines
Quoted 3 lines
I'd use
__free()instead of introducing another goto, especially since this is the ideal use case for it. There should be a free action defined for this, but I can't remember what it's called.Don't do that. See the guidance on where
__freeshould be used incleanup.h. There are places where it 'could' be used but we don't because it adds fragility under potential code refactoring.Jonathan
Agreed. In v14 I removed the cleanup goto and duplicated pci_dev_put() at the relevant return and error paths. I did not use __free() here.
Signature · 2 lines
Regards, Srirangan
Re: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
In reply to Srirangan MadhavanQuoted 5 lines · On Tue, 22 Sep 2026 08:39:11 +0000 Srirangan Madhavan <[email protected]>…
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.
We don't need that here, so maybe briefly state that you will do in new users coming shortly.
Quoted 2 lines
Signed-off-by Srirangan Madhavan <[email protected]>
Assuming this is the only instance (as Ben asks) and you do what I suggested about not having the good path follow a goto, then
Quoted 119 lines
--- drivers/cxl/core/core.h | 1 + drivers/cxl/core/pci.c | 23 ++++++----------------- drivers/cxl/core/port.c | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-)drivers/cxl/core/core.h +1
-0@@ -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) {drivers/cxl/core/pci.c +6
-17@@ -564,26 +564,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 pci_dev *pdev =cxl_port_get_uport_pci_dev (port);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); - } -if (!pdev)return ;@@ -591,14 +578,14 @@ void read_cdat_data(struct cxl_port *port) CXL_DOE_PROTOCOL_TABLE_ACCESS);if (!doe_mb) {dev_dbg (dev,"No CDAT mailbox\n" ); -return ; + goto out; } port->cdat_available = true;if (cxl_cdat_get_length (dev, doe_mb, &length)) {dev_dbg (dev,"No CDAT length\n" ); -return ; + goto out; }/* @@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out; err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev); }EXPORT_SYMBOL_NS_GPL (read_cdat_data,"CXL" );drivers/cxl/core/port.c +26
-0@@ -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)
Re: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
Quoted 12 lines · Jonathan Cameron
Quoted 2 lines
Factor the lookup into
cxl_port_get_uport_pci_dev()and return a referenced PCI device to make caller ownership explicit.We don't need that here, so maybe briefly state that you will do in new users coming shortly.
Quoted 1 line
Signed-off-by Srirangan Madhavan<[email protected]>Assuming this is the only instance (as Ben asks) and you do what I suggested about not having the good path follow a goto, then
Reviewed-by Jonathan Cameron<[email protected]>Quoted 1 line
---
read_cdat_data() was the only existing instance I found. The helper now has two new users in the HDM cache-update paths later in the series.
In v14 I duplicated pci_dev_put() at the early-return and error paths, so the successful path no longer follows a cleanup goto. I also added your Reviewed-by tag.
Signature · 2 lines
Regards, Srirangan
Re: [PATCH v13 02/15] cxl: Share CXL port upstream PCI device lookup
In reply to Srirangan MadhavanQuoted 7 lines · Srirangan Madhavan
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.Signed-off-by Srirangan Madhavan <[email protected]>
Quoted 119 lines
--- drivers/cxl/core/core.h | 1 + drivers/cxl/core/pci.c | 23 ++++++----------------- drivers/cxl/core/port.c | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-)drivers/cxl/core/core.h +1
-0@@ -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) {drivers/cxl/core/pci.c +6
-17@@ -564,26 +564,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 pci_dev *pdev =cxl_port_get_uport_pci_dev (port);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); - } -if (!pdev)return ;@@ -591,14 +578,14 @@ void read_cdat_data(struct cxl_port *port) CXL_DOE_PROTOCOL_TABLE_ACCESS);if (!doe_mb) {dev_dbg (dev,"No CDAT mailbox\n" ); -return ; + goto out; } port->cdat_available = true;if (cxl_cdat_get_length (dev, doe_mb, &length)) {dev_dbg (dev,"No CDAT length\n" ); -return ; + goto out; }/* @@ -625,11 +612,13 @@ void read_cdat_data(struct cxl_port *port) port->cdat.table = buf->data; port->cdat.length = length; -return ; + goto out; err:/* Don't leave table data allocated on error */ devm_kfree (dev, buf);dev_err (dev,"Failed to read/validate CDAT.\n" ); +out: +pci_dev_put (pdev); }EXPORT_SYMBOL_NS_GPL (read_cdat_data,"CXL" );drivers/cxl/core/port.c +26
-0@@ -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)
PATCH v13 3/15 cxl: Move HDM decoder programming helpers
In reply to Srirangan MadhavanMove the HDM decoder programming helpers and cxl_rwsem from hdm.c to resource.c for reuse by reset restoration. Keep their behavior unchanged and expose only the cross-object helpers within cxl_core.
--- drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 4 ++ drivers/cxl/core/hdm.c | 103 +-------------------------------- drivers/cxl/core/resource.c | 110 ++++++++++++++++++++++++++++++++++++ tools/testing/cxl/Kbuild | 1 + 5 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 drivers/cxl/core/resource.c
drivers/cxl/core/Makefile +1 -0
@@ -6,6 +6,7 @@ ccflags-y += -I$(srctree)/drivers/cxl CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o +cxl_core-y += resource.o cxl_core-y += pmem.o cxl_core-y += regs.o cxl_core-y += memdev.o
drivers/cxl/core/core.h +4 -0
@@ -139,6 +139,10 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; +int cxld_await_commit (void __iomem *hdm,int id); +void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt); +void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
drivers/cxl/core/hdm.c +1 -102
121 lines not on this page · raw message
drivers/cxl/core/resource.c new file +110 -0
110 lines not on this page · raw message
tools/testing/cxl/Kbuild +1 -0
@@ -60,6 +60,7 @@ cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o cxl_core-y += $(CXL_CORE_SRC)/hdm.o +cxl_core-y += $(CXL_CORE_SRC)/resource.o cxl_core-y += $(CXL_CORE_SRC)/pmu.o cxl_core-y += $(CXL_CORE_SRC)/cdat.o cxl_core-$(CONFIG_TRACING) += $(CXL_CORE_SRC)/trace.o
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 03/15] cxl: Move HDM decoder programming helpers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
Move the HDM decoder programming helpers and
cxl_rwsemfromhdm.ctoresource.cfor reuse by reset restoration. Keep their behavior unchanged and expose only the cross-object helpers withincxl_core.Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v13 03/15] cxl: Move HDM decoder programming helpers
In reply to Srirangan MadhavanQuoted 3 lines · On Tue, 22 Sep 2026 08:39:12 +0000 Srirangan Madhavan <[email protected]>…
Move the HDM decoder programming helpers and
cxl_rwsemfromhdm.ctoresource.cfor reuse by reset restoration. Keep their behavior unchanged and expose only the cross-object helpers withincxl_core.
I'd very briefly mention that the ordering of patches was chosen for minimal code churn, but that it necessitates some functions are use cross module in a fashion that is dropped in later patches.
Quoted 2 lines
Signed-off-by Srirangan Madhavan <[email protected]>
Otherwise much more readable! Thanks for splitting it all up into more bitesized chunks.
Re: [PATCH v13 03/15] cxl: Move HDM decoder programming helpers
Quoted 15 lines · Jonathan Cameron
External email: Use caution opening links or attachments
Quoted 3 lines · On Tue, 22 Sep 2026 08:39:12 +0000 Srirangan Madhavan <[email protected]>…
Move the HDM decoder programming helpers and
cxl_rwsemfromhdm.ctoresource.cfor reuse by reset restoration. Keep their behavior unchanged and expose only the cross-object helpers withincxl_core.I'd very briefly mention that the ordering of patches was chosen for minimal code churn, but that it necessitates some functions are use cross module in a fashion that is dropped in later patches.
Quoted 1 line
Quoted 5 lines
Quoted 1 line
Signed-off-by Srirangan Madhavan <[email protected]>Otherwise much more readable! Thanks for splitting it all up into more bitesized chunks.
Reviewed-by Jonathan Cameron <[email protected]>
In v14, I refactored the helpers in place and moved them together with the build change in patch 6. That removes the temporary build arrangement you noted. But I added the review tag.
Thank you.
Signature · 2 lines
Regards, Srirangan
Re: [PATCH v13 03/15] cxl: Move HDM decoder programming helpers
In reply to Srirangan MadhavanQuoted 5 lines · Srirangan Madhavan
Move the HDM decoder programming helpers and
cxl_rwsemfromhdm.ctoresource.cfor reuse by reset restoration. Keep their behavior unchanged and expose only the cross-object helpers withincxl_core.Signed-off-by Srirangan Madhavan <[email protected]>
Quoted 292 lines
--- drivers/cxl/core/Makefile | 1 + drivers/cxl/core/core.h | 4 ++ drivers/cxl/core/hdm.c | 103 +-------------------------------- drivers/cxl/core/resource.c | 110 ++++++++++++++++++++++++++++++++++++ tools/testing/cxl/Kbuild | 1 + 5 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 drivers/cxl/core/resource.cdrivers/cxl/core/Makefile +1
-0@@ -6,6 +6,7 @@ ccflags-y += -I$(srctree)/drivers/cxl CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o +cxl_core-y += resource.o cxl_core-y += pmem.o cxl_core-y += regs.o cxl_core-y += memdev.odrivers/cxl/core/core.h +4
-0@@ -139,6 +139,10 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; +int cxld_await_commit (void __iomem *hdm,int id); +void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt); +void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );drivers/cxl/core/hdm.c +1
-102@@ -16,11 +16,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;@@ -707,36 +702,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) -{ -u16 eig; -u8 eiw; - -/* -* 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)) -return ; -if (WARN_ONCE (granularity_to_eig (cxld->interleave_granularity, &eig), -"invalid interleave_granularity: %d\n" , - cxld->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 (struct cxl_decoder *cxld,u32 *ctrl) -{ -u32p_replace_bits (ctrl, - !!(cxld->target_type ==CXL_DECODER_HOSTONLYMEM ), -CXL_HDM_DECODER0_CTRL_HOSTONLY ); -} - -static void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt) +void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt) {struct cxl_dport **t = &cxlsd->target[0 ];int ways = cxlsd->cxld.interleave_ways;@@ -758,73 +724,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 -static 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 void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) -{ -int id = cxld->id; -u64 base, size; -u32 ctrl; - -/* common decoder settings */ - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); -cxld_set_interleave (cxld, &ctrl); -cxld_set_type (cxld, &ctrl); - base = cxld->hpa_range.start; - size =range_len (&cxld->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)); -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)); - -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); - }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); - } - -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); -} -static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);drivers/cxl/core/resource.c new file +110
-0@@ -0,0 +1,110 @@ +// 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/kernel.h> + +#include "cxl.h" +#include "core.h" + +struct cxl_rwsem cxl_rwsem = { + .region =__RWSEM_INITIALIZER (cxl_rwsem.region), + .dpa =__RWSEM_INITIALIZER (cxl_rwsem.dpa), +}; + +static void cxld_set_interleave (struct cxl_decoder *cxld,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 (cxld->interleave_ways, &eiw), +"invalid interleave_ways: %d\n" , cxld->interleave_ways)) +return ; +if (WARN_ONCE (granularity_to_eig (cxld->interleave_granularity, &eig), +"invalid interleave_granularity: %d\n" , + cxld->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 (struct cxl_decoder *cxld,u32 *ctrl) +{ +u32p_replace_bits (ctrl, + !!(cxld->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 ; +} + +void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) +{ +int id = cxld->id; +u64 base, size; +u32 ctrl; + +/* common decoder settings */ + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); +cxld_set_interleave (cxld, &ctrl); +cxld_set_type (cxld, &ctrl); + base = cxld->hpa_range.start; + size =range_len (&cxld->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)); +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)); + +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); + }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); + } + +writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +}tools/testing/cxl/Kbuild +1
-0@@ -60,6 +60,7 @@ cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o cxl_core-y += $(CXL_CORE_SRC)/hdm.o +cxl_core-y += $(CXL_CORE_SRC)/resource.o cxl_core-y += $(CXL_CORE_SRC)/pmu.o cxl_core-y += $(CXL_CORE_SRC)/cdat.o cxl_core-$(CONFIG_TRACING) += $(CXL_CORE_SRC)/trace.o
PATCH v13 4/15 cxl: Move decoder declarations to shared header
In reply to Srirangan MadhavanMove the common decoder declarations to include/cxl/cxl.h for use by shared decoder settings. Preserve their layout and documentation.
--- drivers/cxl/cxl.h | 37 ------------------------------------- include/cxl/cxl.h | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 38 deletions(-)
drivers/cxl/cxl.h +0 -37
@@ -264,45 +264,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 , -}; - -#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
include/cxl/cxl.h +40 -1
55 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 04/15] cxl: Move decoder declarations to shared header
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
Move the common decoder declarations to
include/cxl/cxl.hfor use by shared decoder settings. Preserve their layout and documentation.Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v13 04/15] cxl: Move decoder declarations to shared header
In reply to Srirangan MadhavanQuoted 4 lines · On Tue, 22 Sep 2026 08:39:13 +0000 Srirangan Madhavan <[email protected]>…
Move the common decoder declarations to
include/cxl/cxl.hfor use by shared decoder settings. Preserve their layout and documentation.Signed-off-by Srirangan Madhavan <[email protected]>
Re: [PATCH v13 04/15] cxl: Move decoder declarations to shared header
In reply to Srirangan MadhavanQuoted 4 lines · Srirangan Madhavan
Move the common decoder declarations to
include/cxl/cxl.hfor use by shared decoder settings. Preserve their layout and documentation.Signed-off-by Srirangan Madhavan <[email protected]>
Quoted 116 lines
--- drivers/cxl/cxl.h | 37 ------------------------------------- include/cxl/cxl.h | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 38 deletions(-)drivers/cxl/cxl.h +0
-37@@ -264,45 +264,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 , -}; - -#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 include/cxl/cxl.h +40
-1@@ -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,44 @@ enum cxl_devtype { CXL_DEVTYPE_CLASSMEM, }; -struct device ; +struct cxl_region ; + +enum cxl_decoder_type { +CXL_DECODER_DEVMEM =2 , +CXL_DECODER_HOSTONLYMEM =3 , +}; + +#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,
PATCH v13 5/15 cxl: Introduce reusable HDM decoder settings
In reply to Srirangan MadhavanRepresent HDM programming state with struct cxl_decoder_settings and pass it to the commit helpers. Keep endpoint skip and switch targets at their call sites, place flags with the programming state, and leave runtime region ownership outside the snapshot.
Separate commit initiation from completion waiting so reset restoration can reuse register programming without changing normal DPA-lock policy.
--- drivers/cxl/core/core.h | 5 ++- drivers/cxl/core/hdm.c | 40 +++++++++++++++++++--- drivers/cxl/core/resource.c | 67 ++++++++++++++++++------------------- include/cxl/cxl.h | 27 +++++++++++---- 4 files changed, 90 insertions(+), 49 deletions(-)
drivers/cxl/core/core.h +2 -3
@@ -139,9 +139,8 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; -int cxld_await_commit (void __iomem *hdm,int id); -void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt); -void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm); +void cxl_commit_start (void __iomem *hdm,struct cxl_decoder_settings *settings); +int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings);int cxl_memdev_init (void );void cxl_memdev_exit (void );
drivers/cxl/core/hdm.c +36 -4
@@ -702,7 +702,7 @@ int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, u64 size)return devm_add_action_or_reset (&port->dev, cxl_dpa_release, cxled); } -void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt) +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;
@@ -724,11 +724,41 @@ void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt) *tgt |=FIELD_PREP (GENMASK_ULL (63 ,56 ), t[7 ]->port_id); } +/* +* Endpoint skip and switch targets have different owners. Keep that state with +* its owning type and flatten it only into a transient register-programming +* snapshot. +*/ +static void cxl_decoder_snapshot (struct cxl_decoder *cxld, +struct cxl_decoder_settings *settings) +{ +lockdep_assert_held (&cxl_rwsem.dpa); + + *settings = (struct cxl_decoder_settings ) { + .config = cxld->config, + }; + +if (is_switch_decoder (&cxld->dev)) { +struct cxl_switch_decoder *cxlsd = +to_cxl_switch_decoder (&cxld->dev); +u64 targets; + +cxlsd_set_targets (cxlsd, &targets); + settings->target_or_skip_reg_val = targets; + }else { +struct cxl_endpoint_decoder *cxled = +to_cxl_endpoint_decoder (&cxld->dev); + + settings->target_or_skip_reg_val = cxled->skip; + } +} +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; +struct cxl_decoder_settings settings;int id = cxld->id, rc;if (cxld->flags &CXL_DECODER_F_ENABLE )
@@ -761,10 +791,12 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } } - scoped_guard(rwsem_read, &cxl_rwsem.dpa) -setup_hw_decoder (cxld, hdm); + scoped_guard(rwsem_read, &cxl_rwsem.dpa) { +cxl_decoder_snapshot (cxld, &settings); +cxl_commit_start (hdm, &settings); + } - rc =cxld_await_commit (hdm, cxld->id); + rc =cxl_commit_wait (hdm, &settings);if (rc) {dev_dbg (&port->dev,"%s: error %d committing decoder\n" ,dev_name (&cxld->dev), rc);
drivers/cxl/core/resource.c +32 -35
111 lines not on this page · raw message
include/cxl/cxl.h +20 -7
@@ -42,8 +42,8 @@ enum cxl_decoder_type {* @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 +* @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.
@@ -52,18 +52,31 @@ 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_group_tagged(cxl_decoder_config, config, + int id; +struct range hpa_range; +int interleave_ways; +int interleave_granularity; +enum cxl_decoder_type target_type; +unsigned long flags; + );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); }; +/** +* struct cxl_decoder_settings - CXL HDM decoder programming snapshot +* @config: common decoder configuration +* @target_or_skip_reg_val: switch target list or endpoint skip register value +*/ +struct cxl_decoder_settings { +struct cxl_decoder_config config; +u64 target_or_skip_reg_val; +}; +/* * Using struct_group() allows for per register-block-type helper routines, * without requiring block-type agnostic code to include the prefix.
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Represent HDM programming state with struct
cxl_decoder_settingsand pass it to the commit helpers. Keep endpoint skip and switch targets at their call sites, place flags with the programming state, and leave runtime region ownership outside the snapshot.Separate commit initiation from completion waiting so reset restoration can reuse register programming without changing normal DPA-lock policy.
Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
In reply to Srirangan MadhavanQuoted 210 lines · Srirangan Madhavan
Represent HDM programming state with struct
cxl_decoder_settingsand pass it to the commit helpers. Keep endpoint skip and switch targets at their call sites, place flags with the programming state, and leave runtime region ownership outside the snapshot.Separate commit initiation from completion waiting so reset restoration can reuse register programming without changing normal DPA-lock policy.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/core.h | 5 ++- drivers/cxl/core/hdm.c | 40 +++++++++++++++++++--- drivers/cxl/core/resource.c | 67 ++++++++++++++++++------------------- include/cxl/cxl.h | 27 +++++++++++---- 4 files changed, 90 insertions(+), 49 deletions(-)drivers/cxl/core/core.h +2
-3@@ -139,9 +139,8 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; -int cxld_await_commit (void __iomem *hdm,int id); -void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt); -void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm); +void cxl_commit_start (void __iomem *hdm,struct cxl_decoder_settings *settings); +int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings);int cxl_memdev_init (void );void cxl_memdev_exit (void );drivers/cxl/core/hdm.c +36
-4@@ -702,7 +702,7 @@ int cxl_dpa_alloc(struct cxl_endpoint_decoder *cxled, u64 size)return devm_add_action_or_reset (&port->dev, cxl_dpa_release, cxled); } -void cxlsd_set_targets (struct cxl_switch_decoder *cxlsd,u64 *tgt) +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;@@ -724,11 +724,41 @@ void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt) *tgt |=FIELD_PREP (GENMASK_ULL (63 ,56 ), t[7 ]->port_id); } +/* +* Endpoint skip and switch targets have different owners. Keep that state with +* its owning type and flatten it only into a transient register-programming +* snapshot. +*/ +static void cxl_decoder_snapshot (struct cxl_decoder *cxld, +struct cxl_decoder_settings *settings) +{ +lockdep_assert_held (&cxl_rwsem.dpa); + + *settings = (struct cxl_decoder_settings ) { + .config = cxld->config, + }; + +if (is_switch_decoder (&cxld->dev)) { +struct cxl_switch_decoder *cxlsd = +to_cxl_switch_decoder (&cxld->dev); +u64 targets; + +cxlsd_set_targets (cxlsd, &targets); + settings->target_or_skip_reg_val = targets; + }else { +struct cxl_endpoint_decoder *cxled = +to_cxl_endpoint_decoder (&cxld->dev); + + settings->target_or_skip_reg_val = cxled->skip; + } +} +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; +struct cxl_decoder_settings settings;int id = cxld->id, rc;if (cxld->flags &CXL_DECODER_F_ENABLE )@@ -761,10 +791,12 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } } - scoped_guard(rwsem_read, &cxl_rwsem.dpa) -setup_hw_decoder (cxld, hdm); + scoped_guard(rwsem_read, &cxl_rwsem.dpa) { +cxl_decoder_snapshot (cxld, &settings); +cxl_commit_start (hdm, &settings); + } - rc =cxld_await_commit (hdm, cxld->id); + rc =cxl_commit_wait (hdm, &settings);if (rc) {dev_dbg (&port->dev,"%s: error %d committing decoder\n" ,dev_name (&cxld->dev), rc);drivers/cxl/core/resource.c +24
-32@@ -13,7 +13,7 @@ struct cxl_rwsem cxl_rwsem = { .dpa = __RWSEM_INITIALIZER(cxl_rwsem.dpa), }; -static void cxld_set_interleave (struct cxl_decoder *cxld,u32 *ctrl) +static void cxld_set_interleave (struct cxl_decoder_config *config,u32 *ctrl) {u16 eig;u8 eiw;@@ -22,12 +22,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 (config->interleave_ways, &eiw), +"invalid interleave_ways: %d\n" , config->interleave_ways))return ; -if (WARN_ONCE (granularity_to_eig (cxld->interleave_granularity, &eig), +if (WARN_ONCE (granularity_to_eig (config->interleave_granularity, &eig),"invalid interleave_granularity: %d\n" , - cxld->interleave_granularity)) + config->interleave_granularity))return ;u32p_replace_bits (ctrl, eig,CXL_HDM_DECODER0_CTRL_IG_MASK );@@ -35,10 +35,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 (struct cxl_decoder_config *config,u32 *ctrl) {u32p_replace_bits (ctrl, - !!(cxld->target_type ==CXL_DECODER_HOSTONLYMEM ), + !!(config->target_type ==CXL_DECODER_HOSTONLYMEM ),CXL_HDM_DECODER0_CTRL_HOSTONLY ); }@@ -48,7 +48,7 @@ static void cxld_set_type(struct cxl_decoder *cxld, u32 *ctrl)* clock skew and other marginal behavior */ #define COMMIT_TIMEOUT_MS 20 -int cxld_await_commit (void __iomem *hdm,int id) +static int cxld_await_commit (void __iomem *hdm,int id) {u32 ctrl;int i;@@ -68,43 +68,40 @@ int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; } -void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) +static void setup_hw_decoder (void __iomem *hdm, +struct cxl_decoder_settings *settings) { -int id = cxld->id; +struct cxl_decoder_config *config = &settings->config; +int id = config->id; +u64 target_or_skip_reg_val;u64 base, size;u32 ctrl; -/* common decoder settings */ - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); -cxld_set_interleave (cxld, &ctrl); -cxld_set_type (cxld, &ctrl); - base = cxld->hpa_range.start; - size =range_len (&cxld->hpa_range); + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +cxld_set_interleave (config, &ctrl); +cxld_set_type (config, &ctrl); + base = config->hpa_range.start; + size =range_len (&config->hpa_range); + target_or_skip_reg_val = settings->target_or_skip_reg_val;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)); +/* Target-list and endpoint-skip registers alias the same slot. */ +writel (upper_32_bits (target_or_skip_reg_val), hdm +CXL_HDM_DECODER0_TL_HIGH (id)); +writel (lower_32_bits (target_or_skip_reg_val), hdm +CXL_HDM_DECODER0_TL_LOW (id)); -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); - }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 (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +} -writel (upper_32_bits (cxled->skip), sk_hi);
drivers/cxl/core/resource.c int cxld_await_commit(void __iomem *hdm, int id) · line 104
Quoted 10 lines
drivers/cxl/core/resource.c +6
-3-writel (lower_32_bits (cxled->skip), sk_lo); - } +void cxl_commit_start (void __iomem *hdm,struct cxl_decoder_settings *settings) +{ +lockdep_assert_held (&cxl_rwsem.dpa); +setup_hw_decoder (hdm, settings); +} -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings)
No reason to rename this. Either just use cxld_await_commit() directly, or just change the signature of cxld_await_commit().
Quoted 55 lines
drivers/cxl/core/resource.c +2
-0+{ +return cxld_await_commit (hdm, settings->config.id); }include/cxl/cxl.h +20
-7@@ -42,8 +42,8 @@ enum cxl_decoder_type {* @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 +* @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. @@ -52,18 +52,31 @@ 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_group_tagged(cxl_decoder_config, config, + int id; +struct range hpa_range; +int interleave_ways; +int interleave_granularity; +enum cxl_decoder_type target_type; +unsigned long flags; + );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); }; +/** +* struct cxl_decoder_settings - CXL HDM decoder programming snapshot +* @config: common decoder configuration +* @target_or_skip_reg_val: switch target list or endpoint skip register value +*/ +struct cxl_decoder_settings { +struct cxl_decoder_config config; +u64 target_or_skip_reg_val; +}; +/* * Using struct_group() allows for per register-block-type helper routines, * without requiring block-type agnostic code to include the prefix.
Jonathan Cameron
maintainerRe: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
Quoted 16 lines
Quoted 14 lines
drivers/cxl/core/resource.c +1
-1@@ -48,7 +48,7 @@ static void cxld_set_type(struct cxl_decoder *cxld, u32 *ctrl)* clock skew and other marginal behavior */ #define COMMIT_TIMEOUT_MS 20 -int cxld_await_commit (void __iomem *hdm,int id) +static int cxld_await_commit (void __iomem *hdm,int id) {u32 ctrl;int i;@@ -68,43 +68,40 @@ int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; }
Quoted 8 lines
drivers/cxl/core/resource.c int cxld_await_commit(void __iomem *hdm, int id) · line 104
Quoted 1 line
drivers/cxl/core/resource.c +1
-0+int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings)No reason to rename this. Either just use
cxld_await_commit()directly, or just change the signature ofcxld_await_commit().drivers/cxl/core/resource.c int cxld_await_commit(void __iomem *hdm, int id) · line 107
Quoted 3 lines
drivers/cxl/core/resource.c +2
-0+{ +return cxld_await_commit (hdm, settings->config.id); }
Please crop replies! Saves time if hopefully a number of people are looking at them + ensures that the stuff that actually needs discussing doesn't get missed.
Comment looks good though once I found it!
Thanks,
Jonathan
Re: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
In reply to Cheatham, BenjaminQuoted 10 lines · Benjamin
drivers/cxl/core/resource.c int cxld_await_commit(void __iomem *hdm, int id) · line 104
Quoted 8 lines
drivers/cxl/core/resource.c +6
-1+void cxl_commit_start (void __iomem *hdm,struct cxl_decoder_settings *settings) +{ +lockdep_assert_held (&cxl_rwsem.dpa); +setup_hw_decoder (hdm, settings); +} -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings)No reason to rename this. Either just use
cxld_await_commit()directly, or just change the signature ofcxld_await_commit().
Done in v14. I dropped cxl_commit_wait() and now call cxld_await_commit() directly.
Signature · 2 lines
Regards, Srirangan
Jonathan Cameron
maintainerRe: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
In reply to Srirangan MadhavanQuoted 9 lines · On Tue, 22 Sep 2026 08:39:14 +0000 Srirangan Madhavan <[email protected]>…
Represent HDM programming state with struct
cxl_decoder_settingsand pass it to the commit helpers. Keep endpoint skip and switch targets at their call sites, place flags with the programming state, and leave runtime region ownership outside the snapshot.Separate commit initiation from completion waiting so reset restoration can reuse register programming without changing normal DPA-lock policy.
Signed-off-by Srirangan Madhavan <[email protected]>
Hi Srirangan,
I'm not sure if others will agree, but my inclination here would not be to use the struct_group_tagged machine because of the structure then being embedded in two places.
I'd just define the structure independently and pay the price in churn to access the version in cxl_decoder. The point being to say 'these are the bits of cxl_decoder that we need to store for reset purposes'.
Jonathan
Quoted 15 lines
include/cxl/cxl.h +1
-1@@ -42,8 +42,8 @@ enum cxl_decoder_type {* @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 +* @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.
include/cxl/cxl.h enum cxl_decoder_type { · line 56
Quoted 10 lines
include/cxl/cxl.h +2
-5@@ -52,18 +52,31 @@ 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_group_tagged(cxl_decoder_config, config,
Whilst strictly speaking correct, to me struct_group_tagged() should only really be used when there is only one place that the structure appears. Otherwise it is just a bit obscure to read. It does give us the option of accessing the elements in here without the extra jump, but I'm not sure that is a good idea in this case. So I'd just make this a normal structure. After all what is special about it being defined here that isn't equally true of the cxl_decoder_settings structure below?
Quoted 27 lines
include/cxl/cxl.h +17
-1+int id; +struct range hpa_range; +int interleave_ways; +int interleave_granularity; +enum cxl_decoder_type target_type; +unsigned long flags; + );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); }; +/** +* struct cxl_decoder_settings - CXL HDM decoder programming snapshot +* @config: common decoder configuration +* @target_or_skip_reg_val: switch target list or endpoint skip register value +*/ +struct cxl_decoder_settings { +struct cxl_decoder_config config; +u64 target_or_skip_reg_val; +}; +/* * Using struct_group() allows for per register-block-type helper routines, * without requiring block-type agnostic code to include the prefix.
Re: [PATCH v13 05/15] cxl: Introduce reusable HDM decoder settings
Quoted 9 lines · Jonathan Cameron
Whilst strictly speaking correct, to me
struct_group_tagged()should only really be used when there is only one place that the structure appears. Otherwise it is just a bit obscure to read. It does give us the option of accessing the elements in here without the extra jump, but I'm not sure that is a good idea in this case. So I'd just make this a normal structure. After all what is special about it being defined here that isn't equally true of thecxl_decoder_settingsstructure below?
Ack. Now V14 defines struct cxl_decoder_config independently and embeds it as a normal member in both cxl_decoder and cxl_decoder_settings. I've moved the access changes into a separate mechanical patch, 4/16.
Signature · 2 lines
Regards, Srirangan
PATCH v13 6/15 cxl: Make HDM reset helpers available to built-in PCI code
In reply to Srirangan MadhavanPCI can be built in while CXL_BUS is modular, so PCI reset code cannot depend on helpers provided by the cxl_core module.
Build the HDM resource helpers under CONFIG_CXL_RESET and restrict their exports to cxl_core.
--- drivers/cxl/Kconfig | 4 ++++ drivers/cxl/core/Makefile | 2 +- drivers/cxl/core/resource.c | 4 ++++ tools/testing/cxl/Kbuild | 1 - 4 files changed, 9 insertions(+), 2 deletions(-)
drivers/cxl/Kconfig +4 -0
@@ -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
@@ -244,3 +245,6 @@ config CXL_ATL depends on ACPI_PRMT && AMD_NB endif + +config CXL_RESET + bool
drivers/cxl/core/Makefile +1 -1
@@ -1,12 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o +obj-$(CONFIG_CXL_RESET) += resource.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o ccflags-y += -I$(srctree)/drivers/cxl CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o -cxl_core-y += resource.o cxl_core-y += pmem.o cxl_core-y += regs.o cxl_core-y += memdev.o
drivers/cxl/core/resource.c +4 -0
@@ -3,6 +3,7 @@#include <linux/delay.h> #include <linux/bug.h> #include <linux/errno.h> +#include <linux/export.h> #include <linux/kernel.h> #include "cxl.h"
@@ -12,6 +13,7 @@ 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" );static void cxld_set_interleave (struct cxl_decoder_config *config,u32 *ctrl) {
@@ -100,8 +102,10 @@ void cxl_commit_start(void __iomem *hdm, struct cxl_decoder_settings *settings)lockdep_assert_held (&cxl_rwsem.dpa);setup_hw_decoder (hdm, settings); } +EXPORT_SYMBOL_FOR_MODULES (cxl_commit_start,"cxl_core" );int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings) {return cxld_await_commit (hdm, settings->config.id); } +EXPORT_SYMBOL_FOR_MODULES (cxl_commit_wait,"cxl_core" );
tools/testing/cxl/Kbuild +0 -1
@@ -60,7 +60,6 @@ cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o cxl_core-y += $(CXL_CORE_SRC)/hdm.o -cxl_core-y += $(CXL_CORE_SRC)/resource.o cxl_core-y += $(CXL_CORE_SRC)/pmu.o cxl_core-y += $(CXL_CORE_SRC)/cdat.o cxl_core-$(CONFIG_TRACING) += $(CXL_CORE_SRC)/trace.o
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 06/15] cxl: Make HDM reset helpers available to built-in PCI code
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
PCI can be built in while
CXL_BUSis modular, so PCI reset code cannot depend on helpers provided by thecxl_coremodule.Build the HDM resource helpers under
CONFIG_CXL_RESETand restrict their exports tocxl_core.Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v13 06/15] cxl: Make HDM reset helpers available to built-in PCI code
In reply to Srirangan MadhavanQuoted 6 lines · Srirangan Madhavan
PCI can be built in while
CXL_BUSis modular, so PCI reset code cannot depend on helpers provided by thecxl_coremodule.Build the HDM resource helpers under
CONFIG_CXL_RESETand restrict their exports tocxl_core.
I think you should squash this patch into 3/15. Having this earlier does two things:
1) it makes it more clear why you're moving everything earlier in the set, and 2) bisects across the pass 3 patches will all result in the same configuration.
An alternative option would be to refactor the preceding 3 patches in-place and then move all of the helpers to this file in this patch, either would work.
Quoted 32 lines
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/Kconfig | 4 ++++ drivers/cxl/core/Makefile | 2 +- drivers/cxl/core/resource.c | 4 ++++ tools/testing/cxl/Kbuild | 1 - 4 files changed, 9 insertions(+), 2 deletions(-)drivers/cxl/Kconfig +4
-0@@ -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@@ -244,3 +245,6 @@ config CXL_ATL depends on ACPI_PRMT && AMD_NB endif + +config CXL_RESET + booldrivers/cxl/core/Makefile +0
-0
drivers/cxl/core/Makefile line 3
Quoted 3 lines
drivers/cxl/core/Makefile +1
-0@@ -1,12 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o +obj-$(CONFIG_CXL_RESET) += resource.o
This file should probably be called reset.c instead of resource.c to match the Kconfig functionality.
Quoted 46 lines
drivers/cxl/core/Makefile +0
-1obj-$(CONFIG_CXL_SUSPEND) += suspend.o ccflags-y += -I$(srctree)/drivers/cxl CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o -cxl_core-y += resource.o cxl_core-y += pmem.o cxl_core-y += regs.o cxl_core-y += memdev.odrivers/cxl/core/resource.c +4
-0@@ -3,6 +3,7 @@#include <linux/delay.h> #include <linux/bug.h> #include <linux/errno.h> +#include <linux/export.h> #include <linux/kernel.h> #include "cxl.h" @@ -12,6 +13,7 @@ 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" );static void cxld_set_interleave (struct cxl_decoder_config *config,u32 *ctrl) {@@ -100,8 +102,10 @@ void cxl_commit_start(void __iomem *hdm, struct cxl_decoder_settings *settings)lockdep_assert_held (&cxl_rwsem.dpa);setup_hw_decoder (hdm, settings); } +EXPORT_SYMBOL_FOR_MODULES (cxl_commit_start,"cxl_core" );int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings) {return cxld_await_commit (hdm, settings->config.id); } +EXPORT_SYMBOL_FOR_MODULES (cxl_commit_wait,"cxl_core" );tools/testing/cxl/Kbuild +0
-0
tools/testing/cxl/Kbuild cxl_core-y += $(CXL_CORE_SRC)/memdev.o · line 63
Quoted 4 lines
tools/testing/cxl/Kbuild +0
-1@@ -60,7 +60,6 @@ cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o cxl_core-y += $(CXL_CORE_SRC)/hdm.o -cxl_core-y += $(CXL_CORE_SRC)/resource.o
If you squash this patch you also don't need to bother adding/removing this.
Quoted 3 lines
tools/testing/cxl/Kbuild +0
-0cxl_core-y += $(CXL_CORE_SRC)/pmu.o cxl_core-y += $(CXL_CORE_SRC)/cdat.o cxl_core-$(CONFIG_TRACING) += $(CXL_CORE_SRC)/trace.o
Jonathan Cameron
maintainerRe: [PATCH v13 06/15] cxl: Make HDM reset helpers available to built-in PCI code
Quoted 8 lines · Benjamin
Quoted 6 lines · Srirangan Madhavan
PCI can be built in while
CXL_BUSis modular, so PCI reset code cannot depend on helpers provided by thecxl_coremodule.Build the HDM resource helpers under
CONFIG_CXL_RESETand restrict their exports tocxl_core.
Quoted 6 lines
I think you should squash this patch into 3/15. Having this earlier does two things:
1) it makes it more clear why you're moving everything earlier in the set, and 2) bisects across the pass 3 patches will all result in the same configuration.An alternative option would be to refactor the preceding 3 patches in-place and then move all of the helpers to this file in this patch, either would work.
There may indeed be some better orderings that make it easier to review still - though this is already a great improvement on the earlier versions.
Quoted 4 lines
Quoted 3 lines
Signed-off-by Srirangan Madhavan <[email protected]>---
Quoted 11 lines
drivers/cxl/core/Makefile line 3
Quoted 8 lines
drivers/cxl/core/Makefile +1
-0@@ -1,12 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o +obj-$(CONFIG_CXL_RESET) += resource.oThis file should probably be called
reset.cinstead ofresource.cto match theKconfigfunctionality.
I'm not keen on that change because the functions are core CXL stuff that just happens to also be useful in reset. You could change the object name via some Kconfig magic so that matches, but the c file itself wants to remain with some generic name.
Quoted 10 lines
Quoted 10 lines
drivers/cxl/core/Makefile +0
-1obj-$(CONFIG_CXL_SUSPEND) += suspend.o ccflags-y += -I$(srctree)/drivers/cxl CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o -cxl_core-y += resource.o cxl_core-y += pmem.o cxl_core-y += regs.o cxl_core-y += memdev.o
Re: [PATCH v13 06/15] cxl: Make HDM reset helpers available to built-in PCI code
In reply to Cheatham, BenjaminQuoted 6 lines · Benjamin
Quoted 6 lines
PCI can be built in while
CXL_BUSis modular, so PCI reset code cannot depend on helpers provided by thecxl_coremodule.Build the HDM resource helpers under
CONFIG_CXL_RESETand restrict their exports tocxl_core.
Quoted 6 lines
I think you should squash this patch into 3/15. Having this earlier does two things:
1) it makes it more clear why you're moving everything earlier in the set, and 2) bisects across the pass 3 patches will all result in the same configuration.An alternative option would be to refactor the preceding 3 patches in-place and then move all of the helpers to this file in this patch, either would work.
I took your alternative in v14: patches 3–5 keep the helpers in hdm.c while refactoring them, and patch 6 moves them to resource.c together with the CONFIG_CXL_RESET build change. Also eliminated intermediate add/remove of resource.o from cxl_core-y.
Signature · 2 lines
Regards, Srirangan
PATCH v13 7/15 cxl: Share HDM decoder register unpacking
In reply to Srirangan MadhavanFactor HDM register parsing into cxl_hdm_unpack_decoder(). Validate local values before publishing complete settings, including committed, locked, and target-or-skip state.
Pass endpoint skip state to the DPA reservation helper instead of updating live endpoint state, preserving its ownership.
--- drivers/cxl/core/core.h | 4 ++ drivers/cxl/core/hdm.c | 74 +++++++++++++------------------------ drivers/cxl/core/resource.c | 55 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 48 deletions(-)
drivers/cxl/core/core.h +4 -0
@@ -142,6 +142,10 @@ extern struct cxl_rwsem cxl_rwsem;void cxl_commit_start (void __iomem *hdm,struct cxl_decoder_settings *settings);int cxl_commit_wait (void __iomem *hdm,struct cxl_decoder_settings *settings); +int cxl_hdm_unpack_decoder (struct cxl_decoder_settings *settings,int id, +u32 ctrl,u64 base,u64 size, +u64 target_or_skip_reg_val); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
drivers/cxl/core/hdm.c +26 -48
116 lines not on this page · raw message
drivers/cxl/core/resource.c +55 -0
58 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 07/15] cxl: Share HDM decoder register unpacking
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Factor HDM register parsing into
cxl_hdm_unpack_decoder(). Validate local values before publishing complete settings, including committed, locked, and target-or-skip state.Pass endpoint skip state to the DPA reservation helper instead of updating live endpoint state, preserving its ownership.
Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Jonathan Cameron
maintainerRe: [PATCH v13 07/15] cxl: Share HDM decoder register unpacking
In reply to Srirangan MadhavanQuoted 8 lines · On Tue, 22 Sep 2026 08:39:16 +0000 Srirangan Madhavan <[email protected]>…
Factor HDM register parsing into
cxl_hdm_unpack_decoder(). Validate local values before publishing complete settings, including committed, locked, and target-or-skip state.Pass endpoint skip state to the DPA reservation helper instead of updating live endpoint state, preserving its ownership.
Signed-off-by Srirangan Madhavan <[email protected]>
A few little things inline as slightly more seems to change in here than I'd immediately expect.
Quoted 42 lines
drivers/cxl/core/hdm.c +14
-11@@ -932,8 +932,8 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, u64 *dpa_base, struct cxl_endpoint_dvsec_info *info) { struct cxl_endpoint_decoder *cxled = NULL; -u64 size, base, skip, dpa_size, lo, hi; -bool committed; +struct cxl_decoder_settings settings; +u64 size, base, skip, dpa_size, lo, hi, target_or_skip_reg_val;u32 remainder;int i, rc;u32 ctrl;@@ -953,35 +953,34 @@ static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, 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 ); + lo =readl (hdm +CXL_HDM_DECODER0_TL_LOW (which)); + hi =readl (hdm +CXL_HDM_DECODER0_TL_HIGH (which)); + target_or_skip_reg_val = (hi <<32 ) + lo; + rc =cxl_hdm_unpack_decoder (&settings, which, ctrl, base, size, + target_or_skip_reg_val); +if (rc) { +dev_warn (&port->dev, +"decoder%d.%d: Invalid decoder configuration (ctrl: %#x): %d\n" , + port->id, cxld->id, ctrl, rc); +return rc; + } + 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->id); -return -ENXIO ; - }
drivers/cxl/core/hdm.c static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, · line 975
Quoted 10 lines
drivers/cxl/core/hdm.c +3
-4+ cxld->config = settings.config; + size =range_len (&cxld->hpa_range);if (info) cxled =to_cxl_endpoint_decoder (&cxld->dev); - cxld->hpa_range = (struct range ) { - .start = base, - .end = base + size -1 , - }; +if (!cxled && cxld->interleave_ways >8 ) {
Why does this check make sense now when we didn't have it before (that I can find anyway)? If it isn't tightly coupled to this patch and instead is providing some extra checks that are worthwhile I'd break it out as a separate patch where that usecase can be well described.
Quoted 34 lines
drivers/cxl/core/hdm.c +6
-9+dev_warn (&port->dev, +"decoder%d.%d: Invalid target count: %d\n" , + port->id, cxld->id, cxld->interleave_ways); +return -ENXIO ; + }/* decoders are enabled if committed */ -if (committed) { - cxld->flags |=CXL_DECODER_F_ENABLE ; -if (ctrl &CXL_HDM_DECODER0_CTRL_LOCK ) - cxld->flags |=CXL_DECODER_F_LOCK ; -if (FIELD_GET (CXL_HDM_DECODER0_CTRL_HOSTONLY , ctrl)) - cxld->target_type =CXL_DECODER_HOSTONLYMEM ; -else - cxld->target_type =CXL_DECODER_DEVMEM ; - +if (cxld->flags &CXL_DECODER_F_ENABLE ) {drivers/cxl/core/resource.c +8
-0@@ -109,3 +109,58 @@ int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings)return cxld_await_commit (hdm, settings->config.id); }EXPORT_SYMBOL_FOR_MODULES (cxl_commit_wait,"cxl_core" ); + +int cxl_hdm_unpack_decoder (struct cxl_decoder_settings *settings,int id, +u32 ctrl,u64 base,u64 size, +u64 target_or_skip_reg_val) +{ +bool committed =FIELD_GET (CXL_HDM_DECODER0_CTRL_COMMITTED , ctrl); +enum cxl_decoder_type target_type; +int interleave_granularity;
drivers/cxl/core/resource.c int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings) · line 129
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+int interleave_ways; +unsigned long flags =0 ; +struct range hpa_range; +int rc; + +if (!committed) + size =0 ; +if (base ==U64_MAX || size ==U64_MAX || + (size && base >U64_MAX - (size -1 ))) +return -ENXIO ;
This second block looks like new defences which is fine but I'm not sure it makes sense in here. Also given we are just checking overflow doesn't happen maybe use check_add_overflow(). Saves us thinking too much the maths.
drivers/cxl/core/resource.c int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings) · line 136
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+ + hpa_range = (struct range ) { + .start = base, + .end = base + size -1 , + }; + target_type =FIELD_GET (CXL_HDM_DECODER0_CTRL_HOSTONLY , ctrl) ? +CXL_DECODER_HOSTONLYMEM :CXL_DECODER_DEVMEM ;
Maybe stick to the if / else of the original? I think that is more readable for an extra couple of lines.
Also blank line here to give visual separation before the conditional that follows.
It's a functional change to move the target_type selection out of the committed check. I'd like to see some discussion of why that is fine to do which probably means a separate little patch that has that description on its own.
drivers/cxl/core/resource.c int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings) · line 141
Quoted 5 lines
drivers/cxl/core/resource.c +5
-0+if (committed) { + flags |=CXL_DECODER_F_ENABLE ; +if (ctrl &CXL_HDM_DECODER0_CTRL_LOCK ) + flags |=CXL_DECODER_F_LOCK ; + }
Thanks
Jonathan
Re: [PATCH v13 07/15] cxl: Share HDM decoder register unpacking
Quoted 10 lines · Jonathan Cameron
External email: Use caution opening links or attachments
drivers/cxl/core/hdm.c static int init_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld, · line 975
Quoted 2 lines
drivers/cxl/core/hdm.c +1
-1- }; +if (!cxled && cxld->interleave_ways >8 ) {Why does this check make sense now when we didn't have it before (that I can find anyway)? If it isn't tightly coupled to this patch and instead is providing some extra checks that are worthwhile I'd break it out as a separate patch where that usecase can be well described.
I dropped the new > 8 check from v14 patch 7. It was not present in the original code, and this patch is intended to only share the existing unpacking behavior.
Quoted 7 lines
drivers/cxl/core/resource.c int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings) · line 129
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+ +if (!committed) + size =0 ; +if (base ==U64_MAX || size ==U64_MAX || + (size && base >U64_MAX - (size -1 ))) +return -ENXIO ;
Quoted 5 lines
This second block looks like new defences which is fine but I'm not sure it makes sense in here. Also given we are just checking overflow doesn't happen maybe use
check_add_overflow(). Saves us thinking too much the maths.
Agreed. V14 puts the overflow check in a separate patch, 8/16, and uses check_add_overflow().
Quoted 8 lines
drivers/cxl/core/resource.c int cxl_commit_wait(void __iomem *hdm, struct cxl_decoder_settings *settings) · line 136
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+ + hpa_range = (struct range ) { + .start = base, + .end = base + size -1 , + }; + target_type =FIELD_GET (CXL_HDM_DECODER0_CTRL_HOSTONLY , ctrl) ? +CXL_DECODER_HOSTONLYMEM :CXL_DECODER_DEVMEM ;
Quoted 4 lines
Maybe stick to the if / else of the original? I think that is more readable for an extra couple of lines.
Also blank line here to give visual separation before the conditional that follows.
Ack. V14 keeps the original if/else.
Quoted 5 lines
It's a functional change to move the
target_typeselection out of the committed check. I'd like to see some discussion of why that is fine to do which probably means a separate little patch that has that description on its own.
Ack. V14 selects the target type only when the decoder is committed, preserving the existing behavior.
Signature · 2 lines
Regards, Srirangan
PATCH v13 8/15 cxl: Refresh cached PCI HDM decoder settings
In reply to Srirangan MadhavanEarly 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. Ignore updates when no cache exists and reject decoder-count mismatches.
--- drivers/cxl/core/hdm.c | 66 ++++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 22 ++++++++++++++ include/linux/pci.h | 6 ++++ 3 files changed, 94 insertions(+)
drivers/cxl/core/hdm.c +66 -0
@@ -753,6 +753,51 @@ static void cxl_decoder_snapshot(struct cxl_decoder *cxld, } } +#ifdef CONFIG_CXL_RESET +static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +struct cxl_port *port = cxlhdm->port; +void __iomem *hdm = cxlhdm->regs.hdm_decoder; + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port); +struct cxl_decoder_settings *settings; +struct cxl_hdm_info *info; + +if (!pdev || !hdm) +return ; + +guard (rwsem_write)(&cxl_rwsem.dpa); + info = pdev->hdm; +if (!info) +return ; +if (cxld->id <0 || cxld->id >= info->decoder_count) { +pci_warn (pdev,"CXL HDM decoder %d exceeds cached count %d\n" , + cxld->id, info->decoder_count); +return ; + } + + info->global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + settings = &info->settings[cxld->id]; + +/* +* A disabled decoder's software object may retain its old range and +* target state. Leave only the decoder id in the cached settings so stale +* state is not restored as an enabled decode. +*/ + *settings = (struct cxl_decoder_settings ) { + .config.id = cxld->id, + }; +if (cxld->flags &CXL_DECODER_F_ENABLE ) +cxl_decoder_snapshot (cxld, settings); +} +#else +static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +} +#endif +static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);
@@ -804,6 +849,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } port->commit_end++; cxld->flags |=CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);return 0 ; }
@@ -876,6 +922,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); cxld->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)) {
@@ -1089,9 +1136,27 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, {void __iomem *hdm = cxlhdm->regs.hdm_decoder;struct cxl_port *port = cxlhdm->port; +#ifdef CONFIG_CXL_RESET + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port); +#endif int i;u64 dpa_base =0 ; +#ifdef CONFIG_CXL_RESET +if (is_cxl_endpoint (port) && pdev && hdm) { +guard (rwsem_read)(&cxl_rwsem.dpa); + +if (pdev->hdm && + pdev->hdm->decoder_count != cxlhdm->decoder_count) { +pci_warn (pdev, +"CXL HDM cache decoder count mismatch: cached=%d hdm=%d\n" , + pdev->hdm->decoder_count, cxlhdm->decoder_count); +return -ENXIO ; + } + } +#endif +cxl_settle_decoders (cxlhdm);for (i =0 ; i < cxlhdm->decoder_count; i++) {
@@ -1130,6 +1195,7 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,put_device (&cxld->dev);return rc; } +cxl_hdm_refresh_decoder (cxlhdm, cxld); rc =add_hdm_decoder (port, cxld);if (rc) { dev_warn(&port->dev,
include/cxl/cxl.h +22 -0
@@ -77,6 +77,28 @@ struct cxl_decoder_settings {u64 target_or_skip_reg_val; }; +/** +* 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-decoder programming state +*/ +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_decoder_settings settings[]; +}; +/* * Using struct_group() allows for per register-block-type helper routines, * without requiring block-type agnostic code to include the prefix.
include/linux/pci.h +6 -0
@@ -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 *
@@ -566,6 +569,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
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 08/15] cxl: Refresh cached PCI HDM decoder settings
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
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. Ignore updates when no cache exists and reject decoder-count mismatches.
Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v13 08/15] cxl: Refresh cached PCI HDM decoder settings
In reply to Srirangan MadhavanQuoted 19 lines · 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. Ignore updates when no cache exists and reject decoder-count mismatches.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/hdm.c | 66 ++++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 22 ++++++++++++++ include/linux/pci.h | 6 ++++ 3 files changed, 94 insertions(+)drivers/cxl/core/hdm.c +0
-0
drivers/cxl/core/hdm.c static void cxl_decoder_snapshot(struct cxl_decoder *cxld, · line 756
Quoted 4 lines
drivers/cxl/core/hdm.c +1
-0@@ -753,6 +753,51 @@ static void cxl_decoder_snapshot(struct cxl_decoder *cxld, } } +#ifdef CONFIG_CXL_RESET
The CXL maintainers are normally pretty against #ifdefs in c code. I was going to suggest moving it to resource.c, but it looks like cxl_decoder_snapshot() needs to also be in this file. So, I think the fix is to put:
if (!IS_ENABLED(CONFIG_CXL_RESET))
return;
immediately after the variable declarations.
drivers/cxl/core/hdm.c static void cxl_decoder_snapshot(struct cxl_decoder *cxld, · line 763
Quoted 7 lines
drivers/cxl/core/hdm.c +7
-0+static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +struct cxl_port *port = cxlhdm->port; +void __iomem *hdm = cxlhdm->regs.hdm_decoder; + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port);
This line needs to be at the first usage, i.e. right before "if (!pdev || !hdm)" below, to prevent lifetime bugs.
Quoted 57 lines
drivers/cxl/core/hdm.c +39
-0+struct cxl_decoder_settings *settings; +struct cxl_hdm_info *info; + +if (!pdev || !hdm) +return ; + +guard (rwsem_write)(&cxl_rwsem.dpa); + info = pdev->hdm; +if (!info) +return ; +if (cxld->id <0 || cxld->id >= info->decoder_count) { +pci_warn (pdev,"CXL HDM decoder %d exceeds cached count %d\n" , + cxld->id, info->decoder_count); +return ; + } + + info->global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + settings = &info->settings[cxld->id]; + +/* +* A disabled decoder's software object may retain its old range and +* target state. Leave only the decoder id in the cached settings so stale +* state is not restored as an enabled decode. +*/ + *settings = (struct cxl_decoder_settings ) { + .config.id = cxld->id, + }; +if (cxld->flags &CXL_DECODER_F_ENABLE ) + cxl_decoder_snapshot(cxld, settings); +} +#else +static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +} +#endif +static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);@@ -804,6 +849,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } port->commit_end++; cxld->flags |=CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);return 0 ; }@@ -876,6 +922,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); cxld->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)) {
drivers/cxl/core/hdm.c static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, · line 1142
Quoted 7 lines
drivers/cxl/core/hdm.c +4
-0@@ -1089,9 +1136,27 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, {void __iomem *hdm = cxlhdm->regs.hdm_decoder;struct cxl_port *port = cxlhdm->port; +#ifdef CONFIG_CXL_RESET + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port); +#endif
This shouldn't be here, and if you move it to be inside the below if statement then you won't need the #ifdef as well.
Quoted 7 lines
drivers/cxl/core/hdm.c +4
-0int i;u64 dpa_base =0 ; +#ifdef CONFIG_CXL_RESET +if (is_cxl_endpoint (port) && pdev && hdm) { +guard (rwsem_read)(&cxl_rwsem.dpa); +
drivers/cxl/core/hdm.c static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, · line 1159
Quoted 10 lines
drivers/cxl/core/hdm.c +10
-0+if (pdev->hdm && + pdev->hdm->decoder_count != cxlhdm->decoder_count) { +pci_warn (pdev, +"CXL HDM cache decoder count mismatch: cached=%d hdm=%d\n" , + pdev->hdm->decoder_count, cxlhdm->decoder_count); +return -ENXIO ; + } + } +#endif +
Add IS_ENABLED(CONFIG_CXL_RESET) to the above if condition, like above.
Quoted 68 lines
drivers/cxl/core/hdm.c +1
-0cxl_settle_decoders (cxlhdm);for (i =0 ; i < cxlhdm->decoder_count; i++) {@@ -1130,6 +1195,7 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,put_device (&cxld->dev);return rc; } +cxl_hdm_refresh_decoder (cxlhdm, cxld); rc =add_hdm_decoder (port, cxld);if (rc) { dev_warn(&port->dev,include/cxl/cxl.h +22
-0@@ -77,6 +77,28 @@ struct cxl_decoder_settings {u64 target_or_skip_reg_val; }; +/** +* 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-decoder programming state +*/ +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_decoder_settings settings[]; +}; +/* * Using struct_group() allows for per register-block-type helper routines, * without requiring block-type agnostic code to include the prefix. include/linux/pci.h +6
-0@@ -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 * @@ -566,6 +569,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
Re: [PATCH v13 08/15] cxl: Refresh cached PCI HDM decoder settings
Quoted 32 lines · Benjamin
External email: Use caution opening links or attachments
Quoted 19 lines · 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. Ignore updates when no cache exists and reject decoder-count mismatches.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/hdm.c | 66 ++++++++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 22 ++++++++++++++ include/linux/pci.h | 6 ++++ 3 files changed, 94 insertions(+)drivers/cxl/core/hdm.c +0
-0drivers/cxl/core/hdm.c static void cxl_decoder_snapshot(struct cxl_decoder *cxld, · line 756
Quoted 4 lines
drivers/cxl/core/hdm.c +1
-0@@ -753,6 +753,51 @@ static void cxl_decoder_snapshot(struct cxl_decoder *cxld, } } +#ifdef CONFIG_CXL_RESET The CXL maintainers are normally pretty against #ifdefs in c code. I was going to suggest moving it to
resource.c, but it looks likecxl_decoder_snapshot()needs to also be in this file. So, I think the fix is to put:
Quoted 5 lines
if (!IS_ENABLED(CONFIG_CXL_RESET))
return;immediately after the variable declarations.
Fixed in v14 patch 9. Now cxl_hdm_refresh_decoder() is compiled without it. CXL_BUS selects CXL_RESET in patch 6, so whenever hdm.c is built, reset support is enabled; the IS_ENABLED() check is unnecessary.
Quoted 11 lines
drivers/cxl/core/hdm.c static void cxl_decoder_snapshot(struct cxl_decoder *cxld, · line 763
Quoted 7 lines
drivers/cxl/core/hdm.c +7
-0+static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +struct cxl_port *port = cxlhdm->port; +void __iomem *hdm = cxlhdm->regs.hdm_decoder; + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port);This line needs to be at the first usage, i.e. right before "if (!pdev || !hdm)" below, to prevent lifetime bugs.
In v14, the pdev declaration is immediately before if (!pdev || !hdm), so its cleanup lifetime begins at first use.
Quoted 12 lines
drivers/cxl/core/hdm.c static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, · line 1142
Quoted 8 lines
drivers/cxl/core/hdm.c +4
-0@@ -1089,9 +1136,27 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, {void __iomem *hdm = cxlhdm->regs.hdm_decoder;struct cxl_port *port = cxlhdm->port; +#ifdef CONFIG_CXL_RESET + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (port); +#endif This shouldn't be here, and if you move it to be inside the below if statement then you won't need the #ifdef as well.
<snip>
Quoted 6 lines
drivers/cxl/core/hdm.c
Quoted 3 lines
drivers/cxl/core/hdm.c +2
-0+#endif +Add
IS_ENABLED(CONFIG_CXL_RESET)to the above if condition, like above.
pdev declaration is now inside the endpoint/HDM check, immediately before it is used. Removed the #ifdef. Since CXL_BUS selects CXL_RESET, an IS_ENABLED(CONFIG_CXL_RESET) condition is unnecessary whenever this function is built.
Signature · 2 lines
Regards, Srirangan
Jonathan Cameron
maintainerRe: [PATCH v13 08/15] cxl: Refresh cached PCI HDM decoder settings
In reply to Srirangan MadhavanQuoted 27 lines · On Tue, 22 Sep 2026 08:39:17 +0000 Srirangan Madhavan <[email protected]>…
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. Ignore updates when no cache exists and reject decoder-count mismatches.
Signed-off-by Srirangan Madhavan <[email protected]>include/cxl/cxl.h +10
-0@@ -77,6 +77,28 @@ struct cxl_decoder_settings {u64 target_or_skip_reg_val; }; +/** +* 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-decoder programming state
include/cxl/cxl.h struct cxl_decoder_settings { · line 99
Quoted 10 lines
include/cxl/cxl.h +10
-0+*/ +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_decoder_settings settings[];
Can we use __counted_by(decoder_count) for this?
Quoted 1 line
include/cxl/cxl.h +1
-0+};
PATCH v13 9/15 cxl: Cache endpoint HDM state during PCI enumeration
In reply to Srirangan MadhavanPCI capability initialization runs before BAR resources are finalized, while driver binding is too late for driver-independent reset support. Create pci_dev->hdm during pci_bus_add_device(), after PCI resource setup and before driver binding.
Cache BAR-relative HDM location, global control, decoder settings, and CXL Device DVSEC Control, then publish the completed cache under cxl_rwsem.dpa. Restore PCI_COMMAND after temporary MMIO access and reject decoder-count changes.
--- drivers/cxl/core/Makefile | 3 +- drivers/cxl/core/pci.c | 15 ++- drivers/cxl/core/regs.c | 9 ++ drivers/cxl/core/resource.c | 255 ++++++++++++++++++++++++++++++++++++ drivers/pci/bus.c | 2 + drivers/pci/probe.c | 2 + include/cxl/cxl.h | 21 +++ tools/testing/cxl/Kbuild | 1 - 8 files changed, 304 insertions(+), 4 deletions(-)
drivers/cxl/core/Makefile +1 -2
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o -obj-$(CONFIG_CXL_RESET) += resource.o +obj-$(CONFIG_CXL_RESET) += regs.o resource.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o ccflags-y += -I$(srctree)/drivers/cxl
@@ -8,7 +8,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
drivers/cxl/core/pci.c +14 -1
@@ -180,6 +180,16 @@ int cxl_await_media_ready(struct cxl_dev_state *cxlds) }EXPORT_SYMBOL_NS_GPL (cxl_await_media_ready,"CXL" ); +static void cxl_hdm_cache_dvsec_ctrl (struct pci_dev *pdev,u16 ctrl) +{ +guard (rwsem_write)(&cxl_rwsem.dpa); +if (!pdev->hdm) +return ; + + pdev->hdm->dvsec_ctrl = ctrl; + pdev->hdm->dvsec_ctrl_valid = true; +} +static int cxl_set_mem_enable (struct cxl_dev_state *cxlds,u16 val) {struct pci_dev *pdev =to_pci_dev (cxlds->dev);
@@ -191,8 +201,10 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)if (rc)return pcibios_err_to_errno (rc); -if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) == val) +if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) == val) { +cxl_hdm_cache_dvsec_ctrl (pdev, ctrl);return 1 ; + } ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE ; ctrl |= val;
@@ -200,6 +212,7 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)if (rc)return pcibios_err_to_errno (rc); +cxl_hdm_cache_dvsec_ctrl (pdev, ctrl);return 0 ; }
drivers/cxl/core/regs.c +9 -0
@@ -200,6 +200,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,return ret_val; } +EXPORT_SYMBOL_NS_GPL (devm_cxl_iomap_block,"CXL" ); int cxl_map_component_regs(const struct cxl_register_map *map, struct cxl_component_regs *regs,
@@ -285,6 +286,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;
@@ -309,6 +312,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,
@@ -339,6 +343,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;
@@ -518,6 +524,7 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb)return offset; } +EXPORT_SYMBOL_NS_GPL (cxl_rcrb_to_aer,"CXL" );static resource_size_t cxl_rcrb_to_linkcap (struct device *dev,struct cxl_dport *dport) {
@@ -634,6 +641,7 @@ resource_size_t __rcrb_to_component(struct device *dev, struct cxl_rcrb_info *rireturn component_reg_phys; } +EXPORT_SYMBOL_NS_GPL (__rcrb_to_component,"CXL" );resource_size_t cxl_rcd_component_reg_phys (struct device *dev,struct cxl_dport *dport)
@@ -642,3 +650,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_NS_GPL (cxl_rcd_component_reg_phys,"CXL" );
drivers/cxl/core/resource.c +255 -0
267 lines not on this page · raw message
drivers/pci/bus.c +2 -0
@@ -18,6 +18,7 @@#include <linux/pm_runtime.h> #include <linux/proc_fs.h> #include <linux/slab.h> +#include <cxl/cxl.h> #include "pci.h"
@@ -359,6 +360,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
drivers/pci/probe.c +2 -0
@@ -24,6 +24,7 @@#include <linux/pm_runtime.h> #include <linux/bitfield.h> #include <trace/events/pci.h> +#include <cxl/cxl.h> #include "pci.h" static struct resource busn_resource = {
@@ -2484,6 +2485,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);
include/cxl/cxl.h +21 -0
@@ -26,6 +26,7 @@ enum cxl_devtype { };struct cxl_region ; +struct pci_dev ;enum cxl_decoder_type {CXL_DECODER_DEVMEM =2 ,
@@ -87,6 +88,9 @@ struct cxl_decoder_settings {* @dvsec_ctrl: CXL DVSEC control register * @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state * @settings: per-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;
@@ -172,6 +176,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
@@ -182,6 +188,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;
@@ -190,6 +198,19 @@ struct cxl_register_map { }; }; +#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 +/** * struct cxl_dpa_perf - DPA performance property entry * @dpa_range: range for DPA address
tools/testing/cxl/Kbuild +0 -1
@@ -55,7 +55,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
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 09/15] cxl: Cache endpoint HDM state during PCI enumeration
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
PCI capability initialization runs before BAR resources are finalized, while driver binding is too late for driver-independent reset support. Create
pci_dev->hdmduringpci_bus_add_device(), after PCI resource setup and before driver binding.Cache BAR-relative HDM location, global control, decoder settings, and CXL Device DVSEC Control, then publish the completed cache under cxl_rwsem.dpa. Restore
PCI_COMMANDafter temporary MMIO access and reject decoder-count changes.Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v13 09/15] cxl: Cache endpoint HDM state during PCI enumeration
In reply to Srirangan MadhavanQuoted 169 lines · Srirangan Madhavan
PCI capability initialization runs before BAR resources are finalized, while driver binding is too late for driver-independent reset support. Create
pci_dev->hdmduringpci_bus_add_device(), after PCI resource setup and before driver binding.Cache BAR-relative HDM location, global control, decoder settings, and CXL Device DVSEC Control, then publish the completed cache under cxl_rwsem.dpa. Restore
PCI_COMMANDafter temporary MMIO access and reject decoder-count changes.Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/Makefile | 3 +- drivers/cxl/core/pci.c | 15 ++- drivers/cxl/core/regs.c | 9 ++ drivers/cxl/core/resource.c | 255 ++++++++++++++++++++++++++++++++++++ drivers/pci/bus.c | 2 + drivers/pci/probe.c | 2 + include/cxl/cxl.h | 21 +++ tools/testing/cxl/Kbuild | 1 - 8 files changed, 304 insertions(+), 4 deletions(-)drivers/cxl/core/Makefile +1
-2@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o -obj-$(CONFIG_CXL_RESET) += resource.o +obj-$(CONFIG_CXL_RESET) += regs.o resource.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o ccflags-y += -I$(srctree)/drivers/cxl@@ -8,7 +8,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.odrivers/cxl/core/pci.c +14
-1@@ -180,6 +180,16 @@ int cxl_await_media_ready(struct cxl_dev_state *cxlds) }EXPORT_SYMBOL_NS_GPL (cxl_await_media_ready,"CXL" ); +static void cxl_hdm_cache_dvsec_ctrl (struct pci_dev *pdev,u16 ctrl) +{ +guard (rwsem_write)(&cxl_rwsem.dpa); +if (!pdev->hdm) +return ; + + pdev->hdm->dvsec_ctrl = ctrl; + pdev->hdm->dvsec_ctrl_valid = true; +} +static int cxl_set_mem_enable (struct cxl_dev_state *cxlds,u16 val) {struct pci_dev *pdev =to_pci_dev (cxlds->dev);@@ -191,8 +201,10 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)if (rc)return pcibios_err_to_errno (rc); -if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) == val) +if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) == val) { +cxl_hdm_cache_dvsec_ctrl (pdev, ctrl);return 1 ; + } ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE ; ctrl |= val;@@ -200,6 +212,7 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)if (rc)return pcibios_err_to_errno (rc); +cxl_hdm_cache_dvsec_ctrl (pdev, ctrl);return 0 ; }drivers/cxl/core/regs.c +9
-0@@ -200,6 +200,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,return ret_val; } +EXPORT_SYMBOL_NS_GPL (devm_cxl_iomap_block,"CXL" ); int cxl_map_component_regs(const struct cxl_register_map *map, struct cxl_component_regs *regs,@@ -285,6 +286,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;@@ -309,6 +312,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,@@ -339,6 +343,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;@@ -518,6 +524,7 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb)return offset; } +EXPORT_SYMBOL_NS_GPL (cxl_rcrb_to_aer,"CXL" );static resource_size_t cxl_rcrb_to_linkcap (struct device *dev,struct cxl_dport *dport) {@@ -634,6 +641,7 @@ resource_size_t __rcrb_to_component(struct device *dev, struct cxl_rcrb_info *rireturn component_reg_phys; } +EXPORT_SYMBOL_NS_GPL (__rcrb_to_component,"CXL" );resource_size_t cxl_rcd_component_reg_phys (struct device *dev,struct cxl_dport *dport)@@ -642,3 +650,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_NS_GPL (cxl_rcd_component_reg_phys,"CXL" );drivers/cxl/core/resource.c +9
-0@@ -2,9 +2,17 @@/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ #include <linux/delay.h> #include <linux/bug.h> +#include <linux/bitfield.h> +#include <linux/cleanup.h> #include <linux/errno.h> #include <linux/export.h> +#include <linux/io.h> +#include <linux/ioport.h> #include <linux/kernel.h> +#include <linux/pci.h> +#include <linux/slab.h> + +#include <cxl/pci.h> #include "cxl.h" #include "core.h" @@ -164,3 +172,250 @@ int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id,return 0 ; }EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core" ); +
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 185
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+static int cxl_pci_hdm_dvsec (struct pci_dev *pdev) +{ +u16 cap; +int dvsec; +int rc; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ;
Why -ENOTTY here and below? I would've expected -ENXIO for a missing capability.
Quoted 166 lines
drivers/cxl/core/resource.c +166
-0+ + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap); +if (rc) +return pcibios_err_to_errno (rc); +if (!(cap &PCI_DVSEC_CXL_MEM_CAPABLE ) || + !FIELD_GET (PCI_DVSEC_CXL_HDM_COUNT , cap)) +return -ENOTTY ; + +return dvsec; +} + +static int cxl_pci_hdm_read_decoder (struct pci_dev *pdev, +struct cxl_decoder_settings *settings, +void __iomem *hdm,int id) +{ +u64 target_or_skip, base, size; +u32 ctrl; +int rc; + + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); + base = ((u64 )readl (hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)) <<32 ) | +readl (hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); + size = ((u64 )readl (hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)) <<32 ) | +readl (hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); + target_or_skip = + ((u64 )readl (hdm +CXL_HDM_DECODER0_TL_HIGH (id)) <<32 ) | +readl (hdm +CXL_HDM_DECODER0_TL_LOW (id)); + + rc =cxl_hdm_unpack_decoder (settings, id, ctrl, base, size, + target_or_skip); +if (rc) { +pci_err (pdev, +"CXL HDM decoder %d has invalid configuration: %d\n" , + id, rc); +return rc; + } + +if (!(settings->config.flags &CXL_DECODER_F_ENABLE )) + *settings = (struct cxl_decoder_settings ) { + .config.id = id, + }; + +return 0 ; +} + +static int __cxl_pci_hdm_read_info (struct pci_dev *pdev, +struct cxl_register_map *map,int dvsec, +struct cxl_hdm_info **out) +{ +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; +size_t required; +u16 dvsec_ctrl; +int rc; + + rc =cxl_setup_regs (map); +if (rc) +return rc; +if (!hdm_map->valid) +return -ENODEV ; +if (hdm_map->offset > map->max_size || + hdm_map->size > map->max_size - hdm_map->offset) +return -ENXIO ; +if (check_add_overflow (map->bar_offset, + (resource_size_t )hdm_map->offset, &hdm_offset)) +return -EOVERFLOW ; + + hdm =ioremap (map->resource + hdm_map->offset, hdm_map->size); +if (!hdm) +return -ENOMEM ; + + decoder_count =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (decoder_count <0 ) { + rc = decoder_count; + goto out_unmap; + } + + required =CXL_HDM_DECODER0_TL_HIGH (decoder_count -1 ) + +sizeof (u32); +if (required > hdm_map->size) { +pci_err (pdev, +"CXL HDM decoder count exceeds mapped register block\n" ); + rc = -ENXIO ; + goto out_unmap; + } + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , + &dvsec_ctrl); +if (rc) { + rc =pcibios_err_to_errno (rc); + goto out_unmap; + } + + info =kzalloc (struct_size (info, settings, decoder_count),GFP_KERNEL ); +if (!info) { + rc = -ENOMEM ; + goto out_unmap; + } + + info->decoder_count = decoder_count; + info->hdm_bar = map->bar; + info->hdm_offset = hdm_offset; + info->hdm_size = hdm_map->size; + info->global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + info->dvsec_ctrl = dvsec_ctrl; + info->dvsec_ctrl_valid = true; + +for (int i =0 ; i < decoder_count; i++) { + rc =cxl_pci_hdm_read_decoder (pdev, &info->settings[i], hdm, i); +if (rc) + goto out_unmap; + } + +/* +* A changed decoder count means the snapshot no longer describes the +* complete hardware state. Reject it rather than publish a partial cache. +*/ + rc =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (rc != decoder_count) { +pci_err (pdev,"CXL HDM decoder count changed from %d to %d\n" , + decoder_count, rc); + rc = -ENXIO ; + goto out_unmap; + } + + *out = info; + info =NULL ; + rc =0 ; + +out_unmap: +kfree (info); +iounmap (hdm); +return rc; +} + +static int cxl_pci_hdm_read_info (struct pci_dev *pdev, +struct cxl_register_map *map,int dvsec, +struct cxl_hdm_info **out) +{ +bool restore_command = false; +u16 command; +int rc, rc2; + +guard (pci_dev)(pdev); + + rc =pci_read_config_word (pdev,PCI_COMMAND , &command); +if (rc) +return pcibios_err_to_errno (rc); + +if (!(command &PCI_COMMAND_MEMORY )) { + rc =pci_write_config_word (pdev,PCI_COMMAND , + command |PCI_COMMAND_MEMORY ); +if (rc) +return pcibios_err_to_errno (rc); + restore_command = true; + } + + rc =__cxl_pci_hdm_read_info (pdev, map, dvsec, out); +if (!restore_command) +return rc; + +if (!rc) { + rc =pci_write_config_word (pdev,PCI_COMMAND , command);
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 361
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+return pcibios_err_to_errno (rc); + } + + rc2 =pci_write_config_word (pdev,PCI_COMMAND , command); +if (rc2) +pci_err (pdev, +"failed to restore PCI_COMMAND after CXL HDM cache init: %d\n" , +pcibios_err_to_errno (rc2)); + +return rc;
I think the above would flow better as:
rc = __cxl_pci_hdm_read_info(pdev, map, dvsec, out);
if (rc) {
if (!restore_command)
return rc;
rc2 = pci_write_config_word(pdev, PCI_COMMAND, command);
if (rc2)
...
} else {
rc = pci_write_config_word(pdev, PCI_COMMAND, command);
return pcibios_err_to_errno(rc);
}return rc;
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+} + +static int __pci_cxl_hdm_cache_init (struct pci_dev *pdev) +{ + struct cxl_hdm_info *info __free(kfree) = NULL; +struct cxl_register_map map = { }; +struct resource *bar;
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 378
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+int dvsec; +int rc; + + scoped_guard(rwsem_read, &cxl_rwsem.dpa) +if (pdev->hdm) +return 0 ; + + dvsec =cxl_pci_hdm_dvsec (pdev); +if (dvsec <0 ) +return dvsec;
Might be better to just return 0 here. If the device doesn't have a CXL capability then I wouldn't consider that an error from the perspective of the PCI subsystem.
Quoted 21 lines
drivers/cxl/core/resource.c +21
-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 ) +return -ENODEV ; + + bar = &pdev->resource[map.bar]; +if (!pci_resource_len (pdev, map.bar) || + (bar->flags & (IORESOURCE_UNSET |IORESOURCE_DISABLED )) || +resource_type (bar) !=IORESOURCE_MEM || !bar->start || !bar->end) +return -ENODEV ; + + rc =cxl_pci_hdm_read_info (pdev, &map, dvsec, &info); +if (rc) +return rc; + +guard (rwsem_write)(&cxl_rwsem.dpa); +if (!pdev->hdm) + pdev->hdm =no_free_ptr (info); +
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 409
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+return 0 ; +} + +void pci_cxl_hdm_cache_init (struct pci_dev *pdev) +{ +int rc; + + rc =__pci_cxl_hdm_cache_init (pdev); +if (rc && rc != -ENOTTY && rc != -ENODEV ) +pci_dbg (pdev,"CXL HDM cache init failed: %d\n" , rc);
I don't know if it's worth filtering out -ENOTTY and -ENODEV for a debug message, but that's more of a question for the PCI people.
Quoted 117 lines
drivers/cxl/core/resource.c +12
-0+} +EXPORT_SYMBOL_FOR_MODULES (pci_cxl_hdm_cache_init,"cxl_core" ); + +void pci_cxl_hdm_cache_release (struct pci_dev *pdev) +{ +struct cxl_hdm_info *info; + +guard (rwsem_write)(&cxl_rwsem.dpa); + info = pdev->hdm; + pdev->hdm =NULL ; +kfree (info); +}drivers/pci/bus.c +2
-0@@ -18,6 +18,7 @@#include <linux/pm_runtime.h> #include <linux/proc_fs.h> #include <linux/slab.h> +#include <cxl/cxl.h> #include "pci.h" @@ -359,6 +360,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 drivers/pci/probe.c +2
-0@@ -24,6 +24,7 @@#include <linux/pm_runtime.h> #include <linux/bitfield.h> #include <trace/events/pci.h> +#include <cxl/cxl.h> #include "pci.h" static struct resource busn_resource = {@@ -2484,6 +2485,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);include/cxl/cxl.h +21
-0@@ -26,6 +26,7 @@ enum cxl_devtype { };struct cxl_region ; +struct pci_dev ;enum cxl_decoder_type {CXL_DECODER_DEVMEM =2 ,@@ -87,6 +88,9 @@ struct cxl_decoder_settings {* @dvsec_ctrl: CXL DVSEC control register * @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state * @settings: per-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;@@ -172,6 +176,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 @@ -182,6 +188,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;@@ -190,6 +198,19 @@ struct cxl_register_map { }; }; +#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 +/** * struct cxl_dpa_perf - DPA performance property entry * @dpa_range: range for DPA address tools/testing/cxl/Kbuild +0
-0
tools/testing/cxl/Kbuild obj-m += cxl_core.o · line 58
Quoted 4 lines
tools/testing/cxl/Kbuild +0
-1@@ -55,7 +55,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
Did you run CXL test with this change? I don't think there are any test symbols in regs.c, but you should make sure this doesn't break anything there. You may not need to even fix anything if it does, but just make a note of the regression.
Quoted 3 lines
tools/testing/cxl/Kbuild +0
-0cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o
Re: [PATCH v13 09/15] cxl: Cache endpoint HDM state during PCI enumeration
Quoted 15 lines · Benjamin
External email: Use caution opening links or attachments
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 185
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+static int cxl_pci_hdm_dvsec (struct pci_dev *pdev) +{ +u16 cap; +int dvsec; +int rc; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ;Why
-ENOTTYhere and below? I would've expected-ENXIOfor a missing capability.
Agreed. Fixed this in v14 for this and other similar cases.
Quoted 24 lines
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 361
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+ rc2 =pci_write_config_word (pdev,PCI_COMMAND , command); +if (rc2) +pci_err (pdev, +"failed to restore PCI_COMMAND after CXL HDM cache init: %d\n" , +pcibios_err_to_errno (rc2)); + +return rc;I think the above would flow better as:
rc =
__cxl_pci_hdm_read_info(pdev, map, dvsec, out);if (rc) { if (!restore_command) return rc; rc2 = pci_write_config_word(pdev, PCI_COMMAND, command); if (rc2) ... } else { rc = pci_write_config_word(pdev, PCI_COMMAND, command); return pcibios_err_to_errno(rc); }
Quoted 2 lines
return rc;
I reworked this flow in v14. Sashiko pointed out a small issue with error log. I’ll also fix that missing restore-error log in the next revision.
Quoted 12 lines
Quoted 12 lines
drivers/cxl/core/resource.c +12
-0+} + +static int __pci_cxl_hdm_cache_init (struct pci_dev *pdev) +{ + struct cxl_hdm_info *info __free(kfree) = NULL; +struct cxl_register_map map = { }; +struct resource *bar; +int dvsec; +int rc; + +scoped_guard (rwsem_read, &cxl_rwsem.dpa) + if (pdev->hdm)
Quoted 9 lines
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 378
Quoted 5 lines
drivers/cxl/core/resource.c +5
-0+return 0 ; + + dvsec =cxl_pci_hdm_dvsec (pdev); +if (dvsec <0 ) +return dvsec;Might be better to just return 0 here. If the device doesn't have a CXL capability then I wouldn't consider that an error from the perspective of the PCI subsystem.
Fixed this in v14. pci_cxl_hdm_cache_init() returns void; a missing CXL capability does not fail PCI enumeration now.
Quoted 8 lines
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 409
Quoted 4 lines
drivers/cxl/core/resource.c +4
-0+ + rc =__pci_cxl_hdm_cache_init (pdev); +if (rc && rc != -ENOTTY && rc != -ENODEV ) +pci_dbg (pdev,"CXL HDM cache init failed: %d\n" , rc);I don't know if it's worth filtering out
-ENOTTYand-ENODEVfor a debug message, but that's more of a question for the PCI people.
V14 logs all nonzero cache initialization results at debug level; the error filtering is gone.
Quoted 8 lines
Quoted 3 lines
cxl_core-y := $(CXL_CORE_SRC)/port.o
cxl_core-y += $(CXL_CORE_SRC)/pmem.o
-cxl_core-y += $(CXL_CORE_SRC)/regs.oDid you run CXL test with this change? I don't think there are any test symbols in
regs.c, but you should make sure this doesn't break anything there. You may not need to even fix anything if it does, but just make a note of the regression.
The test build is not broken. For v14, make M=tools/testing/cxl/ passed after this change and at every patch in the series. I haven't tested the runtime yet.
Signature · 2 lines
Regards, Srirangan
Jonathan Cameron
maintainerRe: [PATCH v13 09/15] cxl: Cache endpoint HDM state during PCI enumeration
In reply to Srirangan MadhavanQuoted 11 lines · On Tue, 22 Sep 2026 08:39:18 +0000 Srirangan Madhavan <[email protected]>…
PCI capability initialization runs before BAR resources are finalized, while driver binding is too late for driver-independent reset support. Create
pci_dev->hdmduringpci_bus_add_device(), after PCI resource setup and before driver binding.Cache BAR-relative HDM location, global control, decoder settings, and CXL Device DVSEC Control, then publish the completed cache under cxl_rwsem.dpa. Restore
PCI_COMMANDafter temporary MMIO access and reject decoder-count changes.Signed-off-by Srirangan Madhavan <[email protected]>
A few things inline. Maybe well overlap with Benjamin's comments!
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 236
Quoted 12 lines
drivers/cxl/core/resource.c +6
-0+static int __cxl_pci_hdm_read_info (struct pci_dev *pdev, +struct cxl_register_map *map,int dvsec, +struct cxl_hdm_info **out) +{ +struct cxl_reg_map *hdm_map = &map->component_map.hdm_decoder; +struct cxl_hdm_info *info =NULL ;
Fits in with comments below, but that should not be set to NULL up here.
Quoted 5 lines
drivers/cxl/core/resource.c +5
-0+resource_size_t hdm_offset; +void __iomem *hdm; +int decoder_count; +size_t required; +u16 dvsec_ctrl;
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 251
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+int rc; + + rc =cxl_setup_regs (map); +if (rc) +return rc; +if (!hdm_map->valid) +return -ENODEV ; +if (hdm_map->offset > map->max_size || + hdm_map->size > map->max_size - hdm_map->offset) +return -ENXIO ;
That first lot can happen before setup regs. Does it makes sense to do that, then setup the regs before the follow up check?
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+if (check_add_overflow (map->bar_offset, + (resource_size_t )hdm_map->offset, &hdm_offset)) +return -EOVERFLOW ; + + hdm =ioremap (map->resource + hdm_map->offset, hdm_map->size); +if (!hdm)
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 267
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+return -ENOMEM ; + + decoder_count =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (decoder_count <0 ) { + rc = decoder_count; + goto out_unmap; + } + + required =CXL_HDM_DECODER0_TL_HIGH (decoder_count -1 ) + +sizeof (u32);
I'd add a comment on why this size.
Quoted 5 lines
drivers/cxl/core/resource.c +5
-0+if (required > hdm_map->size) { +pci_err (pdev, +"CXL HDM decoder count exceeds mapped register block\n" ); + rc = -ENXIO ; + goto out_unmap;
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 282
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ } + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , + &dvsec_ctrl); +if (rc) { + rc =pcibios_err_to_errno (rc); + goto out_unmap; + } + + info =kzalloc (struct_size (info, settings, decoder_count),GFP_KERNEL );
Ah. So burried in here is where info is allocated.
If you can use struct_size for the size you can use the _obj allocators instead. Probably kzalloc_flex().
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+if (!info) { + rc = -ENOMEM ; + goto out_unmap; + } + + info->decoder_count = decoder_count; + info->hdm_bar = map->bar;
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 299
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ info->hdm_offset = hdm_offset; + info->hdm_size = hdm_map->size; + info->global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + info->dvsec_ctrl = dvsec_ctrl; + info->dvsec_ctrl_valid = true; + +for (int i =0 ; i < decoder_count; i++) { + rc =cxl_pci_hdm_read_decoder (pdev, &info->settings[i], hdm, i); +if (rc) + goto out_unmap;
with ordering below fixed this should goto free_info; or something like that.
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+ } + +/* +* A changed decoder count means the snapshot no longer describes the +* complete hardware state. Reject it rather than publish a partial cache. +*/
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 315
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ rc =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (rc != decoder_count) { +pci_err (pdev,"CXL HDM decoder count changed from %d to %d\n" , + decoder_count, rc); + rc = -ENXIO ; + goto out_unmap; + } + + *out = info; + info =NULL ;
Don't use this pattern of papering over stuff before then running your error path. Given no __free magic for iounmap just have two exit paths and duplicate the iounmap().
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 320
Quoted 5 lines
drivers/cxl/core/resource.c +5
-0+ rc =0 ; + +out_unmap: +kfree (info); +iounmap (hdm);
FWIW these are in the wrong order. You should unwind in reverse of setup.
Quoted 16 lines
drivers/cxl/core/resource.c +16
-0+return rc; +} + +static int cxl_pci_hdm_read_info (struct pci_dev *pdev, +struct cxl_register_map *map,int dvsec, +struct cxl_hdm_info **out) +{ +bool restore_command = false; +u16 command; +int rc, rc2; + +guard (pci_dev)(pdev); + + rc =pci_read_config_word (pdev,PCI_COMMAND , &command); +if (rc) +return pcibios_err_to_errno (rc);
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 346
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ +if (!(command &PCI_COMMAND_MEMORY )) { + rc =pci_write_config_word (pdev,PCI_COMMAND , + command |PCI_COMMAND_MEMORY ); +if (rc) +return pcibios_err_to_errno (rc); + restore_command = true; + } + + rc =__cxl_pci_hdm_read_info (pdev, map, dvsec, out);
To keep ownership and lifetimes clear I'd have this helper return "out" and use a PTR_ERR() for errors. Then I'd hand over to a __free() in here so that you can then explicitly hand ownership on by again returning out (with a ptr_no_free() or similar). Basically we want that flow of ownership to be obvious. That may lead to dances where you allocate then pass the ptr on via a ptr_no_free() in two lines but that is fine.
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 350
Quoted 4 lines
drivers/cxl/core/resource.c +4
-0+if (!restore_command) +return rc; + +if (!rc) {
This looks like a good path? If so have the bad path out of line not this.
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+ rc =pci_write_config_word (pdev,PCI_COMMAND , command); +return pcibios_err_to_errno (rc); + } + + rc2 =pci_write_config_word (pdev,PCI_COMMAND , command); +if (rc2)
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 366
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+pci_err (pdev, +"failed to restore PCI_COMMAND after CXL HDM cache init: %d\n" , +pcibios_err_to_errno (rc2)); + +return rc; +} + +static int __pci_cxl_hdm_cache_init (struct pci_dev *pdev) +{ + struct cxl_hdm_info *info __free(kfree) = NULL;
See cleanup.h documentation. I have no idea where the = NULL at the top pattern is coming from, but there has been strong push back from Linus and others on that in the kernel because it introduces ordering problems.
Here I have no idea where it is getting allocated. If possible make that allocation function return info.
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 374
Quoted 8 lines
drivers/cxl/core/resource.c +8
-0+struct cxl_register_map map = { }; +struct resource *bar; +int dvsec; +int rc; + + scoped_guard(rwsem_read, &cxl_rwsem.dpa) +if (pdev->hdm) +return 0 ;
Add a comment on why just taking the lock briefly for this check is useful. I'd expect something to need hdm to stick around though perhaps lifetimes are fine and that isn't an issue.
Quoted 13 lines
drivers/cxl/core/resource.c +13
-0+ + dvsec =cxl_pci_hdm_dvsec (pdev); +if (dvsec <0 ) +return dvsec; + + rc =cxl_find_regblock (pdev,CXL_REGLOC_RBI_COMPONENT , &map); +if (rc) +return rc; +if (map.bar <0 || map.bar >=PCI_STD_NUM_BARS ) +return -ENODEV ; + + bar = &pdev->resource[map.bar]; + if (!pci_resource_len(pdev, map.bar) ||
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 397
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ (bar->flags & (IORESOURCE_UNSET |IORESOURCE_DISABLED )) || + resource_type(bar) != IORESOURCE_MEM || !bar->start || !bar->end) + return -ENODEV; + + rc =cxl_pci_hdm_read_info (pdev, &map, dvsec, &info); +if (rc) +return rc; + +guard (rwsem_write)(&cxl_rwsem.dpa); +if (!pdev->hdm)
Needs a comment. I'm going to guess race given this pairs with the one above?
drivers/cxl/core/resource.c int cxl_hdm_unpack_decoder(struct cxl_decoder_settings *settings, int id, · line 419
Quoted 13 lines
drivers/cxl/core/resource.c +12
-0+ pdev->hdm =no_free_ptr (info); + +return 0 ; +} + +void pci_cxl_hdm_cache_release (struct pci_dev *pdev) +{ +struct cxl_hdm_info *info; + +guard (rwsem_write)(&cxl_rwsem.dpa); + info = pdev->hdm; + pdev->hdm =NULL ;
I'd like a comment on why we need to set this NULL. I'd kind of not really expect there to ever be a path by which this is checked after release but maybe there is?
Quoted 2 lines
drivers/cxl/core/resource.c +2
-0+kfree (info); +}
Re: [PATCH v13 09/15] cxl: Cache endpoint HDM state during PCI enumeration
Quoted 1 line · Jonathan Cameron
External email: Use caution opening links or attachments
Thank you.
I reworked these in v14 patch 10. cxl_setup_regs() remains before the hdm_map checks because it populates that map; I added a comment there. The patch also documents the register-block size check, uses kzalloc_flex() after validation, returns the cache pointer directly, unwinds in reverse order, and explains the cache publication and release steps.
Signature · 2 lines
Regards, Srirangan
PATCH v13 10/15 cxl: Add CXL Device Reset sequencing
In reply to Srirangan MadhavanImplement 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. Treat Control2 command bits as transient state, restore cache policy on exit, preserve the primary error, and abort when IOMMU preparation fails.
--- drivers/cxl/core/resource.c | 229 ++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 7 ++ include/uapi/linux/pci_regs.h | 14 +++ 3 files changed, 250 insertions(+)
drivers/cxl/core/resource.c +229 -0
@@ -8,6 +8,8 @@#include <linux/export.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>
@@ -419,3 +421,230 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev) pdev->hdm =NULL ;kfree (info); } + +/* +* 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. +*/ +#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 , +}; + +#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_no_replay (struct pci_dev *pdev,int dvsec, +u16 set,u16 clear) +{ +u16 ctrl2; +int rc; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL2 , &ctrl2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl2)) +return -ENODEV ; + +/* +* 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_no_replay (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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + } + +/* Do not miss completion concurrent with the polling deadline. */ + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + +return -ETIMEDOUT ; +} + +static int cxl_reset_disable_cache (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc; + + rc =cxl_reset_update_ctrl2_no_replay (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_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)) { +int last =ARRAY_SIZE (cxl_reset_timeout_ms) -1 ; + +pci_warn (pdev, +"unknown CXL reset timeout encoding %d; using %u ms\n" , + idx, cxl_reset_timeout_ms[last]); + idx = last; + } + + timeout_ms =max_t (u32, cxl_reset_timeout_ms[idx], +CXL_RESET_RRS_WAIT_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 || status2 ==U16_MAX ) + goto not_ready; +if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + +not_ready: +if (time_after_eq (jiffies, deadline)) +return -ETIMEDOUT ; + +msleep (CXL_RESET_STATUS_POLL_MS ); + } +} + +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc, rc2; + + rc =cxl_reset_disable_cache (pdev, dvsec, cap); +if (rc) + goto out_enable_cache; + +if (!pci_wait_for_pending_transaction (pdev)) { +pci_err (pdev,"timed out waiting for pending transactions\n" ); + rc = -ETIMEDOUT ; + goto out_enable_cache; + } + + rc =pci_dev_reset_iommu_prepare (pdev); +if (rc) { +pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); + goto out_enable_cache; + } + +/* Clear Memory Clear again even if this command write reports failure. */ + rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CXL_RST | +PCI_DVSEC_CXL_RST_MEM_CLR_EN ,0 ); +if (rc) + goto out_clear_memory; + + rc =cxl_reset_wait_done (pdev, dvsec, cap); + +out_clear_memory: + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_RST_MEM_CLR_EN ); +if (rc2) +pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , + rc2); + rc = rc ?: rc2; + +pci_dev_reset_iommu_done (pdev); + +out_enable_cache: +/* +* DISABLE_CACHING was the first preparation step. Restore the original +* cache policy last, after reset exclusion has ended. +*/ + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc2) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc = rc ?: rc2; + +return rc; +} + +int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +int dvsec, rc; +u16 cap, ctrl; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (cap)) +return -ENODEV ; +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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl)) +return -ENODEV ; +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); +}
include/cxl/cxl.h +7 -0
@@ -6,6 +6,7 @@#define __CXL_CXL_H__#include <linux/device.h> +#include <linux/errno.h> #include <linux/node.h> #include <linux/ioport.h> #include <linux/range.h>
@@ -201,6 +202,7 @@ struct cxl_register_map {#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) {
@@ -209,6 +211,11 @@ 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 /**
include/uapi/linux/pci_regs.h +14 -0
@@ -1352,8 +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)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 10/15] cxl: Add CXL Device Reset sequencing
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] cxl: 100s thread stall due to ignoring PCI errors in cxl_reset_wait_done() - [Medium] cxl…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] cxl: 100s thread stall due to ignoring PCI errors in cxl_reset_wait_done()
- [Medium] cxl: spurious reset timeout for devices with <= 100ms budget
--
commit 0940b93cca76ebfd20726ff13344213cdb258009
Author: Srirangan Madhavan <[email protected]>
cxl: Add CXL Device Reset sequencing
This commit implements CXL Device Reset for eligible Type 2 devices. It disables caching, prepares the IOMMU, initiates reset with Memory Clear enabled, and waits for completion.
Quoted 4 lines
drivers/cxl/core/resource.c +0
-0
[ ... ]
Quoted 12 lines
drivers/cxl/core/resource.c +12
-0+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)) { +int last =ARRAY_SIZE (cxl_reset_timeout_ms) -1 ; + + pci_warn(pdev,
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 543
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+"unknown CXL reset timeout encoding %d; using %u ms\n" , + idx, cxl_reset_timeout_ms[last]); + idx = last; + } + + timeout_ms =max_t (u32, cxl_reset_timeout_ms[idx], +CXL_RESET_RRS_WAIT_MS ); +msleep (CXL_RESET_RRS_WAIT_MS ); + deadline = jiffies +msecs_to_jiffies (timeout_ms - +CXL_RESET_RRS_WAIT_MS );
[Severity: Medium]
Does this logic yield a 0-jiffy budget and immediately timeout if the device is not ready on the first poll?
Because timeout_ms is clamped to a minimum of 100ms (CXL_RESET_RRS_WAIT_MS), the calculation of timeout_ms - CXL_RESET_RRS_WAIT_MS results in 0 for devices advertising 10ms or 100ms timeouts.
After the initial 100ms sleep, any U16_MAX response (which is common while the PCIe link is recovering) will immediately trigger -ETIMEDOUT without retrying, providing zero grace period for link recovery.
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 549
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+ +for (;;) { + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc || status2 ==U16_MAX ) + goto not_ready;
[Severity: Medium]
Could this cause a kernel thread to stall for up to 100 seconds if the device experiences a permanent error?
By grouping a non-zero rc (which pci_read_config_word can return for permanent errors like device disconnection, e.g., PCIBIOS_DEVICE_NOT_FOUND) with the transient U16_MAX state, the code falls through to not_ready.
If a device advertises a 100,000 ms timeout, this causes the kernel thread to sleep and poll uselessly for up to 100 seconds when the device is entirely gone, delaying recovery and blocking operations.
Quoted 12 lines
drivers/cxl/core/resource.c +12
-0+if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + +not_ready: +if (time_after_eq (jiffies, deadline)) +return -ETIMEDOUT ; + +msleep (CXL_RESET_STATUS_POLL_MS ); + } +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v13 10/15] cxl: Add CXL Device Reset sequencing
In reply to Srirangan MadhavanQuoted 159 lines · 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. Treat Control2 command bits as transient state, restore cache policy on exit, preserve the primary error, and abort when IOMMU preparation fails.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/resource.c | 229 ++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 7 ++ include/uapi/linux/pci_regs.h | 14 +++ 3 files changed, 250 insertions(+)drivers/cxl/core/resource.c +128
-0@@ -8,6 +8,8 @@#include <linux/export.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> @@ -419,3 +421,230 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev) pdev->hdm =NULL ;kfree (info); } + +/* +* 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. +*/ +#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 , +}; + +#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_no_replay (struct pci_dev *pdev,int dvsec, +u16 set,u16 clear) +{ +u16 ctrl2; +int rc; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL2 , &ctrl2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl2)) +return -ENODEV ; + +/* +* 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_no_replay (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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + } + +/* Do not miss completion concurrent with the polling deadline. */ + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + +return -ETIMEDOUT ; +} + +static int cxl_reset_disable_cache (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc; + + rc =cxl_reset_update_ctrl2_no_replay (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_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)) { +int last =ARRAY_SIZE (cxl_reset_timeout_ms) -1 ; + +pci_warn (pdev, +"unknown CXL reset timeout encoding %d; using %u ms\n" , + idx, cxl_reset_timeout_ms[last]); + idx = last; + } + + timeout_ms =max_t (u32, cxl_reset_timeout_ms[idx], +CXL_RESET_RRS_WAIT_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 || status2 ==U16_MAX ) + goto not_ready;
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 559
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + +not_ready: +if (time_after_eq (jiffies, deadline)) +return -ETIMEDOUT ; + +msleep (CXL_RESET_STATUS_POLL_MS );
The goto here isn't necessary, I think this is functionally equivalent:
if ((rc || status2 == U16_MAX) && time_after_eq(jiffies, deadline)) return -ETIMEDOUT; else if (status2 & PCI_DVSEC_CXL_RST_ERR) return -EIO; else if (status2 & PCI_DVSEC_CXL_RST_DONE) return 0; msleep(CXL_RESET_STATUS_POLL_MS);
Sashiko also brought it up, but the timeout is probably too long. I'd pick something like 20s instead. That's probably still too conservative, but I don't know enough to suggest a more practical value.
Quoted 45 lines
drivers/cxl/core/resource.c +45
-0+ } +} + +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc, rc2; + + rc =cxl_reset_disable_cache (pdev, dvsec, cap); +if (rc) + goto out_enable_cache; + +if (!pci_wait_for_pending_transaction (pdev)) { +pci_err (pdev,"timed out waiting for pending transactions\n" ); + rc = -ETIMEDOUT ; + goto out_enable_cache; + } + + rc =pci_dev_reset_iommu_prepare (pdev); +if (rc) { +pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); + goto out_enable_cache; + } + +/* Clear Memory Clear again even if this command write reports failure. */ + rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CXL_RST | +PCI_DVSEC_CXL_RST_MEM_CLR_EN ,0 ); +if (rc) + goto out_clear_memory; + + rc =cxl_reset_wait_done (pdev, dvsec, cap); + +out_clear_memory: + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_RST_MEM_CLR_EN ); +if (rc2) +pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , + rc2); + rc = rc ?: rc2; + +pci_dev_reset_iommu_done (pdev); + +out_enable_cache: +/* +* DISABLE_CACHING was the first preparation step. Restore the original
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 614
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+* cache policy last, after reset exclusion has ended. +*/ + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc2) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc = rc ?: rc2; + +return rc; +}
This is pretty messy, I think it would be cleaner if you split this into several functions:
static int __cxl_reset_execute(...)
{
int rc;
rc = cxl_reset_disable_cache(...);
if (rc)
return rc;
rc = cxl_clear_memory(...); // could be open coded, made a function for brevity
if (!rc)
rc = cxl_reset_wait_done(...);
rc = rc ?: cxl_clear_memory(...); // This one doesn't have the INIT_CXL_RST flag
pci_dev_reset_iommu_done(pdev);
return rc;
}static int cxl_reset_execute(...)
{
int rc;
rc = __cxl_reset_execute(..);
if (rc)
// log error
return rc ?: cxl_enable_cache(...);
}Hopefully what goes where makes sense just based on the names. I'm also not convinced you need to preserve the original error code throughout the function since it's essentially the same error conditions for all these functions AFAICT.
Quoted 95 lines
drivers/cxl/core/resource.c +36
-0+ +int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +int dvsec, rc; +u16 cap, ctrl; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (cap)) +return -ENODEV ; +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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl)) +return -ENODEV ; +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); +}include/cxl/cxl.h +7
-0@@ -6,6 +6,7 @@#define __CXL_CXL_H__#include <linux/device.h> +#include <linux/errno.h> #include <linux/node.h> #include <linux/ioport.h> #include <linux/range.h> @@ -201,6 +202,7 @@ struct cxl_register_map {#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) {@@ -209,6 +211,11 @@ 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 /** include/uapi/linux/pci_regs.h +14
-0@@ -1352,8 +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)
Dave Jiang
maintainerRe: [PATCH v13 10/15] cxl: Add CXL Device Reset sequencing
Quoted 234 lines · Benjamin
Quoted 159 lines · 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. Treat Control2 command bits as transient state, restore cache policy on exit, preserve the primary error, and abort when IOMMU preparation fails.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/resource.c | 229 ++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 7 ++ include/uapi/linux/pci_regs.h | 14 +++ 3 files changed, 250 insertions(+)drivers/cxl/core/resource.c +128
-0@@ -8,6 +8,8 @@#include <linux/export.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> @@ -419,3 +421,230 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev) pdev->hdm =NULL ;kfree (info); } + +/* +* 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. +*/ +#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 , +}; + +#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_no_replay (struct pci_dev *pdev,int dvsec, +u16 set,u16 clear) +{ +u16 ctrl2; +int rc; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL2 , &ctrl2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl2)) +return -ENODEV ; + +/* +* 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_no_replay (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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + } + +/* Do not miss completion concurrent with the polling deadline. */ + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (status2)) +return -ENODEV ; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + +return -ETIMEDOUT ; +} + +static int cxl_reset_disable_cache (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc; + + rc =cxl_reset_update_ctrl2_no_replay (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_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)) { +int last =ARRAY_SIZE (cxl_reset_timeout_ms) -1 ; + +pci_warn (pdev, +"unknown CXL reset timeout encoding %d; using %u ms\n" , + idx, cxl_reset_timeout_ms[last]); + idx = last; + } + + timeout_ms =max_t (u32, cxl_reset_timeout_ms[idx], +CXL_RESET_RRS_WAIT_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 || status2 ==U16_MAX ) + goto not_ready;drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 559
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + +not_ready: +if (time_after_eq (jiffies, deadline)) +return -ETIMEDOUT ; + +msleep (CXL_RESET_STATUS_POLL_MS );The goto here isn't necessary, I think this is functionally equivalent:
if ((rc || status2 == U16_MAX) && time_after_eq(jiffies, deadline)) return -ETIMEDOUT; else if (status2 & PCI_DVSEC_CXL_RST_ERR) return -EIO; else if (status2 & PCI_DVSEC_CXL_RST_DONE) return 0; msleep(CXL_RESET_STATUS_POLL_MS);Sashiko also brought it up, but the timeout is probably too long. I'd pick something like 20s instead. That's probably still too conservative, but I don't know enough to suggest a more practical value.
Quoted 49 lines
drivers/cxl/core/resource.c +49
-0+ } +} + +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc, rc2; + + rc =cxl_reset_disable_cache (pdev, dvsec, cap); +if (rc) + goto out_enable_cache; + +if (!pci_wait_for_pending_transaction (pdev)) { +pci_err (pdev,"timed out waiting for pending transactions\n" ); + rc = -ETIMEDOUT ; + goto out_enable_cache; + } + + rc =pci_dev_reset_iommu_prepare (pdev); +if (rc) { +pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); + goto out_enable_cache; + } + +/* Clear Memory Clear again even if this command write reports failure. */ + rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CXL_RST | +PCI_DVSEC_CXL_RST_MEM_CLR_EN ,0 ); +if (rc) + goto out_clear_memory; + + rc =cxl_reset_wait_done (pdev, dvsec, cap); + +out_clear_memory: + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_RST_MEM_CLR_EN ); +if (rc2) +pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , + rc2); + rc = rc ?: rc2; + +pci_dev_reset_iommu_done (pdev); + +out_enable_cache: +/* +* DISABLE_CACHING was the first preparation step. Restore the original +* cache policy last, after reset exclusion has ended. +*/ + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING );
Quoted 8 lines
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 614
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+if (rc2) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc = rc ?: rc2; + +return rc; +}This is pretty messy, I think it would be cleaner if you split this into several functions:
If we can avoid using gotos for normal function flows that would be great. It gets really messy to follow otherwise.
DJ
Quoted 131 lines
static int
__cxl_reset_execute(...){ int rc; rc = cxl_reset_disable_cache(...); if (rc) return rc; rc = cxl_clear_memory(...); // could be open coded, made a function for brevity if (!rc) rc = cxl_reset_wait_done(...); rc = rc ?: cxl_clear_memory(...); // This one doesn't have the INIT_CXL_RST flag pci_dev_reset_iommu_done(pdev); return rc; }static int
cxl_reset_execute(...){ int rc; rc = __cxl_reset_execute(..); if (rc) // log error return rc ?: cxl_enable_cache(...); }Hopefully what goes where makes sense just based on the names. I'm also not convinced you need to preserve the original error code throughout the function since it's essentially the same error conditions for all these functions AFAICT.
Quoted 95 lines
drivers/cxl/core/resource.c +36
-0+ +int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +int dvsec, rc; +u16 cap, ctrl; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ; + + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (cap)) +return -ENODEV ; +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 =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl); +if (rc) +return pcibios_err_to_errno (rc); +if (PCI_POSSIBLE_ERROR (ctrl)) +return -ENODEV ; +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); +}include/cxl/cxl.h +7
-0@@ -6,6 +6,7 @@#define __CXL_CXL_H__#include <linux/device.h> +#include <linux/errno.h> #include <linux/node.h> #include <linux/ioport.h> #include <linux/range.h> @@ -201,6 +202,7 @@ struct cxl_register_map {#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) {@@ -209,6 +211,11 @@ 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 /** include/uapi/linux/pci_regs.h +14
-0@@ -1352,8 +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)
Re: [PATCH v13 10/15] cxl: Add CXL Device Reset sequencing
In reply to Cheatham, BenjaminQuoted 29 lines · Benjamin
External email: Use caution opening links or attachments
Quoted 6 lines
drivers/cxl/core/resource.c +6
-0+ +for (;;) { + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc || status2 ==U16_MAX ) + goto not_ready;drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 559
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + +not_ready: +if (time_after_eq (jiffies, deadline)) +return -ETIMEDOUT ; + +msleep (CXL_RESET_STATUS_POLL_MS );The goto here isn't necessary, I think this is functionally equivalent:
if ((rc || status2 == U16_MAX) && time_after_eq(jiffies, deadline)) return -ETIMEDOUT; else if (status2 & PCI_DVSEC_CXL_RST_ERR) return -EIO; else if (status2 & PCI_DVSEC_CXL_RST_DONE) return 0; msleep(CXL_RESET_STATUS_POLL_MS);
Quoted 3 lines
Sashiko also brought it up, but the timeout is probably too long. I'd pick something like 20s instead. That's probably still too conservative, but I don't know enough to suggest a more practical value.
V14 removes the goto, caps the timeout at 20 seconds, and allows one poll after the 100 ms quiet period. A config read error now returns immediately.
Quoted 45 lines
drivers/cxl/core/resource.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 614
Quoted 12 lines
drivers/cxl/core/resource.c +12
-0+/* +* DISABLE_CACHING was the first preparation step. Restore the original +* cache policy last, after reset exclusion has ended. +*/ + rc2 =cxl_reset_update_ctrl2_no_replay ( + pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc2) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc = rc ?: rc2; + +return rc; +}This is pretty messy, I think it would be cleaner if you split this into several functions:
static int
__cxl_reset_execute(...){ int rc; rc = cxl_reset_disable_cache(...); if (rc) return rc; rc = cxl_clear_memory(...); // could be open coded, made a function for brevity if (!rc) rc = cxl_reset_wait_done(...); rc = rc ?: cxl_clear_memory(...); // This one doesn't have the INIT_CXL_RST flag pci_dev_reset_iommu_done(pdev); return rc; }static int
cxl_reset_execute(...){ int rc; rc = __cxl_reset_execute(..); if (rc) // log error return rc ?: cxl_enable_cache(...); }
Quoted 4 lines
Hopefully what goes where makes sense just based on the names. I'm also not convinced you need to preserve the original error code throughout the function since it's essentially the same error conditions for all these functions AFAICT.
Done in v14. I split the reset execution and Memory Clear handling into smaller helpers, following your suggested structure.
Signature · 2 lines
Regards, Srirangan
PATCH v13 11/15 cxl: Validate and synchronize HDM ranges around reset
In reply to Srirangan MadhavanRefuse reset unless enabled system-physical HDM ranges can be reserved exclusively and CPU-cache invalidation is available. Invalidate before reset and again after restoration, holding range reservations and IOMMU exclusion until the second invalidation completes.
Reject normalized-addressing decoders because their cached ranges are not system physical addresses. Ignore zero-size decoders because they map no address range.
--- drivers/cxl/core/resource.c | 244 +++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 5 deletions(-)
drivers/cxl/core/resource.c +239 -5
@@ -11,6 +11,8 @@#include <linux/iommu.h> #include <linux/jiffies.h> #include <linux/kernel.h> +#include <linux/list.h> +#include <linux/memregion.h> #include <linux/pci.h> #include <linux/slab.h>
@@ -437,6 +439,211 @@ static const u32 cxl_reset_timeout_ms[] = {#define CXL_CACHE_WBI_TIMEOUT_US 100000#define CXL_CACHE_WBI_POLL_US 100 +struct cxl_hdm_range { +struct list_head list; +struct pci_dev *pdev; +struct range hpa_range; +u64 len; +struct resource *res; +}; + +struct cxl_hdm_range_context { +struct list_head ranges; +}; + +static void cxl_hdm_range_context_destroy (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range, *next; + +list_for_each_entry_safe (range, next, &ctx->ranges, list) { +list_del (&range->list); +if (range->res) +release_mem_region (range->hpa_range.start, +resource_size (range->res)); +kfree (range); + } +} + +/* +* 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 (struct pci_dev *pdev, +const struct range *hpa_range) +{ +u64 len =range_len (hpa_range); + +if (!len) +return -EINVAL ; + +if (hpa_range->end >RESOURCE_SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds resource address size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +if (len >RESOURCE_SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds resource size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +if (len >SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds cache flush size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +return 0 ; +} + +static int cxl_hdm_range_add (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev,const struct range *hpa_range) +{ +struct cxl_hdm_range *range; +int rc; + + rc =cxl_hdm_range_validate (pdev, hpa_range); +if (rc) +return rc; + +list_for_each_entry (range, &ctx->ranges, list) +if (range->hpa_range.start == hpa_range->start && + range->hpa_range.end == hpa_range->end) +return 0 ; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ; + + range->pdev = pdev; + range->hpa_range = *hpa_range; + range->len =range_len (hpa_range); +list_add_tail (&range->list, &ctx->ranges); + +return 0 ; +} + +static int cxl_hdm_ranges_collect (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev) +{ +struct cxl_hdm_info *info; +int rc; + +guard (rwsem_read)(&cxl_rwsem.dpa); + info = pdev->hdm; +if (!info) { +pci_err (pdev,"CXL HDM decoder state unavailable\n" ); +return -ENXIO ; + } + +for (int i =0 ; i < info->decoder_count; i++) { +struct cxl_decoder_config *config = &info->settings[i].config; + +if (!(config->flags &CXL_DECODER_F_ENABLE )) +continue ; + +/* A committed zero-size decoder maps no HPA. */ +if (!range_len (&config->hpa_range)) +continue ; + +if (config->flags &CXL_DECODER_F_NORMALIZED_ADDRESSING ) { +pci_err (pdev, +"CXL reset does not support normalized address decoders\n" ); +return -EOPNOTSUPP ; + } + + rc =cxl_hdm_range_add (ctx, pdev, &config->hpa_range); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_hdm_ranges_request (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range; + +lockdep_assert_held_write (&cxl_rwsem.region); + +list_for_each_entry (range, &ctx->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) { +pci_err (range->pdev, +"cannot reset while CXL memory range is busy [%#llx-%#llx]\n" , + hpa_range->start, hpa_range->end); +return -EBUSY ; + } + } + +return 0 ; +} + +static int cxl_hdm_ranges_invalidate (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range; +int rc =0 ; + +list_for_each_entry (range, &ctx->ranges, list) { +const struct range *hpa_range = &range->hpa_range; +int rc2; + + rc2 =cpu_cache_invalidate_memregion (hpa_range->start, range->len); +if (rc2) +pci_err (range->pdev, +"failed to invalidate CPU cache [%#llx-%#llx]: %d\n" , + hpa_range->start, hpa_range->end, rc2); + rc = rc ?: rc2; + } + +return rc; +} + +static int cxl_hdm_ranges_prepare (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev) +{ +int rc; + +lockdep_assert_held_write (&cxl_rwsem.region); + +if (!cpu_cache_has_invalidate_memregion ()) { +pci_err (pdev,"CPU cache invalidation unavailable\n" ); +return -ENXIO ; + } + + rc =cxl_hdm_ranges_collect (ctx, pdev); +if (rc) +return rc; + + rc =cxl_hdm_ranges_request (ctx); +if (rc) +return rc; + +return cxl_hdm_ranges_invalidate (ctx); +} + +static int cxl_hdm_ranges_finish (struct cxl_hdm_range_context *ctx) +{ +int rc; + +lockdep_assert_held_write (&cxl_rwsem.region); + + rc =cxl_hdm_ranges_invalidate (ctx); +cxl_hdm_range_context_destroy (ctx); + +return rc; +} +#define CXL_RESET_CTRL2_CMD_MASK \ (PCI_DVSEC_CXL_INIT_CACHE_WBI | PCI_DVSEC_CXL_INIT_CXL_RST)
@@ -560,24 +767,25 @@ static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap) } } -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_range_context *range_ctx) {int rc, rc2; rc =cxl_reset_disable_cache (pdev, dvsec, cap);if (rc) - goto out_enable_cache; + goto out_destroy_ranges;if (!pci_wait_for_pending_transaction (pdev)) {pci_err (pdev,"timed out waiting for pending transactions\n" ); rc = -ETIMEDOUT ; - goto out_enable_cache; + goto out_destroy_ranges; } rc =pci_dev_reset_iommu_prepare (pdev);if (rc) {pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); - goto out_enable_cache; + goto out_destroy_ranges; }/* Clear Memory Clear again even if this command write reports failure. */
@@ -597,7 +805,14 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) rc2); rc = rc ?: rc2; +/* Evict lines fetched during reset before ending DMA exclusion. */ + rc2 =cxl_hdm_ranges_finish (range_ctx); + rc = rc ?: rc2;pci_dev_reset_iommu_done (pdev); + goto out_enable_cache; + +out_destroy_ranges: +cxl_hdm_range_context_destroy (range_ctx); out_enable_cache:/*
@@ -615,6 +830,7 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)int cxl_reset_function (struct pci_dev *pdev,bool probe) { +struct cxl_hdm_range_context range_ctx;int dvsec, rc;u16 cap, ctrl;
@@ -646,5 +862,23 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)if (probe)return 0 ; -return cxl_reset_execute (pdev, dvsec, cap); +/* The cache is owned by @pdev and does not require a bound CXL driver. */ + scoped_guard(rwsem_read, &cxl_rwsem.dpa) +if (!pdev->hdm || !pdev->hdm->hdm_size) +return -ENOTTY ; + +if (!cpu_cache_has_invalidate_memregion ()) +return -ENOTTY ; + +INIT_LIST_HEAD (&range_ctx.ranges); + + scoped_guard(rwsem_write, &cxl_rwsem.region) { + rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); +if (!rc) + rc =cxl_reset_execute (pdev, dvsec, cap, &range_ctx); +else +cxl_hdm_range_context_destroy (&range_ctx); + } + +return rc; }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 11/15] cxl: Validate and synchronize HDM ranges around reset
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Refuse reset unless enabled system-physical HDM ranges can be reserved exclusively and CPU-cache invalidation is available. Invalidate before reset and again after restoration, holding range reservations and IOMMU exclusion until the second invalidation completes.
Reject normalized-addressing decoders because their cached ranges are not system physical addresses. Ignore zero-size decoders because they map no address range.
Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Re: [PATCH v13 11/15] cxl: Validate and synchronize HDM ranges around reset
In reply to Srirangan MadhavanQuoted 98 lines · 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 after restoration, holding range reservations and IOMMU exclusion until the second invalidation completes.
Reject normalized-addressing decoders because their cached ranges are not system physical addresses. Ignore zero-size decoders because they map no address range.
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/resource.c | 244 +++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 5 deletions(-)drivers/cxl/core/resource.c +69
-0@@ -11,6 +11,8 @@#include <linux/iommu.h> #include <linux/jiffies.h> #include <linux/kernel.h> +#include <linux/list.h> +#include <linux/memregion.h> #include <linux/pci.h> #include <linux/slab.h> @@ -437,6 +439,211 @@ static const u32 cxl_reset_timeout_ms[] = {#define CXL_CACHE_WBI_TIMEOUT_US 100000#define CXL_CACHE_WBI_POLL_US 100 +struct cxl_hdm_range { +struct list_head list; +struct pci_dev *pdev; +struct range hpa_range; +u64 len; +struct resource *res; +}; + +struct cxl_hdm_range_context { +struct list_head ranges; +}; + +static void cxl_hdm_range_context_destroy (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range, *next; + +list_for_each_entry_safe (range, next, &ctx->ranges, list) { +list_del (&range->list); +if (range->res) +release_mem_region (range->hpa_range.start, +resource_size (range->res)); +kfree (range); + } +} + +/* +* 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 (struct pci_dev *pdev, +const struct range *hpa_range) +{ +u64 len =range_len (hpa_range); + +if (!len) +return -EINVAL ; + +if (hpa_range->end >RESOURCE_SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds resource address size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +if (len >RESOURCE_SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds resource size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +if (len >SIZE_MAX ) { +pci_err (pdev, +"CXL reset range [%#llx-%#llx] exceeds cache flush size\n" , + hpa_range->start, hpa_range->end); +return -EOVERFLOW ; + } + +return 0 ; +} + +static int cxl_hdm_range_add (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev,const struct range *hpa_range) +{
drivers/cxl/core/resource.c static const u32 cxl_reset_timeout_ms[] = { · line 518
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+struct cxl_hdm_range *range; +int rc; + + rc =cxl_hdm_range_validate (pdev, hpa_range); +if (rc) +return rc; + +list_for_each_entry (range, &ctx->ranges, list) +if (range->hpa_range.start == hpa_range->start && + range->hpa_range.end == hpa_range->end)
Maybe resource_contains() instead? I don't think there'll be any overlap, but no point in reserving a fully contained resource. You'd have to add some extra logic to remove the smaller resource though.
Quoted 26 lines
drivers/cxl/core/resource.c +26
-0+return 0 ; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ; + + range->pdev = pdev; + range->hpa_range = *hpa_range; + range->len =range_len (hpa_range); +list_add_tail (&range->list, &ctx->ranges); + +return 0 ; +} + +static int cxl_hdm_ranges_collect (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev) +{ +struct cxl_hdm_info *info; +int rc; + +guard (rwsem_read)(&cxl_rwsem.dpa); + info = pdev->hdm; +if (!info) { +pci_err (pdev,"CXL HDM decoder state unavailable\n" ); +return -ENXIO ; + }
drivers/cxl/core/resource.c static const u32 cxl_reset_timeout_ms[] = { · line 554
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ +for (int i =0 ; i < info->decoder_count; i++) { +struct cxl_decoder_config *config = &info->settings[i].config; + +if (!(config->flags &CXL_DECODER_F_ENABLE )) +continue ; + +/* A committed zero-size decoder maps no HPA. */ +if (!range_len (&config->hpa_range)) +continue ;
Nit: these two if statements could be on a single line and just put the comment above it, it's pretty obvious which condition the comment is talking about.
Quoted 128 lines
drivers/cxl/core/resource.c +97
-4+ +if (config->flags &CXL_DECODER_F_NORMALIZED_ADDRESSING ) { +pci_err (pdev, +"CXL reset does not support normalized address decoders\n" ); +return -EOPNOTSUPP ; + } + + rc =cxl_hdm_range_add (ctx, pdev, &config->hpa_range); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_hdm_ranges_request (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range; + +lockdep_assert_held_write (&cxl_rwsem.region); + +list_for_each_entry (range, &ctx->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) { +pci_err (range->pdev, +"cannot reset while CXL memory range is busy [%#llx-%#llx]\n" , + hpa_range->start, hpa_range->end); +return -EBUSY ; + } + } + +return 0 ; +} + +static int cxl_hdm_ranges_invalidate (struct cxl_hdm_range_context *ctx) +{ +struct cxl_hdm_range *range; +int rc =0 ; + +list_for_each_entry (range, &ctx->ranges, list) { +const struct range *hpa_range = &range->hpa_range; +int rc2; + + rc2 =cpu_cache_invalidate_memregion (hpa_range->start, range->len); +if (rc2) +pci_err (range->pdev, +"failed to invalidate CPU cache [%#llx-%#llx]: %d\n" , + hpa_range->start, hpa_range->end, rc2); + rc = rc ?: rc2; + } + +return rc; +} + +static int cxl_hdm_ranges_prepare (struct cxl_hdm_range_context *ctx, +struct pci_dev *pdev) +{ +int rc; + +lockdep_assert_held_write (&cxl_rwsem.region); + +if (!cpu_cache_has_invalidate_memregion ()) { +pci_err (pdev,"CPU cache invalidation unavailable\n" ); +return -ENXIO ; + } + + rc =cxl_hdm_ranges_collect (ctx, pdev); +if (rc) +return rc; + + rc =cxl_hdm_ranges_request (ctx); +if (rc) +return rc; + +return cxl_hdm_ranges_invalidate (ctx); +} + +static int cxl_hdm_ranges_finish (struct cxl_hdm_range_context *ctx) +{ +int rc; + +lockdep_assert_held_write (&cxl_rwsem.region); + + rc =cxl_hdm_ranges_invalidate (ctx); +cxl_hdm_range_context_destroy (ctx); + +return rc; +} +#define CXL_RESET_CTRL2_CMD_MASK \ (PCI_DVSEC_CXL_INIT_CACHE_WBI | PCI_DVSEC_CXL_INIT_CXL_RST)@@ -560,24 +767,25 @@ static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap) } } -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_range_context *range_ctx) {int rc, rc2; rc =cxl_reset_disable_cache (pdev, dvsec, cap);if (rc) - goto out_enable_cache; + goto out_destroy_ranges;if (!pci_wait_for_pending_transaction (pdev)) {pci_err (pdev,"timed out waiting for pending transactions\n" ); rc = -ETIMEDOUT ; - goto out_enable_cache; + goto out_destroy_ranges; } rc =pci_dev_reset_iommu_prepare (pdev);if (rc) {pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); - goto out_enable_cache; + goto out_destroy_ranges; }/* Clear Memory Clear again even if this command write reports failure. */ @@ -597,7 +805,14 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) rc2); rc = rc ?: rc2;
drivers/cxl/core/resource.c static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) · line 816
Quoted 10 lines
drivers/cxl/core/resource.c +7
-0+/* Evict lines fetched during reset before ending DMA exclusion. */ + rc2 =cxl_hdm_ranges_finish (range_ctx); + rc = rc ?: rc2;pci_dev_reset_iommu_done (pdev); + goto out_enable_cache; + +out_destroy_ranges: +cxl_hdm_range_context_destroy (range_ctx);
Same thing here as the last patch, but I'll let you do this one ;).
Quoted 22 lines
drivers/cxl/core/resource.c +8
-1out_enable_cache:/* @@ -615,6 +830,7 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)int cxl_reset_function (struct pci_dev *pdev,bool probe) { +struct cxl_hdm_range_context range_ctx;int dvsec, rc;u16 cap, ctrl;@@ -646,5 +862,23 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)if (probe)return 0 ; -return cxl_reset_execute (pdev, dvsec, cap); +/* The cache is owned by @pdev and does not require a bound CXL driver. */ + scoped_guard(rwsem_read, &cxl_rwsem.dpa) +if (!pdev->hdm || !pdev->hdm->hdm_size) +return -ENOTTY ; + +if (!cpu_cache_has_invalidate_memregion ()) +return -ENOTTY ;
drivers/cxl/core/resource.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 881
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ +INIT_LIST_HEAD (&range_ctx.ranges); + + scoped_guard(rwsem_write, &cxl_rwsem.region) { + rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); +if (!rc) + rc =cxl_reset_execute (pdev, dvsec, cap, &range_ctx); +else +cxl_hdm_range_context_destroy (&range_ctx); + }
Probably better to just copy-paste the range context destroy call and re-order as:
rc = cxl_hdm_ranges_prepare(&range_ctx, pdev);
if (rc) {
cxl_hdm_range_context_destroy(&range_ctx);
return rc;
}
rc = cxl_reset_execute(pdev, dvsec, cap, &range_ctx);
cxl_hdm_range_context_destroy(&range_ctx);instead.
Quoted 3 lines
drivers/cxl/core/resource.c +2
-0+ +return rc; }
Re: [PATCH v13 11/15] cxl: Validate and synchronize HDM ranges around reset
Quoted 10 lines · Benjamin
External email: Use caution opening links or attachments
drivers/cxl/core/resource.c static const u32 cxl_reset_timeout_ms[] = { · line 518
Quoted 4 lines
drivers/cxl/core/resource.c +4
-0+ +list_for_each_entry (range, &ctx->ranges, list) +if (range->hpa_range.start == hpa_range->start && + range->hpa_range.end == hpa_range->end)Maybe
resource_contains()instead? I don't think there'll be any overlap, but no point in reserving a fully contained resource. You'd have to add some extra logic to remove the smaller resource though.
Done in v14. I used range_contains() for the struct range values. Already covered is skipped, and a smaller collected range is removed when a larger one arrives.
Quoted 11 lines
drivers/cxl/core/resource.c static const u32 cxl_reset_timeout_ms[] = { · line 554
Quoted 7 lines
drivers/cxl/core/resource.c +7
-0+ +if (!(config->flags &CXL_DECODER_F_ENABLE )) +continue ; + +/* A committed zero-size decoder maps no HPA. */ +if (!range_len (&config->hpa_range)) +continue ;Nit: these two if statements could be on a single line and just put the comment above it, it's pretty obvious which condition the comment is talking about.
Done
Quoted 7 lines
drivers/cxl/core/resource.c static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) · line 815
Quoted 4 lines
drivers/cxl/core/resource.c +3
-0+ +out_destroy_ranges: +cxl_hdm_range_context_destroy (range_ctx);Same thing here as the last patch, but I'll let you do this one ;).
Ack. V14 splits the reset sequence into smaller helpers and removes this cleanup-label flow. Please let me know if it is better now.
Quoted 23 lines
drivers/cxl/core/resource.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 881
Quoted 10 lines
drivers/cxl/core/resource.c +10
-0+ +INIT_LIST_HEAD (&range_ctx.ranges); + + scoped_guard(rwsem_write, &cxl_rwsem.region) { + rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); +if (!rc) + rc =cxl_reset_execute (pdev, dvsec, cap, &range_ctx); +else +cxl_hdm_range_context_destroy (&range_ctx); + }Probably better to just copy-paste the range context destroy call and re-order as:
rc = cxl_hdm_ranges_prepare(&range_ctx, pdev); if (rc) { cxl_hdm_range_context_destroy(&range_ctx); return rc; } rc = cxl_reset_execute(pdev, dvsec, cap, &range_ctx); cxl_hdm_range_context_destroy(&range_ctx);
Quoted 2 lines
instead.
Ack. In v14, cxl_reset_execute() owns the range context from preparation through cleanup, so this handoff between functions is gone.
Signature · 2 lines
Regards, Srirangan
PATCH v13 12/15 PCI/CXL: Reject reset with unsafe function scope
In reply to Srirangan MadhavanThe per-device PCI reset interface must not reset sibling functions. Reject cxl_reset and cxl_bus for multifunction devices and PFs with enabled VFs.
--- drivers/cxl/core/resource.c | 3 +++ drivers/pci/pci.c | 3 +++ 2 files changed, 6 insertions(+)
drivers/cxl/core/resource.c +3 -0
@@ -859,6 +859,9 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))return -ENOTTY ; +if (pdev->multifunction ||pci_num_vf (pdev)) +return -ENOTTY ; +if (probe)return 0 ;
drivers/pci/pci.c +3 -0
@@ -5026,6 +5026,9 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (!dvsec)return -ENOTTY ; +if (dev->multifunction ||pci_num_vf (dev)) +return -ENOTTY ; +if (probe)return 0 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 12/15] PCI/CXL: Reject reset with unsafe function scope
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
The per-device PCI reset interface must not reset sibling functions. Reject
cxl_resetandcxl_busfor multifunction devices and PFs with enabled VFs.
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v13 12/15] PCI/CXL: Reject reset with unsafe function scope
In reply to Srirangan MadhavanQuoted 5 lines · Srirangan Madhavan
The per-device PCI reset interface must not reset sibling functions. Reject
cxl_resetandcxl_busfor multifunction devices and PFs with enabled VFs.Reported-by Alex Williamson <[email protected]>
I'm not sure Reported-by is the right tag, I would expect a Suggested-by instead. I think Reported-by is really more for bug fixes, but I could be wrong.
Thanks,
Ben
Quoted 36 lines
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/resource.c | 3 +++ drivers/pci/pci.c | 3 +++ 2 files changed, 6 insertions(+)drivers/cxl/core/resource.c +3
-0@@ -859,6 +859,9 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))return -ENOTTY ; +if (pdev->multifunction ||pci_num_vf (pdev)) +return -ENOTTY ; +if (probe)return 0 ;drivers/pci/pci.c +3
-0@@ -5026,6 +5026,9 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (!dvsec)return -ENOTTY ; +if (dev->multifunction ||pci_num_vf (dev)) +return -ENOTTY ; +if (probe)return 0 ;
Dave Jiang
maintainerRe: [PATCH v13 12/15] PCI/CXL: Reject reset with unsafe function scope
Quoted 9 lines · Benjamin
Quoted 5 lines · Srirangan Madhavan
The per-device PCI reset interface must not reset sibling functions. Reject
cxl_resetandcxl_busfor multifunction devices and PFs with enabled VFs.Reported-by Alex Williamson <[email protected]>I'm not sure Reported-by is the right tag, I would expect a Suggested-by instead. I think Reported-by is really more for bug fixes, but I could be wrong.
Yes Suggested-by is the right tag.
DJ
Quoted 41 lines
Thanks,
BenQuoted 36 lines
Signed-off-by Srirangan Madhavan <[email protected]>--- drivers/cxl/core/resource.c | 3 +++ drivers/pci/pci.c | 3 +++ 2 files changed, 6 insertions(+)drivers/cxl/core/resource.c +3
-0@@ -859,6 +859,9 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))return -ENOTTY ; +if (pdev->multifunction ||pci_num_vf (pdev)) +return -ENOTTY ; +if (probe)return 0 ;drivers/pci/pci.c +3
-0@@ -5026,6 +5026,9 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (!dvsec)return -ENOTTY ; +if (dev->multifunction ||pci_num_vf (dev)) +return -ENOTTY ; +if (probe)return 0 ;
PATCH v13 13/15 cxl: Restore CXL state after PCI reset
In reply to Srirangan MadhavanRestore the PCI configuration needed for HDM access, followed by cached HDM decoder and CXL Device DVSEC state. Recommit a cleared locked decoder before restoring its lock, but 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.
--- drivers/cxl/core/resource.c | 442 +++++++++++++++++++++++++++++++++--- 1 file changed, 408 insertions(+), 34 deletions(-)
drivers/cxl/core/resource.c +408 -34
@@ -13,6 +13,7 @@#include <linux/kernel.h> #include <linux/list.h> #include <linux/memregion.h> +#include <linux/overflow.h> #include <linux/pci.h> #include <linux/slab.h>
@@ -82,6 +83,26 @@ static int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; } +static int cxld_await_uncommit (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 void setup_hw_decoder (void __iomem *hdm,struct cxl_decoder_settings *settings) {
@@ -424,6 +445,323 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)kfree (info); } +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 ERR_PTR (-EINVAL ); + + res = &pdev->resource[bar]; +if (!pci_resource_len (pdev, bar) || + (res->flags & (IORESOURCE_UNSET |IORESOURCE_DISABLED )) || +resource_type (res) !=IORESOURCE_MEM || !res->start || !res->end) +return ERR_PTR (-ENODEV ); +if (offset >resource_size (res) || size >resource_size (res) - offset) +return ERR_PTR (-ENODEV ); +if (check_add_overflow (res->start, offset, &hdm_start)) +return ERR_PTR (-EOVERFLOW ); + + hdm =ioremap (hdm_start, size); +if (!hdm) { +pci_err (pdev,"failed to remap CXL HDM decoder registers\n" ); +return ERR_PTR (-ENOMEM ); + } + +return hdm; +} + +static int cxl_hdm_decoder_uncommit (struct pci_dev *pdev,void __iomem *hdm, +int id,bool *retained) +{ +u32 ctrl; +int rc; + + *retained = false; + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +if (PCI_POSSIBLE_ERROR (ctrl)) +return -EIO ; +if (ctrl &CXL_HDM_DECODER0_CTRL_COMMIT_ERROR ) { +pci_err (pdev,"CXL HDM decoder %d has commit error set\n" , id); +return -EIO ; + } + +if (ctrl &CXL_HDM_DECODER0_CTRL_LOCK ) { +if (ctrl &CXL_HDM_DECODER0_CTRL_COMMITTED ) { + *retained = true; +return 0 ; + } + +pci_err (pdev,"CXL HDM decoder %d is locked and uncommitted\n" , + id); +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)); + + rc =cxld_await_uncommit (hdm, id); +if (rc) +pci_err (pdev,"CXL HDM decoder %d uncommit failed: %d\n" , id, + rc); + +return rc; +} + +static bool cxl_hdm_decoder_settings_match ( +const struct cxl_decoder_settings *saved, +const struct cxl_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->target_or_skip_reg_val == active->target_or_skip_reg_val; +} + +static int cxl_restore_hdm_decoder_commit ( +void __iomem *hdm,struct cxl_decoder_settings *settings) +{ +guard (rwsem_read)(&cxl_rwsem.dpa); + +cxl_commit_start (hdm, settings); +return cxl_commit_wait (hdm, settings); +} + +static int cxl_restore_hdm_decoder (struct pci_dev *pdev,void __iomem *hdm, +struct cxl_decoder_settings *settings) +{ +struct cxl_decoder_config *config = &settings->config; +bool retained; +u32 ctrl; +int rc; + + rc =cxl_hdm_decoder_uncommit (pdev, hdm, config->id, &retained); +if (rc) +return rc; + +if (retained) { +struct cxl_decoder_settings active; + + rc =cxl_pci_hdm_read_decoder (pdev, &active, hdm, + config->id); +if (rc) +return rc; +if (!cxl_hdm_decoder_settings_match (settings, &active)) { +pci_err (pdev, +"CXL HDM decoder %d retained unexpected locked state\n" , + config->id); +return -EIO ; + } +return 0 ; + } + +if (!(config->flags &CXL_DECODER_F_ENABLE )) +return 0 ; + + rc =cxl_restore_hdm_decoder_commit (hdm, settings); +if (rc) { +pci_err (pdev,"CXL HDM decoder %d restore failed: %d\n" , + config->id, rc); +return rc; + } + +if (!(config->flags &CXL_DECODER_F_LOCK )) +return 0 ; + + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (config->id)); +if (PCI_POSSIBLE_ERROR (ctrl)) +return -EIO ; + ctrl |=CXL_HDM_DECODER0_CTRL_LOCK ; +writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (config->id)); + + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (config->id)); +if (PCI_POSSIBLE_ERROR (ctrl) || + !(ctrl &CXL_HDM_DECODER0_CTRL_LOCK )) { +pci_err (pdev,"CXL HDM decoder %d failed to restore lock\n" , + config->id); +return -EIO ; + } + +return 0 ; +} + +static int cxl_snapshot_hdm (struct pci_dev *pdev, +struct cxl_hdm_info **snapshot) +{ +struct cxl_hdm_info *info; +size_t size; + +guard (rwsem_read)(&cxl_rwsem.dpa); + info = pdev->hdm; +if (!info) +return -ENXIO ; +if (info->decoder_count <=0 ) +return -EINVAL ; + + size =struct_size (info, settings, info->decoder_count); +if (size ==SIZE_MAX ) +return -EOVERFLOW ; + *snapshot =kmemdup (info, size,GFP_KERNEL ); +if (!*snapshot) +return -ENOMEM ; + +return 0 ; +} + +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 ; + +/* +* pci_restore_state() is the available interface for restoring BARs and +* related PCI state needed to reach HDM MMIO. Temporarily alter its saved +* command value so that it 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,u16 saved_ctrl) +{ +int dvsec; +int rc; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return -ENOTTY ; + + rc =pci_write_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , + saved_ctrl); +if (rc) +return pcibios_err_to_errno (rc); + +return 0 ; +} + +static int cxl_restore_state (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot) +{ +void __iomem *hdm; +int count; +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); + } + + count =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (count != snapshot->decoder_count) { +pci_err (pdev,"CXL HDM decoder count changed from %d to %d\n" , + snapshot->decoder_count, count); + rc = -ENXIO ; + goto out_unmap; + } + +/* 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_restore_hdm_decoder (pdev, hdm, + &snapshot->settings[i]); +if (rc) + goto out_unmap; + } + +/* Flush posted HDM writes before restoring protocol enables. */ +readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + +if (!snapshot->dvsec_ctrl_valid) { +pci_err (pdev,"CXL DVSEC control snapshot is invalid\n" ); + rc = -ENXIO ; + goto out_unmap; + } + rc =cxl_restore_dvsec_ctrl (pdev, snapshot->dvsec_ctrl); +if (rc) +pci_err (pdev,"failed to restore CXL DVSEC control: %d\n" , rc); + +out_unmap: +iounmap (hdm); +return rc; +} + +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)); + + 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; +} +/* * 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
@@ -726,6 +1064,18 @@ static int cxl_reset_disable_cache(struct pci_dev *pdev, int dvsec, u16 cap)return cxl_reset_wait_cache_wbi (pdev, dvsec); } +static int cxl_reset_restore_cache_policy (struct pci_dev *pdev,int dvsec) +{ +int rc; + + rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,0 , +PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc); + +return rc; +} +static int cxl_reset_wait_done (struct pci_dev *pdev,int dvsec,u16 cap) {unsigned long deadline;
@@ -767,28 +1117,41 @@ static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap) } } -static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap, -struct cxl_hdm_range_context *range_ctx) +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) { +struct cxl_hdm_info *snapshot =NULL ; +struct cxl_hdm_range_context range_ctx; +u16 command;int rc, rc2; - rc =cxl_reset_disable_cache (pdev, dvsec, cap); +down_write (&cxl_rwsem.region); +INIT_LIST_HEAD (&range_ctx.ranges); + + rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); +if (rc) + goto out_destroy_ranges; + + rc =cxl_snapshot_hdm (pdev, &snapshot);if (rc) goto out_destroy_ranges; + rc =cxl_reset_disable_cache (pdev, dvsec, cap); +if (rc) + goto out_destroy_enable_cache; +if (!pci_wait_for_pending_transaction (pdev)) {pci_err (pdev,"timed out waiting for pending transactions\n" ); rc = -ETIMEDOUT ; - goto out_destroy_ranges; + goto out_destroy_enable_cache; } rc =pci_dev_reset_iommu_prepare (pdev);if (rc) {pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); - goto out_destroy_ranges; + goto out_destroy_enable_cache; } -/* Clear Memory Clear again even if this command write reports failure. */ +/* Clear Memory Clear below even if this command write reports failure. */ rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,PCI_DVSEC_CXL_INIT_CXL_RST |PCI_DVSEC_CXL_RST_MEM_CLR_EN ,0 );
@@ -798,39 +1161,60 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap, rc =cxl_reset_wait_done (pdev, dvsec, cap); out_clear_memory: - rc2 =cxl_reset_update_ctrl2_no_replay ( - pdev, dvsec,0 ,PCI_DVSEC_CXL_RST_MEM_CLR_EN ); + rc2 =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,0 , +PCI_DVSEC_CXL_RST_MEM_CLR_EN );if (rc2)pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , - rc2); + rc2); rc = rc ?: rc2; +if (rc) + goto out_finish_ranges; + +cxl_restore_pci_state_for_hdm_restore (pdev, &command); + rc =cxl_restore_state (pdev, snapshot); +out_finish_ranges:/* Evict lines fetched during reset before ending DMA exclusion. */ - rc2 =cxl_hdm_ranges_finish (range_ctx); + rc2 =cxl_hdm_ranges_finish (&range_ctx); rc = rc ?: rc2; +if (rc) + goto out_fail_closed; +pci_dev_reset_iommu_done (pdev); - goto out_enable_cache; -out_destroy_ranges: -cxl_hdm_range_context_destroy (range_ctx); + rc =cxl_reset_restore_cache_policy (pdev, dvsec); +if (rc) { +cxl_reset_save_disabled_state (pdev); + goto out_unlock; + } -out_enable_cache: -/* -* DISABLE_CACHING was the first preparation step. Restore the original -* cache policy last, after reset exclusion has ended. -*/ - rc2 =cxl_reset_update_ctrl2_no_replay ( - pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING ); -if (rc2) -pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc =cxl_reset_save_restored_state (pdev, command); + goto out_unlock; + +out_fail_closed: +cxl_reset_save_disabled_state (pdev); +pci_dev_reset_iommu_done (pdev); + rc2 =cxl_reset_restore_cache_policy (pdev, dvsec); rc = rc ?: rc2; + goto out_unlock; + +out_destroy_enable_cache: +cxl_hdm_range_context_destroy (&range_ctx); + rc2 =cxl_reset_restore_cache_policy (pdev, dvsec); + rc = rc ?: rc2; + goto out_unlock; + +out_destroy_ranges: +cxl_hdm_range_context_destroy (&range_ctx); +out_unlock: +kfree (snapshot); +up_write (&cxl_rwsem.region);return rc; }int cxl_reset_function (struct pci_dev *pdev,bool probe) { -struct cxl_hdm_range_context range_ctx;int dvsec, rc;u16 cap, ctrl;
@@ -873,15 +1257,5 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)if (!cpu_cache_has_invalidate_memregion ())return -ENOTTY ; -INIT_LIST_HEAD (&range_ctx.ranges); - - scoped_guard(rwsem_write, &cxl_rwsem.region) { - rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); -if (!rc) - rc =cxl_reset_execute (pdev, dvsec, cap, &range_ctx); -else -cxl_hdm_range_context_destroy (&range_ctx); - } - -return rc; +return cxl_reset_execute (pdev, dvsec, cap); }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 13/15] cxl: Restore CXL state after PCI reset
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Restore the PCI configuration needed for HDM access, followed by cached HDM decoder and CXL Device DVSEC state. Recommit a cleared locked decoder before restoring its lock, but 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.
Signed-off-by Srirangan Madhavan <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Jonathan Cameron
maintainerRe: [PATCH v13 13/15] cxl: Restore CXL state after PCI reset
In reply to Srirangan MadhavanQuoted 9 lines · On Tue, 22 Sep 2026 08:39:22 +0000 Srirangan Madhavan <[email protected]>…
Restore the PCI configuration needed for HDM access, followed by cached HDM decoder and CXL Device DVSEC state. Recommit a cleared locked decoder before restoring its lock, but 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.
Signed-off-by Srirangan Madhavan <[email protected]>
Hi Srirangan,
I'm out of time for today so main request it to take a look at that final function. It is running quite a complex statemachine with a bunch of ownership transfers. That's the sort of single function that takes too much energy to review. Please have a revisit and see if there is a simpler stepwise way to do that.
Quoted 126 lines
drivers/cxl/core/resource.c +60
-22@@ -726,6 +1064,18 @@ static int cxl_reset_disable_cache(struct pci_dev *pdev, int dvsec, u16 cap)return cxl_reset_wait_cache_wbi (pdev, dvsec); } +static int cxl_reset_restore_cache_policy (struct pci_dev *pdev,int dvsec) +{ +int rc; + + rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,0 , +PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc) +pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc); + +return rc; +} +static int cxl_reset_wait_done (struct pci_dev *pdev,int dvsec,u16 cap) {unsigned long deadline;@@ -767,28 +1117,41 @@ static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap) } } -static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap, -struct cxl_hdm_range_context *range_ctx) +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) { +struct cxl_hdm_info *snapshot =NULL ; +struct cxl_hdm_range_context range_ctx; +u16 command;int rc, rc2; - rc =cxl_reset_disable_cache (pdev, dvsec, cap); +down_write (&cxl_rwsem.region); +INIT_LIST_HEAD (&range_ctx.ranges); + + rc =cxl_hdm_ranges_prepare (&range_ctx, pdev); +if (rc) + goto out_destroy_ranges; + + rc =cxl_snapshot_hdm (pdev, &snapshot);if (rc) goto out_destroy_ranges; + rc =cxl_reset_disable_cache (pdev, dvsec, cap); +if (rc) + goto out_destroy_enable_cache; +if (!pci_wait_for_pending_transaction (pdev)) {pci_err (pdev,"timed out waiting for pending transactions\n" ); rc = -ETIMEDOUT ; - goto out_destroy_ranges; + goto out_destroy_enable_cache; } rc =pci_dev_reset_iommu_prepare (pdev);if (rc) {pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); - goto out_destroy_ranges; + goto out_destroy_enable_cache; } -/* Clear Memory Clear again even if this command write reports failure. */ +/* Clear Memory Clear below even if this command write reports failure. */ rc =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,PCI_DVSEC_CXL_INIT_CXL_RST |PCI_DVSEC_CXL_RST_MEM_CLR_EN ,0 );@@ -798,39 +1161,60 @@ static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap, rc =cxl_reset_wait_done (pdev, dvsec, cap); out_clear_memory: - rc2 =cxl_reset_update_ctrl2_no_replay ( - pdev, dvsec,0 ,PCI_DVSEC_CXL_RST_MEM_CLR_EN ); + rc2 =cxl_reset_update_ctrl2_no_replay (pdev, dvsec,0 , +PCI_DVSEC_CXL_RST_MEM_CLR_EN );if (rc2)pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , - rc2); + rc2); rc = rc ?: rc2; +if (rc) + goto out_finish_ranges; + +cxl_restore_pci_state_for_hdm_restore (pdev, &command); + rc =cxl_restore_state (pdev, snapshot); +out_finish_ranges:/* Evict lines fetched during reset before ending DMA exclusion. */ - rc2 =cxl_hdm_ranges_finish (range_ctx); + rc2 =cxl_hdm_ranges_finish (&range_ctx); rc = rc ?: rc2; +if (rc) + goto out_fail_closed; +pci_dev_reset_iommu_done (pdev); - goto out_enable_cache; -out_destroy_ranges: -cxl_hdm_range_context_destroy (range_ctx); + rc =cxl_reset_restore_cache_policy (pdev, dvsec); +if (rc) { +cxl_reset_save_disabled_state (pdev); + goto out_unlock; + } -out_enable_cache: -/* -* DISABLE_CACHING was the first preparation step. Restore the original -* cache policy last, after reset exclusion has ended. -*/ - rc2 =cxl_reset_update_ctrl2_no_replay ( - pdev, dvsec,0 ,PCI_DVSEC_CXL_DISABLE_CACHING ); -if (rc2) -pci_err (pdev,"failed to re-enable CXL caching: %d\n" , rc2); + rc =cxl_reset_save_restored_state (pdev, command); + goto out_unlock; + +out_fail_closed: +cxl_reset_save_disabled_state (pdev); +pci_dev_reset_iommu_done (pdev); + rc2 =cxl_reset_restore_cache_policy (pdev, dvsec); rc = rc ?: rc2; + goto out_unlock; + +out_destroy_enable_cache: +cxl_hdm_range_context_destroy (&range_ctx);
drivers/cxl/core/resource.c static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap, · line 1212
Quoted 10 lines
drivers/cxl/core/resource.c +9
-0+ rc2 =cxl_reset_restore_cache_policy (pdev, dvsec); + rc = rc ?: rc2; + goto out_unlock; + +out_destroy_ranges: +cxl_hdm_range_context_destroy (&range_ctx); +out_unlock: +kfree (snapshot); +up_write (&cxl_rwsem.region);
This is going to take some signficant unpicking but as it stands it is very complex to follow with releases quite often out of reverse order of setup and complex hand off from one function to another. Please take another look even if I don't get back to this with more specific comments.
Quoted 2 lines
drivers/cxl/core/resource.c +0
-0return rc; }
Re: [PATCH v13 13/15] cxl: Restore CXL state after PCI reset
Quoted 8 lines · Jonathan Cameron
Quoted 1 line
Signed-off-by Srirangan Madhavan<[email protected]>Hi Srirangan,
I'm out of time for today so main request it to take a look at that final function. It is running quite a complex statemachine with a bunch of ownership transfers. That's the sort of single function that takes too much energy to review. Please have a revisit and see if there is a simpler stepwise way to do that.
Hi Jonathan!
I've reworked this in v14 patch 14. Reset execution and state restoration are separate steps, and cxl_reset_execute() retains ownership of the snapshot and range context through cleanup. The cross-function cleanup handoffs and goto state machine are gone now. Please let me know if this is better now.
Thank you.
Signature · 2 lines
Regards, Srirangan
PATCH v13 14/15 PCI/CXL: Expose CXL Reset as a PCI reset method
In reply to Srirangan MadhavanRegister 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, function scope, cached HDM state, or required cache synchronization do not permit a safe reset.
--- drivers/cxl/core/resource.c | 8 ++++---- drivers/pci/pci.c | 2 ++ include/linux/pci.h | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-)
drivers/cxl/core/resource.c +4 -4
@@ -1225,9 +1225,9 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap);if (rc) -return pcibios_err_to_errno (rc); +return probe ? -ENOTTY :pcibios_err_to_errno (rc);if (PCI_POSSIBLE_ERROR (cap)) -return -ENODEV ; +return probe ? -ENOTTY : -ENODEV ; if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) || !(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
@@ -1236,9 +1236,9 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl);if (rc) -return pcibios_err_to_errno (rc); +return probe ? -ENOTTY :pcibios_err_to_errno (rc);if (PCI_POSSIBLE_ERROR (ctrl)) -return -ENODEV ; +return probe ? -ENOTTY : -ENODEV ;if (!(ctrl &PCI_DVSEC_CXL_CACHE_ENABLE ) || !(ctrl &PCI_DVSEC_CXL_MEM_ENABLE ))return -ENOTTY ;
drivers/pci/pci.c +2 -0
@@ -35,6 +35,7 @@#include <linux/aer.h> #include <linux/bitfield.h> #include <linux/suspend.h> +#include <cxl/cxl.h> #include "pci.h" DEFINE_MUTEX (pci_slot_mutex);
@@ -5148,6 +5149,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" },
include/linux/pci.h +1 -1
@@ -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
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 14/15] PCI/CXL: Expose CXL Reset as a PCI reset method
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] PCI/CXL: unsafe fallback to FLR on cxl_reset safety check failure - [High] PCI/CXL: cxl_reset…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] PCI/CXL: unsafe fallback to FLR on cxl_reset safety check failure
- [High] PCI/CXL: cxl_reset rejected during probe if features disabled
--
commit a3dd9319808c6b4394c9a465a0ccd99ba52161a2
Author: Srirangan Madhavan <[email protected]>
PCI/CXL: Expose CXL Reset as a PCI reset method
This commit registers cxl_reset with the PCI reset-method interface, placing it before FLR. It adds capability and state checks to ensure the device supports and is configured for CXL cache and memory protocols. It also returns -ENOTTY when the device capabilities or required cache synchronization do not permit a safe reset.
Quoted 6 lines
drivers/cxl/core/resource.c +0
-0@@ ... @@
intcxl_reset_function(struct pci_dev *pdev, bool probe){
drivers/cxl/core/resource.c
Quoted 10 lines
drivers/cxl/core/resource.c +0
-0int dvsec, rc;u16 cap, ctrl;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 ;
[Severity: High]
Does returning -ENOTTY here cause the PCI core to silently fall back to FLR during an actual reset request?
Type 3 devices (memory expanders) lack CXL.cache capabilities, so they would fail this check. If they fall back to FLR, it resets the PCIe state but not the CXL protocol state. For devices with active CXL memory, this might lead to Machine Check Exceptions or memory corruption when the CPU interacts with the reset device.
Would returning a hard error like -EOPNOTSUPP during execution safely abort the unsafe reset?
drivers/cxl/core/resource.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 1244
Quoted 11 lines
drivers/cxl/core/resource.c +2
-2rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl);if (rc) -return pcibios_err_to_errno (rc); +return probe ? -ENOTTY :pcibios_err_to_errno (rc);if (PCI_POSSIBLE_ERROR (ctrl)) -return -ENODEV ; +return probe ? -ENOTTY : -ENODEV ;if (!(ctrl &PCI_DVSEC_CXL_CACHE_ENABLE ) || !(ctrl &PCI_DVSEC_CXL_MEM_ENABLE ))return -ENOTTY ;
[Severity: High]
Does evaluating this dynamic state during probe permanently exclude the reset method before the driver can enable the features?
When pci_init_reset_methods() calls cxl_reset_function() with probe == true during early device discovery, returning -ENOTTY causes the PCI core to omit this method entirely from the supported reset methods.
If this check evaluates dynamic state before the early-return for probe, it might cause hot-plugged devices to silently fall back to FLR when a reset is later requested. Could this check be moved after the if (probe) early-return?
drivers/cxl/core/resource.c
Quoted 10 lines
[ ... ]
drivers/cxl/core/resource.c +0
-0if (probe)return 0 ;if (!cpu_cache_has_invalidate_memregion ())return -ENOTTY ;
[Severity: High]
Similar to the capability check above, does returning -ENOTTY here fail to safely abort the reset?
Since the PCI framework interprets -ENOTTY as "method not applicable, try next", this causes a fallback to the unsafe FLR method. Would returning a hard error prevent this unsafe fallback?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH v13 15/15 PCI/CXL: Restore CXL state after CXL bus reset
In reply to Srirangan MadhavanCXL 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.
--- drivers/cxl/core/resource.c | 23 +++++++++++++++++++++-- drivers/pci/pci.c | 8 ++++++++ include/cxl/cxl.h | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-)
drivers/cxl/core/resource.c +21 -2
@@ -601,12 +601,12 @@ static int cxl_restore_hdm_decoder(struct pci_dev *pdev, void __iomem *hdm,return 0 ; } -static int cxl_snapshot_hdm (struct pci_dev *pdev, -struct cxl_hdm_info **snapshot) +int cxl_snapshot_hdm (struct pci_dev *pdev,struct cxl_hdm_info **snapshot) {struct cxl_hdm_info *info;size_t size; +device_lock_assert (&pdev->dev);guard (rwsem_read)(&cxl_rwsem.dpa); info = pdev->hdm;if (!info)
@@ -762,6 +762,25 @@ static int cxl_reset_save_restored_state(struct pci_dev *pdev, u16 command)return rc; } +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); +guard (rwsem_write)(&cxl_rwsem.region); + +cxl_restore_pci_state_for_hdm_restore (pdev, &command); + rc =cxl_restore_state (pdev, snapshot); +if (rc) { +cxl_reset_save_disabled_state (pdev); +return rc; + } + +return cxl_reset_save_restored_state (pdev, command); +} +/* * 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
drivers/pci/pci.c +8 -0
@@ -5015,6 +5015,7 @@ 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_hdm_info *snapshot __free(kfree) = NULL;struct pci_dev *bridge;u16 dvsec, reg, val;int rc;
@@ -5037,6 +5038,10 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (rc)return -ENOTTY ; + rc =cxl_snapshot_hdm (dev, &snapshot); +if (rc && rc != -ENXIO ) +return rc; + rc =pci_dev_reset_iommu_prepare (dev);if (rc) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , rc);
@@ -5057,6 +5062,9 @@ 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); +pci_dev_reset_iommu_done (dev);return rc; }
include/cxl/cxl.h +17 -0
@@ -202,6 +202,9 @@ struct cxl_register_map {#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_snapshot_hdm (struct pci_dev *pdev,struct cxl_hdm_info **snapshot); +int cxl_restore_state_after_pci_reset (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot);int cxl_reset_function (struct pci_dev *pdev,bool probe);#else static inline void pci_cxl_hdm_cache_init (struct pci_dev *pdev)
@@ -212,6 +215,20 @@ static inline void pci_cxl_hdm_cache_release(struct pci_dev *pdev) { } +static inline int cxl_snapshot_hdm (struct pci_dev *pdev, +struct cxl_hdm_info **snapshot) +{ + *snapshot =NULL ; +return -ENXIO ; +} + +static inline int +cxl_restore_state_after_pci_reset (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot) +{ +return 0 ; +} +static inline int cxl_reset_function (struct pci_dev *pdev,bool probe) {return -ENOTTY ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v13 15/15] PCI/CXL: Restore CXL state after CXL bus reset
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] cxl: HDM state restoration bypassed if SBR is natively unmasked --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] cxl: HDM state restoration bypassed if SBR is natively unmasked
--
commit 44f7ddf109a23fa1e25d7dc89752e3e7548f5c17
Author: Srirangan Madhavan <[email protected]>
PCI/CXL: Restore CXL state after CXL bus reset
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.
Quoted 4 lines
drivers/pci/pci.c +0
-0
[ ... ]
drivers/pci/pci.c static int pci_reset_bus_function(struct pci_dev *dev, bool probe) · line 5018
Quoted 5 lines
drivers/pci/pci.c +1
-0@@ -5015,6 +5015,7 @@ 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_hdm_info *snapshot __free(kfree) = NULL;
[Severity: High]
Will this new HDM state restoration logic be bypassed entirely if SBR is natively unmasked?
Looking at pci_reset_fn_methods in drivers/pci/pci.c, "bus" is ordered before "cxl_bus":
{ pci_reset_bus_function, .name = "bus" },
{ cxl_reset_bus_function, .name = "cxl_bus" },When SBR is natively unmasked, pci_reset_bus_function() will succeed during probe and will be placed before cxl_reset_bus_function() in the device's reset_methods array. During a reset, __pci_reset_function_locked() executes "bus" first. If "bus" succeeds and returns 0, it completely bypasses "cxl_bus" and this newly added snapshot/restore logic.
Could this cause CXL HDM decoder state to be lost after a reset on these devices?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15