public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring
@ 2025-03-06 16:44 Robert Richter
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
                   ` (14 more replies)
  0 siblings, 15 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

This series is the first part of adding support for CXL address
translation. It contains cleanup and code refactoring in preparation
of the actual implementation that will be sent in part 2. Cleanup and
code refactoring have been split in a separate series to reduce the
number of patches of the series. Even without address translation on
top this rework improves esp. the region code, cleans it up,
simplifies it and adds debugging messages to better analyze region
creation failures:

Content of patches:

 * Patches 1: Remove else after return.

 * Patches 2-3: Cleanups and comments around cxl_hdm_decode_init().

 * Patches 4, 5: Adding and modifying helper functions.

 * Patches 6-10: Refactoring of endpoint decoder setup.

 * Patches 11-14: Adding and modifying debug messages.

v4:
 * rebased onto cxl/next, commit 0a14566be090 ("cxl/Documentation:
   Remove 'mixed' from sysfs mode doc"),
 * added tags to SOB chain,
 * reworked comments in cxl_hdm_decode_init() (dropped moving comment
   and updated patch that modifies comments) (Jonathan),
 * reworded patch description that removes duplicate call of
   cxl_find_decoder_early() (Jonathan),
 * moved some patches out of this rework and cleanup series (Dave,
   Jonathan),

v3:
 * added tags to SOB chain,
 * fixed NULL pointer dereference in cxl_find_root_decoder() (Alison),
 * updated subject line of patches that add kernel messages and
   included example log messages (Alison),

v2:
 * rebased onto cxl/next,
 * added tags to SOB chain,
 * move patches with cleanups and refactoring into this separate
   series (Dave),
 * added patch "cxl/acpi: Unify CFMWS memory log messages with SRAT
   messages" to improve CFMWS log messages,
 * renamed endpoint decoder functions to cxl_endpoint_decoder_*() (Li),
 * reworded patch description that moves find_cxl_root() and reworks
   cxl_find_root_decoder() (Terry),
 * small changes to cxl_find_root_decoder()/
   cxl_endpoint_decoder_initialize() (Jonanthan),
 * updated comment in cxl_port_find_switch_decoder() (Ben),
 * cxl_endpoint_decoder_initialize(): Simplify variable declaration
   (Jonathan, Ben),
 * cxl_find_decoder_early(): Added comment on function usage (Gregory),
 * reordered patches and reworded some of the subject for a better
   structure.

Robert Richter (14):
  cxl: Remove else after return
  cxl/pci: Moving code in cxl_hdm_decode_init()
  cxl/pci: Add comments to cxl_hdm_decode_init()
  cxl: Introduce parent_port_of() helper
  cxl/region: Rename function to cxl_find_decoder_early()
  cxl/region: Avoid duplicate call of cxl_find_decoder_early()
  cxl/region: Move find_cxl_root() to cxl_add_to_region()
  cxl/region: Factor out code to find the root decoder
  cxl/region: Factor out code to find a root decoder's region
  cxl/region: Add function to find a port's switch decoder by range
  cxl/region: Add a dev_warn() on registration failure
  cxl/region: Add a dev_err() on missing target list entries
  cxl: Add a dev_dbg() when a decoder was added to a port
  cxl/acpi: Unify CFMWS memory log messages with SRAT messages

 drivers/cxl/acpi.c        |  12 ++-
 drivers/cxl/core/cdat.c   |   2 +-
 drivers/cxl/core/hdm.c    |   3 +-
 drivers/cxl/core/pci.c    |  50 +++++++----
 drivers/cxl/core/port.c   |  15 ++--
 drivers/cxl/core/region.c | 170 +++++++++++++++++++++++++-------------
 drivers/cxl/cxl.h         |   7 +-
 drivers/cxl/port.c        |  15 +---
 8 files changed, 172 insertions(+), 102 deletions(-)


base-commit: 0a14566be090ca51a32ebdd8a8e21678062dac08
-- 
2.39.5


^ permalink raw reply	[flat|nested] 52+ messages in thread

* [PATCH v4 01/14] cxl: Remove else after return
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-14 11:27   ` Jonathan Cameron
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
                   ` (13 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Remove unnecessary 'else' after return. Improves readability of code.
It is easier to place comments. Check and fix all occurrences under
drivers/cxl/.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/cdat.c   | 2 +-
 drivers/cxl/core/pci.c    | 3 ++-
 drivers/cxl/core/region.c | 4 +++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/cxl/core/cdat.c b/drivers/cxl/core/cdat.c
index edb4f41eeacc..0ccef2f2a26a 100644
--- a/drivers/cxl/core/cdat.c
+++ b/drivers/cxl/core/cdat.c
@@ -28,7 +28,7 @@ static u32 cdat_normalize(u16 entry, u64 base, u8 type)
 	 */
 	if (entry == 0xffff || !entry)
 		return 0;
-	else if (base > (UINT_MAX / (entry)))
+	if (base > (UINT_MAX / (entry)))
 		return 0;
 
 	/*
diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 96fecb799cbc..33c3bdd35b24 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -415,7 +415,8 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 	 */
 	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
 		return devm_cxl_enable_mem(&port->dev, cxlds);
-	else if (!hdm)
+
+	if (!hdm)
 		return -ENODEV;
 
 	root = to_cxl_port(port->dev.parent);
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 8537b6a9ca18..cbe762abf6b3 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -1940,7 +1940,9 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
 		dev_dbg(&cxlr->dev, "region already active\n");
 		return -EBUSY;
-	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
+	}
+
+	if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
 		dev_dbg(&cxlr->dev, "interleave config missing\n");
 		return -ENXIO;
 	}
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init()
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-14 11:31   ` Jonathan Cameron
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
                   ` (12 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Commit 3f9e07531778 ("cxl/pci: simplify the check of mem_enabled in
cxl_hdm_decode_init()") changed the code flow in this function. The
root port is determined before a check to leave the function. Since
the root port is not used by the check it can be moved to run the
check first. This improves code readability and avoids unnesessary
code execution.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/pci.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 33c3bdd35b24..6386e84e51a4 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -419,14 +419,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 	if (!hdm)
 		return -ENODEV;
 
-	root = to_cxl_port(port->dev.parent);
-	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
-		root = to_cxl_port(root->dev.parent);
-	if (!is_cxl_root(root)) {
-		dev_err(dev, "Failed to acquire root port for HDM enable\n");
-		return -ENODEV;
-	}
-
 	if (!info->mem_enabled) {
 		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
 		if (rc)
@@ -435,6 +427,14 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 		return devm_cxl_enable_mem(&port->dev, cxlds);
 	}
 
+	root = to_cxl_port(port->dev.parent);
+	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
+		root = to_cxl_port(root->dev.parent);
+	if (!is_cxl_root(root)) {
+		dev_err(dev, "Failed to acquire root port for HDM enable\n");
+		return -ENODEV;
+	}
+
 	for (i = 0, allowed = 0; i < info->ranges; i++) {
 		struct device *cxld_dev;
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
  2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-14 11:33   ` Jonathan Cameron
                     ` (3 more replies)
  2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
                   ` (11 subsequent siblings)
  14 siblings, 4 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

There are various configuration cases of HDM decoder registers causing
different code paths. Add comments to cxl_hdm_decode_init() to better
explain them.

Signed-off-by: Robert Richter <rrichter@amd.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/pci.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
index 6386e84e51a4..ef4b08abe424 100644
--- a/drivers/cxl/core/pci.c
+++ b/drivers/cxl/core/pci.c
@@ -416,9 +416,21 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
 		return devm_cxl_enable_mem(&port->dev, cxlds);
 
+	/*
+	 * If the HDM Decoder Capability does not exist and DVSEC was
+	 * not setup, the DVSEC based emulation cannot be used.
+	 */
 	if (!hdm)
 		return -ENODEV;
 
+	/*
+	 * The HDM Decoder Capability exists but is globally disabled.
+	 */
+
+	/*
+	 * If the DVSEC CXL Range registers are not enabled, just
+	 * enable and use the HDM Decoder Capability registers.
+	 */
 	if (!info->mem_enabled) {
 		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
 		if (rc)
@@ -427,6 +439,18 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 		return devm_cxl_enable_mem(&port->dev, cxlds);
 	}
 
+	/*
+	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
+	 * [High,Low] when HDM operation is enabled the range register values
+	 * are ignored by the device, but the spec also recommends matching the
+	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
+	 * are expected even though Linux does not require or maintain that
+	 * match. Check if at least one DVSEC range is enabled and allowed by
+	 * the platform. That is, the DVSEC range must be covered by a locked
+	 * platform window (CFMWS). Fail otherwise as the endpoint's decoders
+	 * cannot be used.
+	 */
+
 	root = to_cxl_port(port->dev.parent);
 	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
 		root = to_cxl_port(root->dev.parent);
@@ -454,15 +478,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
 		return -ENXIO;
 	}
 
-	/*
-	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
-	 * [High,Low] when HDM operation is enabled the range register values
-	 * are ignored by the device, but the spec also recommends matching the
-	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
-	 * are expected even though Linux does not require or maintain that
-	 * match. If at least one DVSEC range is enabled and allowed, skip HDM
-	 * Decoder Capability Enable.
-	 */
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, "CXL");
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 04/14] cxl: Introduce parent_port_of() helper
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (2 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 18:57   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early() Robert Richter
                   ` (10 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Often a parent port must be determined. Introduce the parent_port_of()
helper function for this.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/port.c   | 15 +++++++++------
 drivers/cxl/core/region.c | 11 ++---------
 drivers/cxl/cxl.h         |  1 +
 3 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index 6a44b6dad3c7..25eecb591496 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -602,17 +602,20 @@ struct cxl_port *to_cxl_port(const struct device *dev)
 }
 EXPORT_SYMBOL_NS_GPL(to_cxl_port, "CXL");
 
+struct cxl_port *parent_port_of(struct cxl_port *port)
+{
+	if (!port || !port->parent_dport)
+		return NULL;
+	return port->parent_dport->port;
+}
+EXPORT_SYMBOL_NS_GPL(parent_port_of, "CXL");
+
 static void unregister_port(void *_port)
 {
 	struct cxl_port *port = _port;
-	struct cxl_port *parent;
+	struct cxl_port *parent = parent_port_of(port);
 	struct device *lock_dev;
 
-	if (is_cxl_root(port))
-		parent = NULL;
-	else
-		parent = to_cxl_port(port->dev.parent);
-
 	/*
 	 * CXL root port's and the first level of ports are unregistered
 	 * under the platform firmware device lock, all other ports are
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index cbe762abf6b3..4f79cc17c9c8 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -1748,13 +1748,6 @@ static int cmp_interleave_pos(const void *a, const void *b)
 	return cxled_a->pos - cxled_b->pos;
 }
 
-static struct cxl_port *next_port(struct cxl_port *port)
-{
-	if (!port->parent_dport)
-		return NULL;
-	return port->parent_dport->port;
-}
-
 static int match_switch_decoder_by_range(struct device *dev,
 					 const void *data)
 {
@@ -1781,7 +1774,7 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
 	struct device *dev;
 	int rc = -ENXIO;
 
-	parent = next_port(port);
+	parent = parent_port_of(port);
 	if (!parent)
 		return rc;
 
@@ -1861,7 +1854,7 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
 	 */
 
 	/* Iterate from endpoint to root_port refining the position */
-	for (iter = port; iter; iter = next_port(iter)) {
+	for (iter = port; iter; iter = parent_port_of(iter)) {
 		if (is_cxl_root(iter))
 			break;
 
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index be8a7dc77719..24cec16d02a6 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -724,6 +724,7 @@ static inline bool is_cxl_root(struct cxl_port *port)
 int cxl_num_decoders_committed(struct cxl_port *port);
 bool is_cxl_port(const struct device *dev);
 struct cxl_port *to_cxl_port(const struct device *dev);
+struct cxl_port *parent_port_of(struct cxl_port *port);
 void cxl_port_commit_reap(struct cxl_decoder *cxld);
 struct pci_bus;
 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early()
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (3 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 19:51   ` Dan Williams
  2025-03-06 16:44 ` [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early() Robert Richter
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Current function cxl_region_find_decoder() is used to find a port's
decoder during region setup. The decoder is later used to attach the
port to a region.

Rename function to cxl_find_decoder_early() to emphasize its use only
during region setup in the early pre-init setup stage when an endpoint
is not yet attached to a region (cxl_region_attach()). Once a port is
attached to a region, the region reference can be used to lookup a
region's port and decoder configuration (see struct cxl_region_ref).

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 4f79cc17c9c8..e40ae0fefddc 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -865,10 +865,18 @@ static int match_auto_decoder(struct device *dev, const void *data)
 	return 0;
 }
 
+/*
+ * Only use cxl_find_decoder_early() during region setup in the early
+ * pre-init setup stage when an endpoint is not yet attached to a
+ * region (cxl_region_attach()). Once a port is attached to a region,
+ * the region reference can be used to lookup a region's port and
+ * decoder configuration (see struct cxl_region_ref).
+*/
+
 static struct cxl_decoder *
-cxl_region_find_decoder(struct cxl_port *port,
-			struct cxl_endpoint_decoder *cxled,
-			struct cxl_region *cxlr)
+cxl_find_decoder_early(struct cxl_port *port,
+		       struct cxl_endpoint_decoder *cxled,
+		       struct cxl_region *cxlr)
 {
 	struct device *dev;
 
@@ -932,7 +940,7 @@ alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
 		if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 			struct cxl_decoder *cxld;
 
-			cxld = cxl_region_find_decoder(port, cxled, cxlr);
+			cxld = cxl_find_decoder_early(port, cxled, cxlr);
 			if (auto_order_ok(port, iter->region, cxld))
 				continue;
 		}
@@ -1020,7 +1028,7 @@ static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
 {
 	struct cxl_decoder *cxld;
 
-	cxld = cxl_region_find_decoder(port, cxled, cxlr);
+	cxld = cxl_find_decoder_early(port, cxled, cxlr);
 	if (!cxld) {
 		dev_dbg(&cxlr->dev, "%s: no decoder available\n",
 			dev_name(&port->dev));
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early()
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (4 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early() Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:01   ` Dan Williams
  2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Function cxl_find_decoder_early() is called twice, in
alloc_region_ref() and cxl_rr_alloc_decoder(). Both functions are
subsequently called from cxl_port_attach_region(). Make the decoder a
function argument to both which avoids a duplicate call of
cxl_find_decoder_early().

Moving the call out of alloc_region_ref() also moves it out of the
xa_for_each() loop in there. Now, cxld is determined no longer only
for each auto-generated region, but now once for all regions
regardless of auto-generated or not. This is fine as the cxld argument
is needed for all regions in cxl_rr_alloc_decoder() and an error would
be returned otherwise anyway. So it is better to determine the decoder
in front of all this and fail early if missing instead of running
through all that code with multiple calls of cxl_find_decoder_early().

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index e40ae0fefddc..5a7a1dd583aa 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -924,7 +924,8 @@ static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
 
 static struct cxl_region_ref *
 alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
-		 struct cxl_endpoint_decoder *cxled)
+		 struct cxl_endpoint_decoder *cxled,
+		 struct cxl_decoder *cxld)
 {
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_region_ref *cxl_rr, *iter;
@@ -938,9 +939,6 @@ alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
 			continue;
 
 		if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
-			struct cxl_decoder *cxld;
-
-			cxld = cxl_find_decoder_early(port, cxled, cxlr);
 			if (auto_order_ok(port, iter->region, cxld))
 				continue;
 		}
@@ -1024,17 +1022,9 @@ static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
 
 static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
 				struct cxl_endpoint_decoder *cxled,
-				struct cxl_region_ref *cxl_rr)
+				struct cxl_region_ref *cxl_rr,
+				struct cxl_decoder *cxld)
 {
-	struct cxl_decoder *cxld;
-
-	cxld = cxl_find_decoder_early(port, cxled, cxlr);
-	if (!cxld) {
-		dev_dbg(&cxlr->dev, "%s: no decoder available\n",
-			dev_name(&port->dev));
-		return -EBUSY;
-	}
-
 	if (cxld->region) {
 		dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
 			dev_name(&port->dev), dev_name(&cxld->dev),
@@ -1125,7 +1115,16 @@ static int cxl_port_attach_region(struct cxl_port *port,
 			nr_targets_inc = true;
 		}
 	} else {
-		cxl_rr = alloc_region_ref(port, cxlr, cxled);
+		struct cxl_decoder *cxld;
+
+		cxld = cxl_find_decoder_early(port, cxled, cxlr);
+		if (!cxld) {
+			dev_dbg(&cxlr->dev, "%s: no decoder available\n",
+				dev_name(&port->dev));
+			return -EBUSY;
+		}
+
+		cxl_rr = alloc_region_ref(port, cxlr, cxled, cxld);
 		if (IS_ERR(cxl_rr)) {
 			dev_dbg(&cxlr->dev,
 				"%s: failed to allocate region reference\n",
@@ -1134,7 +1133,7 @@ static int cxl_port_attach_region(struct cxl_port *port,
 		}
 		nr_targets_inc = true;
 
-		rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
+		rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr, cxld);
 		if (rc)
 			goto out_erase;
 	}
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region()
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (5 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early() Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:14   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
                   ` (7 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

When adding an endpoint to a region, the root port is determined
first. Move this directly into cxl_add_to_region(). This is in
preparation of the initialization of endpoints that iterates the port
hierarchy from the endpoint up to the root port.

As a side-effect the root argument is removed from the argument lists
of cxl_add_to_region() and related functions. Now, the endpoint is the
only parameter to add a region. This simplifies the function
interface.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c |  6 ++++--
 drivers/cxl/cxl.h         |  6 ++----
 drivers/cxl/port.c        | 15 +++------------
 3 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 5a7a1dd583aa..8244a27d0fd6 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3384,9 +3384,11 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 	return cxlr;
 }
 
-int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
+int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+	struct cxl_port *port = cxled_to_port(cxled);
+	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
 	struct range *hpa = &cxled->cxld.hpa_range;
 	struct cxl_decoder *cxld = &cxled->cxld;
 	struct device *cxlrd_dev, *region_dev;
@@ -3396,7 +3398,7 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
 	bool attach = false;
 	int rc;
 
-	cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
+	cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
 				      match_root_decoder_by_range);
 	if (!cxlrd_dev) {
 		dev_err(cxlmd->dev.parent,
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 24cec16d02a6..960efcc60476 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -857,8 +857,7 @@ struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port);
 #ifdef CONFIG_CXL_REGION
 bool is_cxl_pmem_region(struct device *dev);
 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev);
-int cxl_add_to_region(struct cxl_port *root,
-		      struct cxl_endpoint_decoder *cxled);
+int cxl_add_to_region(struct cxl_endpoint_decoder *cxled);
 struct cxl_dax_region *to_cxl_dax_region(struct device *dev);
 u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa);
 #else
@@ -870,8 +869,7 @@ static inline struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
 {
 	return NULL;
 }
-static inline int cxl_add_to_region(struct cxl_port *root,
-				    struct cxl_endpoint_decoder *cxled)
+static inline int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 {
 	return 0;
 }
diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
index d2bfd1ff5492..74587a403e3d 100644
--- a/drivers/cxl/port.c
+++ b/drivers/cxl/port.c
@@ -30,7 +30,7 @@ static void schedule_detach(void *cxlmd)
 	schedule_cxl_memdev_detach(cxlmd);
 }
 
-static int discover_region(struct device *dev, void *root)
+static int discover_region(struct device *dev, void *unused)
 {
 	struct cxl_endpoint_decoder *cxled;
 	int rc;
@@ -49,7 +49,7 @@ static int discover_region(struct device *dev, void *root)
 	 * Region enumeration is opportunistic, if this add-event fails,
 	 * continue to the next endpoint decoder.
 	 */
-	rc = cxl_add_to_region(root, cxled);
+	rc = cxl_add_to_region(cxled);
 	if (rc)
 		dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
 			cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
@@ -95,7 +95,6 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)
 	struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
 	struct cxl_hdm *cxlhdm;
-	struct cxl_port *root;
 	int rc;
 
 	rc = cxl_dvsec_rr_decode(cxlds, &info);
@@ -126,19 +125,11 @@ static int cxl_endpoint_port_probe(struct cxl_port *port)
 	if (rc)
 		return rc;
 
-	/*
-	 * This can't fail in practice as CXL root exit unregisters all
-	 * descendant ports and that in turn synchronizes with cxl_port_probe()
-	 */
-	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
-
-	root = &cxl_root->port;
-
 	/*
 	 * Now that all endpoint decoders are successfully enumerated, try to
 	 * assemble regions from committed decoders
 	 */
-	device_for_each_child(&port->dev, root, discover_region);
+	device_for_each_child(&port->dev, NULL, discover_region);
 
 	return 0;
 }
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (6 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:35   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
                   ` (6 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

In function cxl_add_to_region() there is code to determine the root
decoder associated to an endpoint decoder. Factor out that code for
later reuse. This has the benefit of reducing cxl_add_to_region()'s
function complexity.

The reference of cxlrd_dev can be freed earlier. Since the root
decoder exists as long as the root port exists and the endpoint
already holds a reference to the root port, this additional reference
is not needed. Though it looks obvious to use __free() for the
reference of cxlrd_dev here too, this is done in a later rework. So
just move the code.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 55 ++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 18 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 8244a27d0fd6..7d9d9b8f9eea 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3212,6 +3212,38 @@ static int match_root_decoder_by_range(struct device *dev,
 	return range_contains(r1, r2);
 }
 
+static struct cxl_root_decoder *
+cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
+{
+	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+	struct cxl_port *port = cxled_to_port(cxled);
+	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
+	struct cxl_decoder *cxld = &cxled->cxld;
+	struct range *hpa = &cxld->hpa_range;
+	struct device *cxlrd_dev;
+
+	cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
+				      match_root_decoder_by_range);
+	if (!cxlrd_dev) {
+		dev_err(cxlmd->dev.parent,
+			"%s:%s no CXL window for range %#llx:%#llx\n",
+			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
+			cxld->hpa_range.start, cxld->hpa_range.end);
+		return NULL;
+	}
+
+	/*
+	 * device_find_child() created a reference to the root
+	 * decoder. Since the root decoder exists as long as the root
+	 * port exists and the endpoint already holds a reference to
+	 * the root port, this additional reference is not needed.
+	 * Free it here.
+	 */
+	put_device(cxlrd_dev);
+
+	return to_cxl_root_decoder(cxlrd_dev);
+}
+
 static int match_region_by_range(struct device *dev, const void *data)
 {
 	struct cxl_region_params *p;
@@ -3386,29 +3418,17 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 
 int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 {
-	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
-	struct cxl_port *port = cxled_to_port(cxled);
-	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
 	struct range *hpa = &cxled->cxld.hpa_range;
-	struct cxl_decoder *cxld = &cxled->cxld;
-	struct device *cxlrd_dev, *region_dev;
+	struct device *region_dev;
 	struct cxl_root_decoder *cxlrd;
 	struct cxl_region_params *p;
 	struct cxl_region *cxlr;
 	bool attach = false;
 	int rc;
 
-	cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
-				      match_root_decoder_by_range);
-	if (!cxlrd_dev) {
-		dev_err(cxlmd->dev.parent,
-			"%s:%s no CXL window for range %#llx:%#llx\n",
-			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
-			cxld->hpa_range.start, cxld->hpa_range.end);
+	cxlrd = cxl_find_root_decoder(cxled);
+	if (!cxlrd)
 		return -ENXIO;
-	}
-
-	cxlrd = to_cxl_root_decoder(cxlrd_dev);
 
 	/*
 	 * Ensure that if multiple threads race to construct_region() for @hpa
@@ -3426,7 +3446,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 
 	rc = PTR_ERR_OR_ZERO(cxlr);
 	if (rc)
-		goto out;
+		return rc;
 
 	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
 
@@ -3447,8 +3467,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 	}
 
 	put_device(region_dev);
-out:
-	put_device(cxlrd_dev);
+
 	return rc;
 }
 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (7 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:37   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range Robert Richter
                   ` (5 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

In function cxl_add_to_region() there is code to determine a root
decoder's region. Factor that code out. This is in preparation to
further rework and simplify function cxl_add_to_region().

No functional changes.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 7d9d9b8f9eea..70ff4c94fb7a 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3416,10 +3416,22 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
 	return cxlr;
 }
 
+static struct cxl_region *
+cxl_find_region_by_range(struct cxl_root_decoder *cxlrd, struct range *hpa)
+{
+	struct device *region_dev;
+
+	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
+				       match_region_by_range);
+	if (!region_dev)
+		return NULL;
+
+	return to_cxl_region(region_dev);
+}
+
 int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 {
 	struct range *hpa = &cxled->cxld.hpa_range;
-	struct device *region_dev;
 	struct cxl_root_decoder *cxlrd;
 	struct cxl_region_params *p;
 	struct cxl_region *cxlr;
@@ -3435,13 +3447,9 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 	 * one does the construction and the others add to that.
 	 */
 	mutex_lock(&cxlrd->range_lock);
-	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
-				       match_region_by_range);
-	if (!region_dev) {
+	cxlr = cxl_find_region_by_range(cxlrd, hpa);
+	if (!cxlr)
 		cxlr = construct_region(cxlrd, cxled);
-		region_dev = &cxlr->dev;
-	} else
-		cxlr = to_cxl_region(region_dev);
 	mutex_unlock(&cxlrd->range_lock);
 
 	rc = PTR_ERR_OR_ZERO(cxlr);
@@ -3466,7 +3474,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
 				p->res);
 	}
 
-	put_device(region_dev);
+	put_device(&cxlr->dev);			/* cxl_find_region_by_range() */
 
 	return rc;
 }
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (8 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:55   ` Dan Williams
  2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Factor out code to find the switch decoder of a port for a specific
address range. Reuse the code to search a root decoder, create the
function cxl_port_find_switch_decoder() and rework
match_root_decoder_by_range() to be usable for switch decoders too.

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 48 +++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 70ff4c94fb7a..cf58ee284696 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3198,33 +3198,48 @@ static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
 	return rc;
 }
 
-static int match_root_decoder_by_range(struct device *dev,
-				       const void *data)
+static int match_decoder_by_range(struct device *dev, const void *data)
 {
 	const struct range *r1, *r2 = data;
-	struct cxl_root_decoder *cxlrd;
+	struct cxl_decoder *cxld;
 
-	if (!is_root_decoder(dev))
+	if (!is_switch_decoder(dev))
 		return 0;
 
-	cxlrd = to_cxl_root_decoder(dev);
-	r1 = &cxlrd->cxlsd.cxld.hpa_range;
+	cxld = to_cxl_decoder(dev);
+	r1 = &cxld->hpa_range;
 	return range_contains(r1, r2);
 }
 
+static struct cxl_decoder *
+cxl_port_find_switch_decoder(struct cxl_port *port, struct range *hpa)
+{
+	/*
+	 * device_find_child() increments the reference count of the
+	 * the switch decoder's parent port to protect the reference
+	 * to its child. The port is already a parent of the endpoint
+	 * decoder's port, at least indirectly in the port hierarchy.
+	 * Thus, the endpoint already holds a reference for the parent
+	 * port of the switch decoder. Free the unnecessary reference
+	 * here.
+	 */
+	struct device *cxld_dev __free(put_device) =
+		device_find_child(&port->dev, hpa, match_decoder_by_range);
+
+	return cxld_dev ? to_cxl_decoder(cxld_dev) : NULL;
+}
+
 static struct cxl_root_decoder *
 cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
 	struct cxl_port *port = cxled_to_port(cxled);
 	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
-	struct cxl_decoder *cxld = &cxled->cxld;
+	struct cxl_decoder *root, *cxld = &cxled->cxld;
 	struct range *hpa = &cxld->hpa_range;
-	struct device *cxlrd_dev;
 
-	cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
-				      match_root_decoder_by_range);
-	if (!cxlrd_dev) {
+	root = cxl_port_find_switch_decoder(&cxl_root->port, hpa);
+	if (!root) {
 		dev_err(cxlmd->dev.parent,
 			"%s:%s no CXL window for range %#llx:%#llx\n",
 			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
@@ -3232,16 +3247,9 @@ cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
 		return NULL;
 	}
 
-	/*
-	 * device_find_child() created a reference to the root
-	 * decoder. Since the root decoder exists as long as the root
-	 * port exists and the endpoint already holds a reference to
-	 * the root port, this additional reference is not needed.
-	 * Free it here.
-	 */
-	put_device(cxlrd_dev);
 
-	return to_cxl_root_decoder(cxlrd_dev);
+
+	return to_cxl_root_decoder(&root->dev);
 }
 
 static int match_region_by_range(struct device *dev, const void *data)
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (9 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:57   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
                   ` (3 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Esp. in complex system configurations with multiple endpoints and
interleaving setups it is hard to detect region setup failures as its
registration may silently fail. Add messages to show registration
failures.

Example log message:

  cxl region5: region sort successful
  cxl region5: mem0:endpoint5 decoder5.0 add: mem0:decoder5.0 @ 0 next: none nr_eps: 1 nr_targets: 1
  cxl_port endpoint5: decoder5.0: range: 0x22350000000-0x2634fffffff iw: 1 ig: 256
  cxl region5: pci0000:e0:port1 decoder1.2 add: mem0:decoder5.0 @ 0 next: mem0 nr_eps: 1 nr_targets: 1
  cxl region5: pci0000:e0:port1 iw: 1 ig: 256
  cxl region5: pci0000:e0:port1: decoder1.2 expected 0000:e0:01.2 at 0
  cxl endpoint5: failed to attach decoder5.0 to region5: -6
  cxl_port endpoint5: probe: 0

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index cf58ee284696..bd1ce9d8bed7 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -2162,6 +2162,12 @@ static int attach_target(struct cxl_region *cxlr,
 	rc = cxl_region_attach(cxlr, cxled, pos);
 	up_read(&cxl_dpa_rwsem);
 	up_write(&cxl_region_rwsem);
+
+	if (rc)
+		dev_warn(cxled->cxld.dev.parent,
+			"failed to attach %s to %s: %d\n",
+			dev_name(&cxled->cxld.dev), dev_name(&cxlr->dev), rc);
+
 	return rc;
 }
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (10 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 20:58   ` Dan Williams
                     ` (2 more replies)
  2025-03-06 16:44 ` [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port Robert Richter
                   ` (2 subsequent siblings)
  14 siblings, 3 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Broken target lists are hard to discover as the driver fails at a
later initialization stage. Add an error message for this.

Example log messages:

  cxl_mem mem1: failed to find endpoint6:0000:e0:01.3 in target list of decoder1.1
  cxl_port endpoint6: failed to register decoder6.0: -6
  cxl_port endpoint6: probe: 0

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/core/region.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index bd1ce9d8bed7..175f5f600c5d 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -1805,6 +1805,13 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
 	}
 	put_device(dev);
 
+	if (rc)
+		dev_err(port->uport_dev,
+			"failed to find %s:%s in target list of %s\n",
+			dev_name(&port->dev),
+			dev_name(port->parent_dport->dport_dev),
+			dev_name(&cxlsd->cxld.dev));
+
 	return rc;
 }
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (11 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 21:06   ` Dan Williams
  2025-04-15 21:36   ` Alison Schofield
  2025-03-06 16:44 ` [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages Robert Richter
  2025-04-15 23:23 ` [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Alison Schofield
  14 siblings, 2 replies; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Improve debugging by adding and unifying messages whenever a decoder
was added to a port. It is especially useful to get the decoder
mapping of the involved CXL host bridge or PCI device. This avoids a
complex lookup of the decoder/port/device mappings in sysfs.

Example log messages:

  cxl_acpi ACPI0017:00: decoder0.0 added to root0
  cxl_acpi ACPI0017:00: decoder0.1 added to root0
  ...
   pci0000:e0: decoder1.0 added to port1
   pci0000:e0: decoder1.1 added to port1
  ...
  cxl_mem mem0: decoder5.0 added to endpoint5
  cxl_mem mem0: decoder5.1 added to endpoint5

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/acpi.c     | 10 +++++++++-
 drivers/cxl/core/hdm.c |  3 ++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/cxl/acpi.c b/drivers/cxl/acpi.c
index cb14829bb9be..3e75e612cbc4 100644
--- a/drivers/cxl/acpi.c
+++ b/drivers/cxl/acpi.c
@@ -421,7 +421,15 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,
 	rc = cxl_decoder_add(cxld, target_map);
 	if (rc)
 		return rc;
-	return cxl_root_decoder_autoremove(dev, no_free_ptr(cxlrd));
+
+	rc = cxl_root_decoder_autoremove(dev, no_free_ptr(cxlrd));
+	if (rc)
+		return rc;
+
+	dev_dbg(root_port->dev.parent, "%s added to %s\n",
+		dev_name(&cxld->dev), dev_name(&root_port->dev));
+
+	return 0;
 }
 
 static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg,
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 70cae4ebf8a4..00c2de629a34 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -34,7 +34,8 @@ static int add_hdm_decoder(struct cxl_port *port, struct cxl_decoder *cxld,
 	if (rc)
 		return rc;
 
-	dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
+	dev_dbg(port->uport_dev, "%s added to %s\n",
+		dev_name(&cxld->dev), dev_name(&port->dev));
 
 	return 0;
 }
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (12 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port Robert Richter
@ 2025-03-06 16:44 ` Robert Richter
  2025-03-06 21:12   ` Dan Williams
  2025-04-15 23:23 ` [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Alison Schofield
  14 siblings, 1 reply; 52+ messages in thread
From: Robert Richter @ 2025-03-06 16:44 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

CFMWS entries have a similar importance as SRAT table entries to
describe memory regions. For CXL error analysis and memory debugging
information of both is needed. Unify output of both messages to
improve logging. Change the style of CFMWS message according to SRAT
output. Also, turn messages into a dev_info() same as for SRAT.

SRAT pr_info() for reference:

drivers/acpi/numa/srat.c:
  pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",

Signed-off-by: Robert Richter <rrichter@amd.com>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Tested-by: Gregory Price <gourry@gourry.net>
---
 drivers/cxl/acpi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cxl/acpi.c b/drivers/cxl/acpi.c
index 3e75e612cbc4..93c73b163c28 100644
--- a/drivers/cxl/acpi.c
+++ b/drivers/cxl/acpi.c
@@ -447,7 +447,7 @@ static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg,
 			cfmws->base_hpa,
 			cfmws->base_hpa + cfmws->window_size - 1, rc);
 	else
-		dev_dbg(dev, "decode range: node: %d range [%#llx - %#llx]\n",
+		dev_info(dev, "ACPI: CFMWS: Node %u [mem %#010Lx-%#010Lx]\n",
 			phys_to_target_node(cfmws->base_hpa), cfmws->base_hpa,
 			cfmws->base_hpa + cfmws->window_size - 1);
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 04/14] cxl: Introduce parent_port_of() helper
  2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
@ 2025-03-06 18:57   ` Dan Williams
  2025-04-15 21:30   ` Alison Schofield
  2025-04-21 17:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 18:57 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Often a parent port must be determined. Introduce the parent_port_of()
> helper function for this.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/port.c   | 15 +++++++++------
>  drivers/cxl/core/region.c | 11 ++---------
>  drivers/cxl/cxl.h         |  1 +
>  3 files changed, 12 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index 6a44b6dad3c7..25eecb591496 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c
> @@ -602,17 +602,20 @@ struct cxl_port *to_cxl_port(const struct device *dev)
>  }
>  EXPORT_SYMBOL_NS_GPL(to_cxl_port, "CXL");
>  
> +struct cxl_port *parent_port_of(struct cxl_port *port)
> +{
> +	if (!port || !port->parent_dport)
> +		return NULL;
> +	return port->parent_dport->port;
> +}
> +EXPORT_SYMBOL_NS_GPL(parent_port_of, "CXL");

Why is this exported? All of the callers are within this patch are
within the core.

Also, if it is going to be exported for modular callers it should have
"cxl" in the name like all the other CXL exports. Save exporting it to
the patch that needs that.

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early()
  2025-03-06 16:44 ` [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early() Robert Richter
@ 2025-03-06 19:51   ` Dan Williams
  0 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 19:51 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Current function cxl_region_find_decoder() is used to find a port's
> decoder during region setup. The decoder is later used to attach the
> port to a region.
> 
> Rename function to cxl_find_decoder_early() to emphasize its use only
> during region setup in the early pre-init setup stage when an endpoint
> is not yet attached to a region (cxl_region_attach()). Once a port is
> attached to a region, the region reference can be used to lookup a
> region's port and decoder configuration (see struct cxl_region_ref).
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Tested-by: Gregory Price <gourry@gourry.net>

"_early" to me has always meant "before some other subsystem dependency
is available", like the "page allocator not initialized". So I do not
think this name helps readability.

> ---
>  drivers/cxl/core/region.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 4f79cc17c9c8..e40ae0fefddc 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -865,10 +865,18 @@ static int match_auto_decoder(struct device *dev, const void *data)
>  	return 0;
>  }
>  
> +/*
> + * Only use cxl_find_decoder_early() during region setup in the early
> + * pre-init setup stage when an endpoint is not yet attached to a
> + * region (cxl_region_attach()). Once a port is attached to a region,
> + * the region reference can be used to lookup a region's port and
> + * decoder configuration (see struct cxl_region_ref).
> +*/

I like the sentiment that this function is only for setup paths before
the given port has a cxl_region_ref established for the region, but
"pre-init" is not defined. Let's just call this function what it is and
document when to use without making the reader of this comment wonder
what "pre-init" means. Something like:

/**
 * cxl_port_pick_region_decoder() - assign or lookup a decoder for a region
 * @port: a port in the ancestry of the endpoint implied by @cxled
 * @cxled: endpoint decoder to be, or currently, mapped by @port
 * @cxlr: region to establish, or validate, decode @port
 *
 * In the region creation path cxl_port_pick_region_decoder() is an
 * allocator to find a free port. In the region assembly path, it is
 * recalling the decoder that platform firmware picked for validation
 * purposes.
 *
 * The result is recorded in a 'struct cxl_region_ref' in @port for
 * later recall when other endpoints might also be targets of the picked
 * decoder.
 */

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early()
  2025-03-06 16:44 ` [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early() Robert Richter
@ 2025-03-06 20:01   ` Dan Williams
  0 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:01 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Function cxl_find_decoder_early() is called twice, in
> alloc_region_ref() and cxl_rr_alloc_decoder(). Both functions are
> subsequently called from cxl_port_attach_region(). Make the decoder a
> function argument to both which avoids a duplicate call of
> cxl_find_decoder_early().
> 
> Moving the call out of alloc_region_ref() also moves it out of the
> xa_for_each() loop in there. Now, cxld is determined no longer only
> for each auto-generated region, but now once for all regions
> regardless of auto-generated or not. This is fine as the cxld argument
> is needed for all regions in cxl_rr_alloc_decoder() and an error would
> be returned otherwise anyway. So it is better to determine the decoder
> in front of all this and fail early if missing instead of running
> through all that code with multiple calls of cxl_find_decoder_early().
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/region.c | 31 +++++++++++++++----------------
>  1 file changed, 15 insertions(+), 16 deletions(-)

The change of order makes some sense, but now none of the naming makes
any sense.

So, if you pickup the cxl_port_pick_region_decoder() rename, that
implies an allocation. Then it follows that cxl_rr_alloc_decoder()
should be renamed to something like cxl_rr_assign_decoder(), because the
"alloc" was what cxl_region_find_decoder() was doing in that function.

That also would imply that cxl_rr_assign_decoder() cannot fail if all
the allocation work is done ahead of time.

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region()
  2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
@ 2025-03-06 20:14   ` Dan Williams
  2025-04-15 21:32   ` Alison Schofield
  2025-04-21 21:21   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:14 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> When adding an endpoint to a region, the root port is determined
> first. Move this directly into cxl_add_to_region(). This is in
> preparation of the initialization of endpoints that iterates the port
> hierarchy from the endpoint up to the root port.
> 
> As a side-effect the root argument is removed from the argument lists
> of cxl_add_to_region() and related functions. Now, the endpoint is the
> only parameter to add a region. This simplifies the function
> interface.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Looks good to me:

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder
  2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
@ 2025-03-06 20:35   ` Dan Williams
  2025-04-15 21:32   ` Alison Schofield
  2025-04-22 16:09   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:35 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine the root
> decoder associated to an endpoint decoder. Factor out that code for
> later reuse. This has the benefit of reducing cxl_add_to_region()'s
> function complexity.
> 
> The reference of cxlrd_dev can be freed earlier. Since the root
> decoder exists as long as the root port exists and the endpoint
> already holds a reference to the root port, this additional reference
> is not needed. Though it looks obvious to use __free() for the
> reference of cxlrd_dev here too, this is done in a later rework. So
> just move the code.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Looks good to me:

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region
  2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
@ 2025-03-06 20:37   ` Dan Williams
  2025-04-15 21:33   ` Alison Schofield
  2025-04-23 15:32   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:37 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine a root
> decoder's region. Factor that code out. This is in preparation to
> further rework and simplify function cxl_add_to_region().
> 
> No functional changes.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/region.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 7d9d9b8f9eea..70ff4c94fb7a 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -3416,10 +3416,22 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
>  	return cxlr;
>  }
>  
> +static struct cxl_region *
> +cxl_find_region_by_range(struct cxl_root_decoder *cxlrd, struct range *hpa)
> +{
> +	struct device *region_dev;
> +
> +	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
> +				       match_region_by_range);
> +	if (!region_dev)
> +		return NULL;
> +
> +	return to_cxl_region(region_dev);
> +}
> +
>  int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  {
>  	struct range *hpa = &cxled->cxld.hpa_range;
> -	struct device *region_dev;
>  	struct cxl_root_decoder *cxlrd;
>  	struct cxl_region_params *p;
>  	struct cxl_region *cxlr;
> @@ -3435,13 +3447,9 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  	 * one does the construction and the others add to that.
>  	 */
>  	mutex_lock(&cxlrd->range_lock);
> -	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
> -				       match_region_by_range);
> -	if (!region_dev) {
> +	cxlr = cxl_find_region_by_range(cxlrd, hpa);
> +	if (!cxlr)
>  		cxlr = construct_region(cxlrd, cxled);
> -		region_dev = &cxlr->dev;
> -	} else
> -		cxlr = to_cxl_region(region_dev);
>  	mutex_unlock(&cxlrd->range_lock);
>  
>  	rc = PTR_ERR_OR_ZERO(cxlr);
> @@ -3466,7 +3474,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  				p->res);
>  	}
>  
> -	put_device(region_dev);
> +	put_device(&cxlr->dev);			/* cxl_find_region_by_range() */

I would not mind a DEFINE_FREE(put_cxl_region, ...) to automate this cleanup,
but other than that:

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range
  2025-03-06 16:44 ` [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range Robert Richter
@ 2025-03-06 20:55   ` Dan Williams
  0 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:55 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Factor out code to find the switch decoder of a port for a specific
> address range. Reuse the code to search a root decoder, create the
> function cxl_port_find_switch_decoder() and rework
> match_root_decoder_by_range() to be usable for switch decoders too.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/region.c | 48 +++++++++++++++++++++++----------------
>  1 file changed, 28 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 70ff4c94fb7a..cf58ee284696 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -3198,33 +3198,48 @@ static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
>  	return rc;
>  }
>  
> -static int match_root_decoder_by_range(struct device *dev,
> -				       const void *data)
> +static int match_decoder_by_range(struct device *dev, const void *data)
>  {
>  	const struct range *r1, *r2 = data;
> -	struct cxl_root_decoder *cxlrd;
> +	struct cxl_decoder *cxld;
>  
> -	if (!is_root_decoder(dev))
> +	if (!is_switch_decoder(dev))
>  		return 0;
>  
> -	cxlrd = to_cxl_root_decoder(dev);
> -	r1 = &cxlrd->cxlsd.cxld.hpa_range;
> +	cxld = to_cxl_decoder(dev);
> +	r1 = &cxld->hpa_range;
>  	return range_contains(r1, r2);
>  }
>  
> +static struct cxl_decoder *
> +cxl_port_find_switch_decoder(struct cxl_port *port, struct range *hpa)
> +{
> +	/*
> +	 * device_find_child() increments the reference count of the
> +	 * the switch decoder's parent port to protect the reference
> +	 * to its child. The port is already a parent of the endpoint
> +	 * decoder's port, at least indirectly in the port hierarchy.
> +	 * Thus, the endpoint already holds a reference for the parent
> +	 * port of the switch decoder. Free the unnecessary reference
> +	 * here.
> +	 */
> +	struct device *cxld_dev __free(put_device) =
> +		device_find_child(&port->dev, hpa, match_decoder_by_range);
> +
> +	return cxld_dev ? to_cxl_decoder(cxld_dev) : NULL;

After seeing this comment block repeated, I am less enthusiastic about
this approach.

I am worried about cases where something wants to potentially hold the
reference outside of the lifetime of the endpoint. I do not think we
necessarily have those today but there is now a mix of "find" helpers
that assume the caller will maintain an endpoint port reference and
subtle bugs looming if that assumption is violated.

Part of the reason for the "put_device early with a comment" approach
was to not need to deal with some of the awkwardness of dropping the
reference in all exit paths.

However, the scope-based cleanup helpers now automate that awkwardness.
So, I would much rather have a DEFINE_FREE(put_cxl_<object>) to pair
with each "find" operation and drop all these repeated "its ok to
violate typical reference expectations" comment blocks.

So, that's a nak for this approach from me.

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure
  2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
@ 2025-03-06 20:57   ` Dan Williams
  2025-04-15 21:34   ` Alison Schofield
  2025-04-23 20:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:57 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Esp. in complex system configurations with multiple endpoints and
> interleaving setups it is hard to detect region setup failures as its
> registration may silently fail. Add messages to show registration
> failures.
> 
> Example log message:
> 
>   cxl region5: region sort successful
>   cxl region5: mem0:endpoint5 decoder5.0 add: mem0:decoder5.0 @ 0 next: none nr_eps: 1 nr_targets: 1
>   cxl_port endpoint5: decoder5.0: range: 0x22350000000-0x2634fffffff iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1 decoder1.2 add: mem0:decoder5.0 @ 0 next: mem0 nr_eps: 1 nr_targets: 1
>   cxl region5: pci0000:e0:port1 iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1: decoder1.2 expected 0000:e0:01.2 at 0
>   cxl endpoint5: failed to attach decoder5.0 to region5: -6
>   cxl_port endpoint5: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

LGTM

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries
  2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
@ 2025-03-06 20:58   ` Dan Williams
  2025-04-15 21:35   ` Alison Schofield
  2025-04-23 20:50   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 20:58 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Broken target lists are hard to discover as the driver fails at a
> later initialization stage. Add an error message for this.
> 
> Example log messages:
> 
>   cxl_mem mem1: failed to find endpoint6:0000:e0:01.3 in target list of decoder1.1
>   cxl_port endpoint6: failed to register decoder6.0: -6
>   cxl_port endpoint6: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

LGTM

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port
  2025-03-06 16:44 ` [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port Robert Richter
@ 2025-03-06 21:06   ` Dan Williams
  2025-04-29 13:43     ` Robert Richter
  2025-04-15 21:36   ` Alison Schofield
  1 sibling, 1 reply; 52+ messages in thread
From: Dan Williams @ 2025-03-06 21:06 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> Improve debugging by adding and unifying messages whenever a decoder
> was added to a port. It is especially useful to get the decoder
> mapping of the involved CXL host bridge or PCI device. This avoids a
> complex lookup of the decoder/port/device mappings in sysfs.

I am not opposed to this debug statement, but I hope that most use cases
for associating objects are handled by tooling and we don't need to keep
extending kernel debug statements for these queries.

E.g.: show all the decoders and targets of port1

# cxl list -PT -p 1 -Di
[
  {
    "port":"port1",
    "host":"pci0000:34",
    "depth":1,
    "decoders_committed":0,
    "nr_dports":1,
    "dports":[
      {
        "dport":"0000:34:00.0",
        "id":0
      }
    ],
    "decoders:port1":[
      {
        "decoder":"decoder2.0",
        "interleave_ways":1,
        "state":"disabled"
      },
      {
        "decoder":"decoder2.1",
        "interleave_ways":1,
        "state":"disabled"
      },
      {
        "decoder":"decoder2.2",
        "interleave_ways":1,
        "state":"disabled"
      },
      {
        "decoder":"decoder2.3",
        "interleave_ways":1,
        "state":"disabled"
      },
      {
        "decoder":"decoder1.0",
        "interleave_ways":1,
        "state":"disabled",
        "nr_targets":1,
        "targets":[
          {
            "target":"0000:34:00.0",
            "position":0,
            "id":0
          }
        ]
      }
    ]
  }
]

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages
  2025-03-06 16:44 ` [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages Robert Richter
@ 2025-03-06 21:12   ` Dan Williams
  0 siblings, 0 replies; 52+ messages in thread
From: Dan Williams @ 2025-03-06 21:12 UTC (permalink / raw)
  To: Robert Richter, Alison Schofield, Vishal Verma, Ira Weiny,
	Dan Williams, Jonathan Cameron, Dave Jiang, Davidlohr Bueso
  Cc: linux-cxl, linux-kernel, Gregory Price, Fabio M. De Francesco,
	Terry Bowman, Robert Richter

Robert Richter wrote:
> CFMWS entries have a similar importance as SRAT table entries to
> describe memory regions. For CXL error analysis and memory debugging
> information of both is needed. Unify output of both messages to
> improve logging. Change the style of CFMWS message according to SRAT
> output. Also, turn messages into a dev_info() same as for SRAT.
> 
> SRAT pr_info() for reference:
> 
> drivers/acpi/numa/srat.c:
>   pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/acpi.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

NAK on this one. If you want to add this print the best place would be
in acpi_parse_cfmws() so that it can include the PXM information and be
independent of the cxl_acpi module loading.

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 01/14] cxl: Remove else after return
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
@ 2025-03-14 11:27   ` Jonathan Cameron
  2025-04-15 21:27   ` Alison Schofield
  2025-04-18 16:23   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Jonathan Cameron @ 2025-03-14 11:27 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, 6 Mar 2025 17:44:35 +0100
Robert Richter <rrichter@amd.com> wrote:

> Remove unnecessary 'else' after return. Improves readability of code.
> It is easier to place comments. Check and fix all occurrences under
> drivers/cxl/.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Dave, if you see this in time, can we sneak this one in for the
merge window? It's the sort of cleanup that just distracts (a little)
from more substantial rest of this series and is good in it's
own right.

> ---
>  drivers/cxl/core/cdat.c   | 2 +-
>  drivers/cxl/core/pci.c    | 3 ++-
>  drivers/cxl/core/region.c | 4 +++-
>  3 files changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cxl/core/cdat.c b/drivers/cxl/core/cdat.c
> index edb4f41eeacc..0ccef2f2a26a 100644
> --- a/drivers/cxl/core/cdat.c
> +++ b/drivers/cxl/core/cdat.c
> @@ -28,7 +28,7 @@ static u32 cdat_normalize(u16 entry, u64 base, u8 type)
>  	 */
>  	if (entry == 0xffff || !entry)
>  		return 0;
> -	else if (base > (UINT_MAX / (entry)))
> +	if (base > (UINT_MAX / (entry)))
>  		return 0;
>  
>  	/*
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 96fecb799cbc..33c3bdd35b24 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -415,7 +415,8 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	 */
>  	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
> -	else if (!hdm)
> +
> +	if (!hdm)
>  		return -ENODEV;
>  
>  	root = to_cxl_port(port->dev.parent);
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 8537b6a9ca18..cbe762abf6b3 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -1940,7 +1940,9 @@ static int cxl_region_attach(struct cxl_region *cxlr,
>  	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
>  		dev_dbg(&cxlr->dev, "region already active\n");
>  		return -EBUSY;
> -	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
> +	}
> +
> +	if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
>  		dev_dbg(&cxlr->dev, "interleave config missing\n");
>  		return -ENXIO;
>  	}


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
@ 2025-03-14 11:31   ` Jonathan Cameron
  2025-04-15 21:29   ` Alison Schofield
  2025-04-18 16:28   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Jonathan Cameron @ 2025-03-14 11:31 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, 6 Mar 2025 17:44:36 +0100
Robert Richter <rrichter@amd.com> wrote:

> Commit 3f9e07531778 ("cxl/pci: simplify the check of mem_enabled in
> cxl_hdm_decode_init()") changed the code flow in this function. The
> root port is determined before a check to leave the function. Since
> the root port is not used by the check it can be moved to run the
> check first. This improves code readability and avoids unnesessary
> code execution.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>  drivers/cxl/core/pci.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 33c3bdd35b24..6386e84e51a4 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -419,14 +419,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	if (!hdm)
>  		return -ENODEV;
>  
> -	root = to_cxl_port(port->dev.parent);
> -	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
> -		root = to_cxl_port(root->dev.parent);
> -	if (!is_cxl_root(root)) {
> -		dev_err(dev, "Failed to acquire root port for HDM enable\n");
> -		return -ENODEV;
> -	}
> -
>  	if (!info->mem_enabled) {
>  		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
>  		if (rc)
> @@ -435,6 +427,14 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  	}
>  
> +	root = to_cxl_port(port->dev.parent);
> +	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
> +		root = to_cxl_port(root->dev.parent);
> +	if (!is_cxl_root(root)) {
> +		dev_err(dev, "Failed to acquire root port for HDM enable\n");

Is this error message suffering from code rot?  In the path where
root is used, use no HDM enabling occurs.

> +		return -ENODEV;
> +	}
> +
>  	for (i = 0, allowed = 0; i < info->ranges; i++) {
>  		struct device *cxld_dev;
>  


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
@ 2025-03-14 11:33   ` Jonathan Cameron
  2025-04-17 13:11     ` Robert Richter
  2025-04-15 21:30   ` Alison Schofield
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 52+ messages in thread
From: Jonathan Cameron @ 2025-03-14 11:33 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, 6 Mar 2025 17:44:37 +0100
Robert Richter <rrichter@amd.com> wrote:

> There are various configuration cases of HDM decoder registers causing
> different code paths. Add comments to cxl_hdm_decode_init() to better
> explain them.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Tested-by: Gregory Price <gourry@gourry.net>
Trivial comment inline.
Otherwise I think this is fine.
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/cxl/core/pci.c | 33 ++++++++++++++++++++++++---------
>  1 file changed, 24 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 6386e84e51a4..ef4b08abe424 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -416,9 +416,21 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  
> +	/*
> +	 * If the HDM Decoder Capability does not exist and DVSEC was
> +	 * not setup, the DVSEC based emulation cannot be used.
> +	 */
>  	if (!hdm)
>  		return -ENODEV;
>  
> +	/*
> +	 * The HDM Decoder Capability exists but is globally disabled.

Single line comment syntax appropriate here.

> +	 */
> +
> +	/*
> +	 * If the DVSEC CXL Range registers are not enabled, just
> +	 * enable and use the HDM Decoder Capability registers.
> +	 */
>  	if (!info->mem_enabled) {
>  		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
>  		if (rc)
> @@ -427,6 +439,18 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  	}
>  
> +	/*
> +	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
> +	 * [High,Low] when HDM operation is enabled the range register values
> +	 * are ignored by the device, but the spec also recommends matching the
> +	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
> +	 * are expected even though Linux does not require or maintain that
> +	 * match. Check if at least one DVSEC range is enabled and allowed by
> +	 * the platform. That is, the DVSEC range must be covered by a locked
> +	 * platform window (CFMWS). Fail otherwise as the endpoint's decoders
> +	 * cannot be used.
> +	 */
> +
>  	root = to_cxl_port(port->dev.parent);
>  	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
>  		root = to_cxl_port(root->dev.parent);
> @@ -454,15 +478,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return -ENXIO;
>  	}
>  
> -	/*
> -	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
> -	 * [High,Low] when HDM operation is enabled the range register values
> -	 * are ignored by the device, but the spec also recommends matching the
> -	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
> -	 * are expected even though Linux does not require or maintain that
> -	 * match. If at least one DVSEC range is enabled and allowed, skip HDM
> -	 * Decoder Capability Enable.
> -	 */
>  	return 0;
>  }
>  EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, "CXL");


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 01/14] cxl: Remove else after return
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
  2025-03-14 11:27   ` Jonathan Cameron
@ 2025-04-15 21:27   ` Alison Schofield
  2025-04-18 16:23   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:27 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:35PM +0100, Robert Richter wrote:
> Remove unnecessary 'else' after return. Improves readability of code.
> It is easier to place comments. Check and fix all occurrences under
> drivers/cxl/.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>



^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
  2025-03-14 11:31   ` Jonathan Cameron
@ 2025-04-15 21:29   ` Alison Schofield
  2025-04-18 16:28   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:29 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:36PM +0100, Robert Richter wrote:
> Commit 3f9e07531778 ("cxl/pci: simplify the check of mem_enabled in
> cxl_hdm_decode_init()") changed the code flow in this function. The
> root port is determined before a check to leave the function. Since
> the root port is not used by the check it can be moved to run the
> check first. This improves code readability and avoids unnesessary
> code execution.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
  2025-03-14 11:33   ` Jonathan Cameron
@ 2025-04-15 21:30   ` Alison Schofield
  2025-04-17 13:48   ` Gregory Price
  2025-04-18 16:34   ` Fabio M. De Francesco
  3 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:30 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:37PM +0100, Robert Richter wrote:
> There are various configuration cases of HDM decoder registers causing
> different code paths. Add comments to cxl_hdm_decode_init() to better
> explain them.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Reviewed-by: Alison Schofield <alison.schofield@intel.com>



^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 04/14] cxl: Introduce parent_port_of() helper
  2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
  2025-03-06 18:57   ` Dan Williams
@ 2025-04-15 21:30   ` Alison Schofield
  2025-04-21 17:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:30 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:38PM +0100, Robert Richter wrote:
> Often a parent port must be determined. Introduce the parent_port_of()
> helper function for this.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region()
  2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
  2025-03-06 20:14   ` Dan Williams
@ 2025-04-15 21:32   ` Alison Schofield
  2025-04-21 21:21   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:32 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:41PM +0100, Robert Richter wrote:
> When adding an endpoint to a region, the root port is determined
> first. Move this directly into cxl_add_to_region(). This is in
> preparation of the initialization of endpoints that iterates the port
> hierarchy from the endpoint up to the root port.
> 
> As a side-effect the root argument is removed from the argument lists
> of cxl_add_to_region() and related functions. Now, the endpoint is the
> only parameter to add a region. This simplifies the function
> interface.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder
  2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
  2025-03-06 20:35   ` Dan Williams
@ 2025-04-15 21:32   ` Alison Schofield
  2025-04-22 16:09   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:32 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:42PM +0100, Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine the root
> decoder associated to an endpoint decoder. Factor out that code for
> later reuse. This has the benefit of reducing cxl_add_to_region()'s
> function complexity.
> 
> The reference of cxlrd_dev can be freed earlier. Since the root
> decoder exists as long as the root port exists and the endpoint
> already holds a reference to the root port, this additional reference
> is not needed. Though it looks obvious to use __free() for the
> reference of cxlrd_dev here too, this is done in a later rework. So
> just move the code.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region
  2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
  2025-03-06 20:37   ` Dan Williams
@ 2025-04-15 21:33   ` Alison Schofield
  2025-04-23 15:32   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:33 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:43PM +0100, Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine a root
> decoder's region. Factor that code out. This is in preparation to
> further rework and simplify function cxl_add_to_region().
> 
> No functional changes.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>



^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure
  2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
  2025-03-06 20:57   ` Dan Williams
@ 2025-04-15 21:34   ` Alison Schofield
  2025-04-23 20:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:34 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:45PM +0100, Robert Richter wrote:
> Esp. in complex system configurations with multiple endpoints and
> interleaving setups it is hard to detect region setup failures as its
> registration may silently fail. Add messages to show registration
> failures.
> 
> Example log message:
> 
>   cxl region5: region sort successful
>   cxl region5: mem0:endpoint5 decoder5.0 add: mem0:decoder5.0 @ 0 next: none nr_eps: 1 nr_targets: 1
>   cxl_port endpoint5: decoder5.0: range: 0x22350000000-0x2634fffffff iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1 decoder1.2 add: mem0:decoder5.0 @ 0 next: mem0 nr_eps: 1 nr_targets: 1
>   cxl region5: pci0000:e0:port1 iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1: decoder1.2 expected 0000:e0:01.2 at 0
>   cxl endpoint5: failed to attach decoder5.0 to region5: -6
>   cxl_port endpoint5: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>



^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries
  2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
  2025-03-06 20:58   ` Dan Williams
@ 2025-04-15 21:35   ` Alison Schofield
  2025-04-23 20:50   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:35 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:46PM +0100, Robert Richter wrote:
> Broken target lists are hard to discover as the driver fails at a
> later initialization stage. Add an error message for this.
> 
> Example log messages:
> 
>   cxl_mem mem1: failed to find endpoint6:0000:e0:01.3 in target list of decoder1.1
>   cxl_port endpoint6: failed to register decoder6.0: -6
>   cxl_port endpoint6: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port
  2025-03-06 16:44 ` [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port Robert Richter
  2025-03-06 21:06   ` Dan Williams
@ 2025-04-15 21:36   ` Alison Schofield
  1 sibling, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 21:36 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:47PM +0100, Robert Richter wrote:
> Improve debugging by adding and unifying messages whenever a decoder
> was added to a port. It is especially useful to get the decoder
> mapping of the involved CXL host bridge or PCI device. This avoids a
> complex lookup of the decoder/port/device mappings in sysfs.
> 
> Example log messages:
> 
>   cxl_acpi ACPI0017:00: decoder0.0 added to root0
>   cxl_acpi ACPI0017:00: decoder0.1 added to root0
>   ...
>    pci0000:e0: decoder1.0 added to port1
>    pci0000:e0: decoder1.1 added to port1
>   ...
>   cxl_mem mem0: decoder5.0 added to endpoint5
>   cxl_mem mem0: decoder5.1 added to endpoint5
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Alison Schofield <alison.schofield@intel.com>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring
  2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
                   ` (13 preceding siblings ...)
  2025-03-06 16:44 ` [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages Robert Richter
@ 2025-04-15 23:23 ` Alison Schofield
  14 siblings, 0 replies; 52+ messages in thread
From: Alison Schofield @ 2025-04-15 23:23 UTC (permalink / raw)
  To: Robert Richter
  Cc: Vishal Verma, Ira Weiny, Dan Williams, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:34PM +0100, Robert Richter wrote:
> This series is the first part of adding support for CXL address
> translation. It contains cleanup and code refactoring in preparation
> of the actual implementation that will be sent in part 2. Cleanup and
> code refactoring have been split in a separate series to reduce the
> number of patches of the series. Even without address translation on
> top this rework improves esp. the region code, cleans it up,
> simplifies it and adds debugging messages to better analyze region
> creation failures:
> 
> Content of patches:
> 
>  * Patches 1: Remove else after return.
> 
>  * Patches 2-3: Cleanups and comments around cxl_hdm_decode_init().
> 
>  * Patches 4, 5: Adding and modifying helper functions.
> 
>  * Patches 6-10: Refactoring of endpoint decoder setup.
> 
>  * Patches 11-14: Adding and modifying debug messages.
> 

Hi Robert, 

cross-post from Discord here:

@Robert Richter @Dave Jiang (Intel)  I sync'd my work on top of Roberts
v4 Part One, and then walked thru and tagged most of the 14 patch set.
Can a bunch of those go onto cxl/next now?  I saw 'hard' no's on 5,6,10
and a couple of maybes 2,3,9 but even if you omit all those, we could
get 8 patches simmering on cxl/next now.

In the collab sync you mentioned working this set. After using and
reviewing today, and seeing I'm not the first person to suggest this,
I think we would benefit from merging part of this set while you 
refine the rest.

I believe they all have plenty of tags, so it's based on what you are
willing to let fly!

-- Alison


> v4:
>  * rebased onto cxl/next, commit 0a14566be090 ("cxl/Documentation:
>    Remove 'mixed' from sysfs mode doc"),
>  * added tags to SOB chain,
>  * reworked comments in cxl_hdm_decode_init() (dropped moving comment
>    and updated patch that modifies comments) (Jonathan),
>  * reworded patch description that removes duplicate call of
>    cxl_find_decoder_early() (Jonathan),
>  * moved some patches out of this rework and cleanup series (Dave,
>    Jonathan),
> 
> v3:
>  * added tags to SOB chain,
>  * fixed NULL pointer dereference in cxl_find_root_decoder() (Alison),
>  * updated subject line of patches that add kernel messages and
>    included example log messages (Alison),
> 
> v2:
>  * rebased onto cxl/next,
>  * added tags to SOB chain,
>  * move patches with cleanups and refactoring into this separate
>    series (Dave),
>  * added patch "cxl/acpi: Unify CFMWS memory log messages with SRAT
>    messages" to improve CFMWS log messages,
>  * renamed endpoint decoder functions to cxl_endpoint_decoder_*() (Li),
>  * reworded patch description that moves find_cxl_root() and reworks
>    cxl_find_root_decoder() (Terry),
>  * small changes to cxl_find_root_decoder()/
>    cxl_endpoint_decoder_initialize() (Jonanthan),
>  * updated comment in cxl_port_find_switch_decoder() (Ben),
>  * cxl_endpoint_decoder_initialize(): Simplify variable declaration
>    (Jonathan, Ben),
>  * cxl_find_decoder_early(): Added comment on function usage (Gregory),
>  * reordered patches and reworded some of the subject for a better
>    structure.
> 
> Robert Richter (14):
>   cxl: Remove else after return
>   cxl/pci: Moving code in cxl_hdm_decode_init()
>   cxl/pci: Add comments to cxl_hdm_decode_init()
>   cxl: Introduce parent_port_of() helper
>   cxl/region: Rename function to cxl_find_decoder_early()
>   cxl/region: Avoid duplicate call of cxl_find_decoder_early()
>   cxl/region: Move find_cxl_root() to cxl_add_to_region()
>   cxl/region: Factor out code to find the root decoder
>   cxl/region: Factor out code to find a root decoder's region
>   cxl/region: Add function to find a port's switch decoder by range
>   cxl/region: Add a dev_warn() on registration failure
>   cxl/region: Add a dev_err() on missing target list entries
>   cxl: Add a dev_dbg() when a decoder was added to a port
>   cxl/acpi: Unify CFMWS memory log messages with SRAT messages
> 
>  drivers/cxl/acpi.c        |  12 ++-
>  drivers/cxl/core/cdat.c   |   2 +-
>  drivers/cxl/core/hdm.c    |   3 +-
>  drivers/cxl/core/pci.c    |  50 +++++++----
>  drivers/cxl/core/port.c   |  15 ++--
>  drivers/cxl/core/region.c | 170 +++++++++++++++++++++++++-------------
>  drivers/cxl/cxl.h         |   7 +-
>  drivers/cxl/port.c        |  15 +---
>  8 files changed, 172 insertions(+), 102 deletions(-)
> 
> 
> base-commit: 0a14566be090ca51a32ebdd8a8e21678062dac08
> -- 
> 2.39.5
> 

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-14 11:33   ` Jonathan Cameron
@ 2025-04-17 13:11     ` Robert Richter
  0 siblings, 0 replies; 52+ messages in thread
From: Robert Richter @ 2025-04-17 13:11 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

On 14.03.25 11:33:37, Jonathan Cameron wrote:
> On Thu, 6 Mar 2025 17:44:37 +0100
> Robert Richter <rrichter@amd.com> wrote:
> 
> > There are various configuration cases of HDM decoder registers causing
> > different code paths. Add comments to cxl_hdm_decode_init() to better
> > explain them.
> > 
> > Signed-off-by: Robert Richter <rrichter@amd.com>
> > Tested-by: Gregory Price <gourry@gourry.net>
> Trivial comment inline.
> Otherwise I think this is fine.
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> > +	/*
> > +	 * The HDM Decoder Capability exists but is globally disabled.
> 
> Single line comment syntax appropriate here.
> 
> > +	 */

Changed that.

Thanks for review.

-Robert

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
  2025-03-14 11:33   ` Jonathan Cameron
  2025-04-15 21:30   ` Alison Schofield
@ 2025-04-17 13:48   ` Gregory Price
  2025-04-18 16:34   ` Fabio M. De Francesco
  3 siblings, 0 replies; 52+ messages in thread
From: Gregory Price @ 2025-04-17 13:48 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, linux-cxl,
	linux-kernel, Fabio M. De Francesco, Terry Bowman

On Thu, Mar 06, 2025 at 05:44:37PM +0100, Robert Richter wrote:
> There are various configuration cases of HDM decoder registers causing
> different code paths. Add comments to cxl_hdm_decode_init() to better
> explain them.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Reviewed-by: Gregory Price <gourry@gourry.net>


^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 01/14] cxl: Remove else after return
  2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
  2025-03-14 11:27   ` Jonathan Cameron
  2025-04-15 21:27   ` Alison Schofield
@ 2025-04-18 16:23   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-18 16:23 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:35 PM Central European Summer Time Robert Richter wrote:
> Remove unnecessary 'else' after return. Improves readability of code.
> It is easier to place comments. Check and fix all occurrences under
> drivers/cxl/.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> 
Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
>
> ---
>  drivers/cxl/core/cdat.c   | 2 +-
>  drivers/cxl/core/pci.c    | 3 ++-
>  drivers/cxl/core/region.c | 4 +++-
>  3 files changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cxl/core/cdat.c b/drivers/cxl/core/cdat.c
> index edb4f41eeacc..0ccef2f2a26a 100644
> --- a/drivers/cxl/core/cdat.c
> +++ b/drivers/cxl/core/cdat.c
> @@ -28,7 +28,7 @@ static u32 cdat_normalize(u16 entry, u64 base, u8 type)
>  	 */
>  	if (entry == 0xffff || !entry)
>  		return 0;
> -	else if (base > (UINT_MAX / (entry)))
> +	if (base > (UINT_MAX / (entry)))
>  		return 0;
>  
>  	/*
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 96fecb799cbc..33c3bdd35b24 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -415,7 +415,8 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	 */
>  	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
> -	else if (!hdm)
> +
> +	if (!hdm)
>  		return -ENODEV;
>  
>  	root = to_cxl_port(port->dev.parent);
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 8537b6a9ca18..cbe762abf6b3 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -1940,7 +1940,9 @@ static int cxl_region_attach(struct cxl_region *cxlr,
>  	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
>  		dev_dbg(&cxlr->dev, "region already active\n");
>  		return -EBUSY;
> -	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
> +	}
> +
> +	if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
>  		dev_dbg(&cxlr->dev, "interleave config missing\n");
>  		return -ENXIO;
>  	}
> 





^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
  2025-03-14 11:31   ` Jonathan Cameron
  2025-04-15 21:29   ` Alison Schofield
@ 2025-04-18 16:28   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-18 16:28 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

[-- Attachment #1: Type: text/plain, Size: 2106 bytes --]

On Thursday, March 6, 2025 5:44:36 PM Central European Summer Time Robert Richter wrote:
> Commit 3f9e07531778 ("cxl/pci: simplify the check of mem_enabled in
> cxl_hdm_decode_init()") changed the code flow in this function. The
> root port is determined before a check to leave the function. Since
> the root port is not used by the check it can be moved to run the
> check first. This improves code readability and avoids unnesessary
> code execution.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
> Tested-by: Gregory Price <gourry@gourry.net>
>
Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
>
> ---
>  drivers/cxl/core/pci.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 33c3bdd35b24..6386e84e51a4 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -419,14 +419,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	if (!hdm)
>  		return -ENODEV;
>  
> -	root = to_cxl_port(port->dev.parent);
> -	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
> -		root = to_cxl_port(root->dev.parent);
> -	if (!is_cxl_root(root)) {
> -		dev_err(dev, "Failed to acquire root port for HDM enable\n");
> -		return -ENODEV;
> -	}
> -
>  	if (!info->mem_enabled) {
>  		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
>  		if (rc)
> @@ -435,6 +427,14 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  	}
>  
> +	root = to_cxl_port(port->dev.parent);
> +	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
> +		root = to_cxl_port(root->dev.parent);
> +	if (!is_cxl_root(root)) {
> +		dev_err(dev, "Failed to acquire root port for HDM enable\n");
> +		return -ENODEV;
> +	}
> +
>  	for (i = 0, allowed = 0; i < info->ranges; i++) {
>  		struct device *cxld_dev;
>  
> 


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init()
  2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
                     ` (2 preceding siblings ...)
  2025-04-17 13:48   ` Gregory Price
@ 2025-04-18 16:34   ` Fabio M. De Francesco
  3 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-18 16:34 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:37 PM Central European Summer Time Robert Richter wrote:
> There are various configuration cases of HDM decoder registers causing
> different code paths. Add comments to cxl_hdm_decode_init() to better
> explain them.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Tested-by: Gregory Price <gourry@gourry.net>
>
Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
>
> ---
>  drivers/cxl/core/pci.c | 33 ++++++++++++++++++++++++---------
>  1 file changed, 24 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/cxl/core/pci.c b/drivers/cxl/core/pci.c
> index 6386e84e51a4..ef4b08abe424 100644
> --- a/drivers/cxl/core/pci.c
> +++ b/drivers/cxl/core/pci.c
> @@ -416,9 +416,21 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  	if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  
> +	/*
> +	 * If the HDM Decoder Capability does not exist and DVSEC was
> +	 * not setup, the DVSEC based emulation cannot be used.
> +	 */
>  	if (!hdm)
>  		return -ENODEV;
>  
> +	/*
> +	 * The HDM Decoder Capability exists but is globally disabled.
> +	 */
> +
> +	/*
> +	 * If the DVSEC CXL Range registers are not enabled, just
> +	 * enable and use the HDM Decoder Capability registers.
> +	 */
>  	if (!info->mem_enabled) {
>  		rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
>  		if (rc)
> @@ -427,6 +439,18 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return devm_cxl_enable_mem(&port->dev, cxlds);
>  	}
>  
> +	/*
> +	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
> +	 * [High,Low] when HDM operation is enabled the range register values
> +	 * are ignored by the device, but the spec also recommends matching the
> +	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
> +	 * are expected even though Linux does not require or maintain that
> +	 * match. Check if at least one DVSEC range is enabled and allowed by
> +	 * the platform. That is, the DVSEC range must be covered by a locked
> +	 * platform window (CFMWS). Fail otherwise as the endpoint's decoders
> +	 * cannot be used.
> +	 */
> +
>  	root = to_cxl_port(port->dev.parent);
>  	while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
>  		root = to_cxl_port(root->dev.parent);
> @@ -454,15 +478,6 @@ int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
>  		return -ENXIO;
>  	}
>  
> -	/*
> -	 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
> -	 * [High,Low] when HDM operation is enabled the range register values
> -	 * are ignored by the device, but the spec also recommends matching the
> -	 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
> -	 * are expected even though Linux does not require or maintain that
> -	 * match. If at least one DVSEC range is enabled and allowed, skip HDM
> -	 * Decoder Capability Enable.
> -	 */
>  	return 0;
>  }
>  EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, "CXL");
> 





^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 04/14] cxl: Introduce parent_port_of() helper
  2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
  2025-03-06 18:57   ` Dan Williams
  2025-04-15 21:30   ` Alison Schofield
@ 2025-04-21 17:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-21 17:49 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:38 PM Central European Summer Time Robert Richter wrote:
> Often a parent port must be determined. Introduce the parent_port_of()
> helper function for this.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
>
I think that "often" doesn't provide any relevant information.
I would rephrase the commit message with something like that:

"Introduce the parent_port_of() helper function to avoid open
coding of determination of a parent port.".

Other than that:

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
>
>  drivers/cxl/core/port.c   | 15 +++++++++------
>  drivers/cxl/core/region.c | 11 ++---------
>  drivers/cxl/cxl.h         |  1 +
>  3 files changed, 12 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index 6a44b6dad3c7..25eecb591496 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c
> @@ -602,17 +602,20 @@ struct cxl_port *to_cxl_port(const struct device *dev)
>  }
>  EXPORT_SYMBOL_NS_GPL(to_cxl_port, "CXL");
>  
> +struct cxl_port *parent_port_of(struct cxl_port *port)
> +{
> +	if (!port || !port->parent_dport)
> +		return NULL;
> +	return port->parent_dport->port;
> +}
> +EXPORT_SYMBOL_NS_GPL(parent_port_of, "CXL");
> +
>  static void unregister_port(void *_port)
>  {
>  	struct cxl_port *port = _port;
> -	struct cxl_port *parent;
> +	struct cxl_port *parent = parent_port_of(port);
>  	struct device *lock_dev;
>  
> -	if (is_cxl_root(port))
> -		parent = NULL;
> -	else
> -		parent = to_cxl_port(port->dev.parent);
> -
>  	/*
>  	 * CXL root port's and the first level of ports are unregistered
>  	 * under the platform firmware device lock, all other ports are
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index cbe762abf6b3..4f79cc17c9c8 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -1748,13 +1748,6 @@ static int cmp_interleave_pos(const void *a, const void *b)
>  	return cxled_a->pos - cxled_b->pos;
>  }
>  
> -static struct cxl_port *next_port(struct cxl_port *port)
> -{
> -	if (!port->parent_dport)
> -		return NULL;
> -	return port->parent_dport->port;
> -}
> -
>  static int match_switch_decoder_by_range(struct device *dev,
>  					 const void *data)
>  {
> @@ -1781,7 +1774,7 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
>  	struct device *dev;
>  	int rc = -ENXIO;
>  
> -	parent = next_port(port);
> +	parent = parent_port_of(port);
>  	if (!parent)
>  		return rc;
>  
> @@ -1861,7 +1854,7 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
>  	 */
>  
>  	/* Iterate from endpoint to root_port refining the position */
> -	for (iter = port; iter; iter = next_port(iter)) {
> +	for (iter = port; iter; iter = parent_port_of(iter)) {
>  		if (is_cxl_root(iter))
>  			break;
>  
> diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
> index be8a7dc77719..24cec16d02a6 100644
> --- a/drivers/cxl/cxl.h
> +++ b/drivers/cxl/cxl.h
> @@ -724,6 +724,7 @@ static inline bool is_cxl_root(struct cxl_port *port)
>  int cxl_num_decoders_committed(struct cxl_port *port);
>  bool is_cxl_port(const struct device *dev);
>  struct cxl_port *to_cxl_port(const struct device *dev);
> +struct cxl_port *parent_port_of(struct cxl_port *port);
>  void cxl_port_commit_reap(struct cxl_decoder *cxld);
>  struct pci_bus;
>  int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
> 





^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region()
  2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
  2025-03-06 20:14   ` Dan Williams
  2025-04-15 21:32   ` Alison Schofield
@ 2025-04-21 21:21   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-21 21:21 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:41 PM Central European Summer Time Robert Richter wrote:
> When adding an endpoint to a region, the root port is determined
> first. Move this directly into cxl_add_to_region(). This is in
> preparation of the initialization of endpoints that iterates the port
> hierarchy from the endpoint up to the root port.
> 
> As a side-effect the root argument is removed from the argument lists
> of cxl_add_to_region() and related functions. Now, the endpoint is the
> only parameter to add a region. This simplifies the function
> interface.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>




^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder
  2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
  2025-03-06 20:35   ` Dan Williams
  2025-04-15 21:32   ` Alison Schofield
@ 2025-04-22 16:09   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-22 16:09 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:42 PM Central European Summer Time Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine the root
> decoder associated to an endpoint decoder. Factor out that code for
> later reuse. This has the benefit of reducing cxl_add_to_region()'s
> function complexity.
> 
> The reference of cxlrd_dev can be freed earlier. Since the root
> decoder exists as long as the root port exists and the endpoint
> already holds a reference to the root port, this additional reference
> is not needed. Though it looks obvious to use __free() for the
> reference of cxlrd_dev here too, this is done in a later rework. So
> just move the code.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>

>  drivers/cxl/core/region.c | 55 ++++++++++++++++++++++++++-------------
>  1 file changed, 37 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 8244a27d0fd6..7d9d9b8f9eea 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -3212,6 +3212,38 @@ static int match_root_decoder_by_range(struct device *dev,
>  	return range_contains(r1, r2);
>  }
>  
> +static struct cxl_root_decoder *
> +cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
> +{
> +	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
> +	struct cxl_port *port = cxled_to_port(cxled);
> +	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
> +	struct cxl_decoder *cxld = &cxled->cxld;
> +	struct range *hpa = &cxld->hpa_range;
> +	struct device *cxlrd_dev;
> +
> +	cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
> +				      match_root_decoder_by_range);
> +	if (!cxlrd_dev) {
> +		dev_err(cxlmd->dev.parent,
> +			"%s:%s no CXL window for range %#llx:%#llx\n",
> +			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
> +			cxld->hpa_range.start, cxld->hpa_range.end);
> +		return NULL;
> +	}
> +
> +	/*
> +	 * device_find_child() created a reference to the root
> +	 * decoder. Since the root decoder exists as long as the root
> +	 * port exists and the endpoint already holds a reference to
> +	 * the root port, this additional reference is not needed.
> +	 * Free it here.
> +	 */
> +	put_device(cxlrd_dev);
> +
> +	return to_cxl_root_decoder(cxlrd_dev);
> +}
> +
>  static int match_region_by_range(struct device *dev, const void *data)
>  {
>  	struct cxl_region_params *p;
> @@ -3386,29 +3418,17 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
>  
>  int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  {
> -	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
> -	struct cxl_port *port = cxled_to_port(cxled);
> -	struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
>  	struct range *hpa = &cxled->cxld.hpa_range;
> -	struct cxl_decoder *cxld = &cxled->cxld;
> -	struct device *cxlrd_dev, *region_dev;
> +	struct device *region_dev;
>  	struct cxl_root_decoder *cxlrd;
>  	struct cxl_region_params *p;
>  	struct cxl_region *cxlr;
>  	bool attach = false;
>  	int rc;
>  
> -	cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
> -				      match_root_decoder_by_range);
> -	if (!cxlrd_dev) {
> -		dev_err(cxlmd->dev.parent,
> -			"%s:%s no CXL window for range %#llx:%#llx\n",
> -			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
> -			cxld->hpa_range.start, cxld->hpa_range.end);
> +	cxlrd = cxl_find_root_decoder(cxled);
> +	if (!cxlrd)
>  		return -ENXIO;
> -	}
> -
> -	cxlrd = to_cxl_root_decoder(cxlrd_dev);
>  
>  	/*
>  	 * Ensure that if multiple threads race to construct_region() for @hpa
> @@ -3426,7 +3446,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  
>  	rc = PTR_ERR_OR_ZERO(cxlr);
>  	if (rc)
> -		goto out;
> +		return rc;
>  
>  	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
>  
> @@ -3447,8 +3467,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>  	}
>  
>  	put_device(region_dev);
> -out:
> -	put_device(cxlrd_dev);
> +
>  	return rc;
>  }
>  EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
> 





^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region
  2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
  2025-03-06 20:37   ` Dan Williams
  2025-04-15 21:33   ` Alison Schofield
@ 2025-04-23 15:32   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-23 15:32 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:43 PM Central European Summer Time Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine a root
> decoder's region. Factor that code out. This is in preparation to
> further rework and simplify function cxl_add_to_region().
> 
> No functional changes.
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>




^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure
  2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
  2025-03-06 20:57   ` Dan Williams
  2025-04-15 21:34   ` Alison Schofield
@ 2025-04-23 20:49   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-23 20:49 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:45 PM Central European Summer Time Robert Richter wrote:
> Esp. in complex system configurations with multiple endpoints and
> interleaving setups it is hard to detect region setup failures as its
> registration may silently fail. Add messages to show registration
> failures.
> 
> Example log message:
> 
>   cxl region5: region sort successful
>   cxl region5: mem0:endpoint5 decoder5.0 add: mem0:decoder5.0 @ 0 next: none nr_eps: 1 nr_targets: 1
>   cxl_port endpoint5: decoder5.0: range: 0x22350000000-0x2634fffffff iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1 decoder1.2 add: mem0:decoder5.0 @ 0 next: mem0 nr_eps: 1 nr_targets: 1
>   cxl region5: pci0000:e0:port1 iw: 1 ig: 256
>   cxl region5: pci0000:e0:port1: decoder1.2 expected 0000:e0:01.2 at 0
>   cxl endpoint5: failed to attach decoder5.0 to region5: -6
>   cxl_port endpoint5: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>




^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries
  2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
  2025-03-06 20:58   ` Dan Williams
  2025-04-15 21:35   ` Alison Schofield
@ 2025-04-23 20:50   ` Fabio M. De Francesco
  2 siblings, 0 replies; 52+ messages in thread
From: Fabio M. De Francesco @ 2025-04-23 20:50 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
	Jonathan Cameron, Dave Jiang, Davidlohr Bueso, Robert Richter
  Cc: linux-cxl, linux-kernel, Gregory Price, Terry Bowman,
	Robert Richter

On Thursday, March 6, 2025 5:44:46 PM Central European Summer Time Robert Richter wrote:
> Broken target lists are hard to discover as the driver fails at a
> later initialization stage. Add an error message for this.
> 
> Example log messages:
> 
>   cxl_mem mem1: failed to find endpoint6:0000:e0:01.3 in target list of decoder1.1
>   cxl_port endpoint6: failed to register decoder6.0: -6
>   cxl_port endpoint6: probe: 0
> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> Reviewed-by: Gregory Price <gourry@gourry.net>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---

Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>




^ permalink raw reply	[flat|nested] 52+ messages in thread

* Re: [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port
  2025-03-06 21:06   ` Dan Williams
@ 2025-04-29 13:43     ` Robert Richter
  0 siblings, 0 replies; 52+ messages in thread
From: Robert Richter @ 2025-04-29 13:43 UTC (permalink / raw)
  To: Dan Williams
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Jonathan Cameron,
	Dave Jiang, Davidlohr Bueso, linux-cxl, linux-kernel,
	Gregory Price, Fabio M. De Francesco, Terry Bowman

Dan,

thanks for all your reviews.

On 06.03.25 13:06:20, Dan Williams wrote:
> Robert Richter wrote:
> > Improve debugging by adding and unifying messages whenever a decoder
> > was added to a port. It is especially useful to get the decoder
> > mapping of the involved CXL host bridge or PCI device. This avoids a
> > complex lookup of the decoder/port/device mappings in sysfs.
> 
> I am not opposed to this debug statement, but I hope that most use cases
> for associating objects are handled by tooling and we don't need to keep
> extending kernel debug statements for these queries.
> 
> E.g.: show all the decoders and targets of port1
> 
> # cxl list -PT -p 1 -Di

I kept the patch in the v5 series that I sent yesterday.

The issue I am seeing is that in case of failures the devices
including their sysfs entries are not created or bound to a driver.
Tools do not see them then nor there is a hint why there was a
failure.

-Robert

^ permalink raw reply	[flat|nested] 52+ messages in thread

end of thread, other threads:[~2025-04-29 13:43 UTC | newest]

Thread overview: 52+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-06 16:44 [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Robert Richter
2025-03-06 16:44 ` [PATCH v4 01/14] cxl: Remove else after return Robert Richter
2025-03-14 11:27   ` Jonathan Cameron
2025-04-15 21:27   ` Alison Schofield
2025-04-18 16:23   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 02/14] cxl/pci: Moving code in cxl_hdm_decode_init() Robert Richter
2025-03-14 11:31   ` Jonathan Cameron
2025-04-15 21:29   ` Alison Schofield
2025-04-18 16:28   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 03/14] cxl/pci: Add comments to cxl_hdm_decode_init() Robert Richter
2025-03-14 11:33   ` Jonathan Cameron
2025-04-17 13:11     ` Robert Richter
2025-04-15 21:30   ` Alison Schofield
2025-04-17 13:48   ` Gregory Price
2025-04-18 16:34   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 04/14] cxl: Introduce parent_port_of() helper Robert Richter
2025-03-06 18:57   ` Dan Williams
2025-04-15 21:30   ` Alison Schofield
2025-04-21 17:49   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 05/14] cxl/region: Rename function to cxl_find_decoder_early() Robert Richter
2025-03-06 19:51   ` Dan Williams
2025-03-06 16:44 ` [PATCH v4 06/14] cxl/region: Avoid duplicate call of cxl_find_decoder_early() Robert Richter
2025-03-06 20:01   ` Dan Williams
2025-03-06 16:44 ` [PATCH v4 07/14] cxl/region: Move find_cxl_root() to cxl_add_to_region() Robert Richter
2025-03-06 20:14   ` Dan Williams
2025-04-15 21:32   ` Alison Schofield
2025-04-21 21:21   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 08/14] cxl/region: Factor out code to find the root decoder Robert Richter
2025-03-06 20:35   ` Dan Williams
2025-04-15 21:32   ` Alison Schofield
2025-04-22 16:09   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 09/14] cxl/region: Factor out code to find a root decoder's region Robert Richter
2025-03-06 20:37   ` Dan Williams
2025-04-15 21:33   ` Alison Schofield
2025-04-23 15:32   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 10/14] cxl/region: Add function to find a port's switch decoder by range Robert Richter
2025-03-06 20:55   ` Dan Williams
2025-03-06 16:44 ` [PATCH v4 11/14] cxl/region: Add a dev_warn() on registration failure Robert Richter
2025-03-06 20:57   ` Dan Williams
2025-04-15 21:34   ` Alison Schofield
2025-04-23 20:49   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 12/14] cxl/region: Add a dev_err() on missing target list entries Robert Richter
2025-03-06 20:58   ` Dan Williams
2025-04-15 21:35   ` Alison Schofield
2025-04-23 20:50   ` Fabio M. De Francesco
2025-03-06 16:44 ` [PATCH v4 13/14] cxl: Add a dev_dbg() when a decoder was added to a port Robert Richter
2025-03-06 21:06   ` Dan Williams
2025-04-29 13:43     ` Robert Richter
2025-04-15 21:36   ` Alison Schofield
2025-03-06 16:44 ` [PATCH v4 14/14] cxl/acpi: Unify CFMWS memory log messages with SRAT messages Robert Richter
2025-03-06 21:12   ` Dan Williams
2025-04-15 23:23 ` [PATCH v4 00/14] cxl: Address translation support, part 1: Cleanups and refactoring Alison Schofield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox