Skip to contentSkip to conversation

cxl/region: Autodiscovery position repair

PATCH Superseded [email protected] 3 participants 7 messages Oct 25, 2023 CXL

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v3 cover cxl/region: Autodiscovery position repair

From: Alison Schofield <[email protected]>

In addition to eyes on the code, I'd appreciate some close reads of the kernel doc comments added in Patch 2. Are they useful? How can they be improved? As I completed them I started to think they were silly, but perhaps that's because I am getting quite 'steeped' in this topic.

Changes in v3:
- Collapse the local position initialization into the iterative loop. (Jim)
- Match on exact resource range when looking up a switch decoder. (Jim, Dan)
- Update changelogs comments & remove extra fixes & reported-by tags (Dan)
- Add kernel doc to calc_interleave_pos() and find_pos_and_ways() (Dan)
- Add cxl_ prefix to calc_interleave_pos() and find_pos_and_ways()
- Expand in code comments at the dev_dbg test of cxl_calc_interleave_pos()
- Reword the dev_dbg message at test of cxl_calc_interleave_pos()
- Remove stray brace in Patch 1/3 (Jim)
- Init rc to -ENXIO, not -1 (Dan)
- Remove the skip logic (was index logic in v1)
The skip logic was based on my misunderstanding of valid configs.
Once I understood that that targets cannot repeat in a decoder list,
ie a CFMWS of {0 1 0 1} allowing two hosts bridges to appear twice
in a root decoder was garbage, the prior skip logic and related shenanigans were removed.
- v2: https://lore.kernel.org/linux-cxl/[email protected]/

Changes in v2:
- Use a 'skip', which is a number of siblings to skip over, rather than
an 'index' when finding a child's position in a parent interleave.
- Tidy up commit messages for clarity and grammar. (DaveJ)
- Update this cover letter with added testing configs that led to the 'skip' change in the calculation.
- v1: https://lore.kernel.org/linux-cxl/[email protected]/

Begin original cover letter: (with updates to the 'Testing' paragraph)

Some region configurations fail to assemble through the auto-discovered region path. These are valid region configurations that can be assembled correctly if presented as user defined regions.

The difference being that user defined regions arrive at the driver with their targets in interleave order, whereas with autodiscovered regions, the driver needs to assign each target in the interleave set a correct position. And, in some cases, that fails.

cxl_region_sort_targets() uses the kernel sort() function to put the targets in relative order. Once the relative ordering is complete, positions are assigned based on each targets index in the sorted list.

That relative sort doesn't consider the offset of a port into its parent port. In the failure case, a 2 + 2 config (2 host bridges each with 2 endpoints), this causes the sort to put all targets of one port ahead of another port, when they were expected to be interleaved.

While examining the problem and weighing the option of repairing the existing sort algorithm with assigning positions another way, I chose the latter. Each endpoint can be examined individually to discover its position in the region interleave.

The presentation of this patchset was a challenge. While the changes are essentially a replacement, the resulting diff is horrible. (I did try multiple git diff algs). So after a small preparation patch (Patch 1), it's presented like this:

Patch 2:The new method, cxl_calc_interleave_pos(), is introduced and used
in a dev_dbg() exercise on user defined regions.

Patch 3:cxl_calc_interleave_pos() replaces the relative sort() in
cxl_region_sort_targets() for auto-discoverd regions and the now obsolete sort helpers are removed.

The only function that seems useful for a side by side diff viewing is cxl_region_sort_targets() and it is visible in Patch 3.

Testing passes on pre-production hardware with BIOS defined regions that natively trigger this autodiscovery path of the region driver. Testing passes a CXL unit test using the dev_dbg() calculation test (see cxl_region_attach()) across an expanded set of region configs:
1, 1, 1+1, 1+1+1, 2, 2+2, 2+2+2, 2+2+2+2, 4, 4+4, where each number represents the count of endpoints per host bridge.

Alison Schofield (3):
cxl/region: Prepare the decoder match range helper for reuse cxl/region: Calculate a target position in a region interleave cxl/region: Use cxl_calc_interleave_pos() for auto-discovery

 drivers/cxl/core/region.c | 242 +++++++++++++++++++++++---------------
 1 file changed, 145 insertions(+), 97 deletions(-)

base-commit: 8a749fd1a8720d4619c91c8b6e7528c0a355c0aa

Signature · 1 line
2.37.3
author maintainer
#1 lore raw

PATCH v3 1/3 cxl/region: Prepare the decoder match range helper for reuse

From: Alison Schofield <[email protected]>

match_decoder_by_range() and decoder_match_range() both determine if an HPA range matches a decoder. The first does it for root decoders and the second one operates on switch decoders.

Tidy these up with clear naming and make the switch helper more like the root decoder helper in style and functionality. Make it take the actual range, rather than an endpoint decoder from which it extracts the range. Require an exact match on switch decoders, because unlike a root decoder that maps an entire region, Linux only supports 1:1 mapping of switch to endpoint decoders.

Aside from aesthetics and maintainability, this is in preparation for reuse.

---
 drivers/cxl/core/region.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/cxl/core/region.c +9 -6
index 6d63b8798c29..eea8e89a6860 100644
@@ -1487,16 +1487,18 @@ static struct cxl_port *next_port(struct cxl_port *port)
 	return port->parent_dport->port;
 }
 
-static int decoder_match_range(struct device *dev, void *data)
+static int match_switch_decoder_by_range(struct device *dev, void *data)
 {
-	struct cxl_endpoint_decoder *cxled = data;
 	struct cxl_switch_decoder *cxlsd;
+	struct range *r1, *r2 = data;
 
 	if (!is_switch_decoder(dev))
 		return 0;
 
 	cxlsd = to_cxl_switch_decoder(dev);
-	return range_contains(&cxlsd->cxld.hpa_range, &cxled->cxld.hpa_range);
+	r1 = &cxlsd->cxld.hpa_range;
+
+	return (r1->start == r2->start && r1->end == r2->end);
 }
 
 static void find_positions(const struct cxl_switch_decoder *cxlsd,
@@ -1565,7 +1567,8 @@ static int cmp_decode_pos(const void *a, const void *b)
 		goto err;
 	}
 
-	dev = device_find_child(&port->dev, cxled_a, decoder_match_range);
+	dev = device_find_child(&port->dev, &cxled_a->cxld.hpa_range,
+				match_switch_decoder_by_range);
 	if (!dev) {
 		struct range *range = &cxled_a->cxld.hpa_range;
 
@@ -2696,7 +2699,7 @@ static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
 	return rc;
 }
 
-static int match_decoder_by_range(struct device *dev, void *data)
+static int match_root_decoder_by_range(struct device *dev, void *data)
 {
 	struct range *r1, *r2 = data;
 	struct cxl_root_decoder *cxlrd;
@@ -2827,7 +2830,7 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
 	int rc;
 
 	cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
-				      match_decoder_by_range);
+				      match_root_decoder_by_range);
 	if (!cxlrd_dev) {
 		dev_err(cxlmd->dev.parent,
 			"%s:%s no CXL window for range %#llx:%#llx\n",
Signature · 1 line
2.37.3
author maintainer
#2 lore raw

PATCH v3 2/3 cxl/region: Calculate a target position in a region interleave

In reply to [email protected]

From: Alison Schofield <[email protected]>

Introduce a calculation to find a target's position in a region interleave. Perform a self-test of the calculation on user-defined regions.

The region driver uses the kernel sort() function to put region targets in relative order. Positions are assigned based on each target's index in that sorted list. That relative sort doesn't consider the offset of a port into its parent port which causes some auto-discovered regions to fail creation. In one failure case, a 2 + 2 config (2 host bridges each with 2 endpoints), the sort puts all the targets of one port ahead of another port when they were expected to be interleaved.

In preparation for repairing the autodiscovery region assembly, introduce a new method for discovering a target position in the region interleave.

cxl_calc_interleave_pos() adds a method to find the target position by ascending from an endpoint to a root decoder. The calculation starts with the endpoint's local position and position in the parent port. It traverses towards the root decoder and examines both position and ways in order to allow the position to be refined all the way to the root decoder.

This calculation: position = position * parent_ways + parent_pos; applied iteratively yields the correct position.

Include a self-test that exercises this new position calculation against every successfully configured user-defined region.

---
 drivers/cxl/core/region.c | 142 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
drivers/cxl/core/region.c +142 -0
index eea8e89a6860..bea43da7e8f2 100644
@@ -1501,6 +1501,128 @@ static int match_switch_decoder_by_range(struct device *dev, void *data)
 	return (r1->start == r2->start && r1->end == r2->end);
 }
 
+/**
+ * cxl_find_pos_and_ways() - find the position of a port in a parent
+ *			     and the parent interleave ways
+ *
+ * @port: the port in search of parent info
+ * @range: the hpa range that the parent must map
+ * @pos: the position of @port in the parent decoder target list
+ * @ways: the interleave ways of the parent decoder
+ *
+ * This helper function for cxl_calc_interleave_pos() provides the
+ * @pos and @ways used to calculate the endpoint position in a region
+ * interleave.
+ *
+ * Use @range to find @port's parent port and follow that to the
+ * parent switch decoder. Extract @ways from the switch decoder and
+ * lookup the @port @pos in the switch decoder target list.
+ *
+ * Returns:	0: success, @pos and @ways are updated
+ *	   -ENXIO: failed to find @pos or @ways
+ */
+static int find_pos_and_ways(struct cxl_port *port, struct range *range,
+			     int *pos, int *ways)
+{
+	struct cxl_switch_decoder *cxlsd;
+	struct cxl_port *parent;
+	struct device *dev;
+	int rc = -ENXIO;
+
+	parent = next_port(port);
+	if (!parent)
+		return rc;
+
+	dev = device_find_child(&parent->dev, range,
+				match_switch_decoder_by_range);
+	if (!dev) {
+		dev_err(port->uport_dev,
+			"failed to find decoder mapping %#llx-%#llx\n",
+			range->start, range->end);
+		return rc;
+	}
+	cxlsd = to_cxl_switch_decoder(dev);
+	*ways = cxlsd->cxld.interleave_ways;
+
+	for (int i = 0; i < *ways; i++) {
+		if (cxlsd->target[i] == port->parent_dport) {
+			*pos = i;
+			rc = 0;
+			break;
+		}
+	}
+	put_device(dev);
+
+	return rc;
+}
+
+/**
+ * cxl_calc_interleave_pos() - calculate an endpoint position in a region
+ * @cxled: the endpoint decoder
+ *
+ * The endpoint position is calculated by traversing from the endpoint to
+ * the root decoder and iteratively applying this calculation:
+ *	position = position * parent_ways + parent_pos;
+ *
+ * For example, the expected interleave order of the 4-way region shown
+ * below is: mem0, mem2, mem1, mem3
+ *
+ *		  root_port
+ *                 /      \
+ *      host_bridge_0    host_bridge_1
+ *        |    |           |    |
+ *       mem0 mem1        mem2 mem3
+ *
+ * In the example the calculator will iterate twice. The first iteration
+ * uses the mem position in the host-bridge and the ways of the host-
+ * bridge to generate the first, or local, position. The second iteration
+ * uses the host-bridge position in the root_port and the ways of the
+ * root_port to refine the position.
+ *
+ * A trace of the calculation per endpoint looks like this:
+ * mem0:	pos = 0 * 2 + 0		mem2:	pos = 0 * 2 + 0
+ *		pos = 0 * 2 + 0			pos = 0 * 2 + 1
+ *		pos: 0				pos: 1
+ *
+ * mem1:	pos = 0 * 2 + 1		mem3:	pos = 0 * 2 + 1
+ *		pos = 1 * 2 + 0			pos = 1 * 2 + 1
+ *		pos: 2				pos = 3
+ *
+ * Note that while this example is simple, the method applies to more
+ * complex topologies, including those with switches.
+ *
+ * Return: position >= 0 on success
+ *	   -ENXIO on failure
+ */
+
+static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
+{
+	struct cxl_port *iter, *port = cxled_to_port(cxled);
+	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+	struct range *range = &cxled->cxld.hpa_range;
+	int parent_ways = 0, parent_pos = 0, pos = 0;
+	int rc;
+
+	/* Iterate from endpoint to root_port refining the position */
+	for (iter = port; iter; iter = next_port(iter)) {
+		if (is_cxl_root(iter))
+			break;
+
+		rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways);
+		if (rc)
+			return rc;
+
+		pos = pos * parent_ways + parent_pos;
+	}
+
+	dev_dbg(&cxlmd->dev,
+		"decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
+		dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
+		dev_name(&port->dev), range->start, range->end, pos);
+
+	return pos;
+}
+
 static void find_positions(const struct cxl_switch_decoder *cxlsd,
 			   const struct cxl_port *iter_a,
 			   const struct cxl_port *iter_b, int *a_pos,
@@ -1764,6 +1886,26 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		.end = p->res->end,
 	};
 
+	if (p->nr_targets != p->interleave_ways)
+		return 0;
+
+	/*
+	 * Test the auto-discovery position calculator function
+	 * against this successfully created user-defined region.
+	 * A fail message here means that this interleave config
+	 * will fail when presented as CXL_REGION_F_AUTO.
+	 */
+	for (int i = 0; i < p->nr_targets; i++) {
+		struct cxl_endpoint_decoder *cxled = p->targets[i];
+		int test_pos;
+
+		test_pos = cxl_calc_interleave_pos(cxled);
+		dev_dbg(&cxled->cxld.dev,
+			"Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
+			(test_pos == cxled->pos) ? "success" : "fail",
+			test_pos, cxled->pos);
+	}
+
 	return 0;
 
 err_decrement:
Signature · 1 line
2.37.3
author maintainer
#5 lore raw

PATCH v3 3/3 cxl/region: Use cxl_calc_interleave_pos() for auto-discovery

In reply to [email protected]

From: Alison Schofield <[email protected]>

For auto-discovered regions the driver must assign each target to a valid position in the region interleave set based on the decoder topology.

The current implementation fails to parse valid decode topologies, as it does not consider the child offset into a parent port. The sort put all targets of one port ahead of another port when an interleave was expected, causing the region assembly to fail.

Replace the existing relative sort with cxl_calc_interleave_pos() that finds the exact position in a region interleave for an endpoint based on a walk up the ancestral tree from endpoint to root decoder.

cxl_calc_interleave_pos() was introduced in a prior patch, so the work here is to use it in cxl_region_sort_targets().

Remove the obsoleted helper functions from the prior sort.

Testing passes on pre-production hardware with BIOS defined regions that natively trigger this autodiscovery path of the region driver. Testing passes a CXL unit test using the dev_dbg() calculation test (see cxl_region_attach()) across an expanded set of region configs:
1, 1, 1+1, 1+1+1, 2, 2+2, 2+2+2, 2+2+2+2, 4, 4+4, where each number represents the count of endpoints per host bridge.

Fixes a32320b71f08 ("cxl/region: Add region autodiscovery")
---
 drivers/cxl/core/region.c | 127 +++++---------------------------------
 1 file changed, 15 insertions(+), 112 deletions(-)
drivers/cxl/core/region.c +15 -112
index bea43da7e8f2..55836fddb494 100644
@@ -1480,6 +1480,14 @@ static int cxl_region_attach_auto(struct cxl_region *cxlr,
 	return 0;
 }
 
+static int cmp_interleave_pos(const void *a, const void *b)
+{
+	struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
+	struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
+
+	return cxled_a->pos - cxled_b->pos;
+}
+
 static struct cxl_port *next_port(struct cxl_port *port)
 {
 	if (!port->parent_dport)
@@ -1623,131 +1631,26 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
 	return pos;
 }
 
-static void find_positions(const struct cxl_switch_decoder *cxlsd,
-			   const struct cxl_port *iter_a,
-			   const struct cxl_port *iter_b, int *a_pos,
-			   int *b_pos)
-{
-	int i;
-
-	for (i = 0, *a_pos = -1, *b_pos = -1; i < cxlsd->nr_targets; i++) {
-		if (cxlsd->target[i] == iter_a->parent_dport)
-			*a_pos = i;
-		else if (cxlsd->target[i] == iter_b->parent_dport)
-			*b_pos = i;
-		if (*a_pos >= 0 && *b_pos >= 0)
-			break;
-	}
-}
-
-static int cmp_decode_pos(const void *a, const void *b)
-{
-	struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
-	struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
-	struct cxl_memdev *cxlmd_a = cxled_to_memdev(cxled_a);
-	struct cxl_memdev *cxlmd_b = cxled_to_memdev(cxled_b);
-	struct cxl_port *port_a = cxled_to_port(cxled_a);
-	struct cxl_port *port_b = cxled_to_port(cxled_b);
-	struct cxl_port *iter_a, *iter_b, *port = NULL;
-	struct cxl_switch_decoder *cxlsd;
-	struct device *dev;
-	int a_pos, b_pos;
-	unsigned int seq;
-
-	/* Exit early if any prior sorting failed */
-	if (cxled_a->pos < 0 || cxled_b->pos < 0)
-		return 0;
-
-	/*
-	 * Walk up the hierarchy to find a shared port, find the decoder that
-	 * maps the range, compare the relative position of those dport
-	 * mappings.
-	 */
-	for (iter_a = port_a; iter_a; iter_a = next_port(iter_a)) {
-		struct cxl_port *next_a, *next_b;
-
-		next_a = next_port(iter_a);
-		if (!next_a)
-			break;
-
-		for (iter_b = port_b; iter_b; iter_b = next_port(iter_b)) {
-			next_b = next_port(iter_b);
-			if (next_a != next_b)
-				continue;
-			port = next_a;
-			break;
-		}
-
-		if (port)
-			break;
-	}
-
-	if (!port) {
-		dev_err(cxlmd_a->dev.parent,
-			"failed to find shared port with %s\n",
-			dev_name(cxlmd_b->dev.parent));
-		goto err;
-	}
-
-	dev = device_find_child(&port->dev, &cxled_a->cxld.hpa_range,
-				match_switch_decoder_by_range);
-	if (!dev) {
-		struct range *range = &cxled_a->cxld.hpa_range;
-
-		dev_err(port->uport_dev,
-			"failed to find decoder that maps %#llx-%#llx\n",
-			range->start, range->end);
-		goto err;
-	}
-
-	cxlsd = to_cxl_switch_decoder(dev);
-	do {
-		seq = read_seqbegin(&cxlsd->target_lock);
-		find_positions(cxlsd, iter_a, iter_b, &a_pos, &b_pos);
-	} while (read_seqretry(&cxlsd->target_lock, seq));
-
-	put_device(dev);
-
-	if (a_pos < 0 || b_pos < 0) {
-		dev_err(port->uport_dev,
-			"failed to find shared decoder for %s and %s\n",
-			dev_name(cxlmd_a->dev.parent),
-			dev_name(cxlmd_b->dev.parent));
-		goto err;
-	}
-
-	dev_dbg(port->uport_dev, "%s comes %s %s\n",
-		dev_name(cxlmd_a->dev.parent),
-		a_pos - b_pos < 0 ? "before" : "after",
-		dev_name(cxlmd_b->dev.parent));
-
-	return a_pos - b_pos;
-err:
-	cxled_a->pos = -1;
-	return 0;
-}
-
 static int cxl_region_sort_targets(struct cxl_region *cxlr)
 {
 	struct cxl_region_params *p = &cxlr->params;
 	int i, rc = 0;
 
-	sort(p->targets, p->nr_targets, sizeof(p->targets[0]), cmp_decode_pos,
-	     NULL);
-
 	for (i = 0; i < p->nr_targets; i++) {
 		struct cxl_endpoint_decoder *cxled = p->targets[i];
 
+		cxled->pos = cxl_calc_interleave_pos(cxled);
 		/*
-		 * Record that sorting failed, but still continue to restore
-		 * cxled->pos with its ->targets[] position so that follow-on
-		 * code paths can reliably do p->targets[cxled->pos] to
-		 * self-reference their entry.
+		 * Record that sorting failed, but still continue to calc
+		 * cxled->pos so that follow-on code paths can reliably
+		 * do p->targets[cxled->pos] to self-reference their entry.
 		 */
 		if (cxled->pos < 0)
 			rc = -ENXIO;
-		cxled->pos = i;
 	}
+	/* Keep the cxlr target list in interleave position order */
+	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
+	     cmp_interleave_pos, NULL);
 
 	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
 	return rc;
Signature · 1 line
2.37.3
#3 lore raw

RE: [PATCH v3 2/3] cxl/region: Calculate a target position in a region interleave

In reply to [email protected]
Quoted 42 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

Introduce a calculation to find a target's position in a region interleave. Perform a self-test of the calculation on user-defined regions.

The region driver uses the kernel sort() function to put region targets in relative order. Positions are assigned based on each target's index in that sorted list. That relative sort doesn't consider the offset of a port into its parent port which causes some auto-discovered regions to fail creation. In one failure case, a 2 + 2 config (2 host bridges each with 2 endpoints), the sort puts all the targets of one port ahead of another port when they were expected to be interleaved.

In preparation for repairing the autodiscovery region assembly, introduce a new method for discovering a target position in the region interleave.

cxl_calc_interleave_pos() adds a method to find the target position by ascending from an endpoint to a root decoder. The calculation starts with the endpoint's local position and position in the parent port. It traverses towards the root decoder and examines both position and ways in order to allow the position to be refined all the way to the root decoder.

This calculation: position = position * parent_ways + parent_pos; applied iteratively yields the correct position.

Include a self-test that exercises this new position calculation against every successfully configured user-defined region.

---
 drivers/cxl/core/region.c | 142 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 142 insertions(+)
drivers/cxl/core/region.c +0 -0
index eea8e89a6860..bea43da7e8f2 100644

drivers/cxl/core/region.c static int match_switch_decoder_by_range(struct device *dev, void *data) · line 1506

Quoted 6 lines
drivers/cxl/core/region.c +3 -0
@@ -1501,6 +1501,128 @@ static int match_switch_decoder_by_range(struct device *dev, void *data)
 	return (r1->start == r2->start && r1->end == r2->end);
 }
 
+/**
+ * cxl_find_pos_and_ways() - find the position of a port in a parent
+ *			     and the parent interleave ways

Given that this function is local to this file I do not think the cxl_ prefix is needed, and it does not match the below which is still find_pos_and_ways().

drivers/cxl/core/region.c static int match_switch_decoder_by_range(struct device *dev, void *data) · line 1519

Quoted 13 lines
drivers/cxl/core/region.c +13 -0
+ *
+ * @port: the port in search of parent info
+ * @range: the hpa range that the parent must map
+ * @pos: the position of @port in the parent decoder target list
+ * @ways: the interleave ways of the parent decoder
+ *
+ * This helper function for cxl_calc_interleave_pos() provides the
+ * @pos and @ways used to calculate the endpoint position in a region
+ * interleave.
+ *
+ * Use @range to find @port's parent port and follow that to the
+ * parent switch decoder. Extract @ways from the switch decoder and
+ * lookup the @port @pos in the switch decoder target list.

Hmm, per your concern in the cover letter, yes, this find_pos_and_ways()() commentary is not adding much for describing the theory of operation. Lets just go with the calc_interleave_pos() comment block which is answering all my concerns about documenting what is going on here.

Quoted 63 lines
drivers/cxl/core/region.c +63 -0
+ *
+ * Returns:	0: success, @pos and @ways are updated
+ *	   -ENXIO: failed to find @pos or @ways
+ */
+static int find_pos_and_ways(struct cxl_port *port, struct range *range,
+			     int *pos, int *ways)
+{
+	struct cxl_switch_decoder *cxlsd;
+	struct cxl_port *parent;
+	struct device *dev;
+	int rc = -ENXIO;
+
+	parent = next_port(port);
+	if (!parent)
+		return rc;
+
+	dev = device_find_child(&parent->dev, range,
+				match_switch_decoder_by_range);
+	if (!dev) {
+		dev_err(port->uport_dev,
+			"failed to find decoder mapping %#llx-%#llx\n",
+			range->start, range->end);
+		return rc;
+	}
+	cxlsd = to_cxl_switch_decoder(dev);
+	*ways = cxlsd->cxld.interleave_ways;
+
+	for (int i = 0; i < *ways; i++) {
+		if (cxlsd->target[i] == port->parent_dport) {
+			*pos = i;
+			rc = 0;
+			break;
+		}
+	}
+	put_device(dev);
+
+	return rc;
+}
+
+/**
+ * cxl_calc_interleave_pos() - calculate an endpoint position in a region
+ * @cxled: the endpoint decoder
+ *
+ * The endpoint position is calculated by traversing from the endpoint to
+ * the root decoder and iteratively applying this calculation:
+ *	position = position * parent_ways + parent_pos;
+ *
+ * For example, the expected interleave order of the 4-way region shown
+ * below is: mem0, mem2, mem1, mem3
+ *
+ *		  root_port
+ *                 /      \
+ *      host_bridge_0    host_bridge_1
+ *        |    |           |    |
+ *       mem0 mem1        mem2 mem3
+ *
+ * In the example the calculator will iterate twice. The first iteration
+ * uses the mem position in the host-bridge and the ways of the host-
+ * bridge to generate the first, or local, position. The second iteration
+ * uses the host-bridge position in the root_port and the ways of the
+ * root_port to refine the position.
+ *
+ * A trace of the calculation per endpoint looks like this:

drivers/cxl/core/region.c static int match_switch_decoder_by_range(struct device *dev, void *data) · line 1592

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+ * mem0:	pos = 0 * 2 + 0		mem2:	pos = 0 * 2 + 0
+ *		pos = 0 * 2 + 0			pos = 0 * 2 + 1
+ *		pos: 0				pos: 1
+ *
+ * mem1:	pos = 0 * 2 + 1		mem3:	pos = 0 * 2 + 1
+ *		pos = 1 * 2 + 0			pos = 1 * 2 + 1
+ *		pos: 2				pos = 3
+ *
+ * Note that while this example is simple, the method applies to more
+ * complex topologies, including those with switches.

Looks good.

Just delete the find_pos_and_ways() kdoc and we can call this one good to go.

#6 lore raw

RE: [PATCH v3 3/3] cxl/region: Use cxl_calc_interleave_pos() for auto-discovery

In reply to [email protected]
Quoted 27 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

For auto-discovered regions the driver must assign each target to a valid position in the region interleave set based on the decoder topology.

The current implementation fails to parse valid decode topologies, as it does not consider the child offset into a parent port. The sort put all targets of one port ahead of another port when an interleave was expected, causing the region assembly to fail.

Replace the existing relative sort with cxl_calc_interleave_pos() that finds the exact position in a region interleave for an endpoint based on a walk up the ancestral tree from endpoint to root decoder.

cxl_calc_interleave_pos() was introduced in a prior patch, so the work here is to use it in cxl_region_sort_targets().

Remove the obsoleted helper functions from the prior sort.

Testing passes on pre-production hardware with BIOS defined regions that natively trigger this autodiscovery path of the region driver. Testing passes a CXL unit test using the dev_dbg() calculation test (see cxl_region_attach()) across an expanded set of region configs:
1, 1, 1+1, 1+1+1, 2, 2+2, 2+2+2, 2+2+2+2, 4, 4+4, where each number represents the count of endpoints per host bridge.

Quoted 5 lines
Fixes a32320b71f08 ("cxl/region: Add region autodiscovery")

Looks good.

#4 lore raw

Re: [PATCH v3 2/3] cxl/region: Calculate a target position in a region interleave

In reply to [email protected]
kernel test robot noticed the following build warnings:

Hi,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 8a749fd1a8720d4619c91c8b6e7528c0a355c0aa]

url:    https://github.com/intel-lab-lkp/linux/commits/alison-schofield-intel-com/cxl-region-Prepare-the-decoder-match-range-helper-for-reuse/20231026-013232
base:   8a749fd1a8720d4619c91c8b6e7528c0a355c0aa
patch link:    https://lore.kernel.org/r/7709896423244c523e810d9f5d2ef625e7babf3b.1698254338.git.alison.schofield%40intel.com
patch subject: [PATCH v3 2/3] cxl/region: Calculate a target position in a region interleave
config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20231026/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231026/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/cxl/core/region.c:1526: warning: expecting prototype for cxl_find_pos_and_ways(). Prototype was for find_pos_and_ways() instead

vim +1526 drivers/cxl/core/region.c

1503
1504 /**
1505 * cxl_find_pos_and_ways() - find the position of a port in a parent
1506 * and the parent interleave ways
1507 *
1508 * @port: the port in search of parent info
1509 * @range: the hpa range that the parent must map
1510 * @pos: the position of @port in the parent decoder target list
1511 * @ways: the interleave ways of the parent decoder
1512 *
1513 * This helper function for cxl_calc_interleave_pos() provides the
1514 * @pos and @ways used to calculate the endpoint position in a region
1515 * interleave.
1516 *
1517 * Use @range to find @port's parent port and follow that to the
1518 * parent switch decoder. Extract @ways from the switch decoder and
1519 * lookup the @port @pos in the switch decoder target list.
1520 *
1521 * Returns: 0: success, @pos and @ways are updated
1522 * -ENXIO: failed to find @pos or @ways
1523 */
1524 static int find_pos_and_ways(struct cxl_port *port, struct range *range,
1525 int *pos, int *ways)

> 1526	{

1527 struct cxl_switch_decoder *cxlsd;
1528 struct cxl_port *parent;
1529 struct device *dev;
1530 int rc = -ENXIO;
1531
1532 parent = next_port(port);
1533 if (!parent)
1534 return rc;
1535
1536 dev = device_find_child(&parent->dev, range,
1537 match_switch_decoder_by_range);
1538 if (!dev) {
1539 dev_err(port->uport_dev,
1540 "failed to find decoder mapping %#llx-%#llx\n",
1541 range->start, range->end);
1542 return rc;
1543 }
1544 cxlsd = to_cxl_switch_decoder(dev);
1545 *ways = cxlsd->cxld.interleave_ways;
1546
1547 for (int i = 0; i < *ways; i++) {
1548 if (cxlsd->target[i] == port->parent_dport) {
1549 *pos = i;
1550 rc = 0;
1551 break;
1552 }
1553 }
1554 put_device(dev);
1555
1556 return rc;
1557 }
1558

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki