Skip to contentSkip to conversation

cxl: Support mixed-granularity region interleaves

PATCH Superseded Alison Schofield 5 participants 25 messages Jun 11 – Aug 21

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alison Schofield

author maintainer
#0 lore raw

v2 cover cxl: Support mixed-granularity region interleaves

Changes in v2:
- Patch 1,2: Defer the unused selector var store to keep P1 bisectable (Sashiko)
- Patch 1: Make divide by 3 in get_selctor() work on 32-bit builds) (lkp)
- Patch 4: Use local vars in cxl_region_attach() for readability (DaveJ)
- Patch 5: Add NULL checks on unused mock arrays (Sashiko)
- Resolved errant err_rch unwind with rc7 merge (DaveJ)
- Rebase onto 7.1-rc7
- Update commit logs in 1,2,5 to align w changes in v2
Link to v1:
https://lore.kernel.org/all/[email protected]/

Begin Cover Letter:

A CXL region interleaves across decoder levels (root, optional switches, endpoint). CXL Spec 4.0 Section 9.13.1 requires only that each level use a different, consecutive range of HPA bits to select its target. The driver has historically required equal region and root decoder granularities. That blocks the legal mixed-granularity arrangements permitted by Section 9.13.1, and makes the 6-way and 12-way configurations defined in Section 9.13.1.1 (Tables 9-6, 9-7, and 9-8) impossible to create.

Two prior proposals addressed parts of this gap:

AlisonS added position arithmetic and sysfs gating to allow auto and user-created regions for the 6-way and 12-way configurations that have no same-granularity alternative:
https://lore.kernel.org/all/[email protected]/

RobertR introduced an HPA selector-bit model to allow multi-level regions regardless of granularity ordering for auto regions:
https://lore.kernel.org/all/[email protected]/

This series combines those two approaches into a complete mixed- granularity implementation. It extends Robert's selector-bit model from auto regions to user-created regions, extends AlisonS's position-arithmetic and gating work to all mixed-gran layouts, and adds the remaining validation needed for both paths. The result is support for every mixed-granularity arrangement defined by the CXL specification.

Series structure
----------------
Patches 1 and 2 replace the old granularity ordering rule with selector-bit validation. Patches 3 and 4 propagate that model through the remaining region-creation paths. Patch 5 adds cxl_test coverage for the new layouts. Patch 6 documents the region granularity model and multi-level interleaving rules.

A companion NDCTL patchset that allows mixed-gran 'cxl create-region' and adds the unit test is posted here:
https://lore.kernel.org/all/fa5c109f08824180f58341ebd9055545a2ff3142.1780099216.git.alison.schofield@intel.com/

Alison Schofield (6):
cxl/region: Validate interleave selector bits
cxl/region: Derive port granularity from selector bits cxl/region: Account for mixed-granularity in position calculations cxl/region: Validate mixed-granularity at sysfs and attach gates cxl/test: Add a topology to test mixed-granularity regions Documentation/cxl: Add region granularity and multi-level interleave
guide

 Documentation/driver-api/cxl/index.rst        |   1 +
 .../cxl/linux/region-granularity.rst          | 486 +++++++++++++++++
 drivers/cxl/core/region.c                     | 312 +++++++----
 tools/testing/cxl/test/cxl.c                  | 503 ++++++++++++++++--
 4 files changed, 1159 insertions(+), 143 deletions(-)
 create mode 100644 Documentation/driver-api/cxl/linux/region-granularity.rst

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

Signature · 1 line
2.37.3
author maintainer
#1 lore raw

PATCH v2 1/6 cxl/region: Validate interleave selector bits

Multi-level interleaves are valid when each decoder level claims a distinct portion of the region HPA selector. The existing setup is too restrictive as it requires that each level's granularity must equal the parent granularity multiplied by the parent ways. That forces the root to sit at the innermost selector bit and rejects legal layouts where the root claims an outer bit.

Add selector-bit accounting to cxl_port_setup_targets(). Accumulate selector bits from the root toward the current port, reject overlap, and reject any accumulated selector that does not fit within the region selector.

Preserve the existing same-granularity behavior using values derived from the selector walk.

Block mixed-granularity attachments until position arithmetic is updated by a later patch.

---
 drivers/cxl/core/region.c | 192 +++++++++++++++++++-------------------
 1 file changed, 96 insertions(+), 96 deletions(-)
drivers/cxl/core/region.c +96 -96
index e50dc716d4e8..c859c8661281 100644
@@ -1423,12 +1423,34 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	return 0;
 }
 
+/**
+ * get_selector() - Return the HPA selector mask for one level
+ * @ways: interleave ways
+ * @gran: interleave granularity in bytes
+ *
+ * Return: ``(ways - 1) * gran`` for power-of-2 interleaves, or the
+ * selector for the power-of-2 component of a 6-way or 12-way interleave.
+ *
+ * A 3-way interleave consumes no HPA selector bits.
+ */
+static inline u64 get_selector(int ways, int gran)
+{
+	if (!is_power_of_2(ways))
+		ways /= 3;
+
+	if (!is_power_of_2(ways) || !is_power_of_2(gran))
+		return 0;
+
+	return (u64)(ways - 1) * gran;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	int parent_iw, parent_ig, ig, iw, rc, pos = cxled->pos;
+	int distance, parent_distance;
+	int ig, iw, rc, pos = cxled->pos;
 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
@@ -1436,9 +1458,10 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_decoder *cxld = cxl_rr->decoder;
 	struct cxl_switch_decoder *cxlsd;
-	struct cxl_port *iter = port;
-	u16 eig, peig;
-	u8 eiw, peiw;
+	u64 selector, cxlr_sel;
+	struct cxl_port *iter;
+	u16 eig;
+	u8 eiw;
 
 	/*
 	 * While root level decoders support x3, x6, x12, switch level
@@ -1452,28 +1475,66 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	}
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
+	iw = cxl_rr->nr_targets;
+
+	if (iw > 8 || iw > cxlsd->nr_targets) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s:%s: ways: %d overflows targets: %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
+		return -ENXIO;
+	}
+
+	/* Track selector bits already claimed by ancestor decoders */
+	selector = get_selector(cxlrd->cxlsd.cxld.interleave_ways,
+				cxlrd->cxlsd.cxld.interleave_granularity);
+
+	/* Track distances used in passthrough granularity and peer checks */
+	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(parent_distance))
+		parent_distance /= 3;
+	distance = cxlrd->cxlsd.cxld.interleave_ways;
+
+	for (iter = parent_port; !is_cxl_root(iter);
+	     iter = to_cxl_port(iter->dev.parent)) {
+		struct cxl_region_ref *cxl_rr_iter = cxl_rr_load(iter, cxlr);
+		struct cxl_decoder *cxld_iter = cxl_rr_iter->decoder;
+		u64 cxld_sel;
+
+		if (cxld_iter->interleave_ways == 1)
+			continue;
+
+		cxld_sel = get_selector(cxld_iter->interleave_ways,
+					cxld_iter->interleave_granularity);
+
+		if (cxld_sel & selector) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s: overlapping selectors: %#llx:%#llx\n",
+				dev_name(iter->uport_dev),
+				dev_name(&iter->dev), cxld_sel, selector);
+			return -ENXIO;
+		}
+
+		selector |= cxld_sel;
+
+		parent_distance *= cxl_rr_iter->nr_targets;
+		distance *= cxl_rr_iter->nr_targets;
+	}
+
+	distance *= iw;
+
+	/* Accumulated bits must fit within the region selector */
+	cxlr_sel = get_selector(p->interleave_ways, p->interleave_granularity);
+	if ((cxlr_sel & selector) != selector) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: invalid selectors: cxlr %#llx accum %#llx\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			cxlr_sel, selector);
+		return -ENXIO;
+	}
+
 	if (cxl_rr->nr_targets_set) {
-		int i, distance = 1;
-		struct cxl_region_ref *cxl_rr_iter;
-
-		/*
-		 * The "distance" between peer downstream ports represents which
-		 * endpoint positions in the region interleave a given port can
-		 * host.
-		 *
-		 * For example, at the root of a hierarchy the distance is
-		 * always 1 as every index targets a different host-bridge. At
-		 * each subsequent switch level those ports map every Nth region
-		 * position where N is the width of the switch == distance.
-		 */
-		do {
-			cxl_rr_iter = cxl_rr_load(iter, cxlr);
-			distance *= cxl_rr_iter->nr_targets;
-			iter = to_cxl_port(iter->dev.parent);
-		} while (!is_cxl_root(iter));
-		distance *= cxlrd->cxlsd.cxld.interleave_ways;
-
-		for (i = 0; i < cxl_rr->nr_targets_set; i++)
+		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
@@ -1484,84 +1545,18 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;
 	}
 
-	if (is_cxl_root(parent_port)) {
-		/*
-		 * Root decoder IG is always set to value in CFMWS which
-		 * may be different than this region's IG.  We can use the
-		 * region's IG here since interleave_granularity_store()
-		 * does not allow interleaved host-bridges with
-		 * root IG != region IG.
-		 */
-		parent_ig = p->interleave_granularity;
-		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
-		/*
-		 * For purposes of address bit routing, use power-of-2 math for
-		 * switch ports.
-		 */
-		if (!is_power_of_2(parent_iw))
-			parent_iw /= 3;
-	} else {
-		struct cxl_region_ref *parent_rr;
-		struct cxl_decoder *parent_cxld;
+	ig = p->interleave_granularity * parent_distance;
 
-		parent_rr = cxl_rr_load(parent_port, cxlr);
-		parent_cxld = parent_rr->decoder;
-		parent_ig = parent_cxld->interleave_granularity;
-		parent_iw = parent_cxld->interleave_ways;
-	}
-
-	rc = granularity_to_eig(parent_ig, &peig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_ig);
-		return rc;
-	}
-
-	rc = ways_to_eiw(parent_iw, &peiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_iw);
-		return rc;
-	}
-
-	iw = cxl_rr->nr_targets;
 	rc = ways_to_eiw(iw, &eiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev), iw);
-		return rc;
-	}
-
-	/*
-	 * Interleave granularity is a multiple of @parent_port granularity.
-	 * Multiplier is the parent port interleave ways.
-	 */
-	rc = granularity_to_eig(parent_ig * parent_iw, &eig);
+	if (!rc)
+		rc = granularity_to_eig(ig, &eig);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
-			"%s: invalid granularity calculation (%d * %d)\n",
-			dev_name(&parent_port->dev), parent_ig, parent_iw);
+			"%s:%s: derived ig %d not a valid granularity (iw %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), ig, iw);
 		return rc;
 	}
 
-	rc = eig_to_granularity(eig, &ig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			256 << eig);
-		return rc;
-	}
-
-	if (iw > 8 || iw > cxlsd->nr_targets) {
-		dev_dbg(&cxlr->dev,
-			"%s:%s:%s: ways: %d overflows targets: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
-		return -ENXIO;
-	}
-
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
 		    (iw > 1 && cxld->interleave_granularity != ig) ||
@@ -2067,6 +2062,11 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
+	/* Keep this patch bisectable until position arithmetic is updated */
+	if (cxlrd->cxlsd.cxld.interleave_granularity >
+	    p->interleave_granularity)
+		return -ENXIO;
+
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
Signature · 1 line
2.37.3
#2 lore raw

Re: [PATCH v2 1/6] cxl/region: Validate interleave selector bits

Quoted 11 lines · Alison Schofield

Multi-level interleaves are valid when each decoder level claims a distinct portion of the region HPA selector. The existing setup is too restrictive as it requires that each level's granularity must equal the parent granularity multiplied by the parent ways. That forces the root to sit at the innermost selector bit and rejects legal layouts where the root claims an outer bit.

Add selector-bit accounting to cxl_port_setup_targets(). Accumulate selector bits from the root toward the current port, reject overlap, and reject any accumulated selector that does not fit within the region selector.

I think this is the main goal of the patch, to introduce a selector-bit mask and use that to keep track of ways and granularities. We should remove as much as possible from the original code without introducing functional changes here. Subject and description should emphasize that more.

Quoted 54 lines

Preserve the existing same-granularity behavior using values derived from the selector walk.

Block mixed-granularity attachments until position arithmetic is updated by a later patch.

---
 drivers/cxl/core/region.c | 192 +++++++++++++++++++-------------------
 1 file changed, 96 insertions(+), 96 deletions(-)
drivers/cxl/core/region.c +23 -1
index e50dc716d4e8..c859c8661281 100644
@@ -1423,12 +1423,34 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	return 0;
 }
 
+/**
+ * get_selector() - Return the HPA selector mask for one level
+ * @ways: interleave ways
+ * @gran: interleave granularity in bytes
+ *
+ * Return: ``(ways - 1) * gran`` for power-of-2 interleaves, or the
+ * selector for the power-of-2 component of a 6-way or 12-way interleave.
+ *
+ * A 3-way interleave consumes no HPA selector bits.
+ */
+static inline u64 get_selector(int ways, int gran)
+{
+	if (!is_power_of_2(ways))
+		ways /= 3;
+
+	if (!is_power_of_2(ways) || !is_power_of_2(gran))
+		return 0;
+
+	return (u64)(ways - 1) * gran;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	int parent_iw, parent_ig, ig, iw, rc, pos = cxled->pos;
+	int distance, parent_distance;
+	int ig, iw, rc, pos = cxled->pos;
 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1464

Quoted 10 lines
drivers/cxl/core/region.c +4 -3
@@ -1436,9 +1458,10 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_decoder *cxld = cxl_rr->decoder;
 	struct cxl_switch_decoder *cxlsd;
-	struct cxl_port *iter = port;
-	u16 eig, peig;
-	u8 eiw, peiw;
+	u64 selector, cxlr_sel;
+	struct cxl_port *iter;
+	u16 eig;
+	u8 eiw;

That list of local vars is very long and we should factor out some of the code to helpers.

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1477

Quoted 7 lines
drivers/cxl/core/region.c +0 -0
 	/*
 	 * While root level decoders support x3, x6, x12, switch level
@@ -1452,28 +1475,66 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	}
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);

That could be moved to the declaration section.

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1480

Quoted 3 lines
drivers/cxl/core/region.c +3 -0
+	iw = cxl_rr->nr_targets;
+
+	if (iw > 8 || iw > cxlsd->nr_targets) {

And this can be moved up / merged with the power of 2 check before.

Quoted 6 lines
drivers/cxl/core/region.c +6 -0
+		dev_dbg(&cxlr->dev,
+			"%s:%s:%s: ways: %d overflows targets: %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
+		return -ENXIO;
+	}

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1496

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+
+	/* Track selector bits already claimed by ancestor decoders */
+	selector = get_selector(cxlrd->cxlsd.cxld.interleave_ways,
+				cxlrd->cxlsd.cxld.interleave_granularity);
+
+	/* Track distances used in passthrough granularity and peer checks */
+	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(parent_distance))
+		parent_distance /= 3;
+	distance = cxlrd->cxlsd.cxld.interleave_ways;

Better use only one var here. One can derived from the other once needed.

Actually, we do not really need it at all, see below.

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1498

Quoted 2 lines
drivers/cxl/core/region.c +2 -0
+
+	for (iter = parent_port; !is_cxl_root(iter);

That could become a helper function get_parent_selectors() or so.

Quoted 12 lines
drivers/cxl/core/region.c +12 -0
+	     iter = to_cxl_port(iter->dev.parent)) {
+		struct cxl_region_ref *cxl_rr_iter = cxl_rr_load(iter, cxlr);
+		struct cxl_decoder *cxld_iter = cxl_rr_iter->decoder;
+		u64 cxld_sel;
+
+		if (cxld_iter->interleave_ways == 1)
+			continue;
+
+		cxld_sel = get_selector(cxld_iter->interleave_ways,
+					cxld_iter->interleave_granularity);
+
+		if (cxld_sel & selector) {

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1520

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+			dev_dbg(&cxlr->dev,
+				"%s:%s: overlapping selectors: %#llx:%#llx\n",
+				dev_name(iter->uport_dev),
+				dev_name(&iter->dev), cxld_sel, selector);
+			return -ENXIO;
+		}
+
+		selector |= cxld_sel;
+
+		parent_distance *= cxl_rr_iter->nr_targets;

The parent_distance could be derived from the parent selector and moved out of the loop to simplify factoring out this into a helper.

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1524

Quoted 4 lines
drivers/cxl/core/region.c +4 -0
+		distance *= cxl_rr_iter->nr_targets;
+	}
+
+	distance *= iw;

The decoder's selectors are the next lower bits below the parent selectors, so iw and ig could be directly calculated from that. I think that very much simplifies the code.

There are those 3 selectors we just need to keep track of: switch selector, region selector and parent selector. With that everything else can be determined, e.g. iw and ig.

If we create helper functions to get all 3 selectors, this function will be much smaller. That also moves away error checks to that helpers.

Quoted 97 lines
drivers/cxl/core/region.c +13 -74
+
+	/* Accumulated bits must fit within the region selector */
+	cxlr_sel = get_selector(p->interleave_ways, p->interleave_granularity);
+	if ((cxlr_sel & selector) != selector) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: invalid selectors: cxlr %#llx accum %#llx\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			cxlr_sel, selector);
+		return -ENXIO;
+	}
+
 	if (cxl_rr->nr_targets_set) {
-		int i, distance = 1;
-		struct cxl_region_ref *cxl_rr_iter;
-
-		/*
-		 * The "distance" between peer downstream ports represents which
-		 * endpoint positions in the region interleave a given port can
-		 * host.
-		 *
-		 * For example, at the root of a hierarchy the distance is
-		 * always 1 as every index targets a different host-bridge. At
-		 * each subsequent switch level those ports map every Nth region
-		 * position where N is the width of the switch == distance.
-		 */
-		do {
-			cxl_rr_iter = cxl_rr_load(iter, cxlr);
-			distance *= cxl_rr_iter->nr_targets;
-			iter = to_cxl_port(iter->dev.parent);
-		} while (!is_cxl_root(iter));
-		distance *= cxlrd->cxlsd.cxld.interleave_ways;
-
-		for (i = 0; i < cxl_rr->nr_targets_set; i++)
+		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
@@ -1484,84 +1545,18 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;
 	}
 
-	if (is_cxl_root(parent_port)) {
-		/*
-		 * Root decoder IG is always set to value in CFMWS which
-		 * may be different than this region's IG.  We can use the
-		 * region's IG here since interleave_granularity_store()
-		 * does not allow interleaved host-bridges with
-		 * root IG != region IG.
-		 */
-		parent_ig = p->interleave_granularity;
-		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
-		/*
-		 * For purposes of address bit routing, use power-of-2 math for
-		 * switch ports.
-		 */
-		if (!is_power_of_2(parent_iw))
-			parent_iw /= 3;
-	} else {
-		struct cxl_region_ref *parent_rr;
-		struct cxl_decoder *parent_cxld;
+	ig = p->interleave_granularity * parent_distance;
 
-		parent_rr = cxl_rr_load(parent_port, cxlr);
-		parent_cxld = parent_rr->decoder;
-		parent_ig = parent_cxld->interleave_granularity;
-		parent_iw = parent_cxld->interleave_ways;
-	}
-
-	rc = granularity_to_eig(parent_ig, &peig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_ig);
-		return rc;
-	}
-
-	rc = ways_to_eiw(parent_iw, &peiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_iw);
-		return rc;
-	}
-
-	iw = cxl_rr->nr_targets;
 	rc = ways_to_eiw(iw, &eiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev), iw);
-		return rc;
-	}
-
-	/*
-	 * Interleave granularity is a multiple of @parent_port granularity.
-	 * Multiplier is the parent port interleave ways.
-	 */
-	rc = granularity_to_eig(parent_ig * parent_iw, &eig);

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1558

Quoted 10 lines
drivers/cxl/core/region.c +4 -2
+	if (!rc)
+		rc = granularity_to_eig(ig, &eig);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
-			"%s: invalid granularity calculation (%d * %d)\n",
-			dev_name(&parent_port->dev), parent_ig, parent_iw);
+			"%s:%s: derived ig %d not a valid granularity (iw %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), ig, iw);
 		return rc;
 	}

That is just a value check which could be reworked to check the selector instead. Could be a helper function.

We should consider adding the selector to struct of cxld and only check it once it is finally converted to ig and iw after setup is done.

Quoted 21 lines
drivers/cxl/core/region.c +0 -16
-	rc = eig_to_granularity(eig, &ig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			256 << eig);
-		return rc;
-	}
-
-	if (iw > 8 || iw > cxlsd->nr_targets) {
-		dev_dbg(&cxlr->dev,
-			"%s:%s:%s: ways: %d overflows targets: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
-		return -ENXIO;
-	}
-
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
 		    (iw > 1 && cxld->interleave_granularity != ig) ||

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2069

Quoted 8 lines
drivers/cxl/core/region.c +5 -0
@@ -2067,6 +2062,11 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
+	/* Keep this patch bisectable until position arithmetic is updated */
+	if (cxlrd->cxlsd.cxld.interleave_granularity >
+	    p->interleave_granularity)
+		return -ENXIO;
+

Hmm, that looks odd, even if it's only temporary. Could you explain why that is needed here.

Thanks,

-Robert

Quoted 6 lines
drivers/cxl/core/region.c +0 -0
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
Signature · 1 line
2.37.3
#3 lore raw

Re: [PATCH v2 1/6] cxl/region: Validate interleave selector bits

Quoted 6 lines · Robert Richter
Quoted 4 lines · Alison Schofield
drivers/cxl/core/region.c +0 -0
@@ -2067,6 +2062,11 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
Quoted 8 lines

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2069

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+	/* Keep this patch bisectable until position arithmetic is updated */
+	if (cxlrd->cxlsd.cxld.interleave_granularity >
+	    p->interleave_granularity)
+		return -ENXIO;
+

Hmm, that looks odd, even if it's only temporary. Could you explain why that is needed here.

Ok, I missed the point that the iterator stops at !is_cxl_root(iter) and thus does not consider the cfmws interleaving configs of the root decoders here, which are not part of the parent selector bit mask then.

That creates a 4th selector bit mask for the root decoders to keep track of. With that added I think all the stride handling in patch #3 to calculate the endpoint positions can be simplified.

-Robert

author maintainer
#4 lore raw

Re: [PATCH v2 1/6] cxl/region: Validate interleave selector bits

Quoted 16 lines · Robert Richter
Quoted 6 lines · Robert Richter
Quoted 4 lines · Alison Schofield
drivers/cxl/core/region.c +0 -0
@@ -2067,6 +2062,11 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
Quoted 8 lines

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2069

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+	/* Keep this patch bisectable until position arithmetic is updated */
+	if (cxlrd->cxlsd.cxld.interleave_granularity >
+	    p->interleave_granularity)
+		return -ENXIO;
+

Hmm, that looks odd, even if it's only temporary. Could you explain why that is needed here.

Quoted 8 lines

Ok, I missed the point that the iterator stops at !is_cxl_root(iter) and thus does not consider the cfmws interleaving configs of the root decoders here, which are not part of the parent selector bit mask then.

That creates a 4th selector bit mask for the root decoders to keep track of. With that added I think all the stride handling in patch #3 to calculate the endpoint positions can be simplified.

You're right that the root decoder's configuration has to be part of the model, and v3 adds the root decoder's selectors to the validation accumulation as you describe.

On the second sentence, whether that simplifies the stride handling in 3/6, I did the comparison, and the stride turns out to carry the same information as the root decoder's selectors. See reply on 3/6

-- Alison

Quoted 2 lines

-Robert

author maintainer
#5 lore raw

Re: [PATCH v2 1/6] cxl/region: Validate interleave selector bits

In reply to Robert Richter
Quoted 8 lines · Robert Richter
Quoted 7 lines · Alison Schofield

Multi-level interleaves are valid when each decoder level claims a distinct portion of the region HPA selector. The existing setup is too restrictive as it requires that each level's granularity must equal the parent granularity multiplied by the parent ways. That forces the root to sit at the innermost selector bit and rejects legal layouts where the root claims an outer bit.

Quoted 4 lines
Quoted 4 lines

Add selector-bit accounting to cxl_port_setup_targets(). Accumulate selector bits from the root toward the current port, reject overlap, and reject any accumulated selector that does not fit within the region selector.

Hi Robert,

The question underneath several of your comments, whether the implementation should move to selector-bit arithmetic, is answered in my reply on Patch 3/6.

Below, the local comments on this patch.

Quoted 6 lines

I think this is the main goal of the patch, to introduce a selector-bit mask and use that to keep track of ways and granularities. We should remove as much as possible from the original code without introducing functional changes here. Subject and description should emphasize that more.

Agreed. In v3 (I hope that) this patch becomes a no-functional-change refactor and the log say so explicitly.

Quoted 60 lines
Quoted 60 lines

Preserve the existing same-granularity behavior using values derived from the selector walk.

Block mixed-granularity attachments until position arithmetic is updated by a later patch.

---
 drivers/cxl/core/region.c | 192 +++++++++++++++++++-------------------
 1 file changed, 96 insertions(+), 96 deletions(-)
drivers/cxl/core/region.c +23 -3
index e50dc716d4e8..c859c8661281 100644
@@ -1423,12 +1423,34 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	return 0;
 }
 
+/**
+ * get_selector() - Return the HPA selector mask for one level
+ * @ways: interleave ways
+ * @gran: interleave granularity in bytes
+ *
+ * Return: ``(ways - 1) * gran`` for power-of-2 interleaves, or the
+ * selector for the power-of-2 component of a 6-way or 12-way interleave.
+ *
+ * A 3-way interleave consumes no HPA selector bits.
+ */
+static inline u64 get_selector(int ways, int gran)
+{
+	if (!is_power_of_2(ways))
+		ways /= 3;
+
+	if (!is_power_of_2(ways) || !is_power_of_2(gran))
+		return 0;
+
+	return (u64)(ways - 1) * gran;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	int parent_iw, parent_ig, ig, iw, rc, pos = cxled->pos;
+	int distance, parent_distance;
+	int ig, iw, rc, pos = cxled->pos;
 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
@@ -1436,9 +1458,10 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	struct cxl_region_params *p = &cxlr->params;
 	struct cxl_decoder *cxld = cxl_rr->decoder;
 	struct cxl_switch_decoder *cxlsd;
-	struct cxl_port *iter = port;
-	u16 eig, peig;
Quoted 9 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1464

Quoted 5 lines
drivers/cxl/core/region.c +4 -1
-	u8 eiw, peiw;
+	u64 selector, cxlr_sel;
+	struct cxl_port *iter;
+	u16 eig;
+	u8 eiw;

That list of local vars is very long and we should factor out some of the code to helpers.

Agreed. v3 factors the ancestor walk into a helper and moves the per-level selector computation with it. That takes most of the locals with it.

Quoted 9 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1477

Quoted 7 lines
drivers/cxl/core/region.c +0 -0
 	/*
 	 * While root level decoders support x3, x6, x12, switch level
@@ -1452,28 +1475,66 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	}
 
 	cxlsd = to_cxl_switch_decoder(&cxld->dev);

That could be moved to the declaration section.

Done in v3.

Quoted 7 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1480

Quoted 4 lines
drivers/cxl/core/region.c +3 -0
+	iw = cxl_rr->nr_targets;
+
+	if (iw > 8 || iw > cxlsd->nr_targets) {

And this can be moved up / merged with the power of 2 check before.

Done in v3.

Quoted 20 lines
Quoted 6 lines
drivers/cxl/core/region.c +6 -0
+		dev_dbg(&cxlr->dev,
+			"%s:%s:%s: ways: %d overflows targets: %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
+		return -ENXIO;
+	}

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1496

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+
+	/* Track selector bits already claimed by ancestor decoders */
+	selector = get_selector(cxlrd->cxlsd.cxld.interleave_ways,
+				cxlrd->cxlsd.cxld.interleave_granularity);
+
+	/* Track distances used in passthrough granularity and peer checks */
+	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(parent_distance))
+		parent_distance /= 3;
+	distance = cxlrd->cxlsd.cxld.interleave_ways;

Better use only one var here. One can derived from the other once needed.

Quoted 2 lines

Actually, we do not really need it at all, see below.

v3 keeps a single variable and derives the other form at its one point of use. On whether we need it at all, deriving distance purely from the selector bits works for the power-of-2 part but the selectors cannot carry the factor 3 of a 3/6/12-way root, so a value-space term survives. See Patch 3/6 reply.

Quoted 5 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1498

Quoted 2 lines
drivers/cxl/core/region.c +2 -0
+
+	for (iter = parent_port; !is_cxl_root(iter);

That could become a helper function get_parent_selectors() or so.

Agreed, that's the helper mentioned above.

Quoted 17 lines
Quoted 17 lines
drivers/cxl/core/region.c +17 -0
+	     iter = to_cxl_port(iter->dev.parent)) {
+		struct cxl_region_ref *cxl_rr_iter = cxl_rr_load(iter, cxlr);
+		struct cxl_decoder *cxld_iter = cxl_rr_iter->decoder;
+		u64 cxld_sel;
+
+		if (cxld_iter->interleave_ways == 1)
+			continue;
+
+		cxld_sel = get_selector(cxld_iter->interleave_ways,
+					cxld_iter->interleave_granularity);
+
+		if (cxld_sel & selector) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s: overlapping selectors: %#llx:%#llx\n",
+				dev_name(iter->uport_dev),
+				dev_name(&iter->dev), cxld_sel, selector);
+			return -ENXIO;
Quoted 9 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1520

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+		}
+
+		selector |= cxld_sel;
+
+		parent_distance *= cxl_rr_iter->nr_targets;

The parent_distance could be derived from the parent selector and moved out of the loop to simplify factoring out this into a helper.

For power-of-2 levels, yes, and v3 does that as part of the helper split. The x3 caveat above applies here too.

Quoted 9 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1524

Quoted 4 lines
drivers/cxl/core/region.c +4 -0
+		distance *= cxl_rr_iter->nr_targets;
+	}
+
+	distance *= iw;

The decoder's selectors are the next lower bits below the parent selectors, so iw and ig could be directly calculated from that. I think that very much simplifies the code.

Quoted 7 lines

There are those 3 selectors we just need to keep track of: switch selector, region selector and parent selector. With that everything else can be determined, e.g. iw and ig.

If we create helper functions to get all 3 selectors, this function will be much smaller. That also moves away error checks to that helpers.

This is the core of the selector-model question answered in Path 3/6 reply.

Short version: v3 takes the helper structure and adds the root decoder's selectors to the validation walk (your follow-up), but keeps iw/ig/position derivation in ways/gran value space, because the selector bits cannot represent the factor 3.

Quoted 112 lines
Quoted 98 lines
drivers/cxl/core/region.c +13 -74
+
+	/* Accumulated bits must fit within the region selector */
+	cxlr_sel = get_selector(p->interleave_ways, p->interleave_granularity);
+	if ((cxlr_sel & selector) != selector) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: invalid selectors: cxlr %#llx accum %#llx\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			cxlr_sel, selector);
+		return -ENXIO;
+	}
+
 	if (cxl_rr->nr_targets_set) {
-		int i, distance = 1;
-		struct cxl_region_ref *cxl_rr_iter;
-
-		/*
-		 * The "distance" between peer downstream ports represents which
-		 * endpoint positions in the region interleave a given port can
-		 * host.
-		 *
-		 * For example, at the root of a hierarchy the distance is
-		 * always 1 as every index targets a different host-bridge. At
-		 * each subsequent switch level those ports map every Nth region
-		 * position where N is the width of the switch == distance.
-		 */
-		do {
-			cxl_rr_iter = cxl_rr_load(iter, cxlr);
-			distance *= cxl_rr_iter->nr_targets;
-			iter = to_cxl_port(iter->dev.parent);
-		} while (!is_cxl_root(iter));
-		distance *= cxlrd->cxlsd.cxld.interleave_ways;
-
-		for (i = 0; i < cxl_rr->nr_targets_set; i++)
+		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
 						     distance);
@@ -1484,84 +1545,18 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;
 	}
 
-	if (is_cxl_root(parent_port)) {
-		/*
-		 * Root decoder IG is always set to value in CFMWS which
-		 * may be different than this region's IG.  We can use the
-		 * region's IG here since interleave_granularity_store()
-		 * does not allow interleaved host-bridges with
-		 * root IG != region IG.
-		 */
-		parent_ig = p->interleave_granularity;
-		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
-		/*
-		 * For purposes of address bit routing, use power-of-2 math for
-		 * switch ports.
-		 */
-		if (!is_power_of_2(parent_iw))
-			parent_iw /= 3;
-	} else {
-		struct cxl_region_ref *parent_rr;
-		struct cxl_decoder *parent_cxld;
+	ig = p->interleave_granularity * parent_distance;
 
-		parent_rr = cxl_rr_load(parent_port, cxlr);
-		parent_cxld = parent_rr->decoder;
-		parent_ig = parent_cxld->interleave_granularity;
-		parent_iw = parent_cxld->interleave_ways;
-	}
-
-	rc = granularity_to_eig(parent_ig, &peig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_ig);
-		return rc;
-	}
-
-	rc = ways_to_eiw(parent_iw, &peiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
-			dev_name(parent_port->uport_dev),
-			dev_name(&parent_port->dev), parent_iw);
-		return rc;
-	}
-
-	iw = cxl_rr->nr_targets;
 	rc = ways_to_eiw(iw, &eiw);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev), iw);
-		return rc;
-	}
-
-	/*
-	 * Interleave granularity is a multiple of @parent_port granularity.
-	 * Multiplier is the parent port interleave ways.
-	 */
-	rc = granularity_to_eig(parent_ig * parent_iw, &eig);

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1558

Quoted 10 lines
drivers/cxl/core/region.c +4 -2
+	if (!rc)
+		rc = granularity_to_eig(ig, &eig);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
-			"%s: invalid granularity calculation (%d * %d)\n",
-			dev_name(&parent_port->dev), parent_ig, parent_iw);
+			"%s:%s: derived ig %d not a valid granularity (iw %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), ig, iw);
 		return rc;
 	}

That is just a value check which could be reworked to check the selector instead. Could be a helper function.

Quoted 3 lines

We should consider adding the selector to struct of cxld and only check it once it is finally converted to ig and iw after setup is done.

The check moves into a helper in v3.

About a stored selector in struct cxl_decoder, I don't think it fits the model. When a consumer show up w need, then consider it.

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/region.c +0 -16
-	rc = eig_to_granularity(eig, &ig);
-	if (rc) {
-		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			256 << eig);
-		return rc;
-	}
-
-	if (iw > 8 || iw > cxlsd->nr_targets) {
-		dev_dbg(&cxlr->dev,
-			"%s:%s:%s: ways: %d overflows targets: %d\n",
-			dev_name(port->uport_dev), dev_name(&port->dev),
-			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
-		return -ENXIO;
-	}
-
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
 		    (iw > 1 && cxld->interleave_granularity != ig) ||
@@ -2067,6 +2062,11 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
Quoted 8 lines

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2069

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+	/* Keep this patch bisectable until position arithmetic is updated */
+	if (cxlrd->cxlsd.cxld.interleave_granularity >
+	    p->interleave_granularity)
+		return -ENXIO;
+

Hmm, that looks odd, even if it's only temporary. Could you explain why that is needed here.

Your follow-up answered most of this. This patch makes the selector walk *pass* for mixed-granularity layouts that the old equality rule rejected, while position arithmetic doesn't handle them until 3/6. Without the temporary gate, a bisect landing between the two patches could assemble a mixed-gran region with wrong endpoint positions. The gate keeps every commit in the series correct on its own. It is removed in 3/6 when the position math catches up.

I think that's all on this patch.

-- Alison

Quoted 11 lines

Thanks,

-Robert

Quoted 6 lines
drivers/cxl/core/region.c +0 -0
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
Signature · 1 line
2.37.3
author maintainer
#6 lore raw

PATCH v2 2/6 cxl/region: Derive port granularity from selector bits

In reply to Alison Schofield

Selector-bit validation uses a single same-granularity formula for user-created regions. Mixed-granularity layouts need the programmed granularity to depend on the type of decoder being configured.

Derive port granularity from the selector state:

AUTO: keep the firmware-programmed value; the walk validated it.
iw == 1: passthrough; consume no selector bits and span the parent
stride.
iw > 1: claim the lowest selector bit still available to this
level.

This lets an interleaving switch beneath a mixed-granularity root claim an inner selector bit instead of colliding with the outer bit already owned by the root.

Add a final check that the derived selector fits within the remaining selector bits.

Remove the auto-region granularity equality check that duplicated the old ordering rule. The selector walk now validates auto region granularities.

---
 drivers/cxl/core/region.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +28 -2
index c859c8661281..d5d19ed62171 100644
@@ -1533,6 +1533,9 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return -ENXIO;
 	}
 
+	/* Selector bits still available to this port. */
+	selector = cxlr_sel & ~selector;
+
 	if (cxl_rr->nr_targets_set) {
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
@@ -1545,7 +1548,23 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;
 	}
 
-	ig = p->interleave_granularity * parent_distance;
+	/*
+	 * Auto regions keep the firmware value, passthrough decoders consume
+	 * no selector bits, interleaving decoders claim the lowest available
+	 * selector bit.
+	 */
+	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
+		ig = cxld->interleave_granularity;
+	} else if (iw == 1) {
+		ig = p->interleave_granularity * parent_distance;
+	} else if (selector) {
+		ig = 1ULL << __ffs64(selector);
+	} else {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: no selector bits available for iw %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), iw);
+		return -ENXIO;
+	}
 
 	rc = ways_to_eiw(iw, &eiw);
 	if (!rc)
@@ -1557,9 +1576,16 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return rc;
 	}
 
+	if (iw > 1 && (~selector & get_selector(iw, ig))) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: derived selector %#llx exceeds remaining %#llx (iw %d ig %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			get_selector(iw, ig), selector, iw, ig);
+		return -ENXIO;
+	}
+
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
-		    (iw > 1 && cxld->interleave_granularity != ig) ||
 		    !spa_maps_hpa(p, &cxld->hpa_range) ||
 		    ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
 			dev_err(&cxlr->dev,
Signature · 1 line
2.37.3
#7 lore raw

Re: [PATCH v2 2/6] cxl/region: Derive port granularity from selector bits

Quoted 5 lines · Alison Schofield

Selector-bit validation uses a single same-granularity formula for user-created regions. Mixed-granularity layouts need the programmed granularity to depend on the type of decoder being configured.

Quoted 7 lines

Derive port granularity from the selector state:

AUTO: keep the firmware-programmed value; the walk validated it.
iw == 1: passthrough; consume no selector bits and span the parent
stride.
iw > 1: claim the lowest selector bit still available to this
level.

If we change the implementation to use selector bits, this patch may change again. Anyway, see below.

Quoted 24 lines

This lets an interleaving switch beneath a mixed-granularity root claim an inner selector bit instead of colliding with the outer bit already owned by the root.

Add a final check that the derived selector fits within the remaining selector bits.

Remove the auto-region granularity equality check that duplicated the old ordering rule. The selector walk now validates auto region granularities.

---
 drivers/cxl/core/region.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +0 -0
index c859c8661281..d5d19ed62171 100644

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1537

Quoted 5 lines
drivers/cxl/core/region.c +2 -0
@@ -1533,6 +1533,9 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return -ENXIO;
 	}
 
+	/* Selector bits still available to this port. */
+	selector = cxlr_sel & ~selector;

This is only used to calculate ig for the non-auto case. Can be moved there.

Quoted 6 lines
drivers/cxl/core/region.c +1 -0
+
 	if (cxl_rr->nr_targets_set) {
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
@@ -1545,7 +1548,23 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1557

Quoted 10 lines
drivers/cxl/core/region.c +7 -1
 	}
 
-	ig = p->interleave_granularity * parent_distance;
+	/*
+	 * Auto regions keep the firmware value, passthrough decoders consume
+	 * no selector bits, interleaving decoders claim the lowest available
+	 * selector bit.
+	 */
+	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
+		ig = cxld->interleave_granularity;

Move that to the CXL_REGION_F_AUTO section later below.

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1567

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+	} else if (iw == 1) {
+		ig = p->interleave_granularity * parent_distance;
+	} else if (selector) {
+		ig = 1ULL << __ffs64(selector);
+	} else {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: no selector bits available for iw %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), iw);
+		return -ENXIO;
+	}

That can be moved to non-auto only part.

Quoted 5 lines
drivers/cxl/core/region.c +0 -0
 	rc = ways_to_eiw(iw, &eiw);
 	if (!rc)
@@ -1557,9 +1576,16 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return rc;

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1586

Quoted 10 lines
drivers/cxl/core/region.c +8 -0
 	}
 
+	if (iw > 1 && (~selector & get_selector(iw, ig))) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: derived selector %#llx exceeds remaining %#llx (iw %d ig %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			get_selector(iw, ig), selector, iw, ig);
+		return -ENXIO;
+	}
+

Same with this check, should go to non-auto.

Quoted 9 lines
drivers/cxl/core/region.c +0 -1
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
-		    (iw > 1 && cxld->interleave_granularity != ig) ||
 		    !spa_maps_hpa(p, &cxld->hpa_range) ||
 		    ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
 			dev_err(&cxlr->dev,
Signature · 1 line
2.37.3
author maintainer
#8 lore raw

Re: [PATCH v2 2/6] cxl/region: Derive port granularity from selector bits

Quoted 8 lines · Robert Richter
Quoted 7 lines · Alison Schofield

Selector-bit validation uses a single same-granularity formula for user-created regions. Mixed-granularity layouts need the programmed granularity to depend on the type of decoder being configured.

Derive port granularity from the selector state:

Quoted 9 lines
Quoted 5 lines

AUTO: keep the firmware-programmed value; the walk validated it.
iw == 1: passthrough; consume no selector bits and span the parent
stride.
iw > 1: claim the lowest selector bit still available to this
level.

If we change the implementation to use selector bits, this patch may change again. Anyway, see below.

The selector-model question is answered in my reply on Patch 3/6. v3 keeps position/granularity derivation in value space and keeps the selector bits as the validation representation. This patch is updated per your comments below, but not rewritten around selector arithmetic.

Quoted 27 lines
Quoted 27 lines

This lets an interleaving switch beneath a mixed-granularity root claim an inner selector bit instead of colliding with the outer bit already owned by the root.

Add a final check that the derived selector fits within the remaining selector bits.

Remove the auto-region granularity equality check that duplicated the old ordering rule. The selector walk now validates auto region granularities.

---
 drivers/cxl/core/region.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +0 -0
index c859c8661281..d5d19ed62171 100644
@@ -1533,6 +1533,9 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return -ENXIO;
 	}
 
Quoted 5 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1537

Quoted 2 lines
drivers/cxl/core/region.c +2 -0
+	/* Selector bits still available to this port. */
+	selector = cxlr_sel & ~selector;

This is only used to calculate ig for the non-auto case. Can be moved there.

Done in v3. Calc'd at the point of use in the non-auto path.

Quoted 9 lines
Quoted 9 lines
drivers/cxl/core/region.c +1 -0
+
 	if (cxl_rr->nr_targets_set) {
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
@@ -1545,7 +1548,23 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		goto add_target;
 	}
 
Quoted 10 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1557

Quoted 8 lines
drivers/cxl/core/region.c +7 -1
-	ig = p->interleave_granularity * parent_distance;
+	/*
+	 * Auto regions keep the firmware value, passthrough decoders consume
+	 * no selector bits, interleaving decoders claim the lowest available
+	 * selector bit.
+	 */
+	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
+		ig = cxld->interleave_granularity;

Move that to the CXL_REGION_F_AUTO section later below.

Done in v3.

Quoted 14 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1567

Quoted 11 lines
drivers/cxl/core/region.c +10 -0
+	} else if (iw == 1) {
+		ig = p->interleave_granularity * parent_distance;
+	} else if (selector) {
+		ig = 1ULL << __ffs64(selector);
+	} else {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: no selector bits available for iw %d\n",
+			dev_name(port->uport_dev), dev_name(&port->dev), iw);
+		return -ENXIO;
+	}

That can be moved to non-auto only part.

Done in v3.

Quoted 7 lines
Quoted 7 lines
drivers/cxl/core/region.c +0 -0
 	rc = ways_to_eiw(iw, &eiw);
 	if (!rc)
@@ -1557,9 +1576,16 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		return rc;
 	}
 
Quoted 11 lines

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1586

Quoted 8 lines
drivers/cxl/core/region.c +8 -0
+	if (iw > 1 && (~selector & get_selector(iw, ig))) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s: derived selector %#llx exceeds remaining %#llx (iw %d ig %d)\n",
+			dev_name(port->uport_dev), dev_name(&port->dev),
+			get_selector(iw, ig), selector, iw, ig);
+		return -ENXIO;
+	}
+

Same with this check, should go to non-auto.

Done in v3. With these moves the AUTO path keeps its checks in one block and all derivation is visibly non-auto-only. That reads much better, thanks!

-- Alison

Quoted 9 lines
Quoted 9 lines
drivers/cxl/core/region.c +0 -1
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		if (cxld->interleave_ways != iw ||
-		    (iw > 1 && cxld->interleave_granularity != ig) ||
 		    !spa_maps_hpa(p, &cxld->hpa_range) ||
 		    ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
 			dev_err(&cxlr->dev,
Signature · 1 line
2.37.3
author maintainer
#9 lore raw

PATCH v2 3/6 cxl/region: Account for mixed-granularity in position calculations

In reply to Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
drivers/cxl/core/region.c +58 -21
index d5d19ed62171..9fff538a9ce1 100644
@@ -1444,12 +1444,34 @@ static inline u64 get_selector(int ways, int gran)
 	return (u64)(ways - 1) * gran;
 }
 
+/**
+ * root_pos_stride() - Return root target stride in region positions
+ * @cxlr: region
+ *
+ * Return: root_gran / region_gran when the root granularity is larger
+ * than the region granularity, or 1 otherwise.
+ */
+static inline int root_pos_stride(struct cxl_region *cxlr)
+{
+	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
+	int region_gran = cxlr->params.interleave_granularity;
+
+	if (cxlrd->cxlsd.cxld.interleave_ways <= 1)
+		return 1;
+
+	if (region_gran == 0 || root_gran <= region_gran)
+		return 1;
+
+	return root_gran / region_gran;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	int distance, parent_distance;
+	int sel_distance, pos_distance, stride;
 	int ig, iw, rc, pos = cxled->pos;
 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
@@ -1490,10 +1512,11 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 				cxlrd->cxlsd.cxld.interleave_granularity);
 
 	/* Track distances used in passthrough granularity and peer checks */
-	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
-	if (!is_power_of_2(parent_distance))
-		parent_distance /= 3;
-	distance = cxlrd->cxlsd.cxld.interleave_ways;
+	stride = root_pos_stride(cxlr);
+	sel_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(sel_distance))
+		sel_distance /= 3;
+	pos_distance = stride > 1 ? 1 : cxlrd->cxlsd.cxld.interleave_ways;
 
 	for (iter = parent_port; !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent)) {
@@ -1517,11 +1540,11 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 
 		selector |= cxld_sel;
 
-		parent_distance *= cxl_rr_iter->nr_targets;
-		distance *= cxl_rr_iter->nr_targets;
+		sel_distance *= cxl_rr_iter->nr_targets;
+		pos_distance *= cxl_rr_iter->nr_targets;
 	}
 
-	distance *= iw;
+	pos_distance *= iw;
 
 	/* Accumulated bits must fit within the region selector */
 	cxlr_sel = get_selector(p->interleave_ways, p->interleave_granularity);
@@ -1540,7 +1563,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
-						     distance);
+						     pos_distance);
 				if (rc)
 					return rc;
 				goto out_target_set;
@@ -1556,7 +1579,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		ig = cxld->interleave_granularity;
 	} else if (iw == 1) {
-		ig = p->interleave_granularity * parent_distance;
+		ig = p->interleave_granularity * sel_distance;
 	} else if (selector) {
 		ig = 1ULL << __ffs64(selector);
 	} else {
@@ -1813,14 +1836,17 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
 	struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
 	struct cxl_decoder *cxld = &cxlsd->cxld;
+	int stride = root_pos_stride(cxlr);
 	int iw = cxld->interleave_ways;
 	struct cxl_port *iter;
 	int rc;
 
-	if (dport != cxlrd->cxlsd.target[pos % iw]) {
-		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
+	/* Mixed-granularity roots advance every stride positions */
+	if (dport != cxlrd->cxlsd.target[pos / stride % iw]) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s invalid target position for %s (stride %d, check root/region gran ratio)\n",
 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
-			dev_name(&cxlrd->cxlsd.cxld.dev));
+			dev_name(&cxlrd->cxlsd.cxld.dev), stride);
 		return -ENXIO;
 	}
 
@@ -1951,6 +1977,7 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
  * cxl_calc_interleave_pos() - calculate an endpoint position in a region
  * @cxled: endpoint decoder member of given region
  * @hpa_range: translated HPA range of the endpoint
+ * @stride: root target stride in region positions
  *
  * The endpoint position is calculated by traversing the topology from
  * the endpoint to the root decoder and iteratively applying this
@@ -1960,11 +1987,16 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
  *
  * ...where @position is inferred from switch and root decoder target lists.
  *
+ * For mixed-granularity regions the root contributes outer position
+ * bits, so the root step is:
+ *
+ *    position = position + stride * parent_pos
+ *
  * Return: position >= 0 on success
  *	   -ENXIO on failure
  */
 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
-				   struct range *hpa_range)
+				   struct range *hpa_range, int stride)
 {
 	struct cxl_port *iter, *port = cxled_to_port(cxled);
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
@@ -2010,6 +2042,11 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 		if (rc)
 			return rc;
 
+		if (stride > 1 && is_cxl_root(parent_port_of(iter))) {
+			pos = pos + stride * parent_pos;
+			break;
+		}
+
 		pos = pos * parent_ways + parent_pos;
 	}
 
@@ -2024,12 +2061,14 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 static int cxl_region_sort_targets(struct cxl_region *cxlr)
 {
 	struct cxl_region_params *p = &cxlr->params;
+	int stride = root_pos_stride(cxlr);
 	int i, rc = 0;
 
 	for (i = 0; i < p->nr_targets; i++) {
 		struct cxl_endpoint_decoder *cxled = p->targets[i];
 
-		cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range);
+		cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range,
+						     stride);
 		/*
 		 * Record that sorting failed, but still continue to calc
 		 * cxled->pos so that follow-on code paths can reliably
@@ -2056,6 +2095,7 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_port *ep_port, *root_port;
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
+	int stride;
 
 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
 				  p->interleave_granularity);
@@ -2088,11 +2128,6 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
-	/* Keep this patch bisectable until position arithmetic is updated */
-	if (cxlrd->cxlsd.cxld.interleave_granularity >
-	    p->interleave_granularity)
-		return -ENXIO;
-
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
@@ -2214,11 +2249,13 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	 * A fail message here means that this interleave config
 	 * will fail when presented as CXL_REGION_F_AUTO.
 	 */
+	stride = root_pos_stride(cxlr);
 	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, &cxlr->hpa_range);
+		test_pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range,
+						   stride);
 		dev_dbg(&cxled->cxld.dev,
 			"Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
 			(test_pos == cxled->pos) ? "success" : "fail",
Signature · 1 line
2.37.3
#10 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incorrect uniform `pos_distance` assumption in mixed-granularity regions leads to a kernel panic…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incorrect uniform pos_distance assumption in mixed-granularity regions leads to a kernel panic in check_last_peer().
- [High] Flawed arithmetic in cxl_calc_interleave_pos() breaks auto-discovery for topologies with multiple interleave passes or interleaved downstream switches. --

commit 9cd5598bf3a699e6fbc48008f0af90a4640e7218
Author: Alison Schofield <[email protected]>

cxl/region: Account for mixed-granularity in position calculations

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before advancing. This patch accounts for that stride in endpoint placement, position calculations, and peer-distance checks.

Quoted 4 lines
drivers/cxl/core/region.c +0 -0
index d5d19ed621713..9fff538a9ce16 100644

[ ... ]

Quoted 16 lines
drivers/cxl/core/region.c +5 -4
@@ -1490,10 +1512,11 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 				cxlrd->cxlsd.cxld.interleave_granularity);
 
 	/* Track distances used in passthrough granularity and peer checks */
-	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
-	if (!is_power_of_2(parent_distance))
-		parent_distance /= 3;
-	distance = cxlrd->cxlsd.cxld.interleave_ways;
+	stride = root_pos_stride(cxlr);
+	sel_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(sel_distance))
+		sel_distance /= 3;
+	pos_distance = stride > 1 ? 1 : cxlrd->cxlsd.cxld.interleave_ways;
 
 	for (iter = parent_port; !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent)) {

[ ... ]

drivers/cxl/core/region.c static int cxl_port_setup_targets(struct cxl_port *port, · line 1566

Quoted 6 lines
drivers/cxl/core/region.c +1 -1
@@ -1540,7 +1563,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
-						     distance);
+						     pos_distance);

[Severity: High]
If stride > 1, pos_distance is initially 1 and then scaled by intermediate target counts. Does this uniform distance correctly account for the non-uniform gap between positions routed to the same downstream port in mixed-granularity topologies?

In check_last_peer(), it subtracts this distance to locate a peer endpoint:

drivers/cxl/core/region.c:check_last_peer() {
	...
	cxled_peer = p->targets[pos - distance];
	cxlmd_peer = cxled_to_memdev(cxled_peer);
	ep_peer = cxl_ep_load(port, cxlmd_peer);
	if (ep->dport != ep_peer->dport) {
	...
}

If the calculated distance is flawed, could this inspect a position belonging to a completely different root target's block? If so, cxl_ep_load() might return NULL since the peer endpoint isn't a descendant of the current intermediate switch port. This would lead to a NULL pointer dereference when accessing ep_peer->dport.

Quoted 3 lines
drivers/cxl/core/region.c +0 -0
 				if (rc)
 					return rc;
 				goto out_target_set;

[ ... ]

drivers/cxl/core/region.c static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled, · line 2051

Quoted 11 lines
drivers/cxl/core/region.c +5 -0
@@ -2010,6 +2042,11 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 		if (rc)
 			return rc;
 
+		if (stride > 1 && is_cxl_root(parent_port_of(iter))) {
+			pos = pos + stride * parent_pos;
+			break;
+		}
+
 		pos = pos * parent_ways + parent_pos;
 	}

[Severity: High]
Does this position calculation properly handle interleave wrap-around and higher-order switch interleaving?

By using pos = pos + stride * parent_pos, it appears to assume that the root target always provides the highest-order bits of the region position. In topologies where a root target owns more endpoints than the stride, or where downstream switches decode bits higher than the root decoder, could this result in local position collisions?

For example, if the 5th endpoint on a host bridge has a local pos of 4 and a stride of 4, this would calculate 4 + 4 * 0 = 4 instead of the correct region position 8. Would this cause cxl_region_attach_position() to reject otherwise valid auto-discovered CXL topologies?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#11 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

In reply to Alison Schofield
Quoted 36 lines · Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
drivers/cxl/core/region.c +12 -0
index d5d19ed62171..9fff538a9ce1 100644
@@ -1444,12 +1444,34 @@ static inline u64 get_selector(int ways, int gran)
 	return (u64)(ways - 1) * gran;
 }
 
+/**
+ * root_pos_stride() - Return root target stride in region positions
+ * @cxlr: region
+ *
+ * Return: root_gran / region_gran when the root granularity is larger
+ * than the region granularity, or 1 otherwise.
+ */
+static inline int root_pos_stride(struct cxl_region *cxlr)
+{
+	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
+	int region_gran = cxlr->params.interleave_granularity;

drivers/cxl/core/region.c static inline u64 get_selector(int ways, int gran) · line 1468

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+
+	if (cxlrd->cxlsd.cxld.interleave_ways <= 1)
+		return 1;
+
+	if (region_gran == 0 || root_gran <= region_gran)
+		return 1;
+
+	return root_gran / region_gran;
+}
+

Hi Alison,

This helper computes stride from gran ratio, but nothing from downstream checks that the stride equals the number of endpoints actually fanned out under one root target.
The patch assumes each root target each own stride consecutive region position, when that does not hold, the additive recurrence below and peer-distance seed both mis-account, and we walk off into a foreign endpoint.

Quoted 108 lines
drivers/cxl/core/region.c +24 -14
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	int distance, parent_distance;
+	int sel_distance, pos_distance, stride;
 	int ig, iw, rc, pos = cxled->pos;
 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
@@ -1490,10 +1512,11 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 				cxlrd->cxlsd.cxld.interleave_granularity);
 
 	/* Track distances used in passthrough granularity and peer checks */
-	parent_distance = cxlrd->cxlsd.cxld.interleave_ways;
-	if (!is_power_of_2(parent_distance))
-		parent_distance /= 3;
-	distance = cxlrd->cxlsd.cxld.interleave_ways;
+	stride = root_pos_stride(cxlr);
+	sel_distance = cxlrd->cxlsd.cxld.interleave_ways;
+	if (!is_power_of_2(sel_distance))
+		sel_distance /= 3;
+	pos_distance = stride > 1 ? 1 : cxlrd->cxlsd.cxld.interleave_ways;
 
 	for (iter = parent_port; !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent)) {
@@ -1517,11 +1540,11 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 
 		selector |= cxld_sel;
 
-		parent_distance *= cxl_rr_iter->nr_targets;
-		distance *= cxl_rr_iter->nr_targets;
+		sel_distance *= cxl_rr_iter->nr_targets;
+		pos_distance *= cxl_rr_iter->nr_targets;
 	}
 
-	distance *= iw;
+	pos_distance *= iw;
 
 	/* Accumulated bits must fit within the region selector */
 	cxlr_sel = get_selector(p->interleave_ways, p->interleave_granularity);
@@ -1540,7 +1563,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 		for (int i = 0; i < cxl_rr->nr_targets_set; i++)
 			if (ep->dport == cxlsd->target[i]) {
 				rc = check_last_peer(cxled, ep, cxl_rr,
-						     distance);
+						     pos_distance);
 				if (rc)
 					return rc;
 				goto out_target_set;
@@ -1556,7 +1579,7 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
 		ig = cxld->interleave_granularity;
 	} else if (iw == 1) {
-		ig = p->interleave_granularity * parent_distance;
+		ig = p->interleave_granularity * sel_distance;
 	} else if (selector) {
 		ig = 1ULL << __ffs64(selector);
 	} else {
@@ -1813,14 +1836,17 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
 	struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
 	struct cxl_decoder *cxld = &cxlsd->cxld;
+	int stride = root_pos_stride(cxlr);
 	int iw = cxld->interleave_ways;
 	struct cxl_port *iter;
 	int rc;
 
-	if (dport != cxlrd->cxlsd.target[pos % iw]) {
-		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
+	/* Mixed-granularity roots advance every stride positions */
+	if (dport != cxlrd->cxlsd.target[pos / stride % iw]) {
+		dev_dbg(&cxlr->dev,
+			"%s:%s invalid target position for %s (stride %d, check root/region gran ratio)\n",
 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
-			dev_name(&cxlrd->cxlsd.cxld.dev));
+			dev_name(&cxlrd->cxlsd.cxld.dev), stride);
 		return -ENXIO;
 	}
 
@@ -1951,6 +1977,7 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
  * cxl_calc_interleave_pos() - calculate an endpoint position in a region
  * @cxled: endpoint decoder member of given region
  * @hpa_range: translated HPA range of the endpoint
+ * @stride: root target stride in region positions
  *
  * The endpoint position is calculated by traversing the topology from
  * the endpoint to the root decoder and iteratively applying this
@@ -1960,11 +1987,16 @@ static int find_pos_and_ways(struct cxl_port *port, struct range *range,
  *
  * ...where @position is inferred from switch and root decoder target lists.
  *
+ * For mixed-granularity regions the root contributes outer position
+ * bits, so the root step is:
+ *
+ *    position = position + stride * parent_pos
+ *
  * Return: position >= 0 on success
  *	   -ENXIO on failure
  */
 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
-				   struct range *hpa_range)
+				   struct range *hpa_range, int stride)
 {
 	struct cxl_port *iter, *port = cxled_to_port(cxled);
 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
@@ -2010,6 +2042,11 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 		if (rc)

drivers/cxl/core/region.c static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled, · line 2052

Quoted 10 lines
drivers/cxl/core/region.c +5 -0
 			return rc;
 
+		if (stride > 1 && is_cxl_root(parent_port_of(iter))) {
+			pos = pos + stride * parent_pos;
+			break;
+		}
+
 		pos = pos * parent_ways + parent_pos;
 	}
 

I have a question here, on AUTO path, the check are bitwise and order-independent, so they accept it, then pos comes out d*2+u+4*hb where HPA decodes reads d+2*u+4*hb, so 6 of 12 endpoints get the wrong pos and the region is rejected or mis-mapped.

If my inference is true, it contradicts the cover-letter's "every arrangement" claim.

--Richard

Quoted 55 lines
drivers/cxl/core/region.c +7 -7
@@ -2024,12 +2061,14 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 static int cxl_region_sort_targets(struct cxl_region *cxlr)
 {
 	struct cxl_region_params *p = &cxlr->params;
+	int stride = root_pos_stride(cxlr);
 	int i, rc = 0;
 
 	for (i = 0; i < p->nr_targets; i++) {
 		struct cxl_endpoint_decoder *cxled = p->targets[i];
 
-		cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range);
+		cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range,
+						     stride);
 		/*
 		 * Record that sorting failed, but still continue to calc
 		 * cxled->pos so that follow-on code paths can reliably
@@ -2056,6 +2095,7 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_port *ep_port, *root_port;
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
+	int stride;
 
 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
 				  p->interleave_granularity);
@@ -2088,11 +2128,6 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
-	/* Keep this patch bisectable until position arithmetic is updated */
-	if (cxlrd->cxlsd.cxld.interleave_granularity >
-	    p->interleave_granularity)
-		return -ENXIO;
-
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
@@ -2214,11 +2249,13 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	 * A fail message here means that this interleave config
 	 * will fail when presented as CXL_REGION_F_AUTO.
 	 */
+	stride = root_pos_stride(cxlr);
 	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, &cxlr->hpa_range);
+		test_pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range,
+						   stride);
 		dev_dbg(&cxled->cxld.dev,
 			"Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
 			(test_pos == cxled->pos) ? "success" : "fail",
Signature · 1 line
2.37.3
author maintainer
#12 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

Quoted 50 lines · Richard Cheng
Quoted 36 lines · Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
drivers/cxl/core/region.c +12 -0
index d5d19ed62171..9fff538a9ce1 100644
@@ -1444,12 +1444,34 @@ static inline u64 get_selector(int ways, int gran)
 	return (u64)(ways - 1) * gran;
 }
 
+/**
+ * root_pos_stride() - Return root target stride in region positions
+ * @cxlr: region
+ *
+ * Return: root_gran / region_gran when the root granularity is larger
+ * than the region granularity, or 1 otherwise.
+ */
+static inline int root_pos_stride(struct cxl_region *cxlr)
+{
+	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
+	int region_gran = cxlr->params.interleave_granularity;

drivers/cxl/core/region.c static inline u64 get_selector(int ways, int gran) · line 1468

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+
+	if (cxlrd->cxlsd.cxld.interleave_ways <= 1)
+		return 1;
+
+	if (region_gran == 0 || root_gran <= region_gran)
+		return 1;
+
+	return root_gran / region_gran;
+}
+

Hi Alison,

Quoted 7 lines

This helper computes stride from gran ratio, but nothing from downstream checks that the stride equals the number of endpoints actually fanned out under one root target.
The patch assumes each root target each own stride consecutive region position, when that does not hold, the additive recurrence below and peer-distance seed both mis-account, and we walk off into a foreign endpoint.

addressed below...

snip

Quoted 17 lines

drivers/cxl/core/region.c static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled, · line 2052

Quoted 12 lines
drivers/cxl/core/region.c +5 -0
@@ -2010,6 +2042,11 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 		if (rc)
 			return rc;
 
+		if (stride > 1 && is_cxl_root(parent_port_of(iter))) {
+			pos = pos + stride * parent_pos;
+			break;
+		}
+
 		pos = pos * parent_ways + parent_pos;
 	}
 

I have a question here, on AUTO path, the check are bitwise and order-independent, so they accept it, then pos comes out d*2+u+4*hb where HPA decodes reads d+2*u+4*hb, so 6 of 12 endpoints get the wrong pos and the region is rejected or mis-mapped.

Quoted 3 lines

If my inference is true, it contradicts the cover-letter's "every arrangement" claim.

--Richard

Hi Richard,

(Oh, this got long! Spoiler, I'm proposing we limit to configs where root->endpoint gran is only coarser->finer, ie. monotonically decreasing. No coarser->finer->coarser(again))

Thanks for digging into the position arithmetic! You are right, and your two points land on the same root cause that Sashiko raised from the peer-distance side. The position calculator assumes every level slices inside the chunk of the level above it, root carving the biggest pieces and each switch below it slicing finer. When that is true, the math is correct. So courser->finer granularity as we go from root to endpoints all is good!

However, when a switch slices at a coarser granularity than the level above it, the bits are out of order, the calculator places those endpoints wrong, and the region is either rejected or mapped wrong. The selector checks don't catch it because they only look at which bits are used, not their order, exactly as you described.

First I revisited the CXL spec. The spec does not explicitly forbid courser->finer->courser? It doesn't. Section 9.13.1 only requires that the levels use different, consecutive HPA bits. It doesn't actually mandate an ordering, so the out-of-order layouts are legal.

I looked at the CXL Spec and the Driver Writers Guide examples. Every example is coarse->fine:
CXL Spec Figures 9-16, 9-17, 9-18, and SW Guide 2.7.2, 2.7.3, 2.7.4

The CXL spec enumeration tables go further. Table 9-16 (a 12 way) lists an out of order ordering, but that has an equivalent coarse->fine config that produces the same results.

The gist of all that spec talk, is that I cannot say we won't add the suppport based on what the spec says.

Next, I considered teaching the calculator to handle arbitrary bit ordering, but I don't think it's the right thing for this series. It would mean reworking the core position calculation to gain a case that nothing, no spec figure, no table, no firmware I'm aware of, actually produces, and that users can already reach another way.

So what I'd like to do instead is define the scope explicitly as support for coarse-to-fine mixed-granularity. That is everything this series set out to enable, and a clear superset of today's behavior, where any region granularity different from the root's was rejected outright. Gracefully fail anything out-of-order with a clear error rather than letting it mis-map or oops.

I also believe it meets Robert Richters (AMD) needs because that original patch did not add that support in the selector bit accounting. Find link to Roberts original patch in cover letter.

Does a clean rejection of the out-of-order case work for you?

Other folks?

-- Alison

maintainer
#13 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

Quoted 110 lines · On Tue, 16 Jun 2026 20:10:23 -0700 Alison Schofield…
Quoted 50 lines · Richard Cheng
Quoted 36 lines · Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
drivers/cxl/core/region.c +12 -0
index d5d19ed62171..9fff538a9ce1 100644
@@ -1444,12 +1444,34 @@ static inline u64 get_selector(int ways, int gran)
 	return (u64)(ways - 1) * gran;
 }
 
+/**
+ * root_pos_stride() - Return root target stride in region positions
+ * @cxlr: region
+ *
+ * Return: root_gran / region_gran when the root granularity is larger
+ * than the region granularity, or 1 otherwise.
+ */
+static inline int root_pos_stride(struct cxl_region *cxlr)
+{
+	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
+	int region_gran = cxlr->params.interleave_granularity;

drivers/cxl/core/region.c static inline u64 get_selector(int ways, int gran) · line 1468

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+
+	if (cxlrd->cxlsd.cxld.interleave_ways <= 1)
+		return 1;
+
+	if (region_gran == 0 || root_gran <= region_gran)
+		return 1;
+
+	return root_gran / region_gran;
+}
+  

Hi Alison,

Quoted 7 lines

This helper computes stride from gran ratio, but nothing from downstream checks that the stride equals the number of endpoints actually fanned out under one root target.
The patch assumes each root target each own stride consecutive region position, when that does not hold, the additive recurrence below and peer-distance seed both mis-account, and we walk off into a foreign endpoint.

addressed below...

snip

Quoted 17 lines

drivers/cxl/core/region.c static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled, · line 2052

Quoted 12 lines
drivers/cxl/core/region.c +5 -0
@@ -2010,6 +2042,11 @@ static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
 		if (rc)
 			return rc;
 
+		if (stride > 1 && is_cxl_root(parent_port_of(iter))) {
+			pos = pos + stride * parent_pos;
+			break;
+		}
+
 		pos = pos * parent_ways + parent_pos;
 	}
 

I have a question here, on AUTO path, the check are bitwise and order-independent, so they accept it, then pos comes out d*2+u+4*hb where HPA decodes reads d+2*u+4*hb, so 6 of 12 endpoints get the wrong pos and the region is rejected or mis-mapped.

Quoted 3 lines

If my inference is true, it contradicts the cover-letter's "every arrangement" claim.

--Richard

Hi Richard,

(Oh, this got long! Spoiler, I'm proposing we limit to configs where root->endpoint gran is only coarser->finer, ie. monotonically decreasing. No coarser->finer->coarser(again))

Thanks for digging into the position arithmetic! You are right, and your two points land on the same root cause that Sashiko raised from the peer-distance side. The position calculator assumes every level slices inside the chunk of the level above it, root carving the biggest pieces and each switch below it slicing finer. When that is true, the math is correct. So courser->finer granularity as we go from root to endpoints all is good!

However, when a switch slices at a coarser granularity than the level above it, the bits are out of order, the calculator places those endpoints wrong, and the region is either rejected or mapped wrong. The selector checks don't catch it because they only look at which bits are used, not their order, exactly as you described.

First I revisited the CXL spec. The spec does not explicitly forbid courser->finer->courser? It doesn't. Section 9.13.1 only requires that the levels use different, consecutive HPA bits. It doesn't actually mandate an ordering, so the out-of-order layouts are legal.

Quoted 7 lines

I looked at the CXL Spec and the Driver Writers Guide examples. Every example is coarse->fine:
CXL Spec Figures 9-16, 9-17, 9-18, and SW Guide 2.7.2, 2.7.3, 2.7.4

The CXL spec enumeration tables go further. Table 9-16 (a 12 way) lists an out of order ordering, but that has an equivalent coarse->fine config that produces the same results.

Same result in some sense, but not for what pattern goes down a given link.

Quoted 17 lines

The gist of all that spec talk, is that I cannot say we won't add the suppport based on what the spec says.

Next, I considered teaching the calculator to handle arbitrary bit ordering, but I don't think it's the right thing for this series. It would mean reworking the core position calculation to gain a case that nothing, no spec figure, no table, no firmware I'm aware of, actually produces, and that users can already reach another way.

So what I'd like to do instead is define the scope explicitly as support for coarse-to-fine mixed-granularity. That is everything this series set out to enable, and a clear superset of today's behavior, where any region granularity different from the root's was rejected outright. Gracefully fail anything out-of-order with a clear error rather than letting it mis-map or oops.

Quoted 7 lines

I also believe it meets Robert Richters (AMD) needs because that original patch did not add that support in the selector bit accounting. Find link to Roberts original patch in cover letter.

Does a clean rejection of the out-of-order case work for you?

Other folks?

To cut to the chase, I don't personally care if we limit the non fine to coarse cases.

It's not hard to come up with a hardware design where say 128 / 64 / 256 makes sense.

The coarse at the top would be the host wanting to use most efficient size for the impdef interconnect pre CXL.

With that in mind the system designer may then want to get the overall granularity down as quick as possible to reduce hot spots on the root ports where contention is likely to be highest. So splits things down to 64 bytes.

When we reach the switch, 64 and 128 are gone, so best it can do is 256.

Anyhow, this is one of those - for completeness - messages, if anyone really really cares then can rework things again!

Jonathan

Quoted 5 lines

-- Alison

#14 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

In reply to Alison Schofield

Alison,

Quoted 7 lines · Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

I don't see why root decoders need special handling here (apart from the 3-way factor, but this is already handled). See my earlier comments to the other patches, but if root decoders were just included in the pos calculation, that should give correct results. And, instead of introducing the "stride" term, why not just continue to work with the selector mask also for the root decoders?

This "special case" handling unnecessarily introduces complex logic and code. We should avoid that.

-Robert

Quoted 8 lines

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
author maintainer
#15 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

Quoted 29 lines · Robert Richter

Alison,

Quoted 7 lines · Alison Schofield

Mixed-granularity regions place the root decoder at outer HPA bits. A root target then owns root_gran / region_gran inner positions before the root advances to the next target. That ratio becomes the root target stride in region positions.

Account for that stride in endpoint placement, position calculation, and peer-distance checks.

I don't see why root decoders need special handling here (apart from the 3-way factor, but this is already handled). See my earlier comments to the other patches, but if root decoders were just included in the pos calculation, that should give correct results. And, instead of introducing the "stride" term, why not just continue to work with the selector mask also for the root decoders?

This "special case" handling unnecessarily introduces complex logic and code. We should avoid that.

I don't see why root decoders need special handling here (apart from the 3-way factor, but this is already handled). See my earlier comments to the other patches, but if root decoders were just included in the pos calculation, that should give correct results. And, instead of introducing the "stride" term, why not just continue to work with the selector mask also for the root decoders?

Quoted 2 lines

This "special case" handling unnecessarily introduces complex logic and code. We should avoid that.

Thanks for the review Robert,

It's not added complexity. It is the root decoder's contribution to the position, the piece you identified as missing from the walk in your follow-up on 1/6. Root decoders *are* "just included in the pos calculation" here: stride = root_gran / region_gran is the place value of the root's selector bits in position space, so the root step deposits the root's target index at the root's place value, same as every other level deposits its index at its own. It is the same information as the root decoder selectors from your follow-up on 1/6, just written as a multiplier instead of a bit mask, which matches the multiply/add arithmetic this function already uses. Without it, the endpoints get wrong positions while every selector check still passes.

Nor is it a "special case" of the model. It is the general per-level rule "each level contributes target_idx * level_gran / region_gran, which same-granularity levels reduce to trivially. The root is simply the one level whose granularity can differ from the region's in this series, so it's the one place the general term is visible.

I did check your selector-bit proposal. A selector-derived position formula gave identical results to the current code on every pow2 config. That was equivalent but not simpler. Issue is that it cannot express the factor of 3 of the 3/6/12-way roots, which consumes no HPA bits and is recovered by divide/modulo above the pow2 selector bits. So switching to selector-bit position math would actually require two models: selectors for pow2, plus the value math kept anyway for 3/6/12-way. The value math we use now handles both in one model, so position calculation stays there. Happy to post the comparison traces if useful.

For this patch: stride is renamed and commented as the root's position place value to make its connection to the selector layout explicit.

-- Alison

Quoted 11 lines

-Robert

Quoted 8 lines

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
#16 lore raw

Re: [PATCH v2 3/6] cxl/region: Account for mixed-granularity in position calculations

Hi Alison,

Quoted 14 lines · Alison Schofield

Thanks for the review Robert,

It's not added complexity. It is the root decoder's contribution to the position, the piece you identified as missing from the walk in your follow-up on 1/6. Root decoders *are* "just included in the pos calculation" here: stride = root_gran / region_gran is the place value of the root's selector bits in position space, so the root step deposits the root's target index at the root's place value, same as every other level deposits its index at its own. It is the same information as the root decoder selectors from your follow-up on 1/6, just written as a multiplier instead of a bit mask, which matches the multiply/add arithmetic this function already uses. Without it, the endpoints get wrong positions while every selector check still passes.

But the endpoint position depends only on the factor-of-2 parts of the lower address bits and includes the root decoder selectors. The factor-of-3 can just be dropped for position and selector. "stride" uses a multiplier to describe the positional bit within the total selector mask of the root decoders. The term "stride" only becomes clear from detailed reading of the code and is not obvious. It is then used for position calculations inline, e.g.

pos_distance = stride > 1 ? 1 : cxlrd->cxlsd.cxld.interleave_ways;

or

if (dport != cxlrd->cxlsd.target[pos / stride % iw]) {

That is the complexity I mean, I think the code is hard to maintain.

As said, imo, the position can be just directly calculated from the factor-of-2 portion of the total selector bits without the stride var.

Quoted 7 lines

Nor is it a "special case" of the model. It is the general per-level rule "each level contributes target_idx * level_gran / region_gran, which same-granularity levels reduce to trivially. The root is simply the one level whose granularity can differ from the region's in this series, so it's the one place the general term is visible.

Quoted 5 lines

I did check your selector-bit proposal. A selector-derived position formula gave identical results to the current code on every pow2 config. That was equivalent but not simpler. Issue is that it cannot express the factor of 3 of the 3/6/12-way roots, which consumes no HPA bits and is recovered by divide/modulo above the pow2 selector bits. So switching to

Stride does not contain that information either as it is always power of 2.

Quoted 4 lines

selector-bit position math would actually require two models: selectors for pow2, plus the value math kept anyway for 3/6/12-way. The value math we use now handles both in one model, so position calculation stays there. Happy to post the comparison traces if useful.

Finally a combination of 'mod iw' is used, which could apply to both implementations.

Quoted 3 lines

For this patch: stride is renamed and commented as the root's position place value to make its connection to the selector layout explicit.

That could be an improvement, let's see next version. The implementation should be clear and easily understandable. Right now I think the selector approach would be better.

Thanks,

-Robert

Quoted 14 lines

-- Alison

Quoted 11 lines

-Robert

Quoted 8 lines

Remove the temporary mixed-granularity attach reject now that position arithmetic matches the selector layout.

---
 drivers/cxl/core/region.c | 79 ++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 21 deletions(-)
author maintainer
#17 lore raw

PATCH v2 4/6 cxl/region: Validate mixed-granularity at sysfs and attach gates

In reply to Alison Schofield

User-created regions are validated at sysfs write through set_interleave_granularity(). Auto regions skip that path. Both need the same mixed-granularity rule applied before the per-port selector walk runs.

Replace the same-granularity sysfs check with is_ig_allowed(), which permits any region granularity not greater than the root granularity. Call the same helper at attach time so auto regions get the same rule.

3-way-family roots (3, 6, and 12 ways) need an additional check. A 3-way interleave consumes no selector bits, so selector containment alone cannot prove that the root and region cover the same address span. Add an explicit ways*gran span equality check in cxl_region_attach() for these roots.

Endpoint decoder capability is still checked at attach time by check_interleave_cap().

The new gates reference the root decoder ways and granularity several times through the verbose cxlrd->cxlsd.cxld.interleave_* dereference. Lift those into root_ways and root_gran locals to keep the checks readable.

---
 drivers/cxl/core/region.c | 55 ++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 10 deletions(-)
drivers/cxl/core/region.c +45 -10
index 9fff538a9ce1..311b5052bbc8 100644
@@ -559,10 +559,29 @@ static ssize_t interleave_granularity_show(struct device *dev,
 	return sysfs_emit(buf, "%d\n", p->interleave_granularity);
 }
 
+/**
+ * is_ig_allowed() - Validate region interleave granularity
+ * @cxlrd: root decoder
+ * @ig: proposed region interleave granularity in bytes
+ *
+ * Callers must have validated @ig through granularity_to_eig() first.
+ *
+ * Return: true for a non-interleaving root or any region granularity
+ * not larger than the root granularity.
+ */
+static inline bool is_ig_allowed(struct cxl_root_decoder *cxlrd, int ig)
+{
+	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
+
+	if (cxld->interleave_ways <= 1)
+		return true;
+
+	return ig <= cxld->interleave_granularity;
+}
+
 static int set_interleave_granularity(struct cxl_region *cxlr, int val)
 {
 	struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
-	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
 	struct cxl_region_params *p = &cxlr->params;
 	int rc;
 	u16 ig;
@@ -571,15 +590,7 @@ static int set_interleave_granularity(struct cxl_region *cxlr, int val)
 	if (rc)
 		return rc;
 
-	/*
-	 * When the host-bridge is interleaved, disallow region granularity !=
-	 * root granularity. Regions with a granularity less than the root
-	 * interleave result in needing multiple endpoints to support a single
-	 * slot in the interleave (possible to support in the future). Regions
-	 * with a granularity greater than the root interleave result in invalid
-	 * DPA translations (invalid to support).
-	 */
-	if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
+	if (!is_ig_allowed(cxlrd, val))
 		return -EINVAL;
 
 	lockdep_assert_held_write(&cxl_rwsem.region);
@@ -2096,6 +2107,8 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
 	int stride;
+	int root_ways = cxlrd->cxlsd.cxld.interleave_ways;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
 
 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
 				  p->interleave_granularity);
@@ -2128,6 +2141,28 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 		return -ENXIO;
 	}
 
+	/* Apply the sysfs granularity rule to auto regions too. */
+	if (!is_ig_allowed(cxlrd, p->interleave_granularity)) {
+		dev_dbg(&cxlr->dev,
+			"ig %d incompatible with root ways %d ig %d\n",
+			p->interleave_granularity, root_ways, root_gran);
+		return -ENXIO;
+	}
+
+	/*
+	 * Selector containment cannot prove span equality for 3-way family
+	 * roots because a 3-way interleave consumes no selector bits.
+	 */
+	if (!is_power_of_2(root_ways) &&
+	    (u64)p->interleave_ways * p->interleave_granularity !=
+	    (u64)root_ways * root_gran) {
+		dev_dbg(&cxlr->dev,
+			"region ways*gran (%d*%d) != root ways*gran (%d*%d)\n",
+			p->interleave_ways, p->interleave_granularity,
+			root_ways, root_gran);
+		return -ENXIO;
+	}
+
 	if (p->nr_targets >= p->interleave_ways) {
 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
 			p->nr_targets);
Signature · 1 line
2.37.3
#18 lore raw

Re: [PATCH v2 4/6] cxl/region: Validate mixed-granularity at sysfs and attach gates

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The function check_interleave_cap() ignores the return values of ways_to_eiw() and granularity_to_eig(), leading to the use of uninitialized stack variables (eiw, eig) in bitmask calculations. --

commit 999caa321a85ce92543a94c3525b252f33aab308
Author: Alison Schofield <[email protected]>

cxl/region: Validate mixed-granularity at sysfs and attach gates

Replace the same-granularity sysfs check with is_ig_allowed() to permit valid mixed-granularity regions. Call this helper at attach time to enforce the same rules for auto regions, and add an explicit span equality check for 3-way-family roots.

Quoted 4 lines
drivers/cxl/core/region.c +0 -0
index 9fff538a9ce16..311b5052bbc8f 100644

[ ... ]

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2114

Quoted 9 lines
drivers/cxl/core/region.c +2 -0
@@ -2096,6 +2107,8 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
 	int stride;
+	int root_ways = cxlrd->cxlsd.cxld.interleave_ways;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
 
 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
 				  p->interleave_granularity);

[Severity: High]
This isn't a bug introduced by this patch, but check_interleave_cap() is called here with potentially uninitialized (zero) values for interleave_ways and interleave_granularity prior to state verification.

Inside check_interleave_cap() in drivers/cxl/core/region.c, ways_to_eiw() and granularity_to_eig() are called but their return values are ignored:

	ways_to_eiw(iw, &eiw);
	if (eiw == 0 || eiw == 8)
		return 0;

	granularity_to_eig(ig, &eig);
	if (eiw > 8)
		high_pos = eiw + eig - 1;
	else
		high_pos = eiw + eig + 7;
	low_pos = eig + 8;
	interleave_mask = GENMASK(high_pos, low_pos);

If interleave_ways and interleave_granularity are 0, both ways_to_eiw() and granularity_to_eig() return -EINVAL, leaving eiw and eig uninitialized. Because eig is an uninitialized u16, low_pos can be as large as 65543.

Passing values greater than 64 to GENMASK() results in an out-of-bounds bitwise shift.

On systems with CONFIG_UBSAN_TRAP enabled, could this lead to a kernel panic if a privileged user misorders sysfs writes?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#19 lore raw

Re: [PATCH v2 4/6] cxl/region: Validate mixed-granularity at sysfs and attach gates

Quoted 57 lines · On Thu, Jun 11, 2026 at 06:03:33PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The function check_interleave_cap() ignores the return values of ways_to_eiw() and granularity_to_eig(), leading to the use of uninitialized stack variables (eiw, eig) in bitmask calculations. --

commit 999caa321a85ce92543a94c3525b252f33aab308
Author: Alison Schofield <[email protected]>

cxl/region: Validate mixed-granularity at sysfs and attach gates

Replace the same-granularity sysfs check with is_ig_allowed() to permit valid mixed-granularity regions. Call this helper at attach time to enforce the same rules for auto regions, and add an explicit span equality check for 3-way-family roots.

Quoted 4 lines
drivers/cxl/core/region.c +0 -0
index 9fff538a9ce16..311b5052bbc8f 100644

[ ... ]

drivers/cxl/core/region.c static int cxl_region_attach(struct cxl_region *cxlr, · line 2114

Quoted 9 lines
drivers/cxl/core/region.c +2 -0
@@ -2096,6 +2107,8 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
 	int stride;
+	int root_ways = cxlrd->cxlsd.cxld.interleave_ways;
+	int root_gran = cxlrd->cxlsd.cxld.interleave_granularity;
 
 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
 				  p->interleave_granularity);

[Severity: High]
This isn't a bug introduced by this patch, but check_interleave_cap() is called here with potentially uninitialized (zero) values for interleave_ways and interleave_granularity prior to state verification.

Inside check_interleave_cap() in drivers/cxl/core/region.c, ways_to_eiw() and granularity_to_eig() are called but their return values are ignored:

	ways_to_eiw(iw, &eiw);
	if (eiw == 0 || eiw == 8)
		return 0;

	granularity_to_eig(ig, &eig);
	if (eiw > 8)
		high_pos = eiw + eig - 1;
	else
		high_pos = eiw + eig + 7;
	low_pos = eig + 8;
	interleave_mask = GENMASK(high_pos, low_pos);

If interleave_ways and interleave_granularity are 0, both ways_to_eiw() and granularity_to_eig() return -EINVAL, leaving eiw and eig uninitialized. Because eig is an uninitialized u16, low_pos can be as large as 65543.

Quoted 5 lines

Passing values greater than 64 to GENMASK() results in an out-of-bounds bitwise shift.

On systems with CONFIG_UBSAN_TRAP enabled, could this lead to a kernel panic if a privileged user misorders sysfs writes?

This issue is already fixed for 7.2, sitting in cxl/next here:

https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git/commit/?h=next&id=661c092f9839

cxl: Align interleave decode/encode helpers with their callers

which initializes the encode helper outputs (*eiw = 0, *eig = 0) before the -EINVAL returns. With that, the iw == 0 case reads a defined eiw == 0 and takes the existing early return, so eig is never consumed and the shift is never reached.

ATM, I'm thinking the v3 of this series will appear rebased on 7.2-rc1 (that has the fix) so nothing to see here.

-- Alison

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#20 lore raw

PATCH v2 5/6 cxl/test: Add a topology to test mixed-granularity regions

In reply to Alison Schofield

Add a cxl_test topology for mixed-granularity region coverage.

modprobe cxl_test mixed_gran_regions=1 creates three host bridges with two switch levels below each bridge and twelve endpoints total. The topology advertises one 2-way root CFMWS for power-of-2 mixed-granularity tests and one 3-way root CFMWS for 3-way-family tests.

NULL-check the legacy topology arrays, which are left empty when mixed_gran_regions=1 selects the new topology.

The default and existing special module params are unaffected.

---
 tools/testing/cxl/test/cxl.c | 503 ++++++++++++++++++++++++++++++++---
 1 file changed, 467 insertions(+), 36 deletions(-)
tools/testing/cxl/test/cxl.c +467 -36
index 296516eecfd6..8938508a2692 100644
@@ -17,6 +17,7 @@
 static int interleave_arithmetic;
 static bool extended_linear_cache;
 static bool fail_autoassemble;
+static bool mixed_gran_regions;
 
 #define FAKE_QTG_ID	42
 
@@ -26,7 +27,26 @@ static bool fail_autoassemble;
 #define NR_CXL_ROOT_PORTS 2
 #define NR_CXL_SWITCH_PORTS 2
 #define NR_CXL_PORT_DECODERS 8
-#define NR_BRIDGES (NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
+
+/*
+ * mixed_gran_regions=1: three host bridges, two switch levels, and
+ * twelve endpoints for power-of-2 and 3-way-family region layouts.
+ */
+#define NR_CXL_MIX_GRAN_HB 3
+#define NR_CXL_MIX_GRAN_ROOT_PORTS NR_CXL_MIX_GRAN_HB	/* 1 per HB */
+#define NR_CXL_MIX_GRAN_L1_DPORTS  (NR_CXL_MIX_GRAN_HB * 2)
+#define NR_CXL_MIX_GRAN_L2_SWITCHES NR_CXL_MIX_GRAN_L1_DPORTS
+#define NR_CXL_MIX_GRAN_L2_DPORTS  (NR_CXL_MIX_GRAN_L2_SWITCHES * 2)
+
+#define NR_DEFAULT_BRIDGES \
+	(NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
+#define NR_BRIDGES (NR_DEFAULT_BRIDGES + NR_CXL_MIX_GRAN_HB)
+
+/* CHBS index ranges within mock_cedt.chbs[] */
+#define CHBS_DEFAULT_START 0
+#define CHBS_DEFAULT_END (NR_DEFAULT_BRIDGES - 1)
+#define CHBS_MIX_GRAN_START NR_DEFAULT_BRIDGES
+#define CHBS_MIX_GRAN_END (NR_BRIDGES - 1)
 
 #define MOCK_AUTO_REGION_SIZE_DEFAULT SZ_512M
 static int mock_auto_region_size = MOCK_AUTO_REGION_SIZE_DEFAULT;
@@ -52,6 +72,15 @@ struct platform_device *cxl_mem_single[NR_MEM_SINGLE];
 static struct platform_device *cxl_rch[NR_CXL_RCH];
 static struct platform_device *cxl_rcd[NR_CXL_RCH];
 
+/* Dedicated mixed-granularity topology */
+static struct platform_device *cxl_mix_gran_hb[NR_CXL_MIX_GRAN_HB];
+static struct platform_device *cxl_mix_gran_root_port[NR_CXL_MIX_GRAN_ROOT_PORTS];
+static struct platform_device *cxl_mix_gran_l1_uport[NR_CXL_MIX_GRAN_ROOT_PORTS];
+static struct platform_device *cxl_mix_gran_l1_dport[NR_CXL_MIX_GRAN_L1_DPORTS];
+static struct platform_device *cxl_mix_gran_l2_uport[NR_CXL_MIX_GRAN_L2_SWITCHES];
+static struct platform_device *cxl_mix_gran_l2_dport[NR_CXL_MIX_GRAN_L2_DPORTS];
+static struct platform_device *cxl_mem_mix_gran[NR_CXL_MIX_GRAN_L2_DPORTS];
+
 /*
  * Decoder registry
  *
@@ -115,6 +144,19 @@ static struct acpi_device host_bridge[NR_BRIDGES] = {
 		.handle = &host_bridge[3],
 		.pnp.unique_id = "3",
 	},
+	/* Dedicated mixed-granularity topology */
+	[4] = {
+		.handle = &host_bridge[4],
+		.pnp.unique_id = "4",
+	},
+	[5] = {
+		.handle = &host_bridge[5],
+		.pnp.unique_id = "5",
+	},
+	[6] = {
+		.handle = &host_bridge[6],
+		.pnp.unique_id = "6",
+	},
 };
 
 static bool is_mock_dev(struct device *dev)
@@ -122,13 +164,16 @@ static bool is_mock_dev(struct device *dev)
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_mem); i++)
-		if (dev == &cxl_mem[i]->dev)
+		if (cxl_mem[i] && dev == &cxl_mem[i]->dev)
 			return true;
 	for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++)
-		if (dev == &cxl_mem_single[i]->dev)
+		if (cxl_mem_single[i] && dev == &cxl_mem_single[i]->dev)
 			return true;
 	for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++)
-		if (dev == &cxl_rcd[i]->dev)
+		if (cxl_rcd[i] && dev == &cxl_rcd[i]->dev)
+			return true;
+	for (i = 0; i < ARRAY_SIZE(cxl_mem_mix_gran); i++)
+		if (cxl_mem_mix_gran[i] && dev == &cxl_mem_mix_gran[i]->dev)
 			return true;
 	if (dev == &cxl_acpi->dev)
 		return true;
@@ -188,6 +233,14 @@ static struct {
 		struct acpi_cedt_cfmws cfmws;
 		u32 target[3];
 	} cfmws8;
+	struct {
+		struct acpi_cedt_cfmws cfmws;
+		u32 target[2];
+	} cfmws9;
+	struct {
+		struct acpi_cedt_cfmws cfmws;
+		u32 target[3];
+	} cfmws10;
 	struct {
 		struct acpi_cedt_cxims cxims;
 		u64 xormap_list[2];
@@ -232,6 +285,31 @@ static struct {
 		.uid = 3,
 		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL11,
 	},
+	/* Dedicated mixed-granularity topology */
+	.chbs[4] = {
+		.header = {
+			.type = ACPI_CEDT_TYPE_CHBS,
+			.length = sizeof(mock_cedt.chbs[0]),
+		},
+		.uid = 4,
+		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
+	},
+	.chbs[5] = {
+		.header = {
+			.type = ACPI_CEDT_TYPE_CHBS,
+			.length = sizeof(mock_cedt.chbs[0]),
+		},
+		.uid = 5,
+		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
+	},
+	.chbs[6] = {
+		.header = {
+			.type = ACPI_CEDT_TYPE_CHBS,
+			.length = sizeof(mock_cedt.chbs[0]),
+		},
+		.uid = 6,
+		.cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
+	},
 	.cfmws0 = {
 		.cfmws = {
 			.header = {
@@ -371,6 +449,36 @@ static struct {
 		},
 		.target = { 0, 1, 2, },
 	},
+	.cfmws9 = {
+		.cfmws = {
+			.header = {
+				.type = ACPI_CEDT_TYPE_CFMWS,
+				.length = sizeof(mock_cedt.cfmws9),
+			},
+			.interleave_ways = 1,
+			.granularity = 4,
+			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
+					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
+			.qtg_id = FAKE_QTG_ID,
+			.window_size = SZ_512M * 4UL,
+		},
+		.target = { 4, 5, },
+	},
+	.cfmws10 = {
+		.cfmws = {
+			.header = {
+				.type = ACPI_CEDT_TYPE_CFMWS,
+				.length = sizeof(mock_cedt.cfmws10),
+			},
+			.interleave_ways = 8,
+			.granularity = 1,
+			.restrictions = ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM |
+					ACPI_CEDT_CFMWS_RESTRICT_PMEM,
+			.qtg_id = FAKE_QTG_ID,
+			.window_size = SZ_512M * 6UL,
+		},
+		.target = { 4, 5, 6, },
+	},
 	.cxims0 = {
 		.cxims = {
 			.header = {
@@ -395,6 +503,9 @@ struct acpi_cedt_cfmws *mock_cfmws[] = {
 	[6] = &mock_cedt.cfmws6.cfmws,
 	[7] = &mock_cedt.cfmws7.cfmws,
 	[8] = &mock_cedt.cfmws8.cfmws,
+	/* Dedicated mixed-granularity topology */
+	[9] = &mock_cedt.cfmws9.cfmws,
+	[10] = &mock_cedt.cfmws10.cfmws,
 };
 
 static int cfmws_start;
@@ -403,6 +514,8 @@ static int cfmws_end;
 #define CFMWS_MOD_ARRAY_END   5
 #define CFMWS_XOR_ARRAY_START 6
 #define CFMWS_XOR_ARRAY_END   8
+#define CFMWS_MIX_GRAN_ARRAY_START 9
+#define CFMWS_MIX_GRAN_ARRAY_END   10
 
 struct acpi_cedt_cxims *mock_cxims[1] = {
 	[0] = &mock_cedt.cxims0.cxims,
@@ -475,9 +588,18 @@ static void cfmws_elc_update(struct acpi_cedt_cfmws *window, int index)
 static int populate_cedt(void)
 {
 	struct cxl_mock_res *res;
+	int chbs_lo, chbs_hi;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
+	if (mixed_gran_regions) {
+		chbs_lo = CHBS_MIX_GRAN_START;
+		chbs_hi = CHBS_MIX_GRAN_END;
+	} else {
+		chbs_lo = CHBS_DEFAULT_START;
+		chbs_hi = CHBS_DEFAULT_END;
+	}
+
+	for (i = chbs_lo; i <= chbs_hi; i++) {
 		struct acpi_cedt_chbs *chbs = &mock_cedt.chbs[i];
 		resource_size_t size;
 
@@ -531,12 +653,23 @@ static int mock_acpi_table_parse_cedt(enum acpi_cedt_type id,
 	if (!is_mock_port(dev) && !is_mock_dev(dev))
 		return acpi_table_parse_cedt(id, handler_arg, arg);
 
-	if (id == ACPI_CEDT_TYPE_CHBS)
-		for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
+	if (id == ACPI_CEDT_TYPE_CHBS) {
+		int chbs_lo, chbs_hi;
+
+		if (mixed_gran_regions) {
+			chbs_lo = CHBS_MIX_GRAN_START;
+			chbs_hi = CHBS_MIX_GRAN_END;
+		} else {
+			chbs_lo = CHBS_DEFAULT_START;
+			chbs_hi = CHBS_DEFAULT_END;
+		}
+
+		for (i = chbs_lo; i <= chbs_hi; i++) {
 			h = (union acpi_subtable_headers *)&mock_cedt.chbs[i];
 			end = (unsigned long)&mock_cedt.chbs[i + 1];
 			handler_arg(h, arg, end);
 		}
+	}
 
 	if (id == ACPI_CEDT_TYPE_CFMWS)
 		for (i = cfmws_start; i <= cfmws_end; i++) {
@@ -560,13 +693,16 @@ static bool is_mock_bridge(struct device *dev)
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
-		if (dev == &cxl_host_bridge[i]->dev)
+		if (cxl_host_bridge[i] && dev == &cxl_host_bridge[i]->dev)
 			return true;
 	for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
-		if (dev == &cxl_hb_single[i]->dev)
+		if (cxl_hb_single[i] && dev == &cxl_hb_single[i]->dev)
 			return true;
 	for (i = 0; i < ARRAY_SIZE(cxl_rch); i++)
-		if (dev == &cxl_rch[i]->dev)
+		if (cxl_rch[i] && dev == &cxl_rch[i]->dev)
+			return true;
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_hb); i++)
+		if (cxl_mix_gran_hb[i] && dev == &cxl_mix_gran_hb[i]->dev)
 			return true;
 
 	return false;
@@ -580,27 +716,52 @@ static bool is_mock_port(struct device *dev)
 		return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++)
-		if (dev == &cxl_root_port[i]->dev)
+		if (cxl_root_port[i] && dev == &cxl_root_port[i]->dev)
 			return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++)
-		if (dev == &cxl_switch_uport[i]->dev)
+		if (cxl_switch_uport[i] && dev == &cxl_switch_uport[i]->dev)
 			return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++)
-		if (dev == &cxl_switch_dport[i]->dev)
+		if (cxl_switch_dport[i] && dev == &cxl_switch_dport[i]->dev)
 			return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++)
-		if (dev == &cxl_root_single[i]->dev)
+		if (cxl_root_single[i] && dev == &cxl_root_single[i]->dev)
 			return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++)
-		if (dev == &cxl_swu_single[i]->dev)
+		if (cxl_swu_single[i] && dev == &cxl_swu_single[i]->dev)
 			return true;
 
 	for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++)
-		if (dev == &cxl_swd_single[i]->dev)
+		if (cxl_swd_single[i] && dev == &cxl_swd_single[i]->dev)
+			return true;
+
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_root_port); i++)
+		if (cxl_mix_gran_root_port[i] &&
+		    dev == &cxl_mix_gran_root_port[i]->dev)
+			return true;
+
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_l1_uport); i++)
+		if (cxl_mix_gran_l1_uport[i] &&
+		    dev == &cxl_mix_gran_l1_uport[i]->dev)
+			return true;
+
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_l1_dport); i++)
+		if (cxl_mix_gran_l1_dport[i] &&
+		    dev == &cxl_mix_gran_l1_dport[i]->dev)
+			return true;
+
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_l2_uport); i++)
+		if (cxl_mix_gran_l2_uport[i] &&
+		    dev == &cxl_mix_gran_l2_uport[i]->dev)
+			return true;
+
+	for (i = 0; i < ARRAY_SIZE(cxl_mix_gran_l2_dport); i++)
+		if (cxl_mix_gran_l2_dport[i] &&
+		    dev == &cxl_mix_gran_l2_dport[i]->dev)
 			return true;
 
 	if (is_cxl_memdev(dev))
@@ -671,7 +832,15 @@ static struct acpi_pci_root mock_pci_root[ARRAY_SIZE(mock_pci_bus)] = {
 	[3] = {
 		.bus = &mock_pci_bus[3],
 	},
-
+	[4] = {
+		.bus = &mock_pci_bus[4],
+	},
+	[5] = {
+		.bus = &mock_pci_bus[5],
+	},
+	[6] = {
+		.bus = &mock_pci_bus[6],
+	},
 };
 
 static bool is_mock_bus(struct pci_bus *bus)
@@ -1092,7 +1261,8 @@ static bool mock_init_hdm_decoder(struct cxl_decoder *cxld)
 		/* check is endpoint is attach to host-bridge0 */
 		port = cxled_to_port(cxled);
 		do {
-			if (port->uport_dev == &cxl_host_bridge[0]->dev) {
+			if (cxl_host_bridge[0] &&
+			    port->uport_dev == &cxl_host_bridge[0]->dev) {
 				hb0 = true;
 				break;
 			}
@@ -1229,6 +1399,8 @@ static int mock_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
 
 	if (is_cxl_endpoint(port))
 		target_count = 0;
+	else if (mixed_gran_regions && is_cxl_root(parent_port))
+		target_count = NR_CXL_MIX_GRAN_ROOT_PORTS / NR_CXL_MIX_GRAN_HB;
 	else if (is_cxl_root(parent_port))
 		target_count = NR_CXL_ROOT_PORTS;
 	else
@@ -1328,6 +1500,27 @@ static int get_port_array(struct cxl_port *port,
 	struct platform_device **array;
 	int array_size;
 
+	if (mixed_gran_regions) {
+		if (port->depth == 1) {
+			array_size = ARRAY_SIZE(cxl_mix_gran_root_port);
+			array = cxl_mix_gran_root_port;
+		} else if (port->depth == 2) {
+			array_size = ARRAY_SIZE(cxl_mix_gran_l1_dport);
+			array = cxl_mix_gran_l1_dport;
+		} else if (port->depth == 3) {
+			array_size = ARRAY_SIZE(cxl_mix_gran_l2_dport);
+			array = cxl_mix_gran_l2_dport;
+		} else {
+			dev_WARN_ONCE(&port->dev, 1, "unexpected depth %d\n",
+				      port->depth);
+			return -ENXIO;
+		}
+
+		*port_array = array;
+		*port_array_size = array_size;
+		return 0;
+	}
+
 	if (port->depth == 1) {
 		if (is_multi_bridge(port->uport_dev)) {
 			array_size = ARRAY_SIZE(cxl_root_port);
@@ -1711,10 +1904,190 @@ static void cxl_single_topo_exit(void)
 	}
 }
 
+#define MIX_GRAN_RP_ID_BASE	(NR_MULTI_ROOT + NR_CXL_SINGLE_HOST)
+#define MIX_GRAN_L1U_ID_BASE	MIX_GRAN_RP_ID_BASE
+#define MIX_GRAN_L1D_ID_BASE	(NR_MEM_MULTI + NR_MEM_SINGLE)
+#define MIX_GRAN_L2U_ID_BASE	MIX_GRAN_L1D_ID_BASE
+#define MIX_GRAN_L2D_ID_BASE	\
+	(MIX_GRAN_L1D_ID_BASE + NR_CXL_MIX_GRAN_L1_DPORTS)
+#define MIX_GRAN_MEM_ID_BASE	(NR_MEM_MULTI + NR_MEM_SINGLE + NR_CXL_RCH)
+
+static void cxl_mix_gran_topo_exit(void)
+{
+	int i;
+
+	for (i = NR_CXL_MIX_GRAN_L2_DPORTS - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mix_gran_l2_dport[i]);
+	for (i = NR_CXL_MIX_GRAN_L2_SWITCHES - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mix_gran_l2_uport[i]);
+	for (i = NR_CXL_MIX_GRAN_L1_DPORTS - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mix_gran_l1_dport[i]);
+	for (i = NR_CXL_MIX_GRAN_ROOT_PORTS - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mix_gran_l1_uport[i]);
+	for (i = NR_CXL_MIX_GRAN_ROOT_PORTS - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mix_gran_root_port[i]);
+	for (i = NR_CXL_MIX_GRAN_HB - 1; i >= 0; i--) {
+		struct platform_device *pdev = cxl_mix_gran_hb[i];
+
+		if (!pdev)
+			continue;
+		sysfs_remove_link(&pdev->dev.kobj, "physical_node");
+		platform_device_unregister(pdev);
+	}
+}
+
+static __init int cxl_mix_gran_topo_init(void)
+{
+	int i, rc;
+
+	/* 3 host bridges */
+	for (i = 0; i < NR_CXL_MIX_GRAN_HB; i++) {
+		struct acpi_device *adev = &host_bridge[NR_DEFAULT_BRIDGES + i];
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_host_bridge",
+					     NR_DEFAULT_BRIDGES + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+
+		mock_companion(adev, &pdev->dev);
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+
+		cxl_mix_gran_hb[i] = pdev;
+		mock_pci_bus[NR_DEFAULT_BRIDGES + i].bridge = &pdev->dev;
+		rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
+				       "physical_node");
+		if (rc)
+			goto err;
+	}
+
+	/* 1 root port per host bridge */
+	for (i = 0; i < NR_CXL_MIX_GRAN_ROOT_PORTS; i++) {
+		struct platform_device *bridge = cxl_mix_gran_hb[i];
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_root_port",
+					     MIX_GRAN_RP_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &bridge->dev;
+
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+		cxl_mix_gran_root_port[i] = pdev;
+	}
+
+	/* L1 switch uport under each root port */
+	for (i = 0; i < NR_CXL_MIX_GRAN_ROOT_PORTS; i++) {
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_switch_uport",
+					     MIX_GRAN_L1U_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &cxl_mix_gran_root_port[i]->dev;
+
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+		cxl_mix_gran_l1_uport[i] = pdev;
+	}
+
+	/* 2 L1 dports per L1 switch */
+	for (i = 0; i < NR_CXL_MIX_GRAN_L1_DPORTS; i++) {
+		struct platform_device *uport =
+			cxl_mix_gran_l1_uport[i / NR_CXL_SWITCH_PORTS];
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_switch_dport",
+					     MIX_GRAN_L1D_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &uport->dev;
+
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+		cxl_mix_gran_l1_dport[i] = pdev;
+	}
+
+	/* L2 switch uport under each L1 dport */
+	for (i = 0; i < NR_CXL_MIX_GRAN_L2_SWITCHES; i++) {
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_switch_uport",
+					     MIX_GRAN_L2U_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &cxl_mix_gran_l1_dport[i]->dev;
+
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+		cxl_mix_gran_l2_uport[i] = pdev;
+	}
+
+	/* 2 L2 dports per L2 switch */
+	for (i = 0; i < NR_CXL_MIX_GRAN_L2_DPORTS; i++) {
+		struct platform_device *uport =
+			cxl_mix_gran_l2_uport[i / NR_CXL_SWITCH_PORTS];
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_switch_dport",
+					     MIX_GRAN_L2D_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &uport->dev;
+
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			goto err;
+		}
+		cxl_mix_gran_l2_dport[i] = pdev;
+	}
+
+	return 0;
+err:
+	cxl_mix_gran_topo_exit();
+	return rc;
+}
+
 static void cxl_mem_exit(void)
 {
 	int i;
 
+	if (mixed_gran_regions) {
+		for (i = NR_CXL_MIX_GRAN_L2_DPORTS - 1; i >= 0; i--)
+			platform_device_unregister(cxl_mem_mix_gran[i]);
+		return;
+	}
+
 	for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
 		platform_device_unregister(cxl_rcd[i]);
 	for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
@@ -1723,10 +2096,46 @@ static void cxl_mem_exit(void)
 		platform_device_unregister(cxl_mem[i]);
 }
 
+static int cxl_mem_mix_gran_init(void)
+{
+	int i, rc;
+
+	for (i = 0; i < NR_CXL_MIX_GRAN_L2_DPORTS; i++) {
+		struct platform_device *dport = cxl_mix_gran_l2_dport[i];
+		struct platform_device *pdev;
+
+		pdev = platform_device_alloc("cxl_mem",
+					     MIX_GRAN_MEM_ID_BASE + i);
+		if (!pdev) {
+			rc = -ENOMEM;
+			goto err;
+		}
+		pdev->dev.parent = &dport->dev;
+		set_dev_node(&pdev->dev, i % 2);
+
+		cxl_mem_mix_gran[i] = pdev;
+		rc = platform_device_add(pdev);
+		if (rc) {
+			platform_device_put(pdev);
+			cxl_mem_mix_gran[i] = NULL;
+			goto err;
+		}
+	}
+
+	return 0;
+err:
+	for (i = NR_CXL_MIX_GRAN_L2_DPORTS - 1; i >= 0; i--)
+		platform_device_unregister(cxl_mem_mix_gran[i]);
+	return rc;
+}
+
 static int cxl_mem_init(void)
 {
 	int i, rc;
 
+	if (mixed_gran_regions)
+		return cxl_mem_mix_gran_init();
+
 	for (i = 0; i < ARRAY_SIZE(cxl_mem); i++) {
 		struct platform_device *dport = cxl_switch_dport[i];
 		struct platform_device *pdev;
@@ -1841,7 +2250,10 @@ static __init int cxl_test_init(void)
 	if (rc)
 		goto err_gen_pool_add;
 
-	if (interleave_arithmetic == 1) {
+	if (mixed_gran_regions) {
+		cfmws_start = CFMWS_MIX_GRAN_ARRAY_START;
+		cfmws_end = CFMWS_MIX_GRAN_ARRAY_END;
+	} else if (interleave_arithmetic == 1) {
 		cfmws_start = CFMWS_XOR_ARRAY_START;
 		cfmws_end = CFMWS_XOR_ARRAY_END;
 	} else {
@@ -1853,6 +2265,13 @@ static __init int cxl_test_init(void)
 	if (rc)
 		goto err_populate;
 
+	if (mixed_gran_regions) {
+		rc = cxl_mix_gran_topo_init();
+		if (rc)
+			goto err_populate;
+		goto topo_done;
+	}
+
 	for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++) {
 		struct acpi_device *adev = &host_bridge[i];
 		struct platform_device *pdev;
@@ -1926,9 +2345,10 @@ static __init int cxl_test_init(void)
 	if (rc)
 		goto err_single;
 
+topo_done:
 	cxl_acpi = platform_device_alloc("cxl_acpi", 0);
 	if (!cxl_acpi)
-		goto err_rch;
+		goto err_topo;
 
 	mock_companion(&acpi0017_mock, &cxl_acpi->dev);
 	acpi0017_mock.dev.bus = &platform_bus_type;
@@ -1936,7 +2356,7 @@ static __init int cxl_test_init(void)
 
 	rc = cxl_mock_platform_device_add(cxl_acpi, NULL);
 	if (rc)
-		goto err_rch;
+		goto err_topo;
 
 	rc = cxl_mem_init();
 	if (rc)
@@ -1952,7 +2372,11 @@ static __init int cxl_test_init(void)
 	cxl_mem_exit();
 err_root:
 	platform_device_put(cxl_acpi);
-err_rch:
+err_topo:
+	if (mixed_gran_regions) {
+		cxl_mix_gran_topo_exit();
+		goto err_populate;
+	}
 	cxl_rch_topo_exit();
 err_single:
 	cxl_single_topo_exit();
@@ -2001,21 +2425,25 @@ static __exit void cxl_test_exit(void)
 	hmem_test_exit();
 	cxl_mem_exit();
 	platform_device_unregister(cxl_acpi);
-	cxl_rch_topo_exit();
-	cxl_single_topo_exit();
-	for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
-		platform_device_unregister(cxl_switch_dport[i]);
-	for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
-		platform_device_unregister(cxl_switch_uport[i]);
-	for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
-		platform_device_unregister(cxl_root_port[i]);
-	for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
-		struct platform_device *pdev = cxl_host_bridge[i];
+	if (mixed_gran_regions) {
+		cxl_mix_gran_topo_exit();
+	} else {
+		cxl_rch_topo_exit();
+		cxl_single_topo_exit();
+		for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
+			platform_device_unregister(cxl_switch_dport[i]);
+		for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
+			platform_device_unregister(cxl_switch_uport[i]);
+		for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
+			platform_device_unregister(cxl_root_port[i]);
+		for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
+			struct platform_device *pdev = cxl_host_bridge[i];
 
-		if (!pdev)
-			continue;
-		sysfs_remove_link(&pdev->dev.kobj, "physical_node");
-		platform_device_unregister(cxl_host_bridge[i]);
+			if (!pdev)
+				continue;
+			sysfs_remove_link(&pdev->dev.kobj, "physical_node");
+			platform_device_unregister(cxl_host_bridge[i]);
+		}
 	}
 	depopulate_all_mock_resources();
 	gen_pool_destroy(cxl_mock_pool);
@@ -2030,6 +2458,9 @@ module_param(extended_linear_cache, bool, 0444);
 MODULE_PARM_DESC(extended_linear_cache, "Enable extended linear cache support");
 module_param(fail_autoassemble, bool, 0444);
 MODULE_PARM_DESC(fail_autoassemble, "Simulate missing member of an auto-region");
+module_param(mixed_gran_regions, bool, 0444);
+MODULE_PARM_DESC(mixed_gran_regions,
+		 "Topology supporting mixed granularity regions");
 module_init(cxl_test_init);
 module_exit(cxl_test_exit);
 MODULE_LICENSE("GPL v2");
Signature · 1 line
2.37.3
author maintainer
#21 lore raw

PATCH v2 6/6 Documentation/cxl: Add region granularity and multi-level interleave guide

In reply to Alison Schofield

Document how region and root decoder granularities interact for same-granularity, power-of-2 mixed-granularity, and 3-way-family interleaves.

Explain the selector-bit model used by the CXL specification, the legal layouts that result from it, the additional span constraint required by 3-way-family roots, and how the Linux driver validates those rules for both auto and user-created regions.

Assisted-by Claude:Opus-4.7
---
 Documentation/driver-api/cxl/index.rst        |   1 +
 .../cxl/linux/region-granularity.rst          | 486 ++++++++++++++++++
 2 files changed, 487 insertions(+)
 create mode 100644 Documentation/driver-api/cxl/linux/region-granularity.rst
Documentation/driver-api/cxl/index.rst +1 -0
index 3dfae1d310ca..1ae193ea4020 100644
@@ -42,6 +42,7 @@ that have impacts on each other.  The docs here break up configurations steps.
    linux/dax-driver
    linux/memory-hotplug
    linux/access-coordinates
+   linux/region-granularity
 
 .. toctree::
    :maxdepth: 2
Documentation/driver-api/cxl/linux/region-granularity.rst new file +486 -0

486 lines not on this page · raw message

Signature · 1 line
2.37.3
#22 lore raw

Re: [PATCH v2 6/6] Documentation/cxl: Add region granularity and multi-level interleave guide

Quoted 10 lines · Alison Schofield

Document how region and root decoder granularities interact for same-granularity, power-of-2 mixed-granularity, and 3-way-family interleaves.

Explain the selector-bit model used by the CXL specification, the legal layouts that result from it, the additional span constraint required by 3-way-family roots, and how the Linux driver validates those rules for both auto and user-created regions.

Assisted-by Claude:Opus-4.7

We should drop most of the documentation as this just rewords the specification. We should have a single point of source which should be the spec. We should cite and refer to the spec to explain details.

Quoted 30 lines
---
 Documentation/driver-api/cxl/index.rst        |   1 +
 .../cxl/linux/region-granularity.rst          | 486 ++++++++++++++++++
 2 files changed, 487 insertions(+)
 create mode 100644 Documentation/driver-api/cxl/linux/region-granularity.rst
Documentation/driver-api/cxl/index.rst +1 -0
index 3dfae1d310ca..1ae193ea4020 100644
@@ -42,6 +42,7 @@ that have impacts on each other.  The docs here break up configurations steps.
    linux/dax-driver
    linux/memory-hotplug
    linux/access-coordinates
+   linux/region-granularity
 
 .. toctree::
    :maxdepth: 2
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
new file mode 100644
index 000000000000..7d0d12e6e2bd
@@ -0,0 +1,486 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================================
+Region Granularity and Multi-Level Interleave

Documentation/driver-api/cxl/linux/region-granularity.rst line 14

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+==============================================
+
+CXL memory regions stripe accesses across one or more endpoints by
+routing transactions through a tree of decoders: a root decoder, zero
+or more switch decoders, and an endpoint decoder at each leaf. Each
+decoder in that path uses a contiguous range of HPA address bits to
+pick its target. This document describes how those bit ranges combine
+across levels, the constraints CXL Spec 4.0 places on legal
+combinations, and how the Linux CXL driver validates and programs
+them.

This suggest that cxl spec 4.0 introduces that constraints, but it doesn't. In fact, 9.13.1.1 just documents the combinations of interleaving levels possible with 12-way, 6-way, and 3-way configs which results from factorization of the ways.

Quoted 42 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +42 -0
+
+The relevant spec material is CXL Spec 4.0 Section 9.13.1, especially
+Section 9.13.1.1 ("Legal Interleaving Configurations") and CXL Spec
+4.0 Tables 9-6, 9-7, and 9-8. CXL Spec 4.0 Figure 9-17 (two-level)
+and Figure 9-18 (three-level) illustrate the bit-routing intuition
+this document builds on.
+
+
+HPA Selector Bits
+=================
+
+For a single interleave level with ``W`` ways and granularity ``G``
+bytes, the **selector bits** are the ``log2(W)`` HPA bits the decoder
+inspects to choose among its ``W`` targets. The lowest of those bits
+sits at position ``log2(G)``, so the selector spans
+``[log2(G) + log2(W) - 1 : log2(G)]``. Equivalently, the bitmask is
+``(W - 1) * G``.
+
+A few examples make the geometry concrete::
+
+   2-way at  256B  ->  selector bit 8         (one bit)
+   4-way at  1KB   ->  selector bits[11:10]   (two bits)
+   2-way at  4KB   ->  selector bit 12        (one bit)
+   8-way at  1KB   ->  selector bits[12:10]   (three bits)
+
+Throughout this document "inner bit" means a lower-numbered HPA bit
+(smaller stride, faster cycling among targets) and "outer bit" means
+a higher-numbered HPA bit (larger stride, slower cycling). HPA[8] is
+the innermost legal CXL selector bit; HPA[14] is the outermost.
+
+
+The Multi-Level Rule
+====================
+
+When a region's interleave is split across multiple decoder levels,
+CXL Spec 4.0 Section 9.13.1 requires that:
+
+   "all the levels use different, but consecutive, HPA bits to
+   select the target and no Interleave Set has more than eight
+   devices."
+
+Two requirements live in that sentence:

Documentation/driver-api/cxl/linux/region-granularity.rst line 66

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+
+* **Disjoint** - each level's selector bits must not overlap any
+  other level's bits.
+* **Consecutive** - the union of all level selectors must be a single
+  contiguous run of HPA bits with no gap.
+
+The union of level selectors is the **region selector**, and it must
+equal ``(region_ways - 1) * region_gran``. Because that mask is itself
+a contiguous run by construction, the levels just need to partition
+it cleanly.

The region also includes cross-hostbridge interleaving (described in CFMWS). The region ways could include a factor of 3. For the selector only the 2 factor parts are taken and the factor of 3 removed before. The region selector then includes the cross-hostbridge selectors too.

Below, you refer to root decoder and Cross-HB interleaving. Same as "region", the definition of it must be described in detail as the term "root" is ambiguous (cxl root, root decoder, root port, root complex, etc.).

It could be added that the endpoints are finally programmed with the region's interleaving config (ways and granularity).

Quoted 72 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +72 -0
+
+The seven legal interleave granularity values split into two groups
+(see CXL Spec 4.0 Section 9.13.1 and Table 8-116):
+
+* **Group 1** - 256B, 512B, 1024B, 2048B (interleaving on HPA[8..11])
+* **Group 2** - 4096B, 8192B, 16384B (interleaving on HPA[12..14])
+
+CXL host bridges and switches must support every value in both
+groups. CXL memory devices must support at least one group, advertised
+through their HDM Decoder Capability register.
+
+
+Same-Granularity Interleave
+===========================
+
+The simplest case is ``region_gran == root_gran``. Each level's
+granularity equals the next inner level's IG times that level's
+ways. The region selector bits stack inside-out from the root.
+
+Example: 4-way region on a 2-way root, 256B everywhere::
+
+   Level    Ways   Granularity   Selector bits
+   ------   ----   -----------   -------------
+   Root     2      256B          bit 8
+   Switch   2      512B          bit 9
+   Region   4      256B          bits[9:8]
+
+Each level's IG sits one bit position above the next inner level's
+selector. The region's effective selector spans ``[9:8]``, two bits,
+matching ``(4 - 1) * 256``.
+
+
+Mixed-Granularity Interleave (Power-of-2)
+=========================================
+
+A region's granularity may be **less than** the root decoder's
+granularity. In that case the root claims an outer selector bit and
+the switches beneath it claim inner bits. The arrangement is legal
+whenever both granularities are powers of 2 and the resulting bits
+remain disjoint and consecutive.
+
+Two-Level Example (CXL Spec 4.0 Figure 9-17)
+--------------------------------------------
+
+8-way region, 2-way root at 4KB, 4-way switch at 1KB::
+
+   Level    Ways   Granularity   Selector bits
+   ------   ----   -----------   -------------
+   Root     2      4KB           bit 12
+   Switch   4      1KB           bits[11:10]
+   Region   8      1KB           bits[12:10]
+
+The host bridge bisects the address range at 4KB granularity, sending
+one half to each root port. Beneath each root port a 4-way switch
+splits its half into four 1KB chunks. To each endpoint the region
+appears as an 8-way interleave at 1KB based on bits[12:10]; the
+leftmost endpoint receives 1KB chunks at HPA bases 0, 8KB, 16KB, ...
+
+Three-Level Example (CXL Spec 4.0 Figure 9-18)
+----------------------------------------------
+
+8-way region across cross-HB / HB / switch, all 2-way::
+
+   Level         Ways   Granularity   Selector bits
+   -----------   ----   -----------   -------------
+   Cross-HB      2      4KB           bit 12
+   Host bridge   2      2KB           bit 11
+   Switch        2      1KB           bit 10
+   Region        8      1KB           bits[12:10]
+
+Three levels of 2-way interleave each contribute one bit. Total
+endpoints: ``2 * 2 * 2 = 8``. Selector union:

Documentation/driver-api/cxl/linux/region-granularity.rst line 148

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+``bit 12 | bit 11 | bit 10`` = ``bits[12:10]``. Disjoint and
+consecutive.
+
+General Power-of-2 Rules
+------------------------
+
+For power-of-2 ways at every level:
+
+* ``region_gran <= root_gran`` is allowed; ``region_gran > root_gran``
+  is not (the root's selector would land inside one region stride).

It is unclear what 'root_gran' refers to. I think you mean the root decoder's (cross-HB) gran here (from CFMWS).

Since region gran is the result of the union of all levels, it is always the smalles gran within all levels.

Documentation/driver-api/cxl/linux/region-granularity.rst line 152

Quoted 4 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+* Both granularities must be powers of 2.
+* The ratio ``root_gran / region_gran`` is the number of inner
+  position slots packed beneath one root target. Endpoint position is
+  partitioned by root target rather than interleaved across them.

This does not give any hint of the bits used for the selector. Each level has an individual set of ways and granularities. There are no dependencies. The ratio cannot be used for position and selector calculation.

Documentation/driver-api/cxl/linux/region-granularity.rst line 156

Quoted 4 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+* Mixed-granularity is always optional for power-of-2 topologies. A
+  same-granularity arrangement always exists for the same set of
+  endpoints. Firmware may prefer mixed-granularity to expose a finer
+  switch-level interleave to the CPU.

Why distinguish between Mixed- and same-granularity? Same-granularity is only a subset.

Documentation/driver-api/cxl/linux/region-granularity.rst line 160

Quoted 4 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+
+
+3-Way Family Interleave
+=======================

Same here, this 3-way part is already documented in the spec, I don't see much sense to rewrite it here.

The whole point on 3-way familiy configs is how the HPA translates to the endpoint DPA and how the endpoint is selected:

HPA: <upper_hpa_part><selector_bits><granularity_bits>

base address: <upper_hpa_part><zero_bits>

In 3-way family configs the base addr (upper_hpa_part) is divided by 3 to determine the DPA, the remaining lower bits form the selector up to the gran size. Think of prime factorization of the base address, it will contain at least one factor of 3 and (sel + gran bits) nums of a factor of 2. The selector bits also form the endpoint positions below each host bridge.

Documentation/driver-api/cxl/linux/region-granularity.rst line 372

Quoted 2 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +2 -0
+Linux Driver Implementation
+===========================

See my comments on the patches, a rework will simplify this doc too.

Imo, just add comments to the code where needed.

Quoted 6 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +6 -0
+
+Region geometry validation lives in ``drivers/cxl/core/region.c``.
+The helpers and enforcement points below implement the rules
+described in the preceding sections.
+
+``get_selector(ways, gran)``

Documentation/driver-api/cxl/linux/region-granularity.rst line 388

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+  Each interleave level consumes a contiguous range of HPA bits to
+  pick its target. ``get_selector()`` models that range as a bitmask:
+  ``(ways - 1) * gran`` for power-of-2 interleaves. A strict 3-way
+  interleave consumes no HPA bits and the helper returns zero;
+  6-way and 12-way are folded to expose only their power-of-2
+  component.
+
+  Kernel-doc: :c:func:`get_selector`.
+
+``root_pos_stride(cxlr)``

I rather would like to not introduce the term "stride" here. This only adds confusion. E.g. the root decoder selector could be used instead.

Quoted 33 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +33 -0
+  When ``region_gran < root_gran``, each root target owns more than
+  one region position. The number of positions packed under one root
+  target is ``root_gran / region_gran``: the root advances by
+  ``root_gran`` bytes per target, while region positions advance by
+  ``region_gran`` bytes. Same-granularity regions have a stride of
+  one because the root advances on every region position.
+
+  ``root_pos_stride()`` returns this ratio for the position walk and
+  the peer-distance computation. The same value applies to 3-way
+  family roots: in value space they advance by ``root_gran`` per
+  target whether or not they consume HPA selector bits.
+
+  Kernel-doc: :c:func:`root_pos_stride`.
+
+``is_ig_allowed(cxlrd, ig)``
+  The region granularity must not exceed the root granularity.
+  ``region_gran > root_gran`` would place the root selector inside a
+  single region stride, leaving the root no address bits to choose
+  among its targets. ``region_gran <= root_gran`` is required for
+  every legal layout.
+
+  ``is_ig_allowed()`` enforces that geometric rule. It returns true
+  when the root is not interleaving, or when ``ig`` does not exceed
+  the root granularity. Endpoint capability (the IG groups a memdev
+  advertises) is checked separately at attach time via
+  ``check_interleave_cap()``.
+
+  Kernel-doc: :c:func:`is_ig_allowed`.
+
+``cxl_port_setup_targets()``
+  This is the per-port enforcement point for the multi-level rule.
+  For each level between the current port and the root the function:
+

Documentation/driver-api/cxl/linux/region-granularity.rst line 431

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+  * Rejects overlap. Two levels that share a selector bit fail the
+    disjoint requirement.
+  * Rejects escapes. The accumulated selector must fit within the
+    region selector. A level whose claimed bits land outside the
+    region selector fails the containment requirement.
+  * Derives the granularity to program at the current port for
+    user-created regions. Each interleaving level claims the lowest
+    selector bit still available within the region selector;
+    passthrough levels consume no selector bit and inherit a scaled
+    granularity from their ancestors.

Fine with me, but all this is simple bit mask logic. Just comment the code and use helper functions?

Documentation/driver-api/cxl/linux/region-granularity.rst line 434

Quoted 3 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +3 -0
+
+  Auto regions reuse the same selector checks but trust the
+  granularity firmware already programmed at each level.

Never trust firmware. :-)

Quoted 7 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +7 -0
+
+``cxl_region_attach()``
+  Pre-flight gates that run once per region attach, before the
+  per-port selector walk.
+
+  * Auto regions bypass the sysfs granularity gate, so
+    ``is_ig_allowed()`` is applied here to give them the same

Documentation/driver-api/cxl/linux/region-granularity.rst line 451

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+    geometric check as user-created regions.
+  * 3-way-family roots require an explicit span check. A strict
+    3-way root consumes no HPA selector bits, so selector containment
+    cannot prove that the root and region cover the same address
+    span. The span identity
+    ``region_ways * region_gran == root_ways * root_gran`` restores
+    that constraint. Power-of-2 roots get the equivalent constraint
+    from selector containment and are not affected by this check.
+    See `Span Identity for 3-Way-Family Roots`_ for the full
+    derivation.

Same here, add it as comments to the code.

Quoted 25 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +25 -0
+
+Position calculations use ``root_pos_stride()`` so that both
+same-granularity and mixed-granularity regions produce the correct
+endpoint ordering. The position walk itself lives in
+``cxl_calc_interleave_pos()``.
+
+Kernel-doc: :c:func:`cxl_calc_interleave_pos`.
+
+
+Auto vs User-Created Regions
+============================
+
+**Auto regions** are reconstructed at boot from the decoder values
+firmware programmed. The driver reads each decoder's ``ways`` and
+``granularity``, runs them through the selector walk, and accepts
+the configuration if it passes the disjoint and containment checks
+(plus the span identity for 3-way roots). Firmware is the source of
+truth for the granularity at each level.
+
+**User-created regions** specify ``interleave_ways`` and
+``interleave_granularity`` through sysfs; the driver derives each
+intermediate decoder's granularity. The sysfs path runs
+``is_ig_allowed()`` at granularity-write time, ``set_interleave_ways()``
+at ways-write time, and the full selector walk when the region is
+attached and committed.

Documentation/driver-api/cxl/linux/region-granularity.rst line 486

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+
+Both paths converge on the same geometry validation rules.
+Differences between the paths are limited to how region parameters
+are obtained and when they are validated.
+
+* Whether each intermediate decoder's granularity is read from
+  hardware (auto) or derived (user).
+* Whether the membership of the region is established by walking
+  firmware-programmed decoders (auto) or by user writes to ``targetN``
+  attributes (user).

This could be condensed to:

auto: config is read from decoder registers
user: sysfs is used to setup decoder registers

-Robert

Quoted 3 lines
Signature · 1 line
2.37.3
author maintainer
#23 lore raw

Re: [PATCH v2 6/6] Documentation/cxl: Add region granularity and multi-level interleave guide

Quoted 10 lines · Robert Richter
Quoted 9 lines · Alison Schofield

Document how region and root decoder granularities interact for same-granularity, power-of-2 mixed-granularity, and 3-way-family interleaves.

Explain the selector-bit model used by the CXL specification, the legal layouts that result from it, the additional span constraint required by 3-way-family roots, and how the Linux driver validates those rules for both auto and user-created regions.

Quoted 5 lines
Quoted 1 line
Assisted-by Claude:Opus-4.7

We should drop most of the documentation as this just rewords the specification. We should have a single point of source which should be the spec. We should cite and refer to the spec to explain details.

Hi Robert,

The factual corrections below are all fixed for v3, thanks, the spec-4.0 wording, the undefined terms, and the root ambiguity in particular needed it.

On reducing the documentation overall: I'm going to review it for content before deciding structure. Some of it restates the spec and can likely go; some of it is driver behavior the spec doesn't cover, what the driver validates, what it rejects, the auto/user difference as a region-creator sees it, and that needs a home. I need to do another sort and rethinking before promising what stays vs goes.

More inline below....

-- Alison

Quoted 38 lines
Quoted 38 lines
---
 Documentation/driver-api/cxl/index.rst        |   1 +
 .../cxl/linux/region-granularity.rst          | 486 ++++++++++++++++++
 2 files changed, 487 insertions(+)
 create mode 100644 Documentation/driver-api/cxl/linux/region-granularity.rst
Documentation/driver-api/cxl/index.rst +1 -0
index 3dfae1d310ca..1ae193ea4020 100644
@@ -42,6 +42,7 @@ that have impacts on each other.  The docs here break up configurations steps.
    linux/dax-driver
    linux/memory-hotplug
    linux/access-coordinates
+   linux/region-granularity
 
 .. toctree::
    :maxdepth: 2
Documentation/driver-api/cxl/linux/region-granularity.rst new file +11 -0
new file mode 100644
index 000000000000..7d0d12e6e2bd
@@ -0,0 +1,486 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================================
+Region Granularity and Multi-Level Interleave
+==============================================
+
+CXL memory regions stripe accesses across one or more endpoints by
+routing transactions through a tree of decoders: a root decoder, zero
+or more switch decoders, and an endpoint decoder at each leaf. Each
+decoder in that path uses a contiguous range of HPA address bits to
+pick its target. This document describes how those bit ranges combine
Quoted 9 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 14

Quoted 3 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +3 -0
+across levels, the constraints CXL Spec 4.0 places on legal
+combinations, and how the Linux CXL driver validates and programs
+them.

This suggest that cxl spec 4.0 introduces that constraints, but it doesn't. In fact, 9.13.1.1 just documents the combinations of interleaving levels possible with 12-way, 6-way, and 3-way configs which results from factorization of the ways.

Right, the wording overstated it. Fixed to make clear the constraints follow from the selector geometry and 9.13.1.1 enumerates the resulting legal combinations.

Quoted 58 lines
Quoted 42 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +42 -0
+
+The relevant spec material is CXL Spec 4.0 Section 9.13.1, especially
+Section 9.13.1.1 ("Legal Interleaving Configurations") and CXL Spec
+4.0 Tables 9-6, 9-7, and 9-8. CXL Spec 4.0 Figure 9-17 (two-level)
+and Figure 9-18 (three-level) illustrate the bit-routing intuition
+this document builds on.
+
+
+HPA Selector Bits
+=================
+
+For a single interleave level with ``W`` ways and granularity ``G``
+bytes, the **selector bits** are the ``log2(W)`` HPA bits the decoder
+inspects to choose among its ``W`` targets. The lowest of those bits
+sits at position ``log2(G)``, so the selector spans
+``[log2(G) + log2(W) - 1 : log2(G)]``. Equivalently, the bitmask is
+``(W - 1) * G``.
+
+A few examples make the geometry concrete::
+
+   2-way at  256B  ->  selector bit 8         (one bit)
+   4-way at  1KB   ->  selector bits[11:10]   (two bits)
+   2-way at  4KB   ->  selector bit 12        (one bit)
+   8-way at  1KB   ->  selector bits[12:10]   (three bits)
+
+Throughout this document "inner bit" means a lower-numbered HPA bit
+(smaller stride, faster cycling among targets) and "outer bit" means
+a higher-numbered HPA bit (larger stride, slower cycling). HPA[8] is
+the innermost legal CXL selector bit; HPA[14] is the outermost.
+
+
+The Multi-Level Rule
+====================
+
+When a region's interleave is split across multiple decoder levels,
+CXL Spec 4.0 Section 9.13.1 requires that:
+
+   "all the levels use different, but consecutive, HPA bits to
+   select the target and no Interleave Set has more than eight
+   devices."
+
+Two requirements live in that sentence:

Documentation/driver-api/cxl/linux/region-granularity.rst line 66

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+
+* **Disjoint** - each level's selector bits must not overlap any
+  other level's bits.
+* **Consecutive** - the union of all level selectors must be a single
+  contiguous run of HPA bits with no gap.
+
+The union of level selectors is the **region selector**, and it must
+equal ``(region_ways - 1) * region_gran``. Because that mask is itself
+a contiguous run by construction, the levels just need to partition
+it cleanly.

The region also includes cross-hostbridge interleaving (described in CFMWS). The region ways could include a factor of 3. For the selector only the 2 factor parts are taken and the factor of 3 removed before. The region selector then includes the cross-hostbridge selectors too.

Quoted 4 lines

Below, you refer to root decoder and Cross-HB interleaving. Same as "region", the definition of it must be described in detail as the term "root" is ambiguous (cxl root, root decoder, root port, root complex, etc.).

Agreed on both. The surviving text defines its terms up front ("root decoder" = the CFMWS-described cross-host-bridge interleave) and uses them consistently.

Quoted 4 lines

It could be added that the endpoints are finally programmed with the region's interleaving config (ways and granularity).

Good point, added.

Quoted 77 lines
Quoted 77 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +77 -0
+
+The seven legal interleave granularity values split into two groups
+(see CXL Spec 4.0 Section 9.13.1 and Table 8-116):
+
+* **Group 1** - 256B, 512B, 1024B, 2048B (interleaving on HPA[8..11])
+* **Group 2** - 4096B, 8192B, 16384B (interleaving on HPA[12..14])
+
+CXL host bridges and switches must support every value in both
+groups. CXL memory devices must support at least one group, advertised
+through their HDM Decoder Capability register.
+
+
+Same-Granularity Interleave
+===========================
+
+The simplest case is ``region_gran == root_gran``. Each level's
+granularity equals the next inner level's IG times that level's
+ways. The region selector bits stack inside-out from the root.
+
+Example: 4-way region on a 2-way root, 256B everywhere::
+
+   Level    Ways   Granularity   Selector bits
+   ------   ----   -----------   -------------
+   Root     2      256B          bit 8
+   Switch   2      512B          bit 9
+   Region   4      256B          bits[9:8]
+
+Each level's IG sits one bit position above the next inner level's
+selector. The region's effective selector spans ``[9:8]``, two bits,
+matching ``(4 - 1) * 256``.
+
+
+Mixed-Granularity Interleave (Power-of-2)
+=========================================
+
+A region's granularity may be **less than** the root decoder's
+granularity. In that case the root claims an outer selector bit and
+the switches beneath it claim inner bits. The arrangement is legal
+whenever both granularities are powers of 2 and the resulting bits
+remain disjoint and consecutive.
+
+Two-Level Example (CXL Spec 4.0 Figure 9-17)
+--------------------------------------------
+
+8-way region, 2-way root at 4KB, 4-way switch at 1KB::
+
+   Level    Ways   Granularity   Selector bits
+   ------   ----   -----------   -------------
+   Root     2      4KB           bit 12
+   Switch   4      1KB           bits[11:10]
+   Region   8      1KB           bits[12:10]
+
+The host bridge bisects the address range at 4KB granularity, sending
+one half to each root port. Beneath each root port a 4-way switch
+splits its half into four 1KB chunks. To each endpoint the region
+appears as an 8-way interleave at 1KB based on bits[12:10]; the
+leftmost endpoint receives 1KB chunks at HPA bases 0, 8KB, 16KB, ...
+
+Three-Level Example (CXL Spec 4.0 Figure 9-18)
+----------------------------------------------
+
+8-way region across cross-HB / HB / switch, all 2-way::
+
+   Level         Ways   Granularity   Selector bits
+   -----------   ----   -----------   -------------
+   Cross-HB      2      4KB           bit 12
+   Host bridge   2      2KB           bit 11
+   Switch        2      1KB           bit 10
+   Region        8      1KB           bits[12:10]
+
+Three levels of 2-way interleave each contribute one bit. Total
+endpoints: ``2 * 2 * 2 = 8``. Selector union:
+``bit 12 | bit 11 | bit 10`` = ``bits[12:10]``. Disjoint and
+consecutive.
+
+General Power-of-2 Rules
+------------------------
Quoted 9 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 148

Quoted 5 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +5 -0
+
+For power-of-2 ways at every level:
+
+* ``region_gran <= root_gran`` is allowed; ``region_gran > root_gran``
+  is not (the root's selector would land inside one region stride).

It is unclear what 'root_gran' refers to. I think you mean the root decoder's (cross-HB) gran here (from CFMWS).

Yes, and with terms defined per above this becomes unambiguous.

Quoted 2 lines

Since region gran is the result of the union of all levels, it is always the smalles gran within all levels.

Agreed, and that's a cleaner way to state it than the inequality form; done.

Quoted 10 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 152

Quoted 5 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+* Both granularities must be powers of 2.
+* The ratio ``root_gran / region_gran`` is the number of inner
+  position slots packed beneath one root target. Endpoint position is
+  partitioned by root target rather than interleaved across them.

This does not give any hint of the bits used for the selector. Each level has an individual set of ways and granularities. There are no dependencies. The ratio cannot be used for position and selector calculation.

I want to push back on the last sentence, because the ratio is doing more than the text conveyed. root_gran / region_gran is exactly 2^(root's lowest selector bit - region's lowest selector bit), the place value of the root's selector bits in position space. That is precisely what position calculation needs from the root level, and it is what 3/6 uses. See my reply on 3/6. The doc text failed to make the ratio's connection to the selector bits explicit. I intend to fix that either here or if I move this to in code comments.

Quoted 9 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 156

Quoted 5 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+* Mixed-granularity is always optional for power-of-2 topologies. A
+  same-granularity arrangement always exists for the same set of
+  endpoints. Firmware may prefer mixed-granularity to expose a finer
+  switch-level interleave to the CPU.

Why distinguish between Mixed- and same-granularity? Same-granularity is only a subset.

Fair. v3 presents one model with same-granularity as the special case where every level's granularity equals the region's, rather than two categories.

Quoted 13 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 160

Quoted 4 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +4 -0
+
+
+3-Way Family Interleave
+=======================

Same here, this 3-way part is already documented in the spec, I don't see much sense to rewrite it here.

The whole point on 3-way familiy configs is how the HPA translates to the endpoint DPA and how the endpoint is selected:

HPA: <upper_hpa_part><selector_bits><granularity_bits>

Quoted 9 lines

base address: <upper_hpa_part><zero_bits>

In 3-way family configs the base addr (upper_hpa_part) is divided by 3 to determine the DPA, the remaining lower bits form the selector up to the gran size. Think of prime factorization of the base address, it will contain at least one factor of 3 and (sel + gran bits) nums of a factor of 2. The selector bits also form the endpoint positions below each host bridge.

The spec restatememt goes. The driver relevant consequence stays. Will look at moving to in code comments.

Quoted 7 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 372

Quoted 2 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +2 -0
+Linux Driver Implementation
+===========================

See my comments on the patches, a rework will simplify this doc too.

Imo, just add comments to the code where needed.

Mostly agreed. See the 3/6 reply for what the rework does and doesn't change. The stride section is gone (renamed and documented in code as the root's place value), and the enforcement-point walkthroughs become comments at the functions themselves.

Quoted 11 lines
Quoted 11 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +11 -0
+
+Region geometry validation lives in ``drivers/cxl/core/region.c``.
+The helpers and enforcement points below implement the rules
+described in the preceding sections.
+
+``get_selector(ways, gran)``
+  Each interleave level consumes a contiguous range of HPA bits to
+  pick its target. ``get_selector()`` models that range as a bitmask:
+  ``(ways - 1) * gran`` for power-of-2 interleaves. A strict 3-way
+  interleave consumes no HPA bits and the helper returns zero;
+  6-way and 12-way are folded to expose only their power-of-2
Quoted 8 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 388

Quoted 5 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +5 -0
+  component.
+
+  Kernel-doc: :c:func:`get_selector`.
+
+``root_pos_stride(cxlr)``

I rather would like to not introduce the term "stride" here. This only adds confusion. E.g. the root decoder selector could be used instead.

Probably will drop as a documented term with the code comment describing it as the root's position place value derived from root and region granularity.

Quoted 39 lines
Quoted 39 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +38 -0
+  When ``region_gran < root_gran``, each root target owns more than
+  one region position. The number of positions packed under one root
+  target is ``root_gran / region_gran``: the root advances by
+  ``root_gran`` bytes per target, while region positions advance by
+  ``region_gran`` bytes. Same-granularity regions have a stride of
+  one because the root advances on every region position.
+
+  ``root_pos_stride()`` returns this ratio for the position walk and
+  the peer-distance computation. The same value applies to 3-way
+  family roots: in value space they advance by ``root_gran`` per
+  target whether or not they consume HPA selector bits.
+
+  Kernel-doc: :c:func:`root_pos_stride`.
+
+``is_ig_allowed(cxlrd, ig)``
+  The region granularity must not exceed the root granularity.
+  ``region_gran > root_gran`` would place the root selector inside a
+  single region stride, leaving the root no address bits to choose
+  among its targets. ``region_gran <= root_gran`` is required for
+  every legal layout.
+
+  ``is_ig_allowed()`` enforces that geometric rule. It returns true
+  when the root is not interleaving, or when ``ig`` does not exceed
+  the root granularity. Endpoint capability (the IG groups a memdev
+  advertises) is checked separately at attach time via
+  ``check_interleave_cap()``.
+
+  Kernel-doc: :c:func:`is_ig_allowed`.
+
+``cxl_port_setup_targets()``
+  This is the per-port enforcement point for the multi-level rule.
+  For each level between the current port and the root the function:
+
+  * Rejects overlap. Two levels that share a selector bit fail the
+    disjoint requirement.
+  * Rejects escapes. The accumulated selector must fit within the
+    region selector. A level whose claimed bits land outside the
+    region selector fails the containment requirement.
Quoted 8 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 431

Quoted 5 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +5 -0
+  * Derives the granularity to program at the current port for
+    user-created regions. Each interleaving level claims the lowest
+    selector bit still available within the region selector;
+    passthrough levels consume no selector bit and inherit a scaled
+    granularity from their ancestors.

Fine with me, but all this is simple bit mask logic. Just comment the code and use helper functions?

Yes, with 1/6 factored into helpers per your review, each check gets a short comment at the helper and this section disappears.

Quoted 7 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 434

Quoted 4 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +3 -0
+
+  Auto regions reuse the same selector checks but trust the
+  granularity firmware already programmed at each level.

Never trust firmware. :-)

:-) Poor wording on my part. The AUTO path doesn't trust, it
verifies: firmware-programmed values are run through the same selector walk, containment, and span checks as user values; the only difference is where the values come from. The replacement text says "validated, not trusted."

Quoted 11 lines
Quoted 11 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +11 -0
+
+``cxl_region_attach()``
+  Pre-flight gates that run once per region attach, before the
+  per-port selector walk.
+
+  * Auto regions bypass the sysfs granularity gate, so
+    ``is_ig_allowed()`` is applied here to give them the same
+    geometric check as user-created regions.
+  * 3-way-family roots require an explicit span check. A strict
+    3-way root consumes no HPA selector bits, so selector containment
+    cannot prove that the root and region cover the same address
Quoted 8 lines

Documentation/driver-api/cxl/linux/region-granularity.rst line 451

Quoted 6 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +6 -0
+    span. The span identity
+    ``region_ways * region_gran == root_ways * root_gran`` restores
+    that constraint. Power-of-2 roots get the equivalent constraint
+    from selector containment and are not affected by this check.
+    See `Span Identity for 3-Way-Family Roots`_ for the full
+    derivation.

Same here, add it as comments to the code.

Got it.

Quoted 39 lines
Quoted 26 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +25 -0
+
+Position calculations use ``root_pos_stride()`` so that both
+same-granularity and mixed-granularity regions produce the correct
+endpoint ordering. The position walk itself lives in
+``cxl_calc_interleave_pos()``.
+
+Kernel-doc: :c:func:`cxl_calc_interleave_pos`.
+
+
+Auto vs User-Created Regions
+============================
+
+**Auto regions** are reconstructed at boot from the decoder values
+firmware programmed. The driver reads each decoder's ``ways`` and
+``granularity``, runs them through the selector walk, and accepts
+the configuration if it passes the disjoint and containment checks
+(plus the span identity for 3-way roots). Firmware is the source of
+truth for the granularity at each level.
+
+**User-created regions** specify ``interleave_ways`` and
+``interleave_granularity`` through sysfs; the driver derives each
+intermediate decoder's granularity. The sysfs path runs
+``is_ig_allowed()`` at granularity-write time, ``set_interleave_ways()``
+at ways-write time, and the full selector walk when the region is
+attached and committed.

Documentation/driver-api/cxl/linux/region-granularity.rst line 486

Quoted 10 lines
Documentation/driver-api/cxl/linux/region-granularity.rst new file +10 -0
+
+Both paths converge on the same geometry validation rules.
+Differences between the paths are limited to how region parameters
+are obtained and when they are validated.
+
+* Whether each intermediate decoder's granularity is read from
+  hardware (auto) or derived (user).
+* Whether the membership of the region is established by walking
+  firmware-programmed decoders (auto) or by user writes to ``targetN``
+  attributes (user).

This could be condensed to:

Quoted 2 lines

auto: config is read from decoder registers
user: sysfs is used to setup decoder registers

Got it.

-- Alison

Quoted 6 lines

-Robert

Quoted 3 lines
Signature · 1 line
2.37.3
Tested-by on the v2 series
#24 lore raw
In reply to Alison Schofield
Quoted 8 lines · Alison Schofield

Changes in v2:
- Patch 1,2: Defer the unused selector var store to keep P1 bisectable (Sashiko)
- Patch 1: Make divide by 3 in get_selctor() work on 32-bit builds) (lkp)
- Patch 4: Use local vars in cxl_region_attach() for readability (DaveJ)
- Patch 5: Add NULL checks on unused mock arrays (Sashiko)
- Resolved errant err_rch unwind with rc7 merge (DaveJ)
- Rebase onto 7.1-rc7
- Update commit logs in 1,2,5 to align w changes in v2

For the series: