Skip to contentSkip to conversation

cxl/region: check interleave capability

PATCH Superseded Yao Xingtao 4 participants 7 messages Jun 11, 2024 – Jun 12, 2024 CXL

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 cxl/region: check interleave capability

Since interleave capability is not verified, if the interleave capability of a target does not match the region need, committing decoder should have failed at the device end.

In order to checkout this error as quickly as possible, driver needs to check the interleave capability of target during attaching it to region.

According to the CXL specification (section 8.2.4.20 CXL HDM Decoder Capability Structure), bits 11 and 12 within the 'CXL HDM Decoder Capability Register' indicate the capability to establish interleaving in 3, 6, 12, and 16 ways. If these bits are not set, the target cannot be attached to a region utilizing such interleave ways.

Additionally, bits 8 and 9 in the same register represent the capability of the bits used for interleaving in the address, Linux tracks this in the cxl_port interleave_mask.

Regarding 'Decoder Protection':
If IW is less than 8 (for interleave ways of 2, 4, 8, 16), the interleave bits start at bit position IG + 8 and end at IG + IW + 8 - 1.

If the IW is greater than or equal to 8 (for interleave ways of 6, 12), the interleave bits start at bit position IG + 8 and end at IG + IW - 1.

When the interleave ways is 1 or 3, all the bits of HPA are used, the interleave bits are none, the following check is ignored.

If the interleave mask is insufficient to cover the required interleave bits, the target cannot be attached to the region.

Fixes 384e624bb211 ("cxl/region: Attach endpoint decoders")

---

V5[5] -> V6:
-- fix some typo.
-- update comment.
-- set rc when check faild in cxl_port_attach_region().

V4[4] -> V5:
-- update comment.
-- add nr_targets check while attaching a port to switch.
-- delete passthrough flag and allow all the capabilities for passthrough
decoders.

V3[3] -> V4:
-- update comment.
-- optimize the code.
-- add a passthrough flag to mark the passthrough decoder.

V2[2] -> V3:
-- revert ig_cap_mask to interleave_mask.
-- fix the interleave bits check logical.

V1[1] -> V2:
-- rename interleave_mask to ig_cap_mask. -- add a check for interleave granularity. -- update commit.
-- move hdm caps init to parse_hdm_decoder_caps().

[1]
https://lore.kernel.org/linux-cxl/[email protected]

[2]
https://lore.kernel.org/linux-cxl/[email protected]

[3]
https://lore.kernel.org/linux-cxl/[email protected]

[4]
https://lore.kernel.org/linux-cxl/[email protected]

[5]
https://lore.kernel.org/linux-cxl/[email protected]

---
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +10 -0
index 7d97790b893d..5b7dff19bbfa 100644
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
 
 	cxlsd = cxl_switch_decoder_alloc(port, 1);
 	if (IS_ERR(cxlsd))
@@ -79,6 +84,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
 		cxlhdm->interleave_mask |= GENMASK(11, 8);
 	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
 		cxlhdm->interleave_mask |= GENMASK(14, 12);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(16);
 }
 
 static bool should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)
drivers/cxl/core/region.c +89 -0

113 lines not on this page · raw message

drivers/cxl/cxl.h +2 -0
index 036d17db68e0..dc8e46a1fe82 100644
@@ -45,6 +45,8 @@
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
 #define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
 #define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define   CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
+#define   CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4
 #define   CXL_HDM_DECODER_ENABLE BIT(1)
 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
drivers/cxl/cxlmem.h +1 -0
index 36cee9c30ceb..6b8cf20ff375 100644
@@ -853,6 +853,7 @@ struct cxl_hdm {
 	unsigned int decoder_count;
 	unsigned int target_count;
 	unsigned int interleave_mask;
+	unsigned long iw_cap_mask;
 	struct cxl_port *port;
 };
 
Signature · 1 line
2.37.3
maintainer
#1 lore raw
Quoted 9 lines · Yao Xingtao

Since interleave capability is not verified, if the interleave capability of a target does not match the region need, committing decoder should have failed at the device end.

In order to checkout this error as quickly as possible, driver needs to check the interleave capability of target during attaching it to region.

According to the CXL specification (section 8.2.4.20 CXL HDM Decoder

We should specify the revision of the spec in case future section number changes.

Quoted 11 lines

Capability Structure), bits 11 and 12 within the 'CXL HDM Decoder Capability Register' indicate the capability to establish interleaving in 3, 6, 12, and 16 ways. If these bits are not set, the target cannot be attached to a region utilizing such interleave ways.

Additionally, bits 8 and 9 in the same register represent the capability of the bits used for interleaving in the address, Linux tracks this in the cxl_port interleave_mask.

Regarding 'Decoder Protection':
If IW is less than 8 (for interleave ways of 2, 4, 8, 16), the

First use of 'IW'. I suggest using 'eIW' and clarify with something like:

encoded Interleave Ways (eIW)

Should do the same for IG as well below.

Once defined, eIW and eIG can be used.

Quoted 80 lines

interleave bits start at bit position IG + 8 and end at IG + IW + 8 - 1.

If the IW is greater than or equal to 8 (for interleave ways of 6, 12), the interleave bits start at bit position IG + 8 and end at IG + IW - 1.

When the interleave ways is 1 or 3, all the bits of HPA are used, the interleave bits are none, the following check is ignored.

If the interleave mask is insufficient to cover the required interleave bits, the target cannot be attached to the region.

Fixes 384e624bb211 ("cxl/region: Attach endpoint decoders")

---

V5[5] -> V6:
-- fix some typo.
-- update comment.
-- set rc when check faild in cxl_port_attach_region().

V4[4] -> V5:
-- update comment.
-- add nr_targets check while attaching a port to switch.
-- delete passthrough flag and allow all the capabilities for passthrough
decoders.

V3[3] -> V4:
-- update comment.
-- optimize the code.
-- add a passthrough flag to mark the passthrough decoder.

V2[2] -> V3:
-- revert ig_cap_mask to interleave_mask.
-- fix the interleave bits check logical.

V1[1] -> V2:
-- rename interleave_mask to ig_cap_mask. -- add a check for interleave granularity. -- update commit.
-- move hdm caps init to parse_hdm_decoder_caps().

[1]
https://lore.kernel.org/linux-cxl/[email protected]

[2]
https://lore.kernel.org/linux-cxl/[email protected]

[3]
https://lore.kernel.org/linux-cxl/[email protected]

[4]
https://lore.kernel.org/linux-cxl/[email protected]

[5]
https://lore.kernel.org/linux-cxl/[email protected]

---
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +5 -0
index 7d97790b893d..5b7dff19bbfa 100644
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
 
 	cxlsd = cxl_switch_decoder_alloc(port, 1);
 	if (IS_ERR(cxlsd))

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) · line 87

Quoted 4 lines
drivers/cxl/core/hdm.c +1 -0
@@ -79,6 +84,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
 		cxlhdm->interleave_mask |= GENMASK(11, 8);
 	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
 		cxlhdm->interleave_mask |= GENMASK(14, 12);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);

Should define a mask for this. Probably should have defined masks for all of the open coded BIT() sets.

Quoted 25 lines
drivers/cxl/core/hdm.c +4 -0
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(16);
 }
 
 static bool should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)
drivers/cxl/core/region.c +10 -0
index 5c186e0a39b9..2716678df5db 100644
@@ -1101,6 +1101,26 @@ static int cxl_port_attach_region(struct cxl_port *port,
 	}
 	cxld = cxl_rr->decoder;
 
+	/*
+	 * the number of targets should not exceed the target_count
+	 * of the decoder
+	 */
+	if (is_switch_decoder(&cxld->dev)) {
+		struct cxl_switch_decoder *cxlsd;
+
+		cxlsd = to_cxl_switch_decoder(&cxld->dev);
+		if (cxl_rr->nr_targets > cxlsd->nr_targets) {
+			dev_dbg(&cxlr->dev,

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

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+				"%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
+				dev_name(port->uport_dev), dev_name(&port->dev),
+				dev_name(&cxld->dev), dev_name(&cxlmd->dev),
+				dev_name(&cxled->cxld.dev), pos,
+				cxlsd->nr_targets);
+			rc = -ENXIO;
+			goto out_erase;
+		}
+	}
+

Should this be a seaprate patch? Seems to be outside the purpose of interleave capability check.

Quoted 12 lines
drivers/cxl/core/region.c +5 -0
 	rc = cxl_rr_ep_add(cxl_rr, cxled);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
@@ -1210,6 +1230,57 @@ static int check_last_peer(struct cxl_endpoint_decoder *cxled,
 	return 0;
 }
 
+static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
+{
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+	unsigned int interleave_mask;

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

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+	u8 eiw;
+	u16 eig;
+	int rc, high_pos, low_pos;
+
+	rc = ways_to_eiw(iw, &eiw);
+	if (rc)
+		return rc;
+
+	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
+		return -ENXIO;

This test can be moved above the ways_to_eiw() call? No need to do ways_to_eiw() if this fails?

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

Quoted 7 lines
drivers/cxl/core/region.c +7 -0
+
+	rc = granularity_to_eig(ig, &eig);
+	if (rc)
+		return rc;
+
+	/*
+	 * Per CXL specification (8.2.4.20.13 Decoder Protection in r3.1),

A nit but it's probably easier to read if you specify the spec revision first

	Per CXL specification r3.1 (8.2.4.20.13...),

DJ

Quoted 89 lines
drivers/cxl/core/region.c +47 -0
+	 * if eiw < 8:
+	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
+	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
+	 *
+	 *   when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
+	 *   interleave bits are none.
+	 *
+	 * if eiw >= 8:
+	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
+	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
+	 *
+	 *   when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
+	 *   interleave bits are none.
+	 */
+	if (eiw == 0 || eiw == 8)
+		return 0;
+
+	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_mask & ~cxlhdm->interleave_mask)
+		return -ENXIO;
+
+	return 0;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
@@ -1360,6 +1431,15 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 			return -ENXIO;
 		}
 	} else {
+		rc = check_interleave_cap(cxld, iw, ig);
+		if (rc) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s iw: %d ig: %d is not supported\n",
+				dev_name(port->uport_dev),
+				dev_name(&port->dev), iw, ig);
+			return rc;
+		}
+
 		cxld->interleave_ways = iw;
 		cxld->interleave_granularity = ig;
 		cxld->hpa_range = (struct range) {
@@ -1796,6 +1876,15 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
 
+	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
+				  p->interleave_granularity);
+	if (rc) {
+		dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
+			dev_name(&cxled->cxld.dev), p->interleave_ways,
+			p->interleave_granularity);
+		return rc;
+	}
+
 	if (cxled->mode != cxlr->mode) {
 		dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
 			dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
drivers/cxl/cxl.h +2 -0
index 036d17db68e0..dc8e46a1fe82 100644
@@ -45,6 +45,8 @@
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
 #define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
 #define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define   CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
+#define   CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4
 #define   CXL_HDM_DECODER_ENABLE BIT(1)
 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
drivers/cxl/cxlmem.h +1 -0
index 36cee9c30ceb..6b8cf20ff375 100644
@@ -853,6 +853,7 @@ struct cxl_hdm {
 	unsigned int decoder_count;
 	unsigned int target_count;
 	unsigned int interleave_mask;
+	unsigned long iw_cap_mask;
 	struct cxl_port *port;
 };
 
#2 lore raw
Quoted 21 lines
Quoted 21 lines
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +5 -0
index 7d97790b893d..5b7dff19bbfa 100644
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
 
Quoted 9 lines

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) · line 87

Quoted 7 lines
drivers/cxl/core/hdm.c +1 -0
 	cxlsd = cxl_switch_decoder_alloc(port, 1);
 	if (IS_ERR(cxlsd))
@@ -79,6 +84,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
 		cxlhdm->interleave_mask |= GENMASK(11, 8);
 	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
 		cxlhdm->interleave_mask |= GENMASK(14, 12);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);  

Should define a mask for this. Probably should have defined masks for all of the open coded BIT() sets.

I don't agreed. These bits are logically independent specifying each supported value of interleave ways.
Put them under a define and I think you end up with less readable code.

Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/hdm.c +4 -0
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(16);
 }
maintainer
#3 lore raw
Quoted 31 lines · Jonathan Cameron
Quoted 21 lines
Quoted 21 lines
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +5 -0
index 7d97790b893d..5b7dff19bbfa 100644
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
 
Quoted 9 lines

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) · line 87

Quoted 7 lines
drivers/cxl/core/hdm.c +1 -0
 	cxlsd = cxl_switch_decoder_alloc(port, 1);
 	if (IS_ERR(cxlsd))
@@ -79,6 +84,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
 		cxlhdm->interleave_mask |= GENMASK(11, 8);
 	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
 		cxlhdm->interleave_mask |= GENMASK(14, 12);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);  

Should define a mask for this. Probably should have defined masks for all of the open coded BIT() sets.

Quoted 4 lines

I don't agreed. These bits are logically independent specifying each supported value of interleave ways.
Put them under a define and I think you end up with less readable code.

Seeing the 1,2,4,8 being set twice looks like a common pattern. but I don't have a strong opinion on this.

Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/hdm.c +4 -0
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(16);
 }
#4 lore raw
In reply to Dave Jiang
Quoted 19 lines

-----Original Message-----
From: Dave Jiang <[email protected]>
Sent: Tuesday, June 11, 2024 11:52 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v6] cxl/region: check interleave capability

Quoted 4 lines · Yao Xingtao

Since interleave capability is not verified, if the interleave capability of a target does not match the region need, committing decoder should have failed at the device end.

Quoted 6 lines
Quoted 5 lines

In order to checkout this error as quickly as possible, driver needs to check the interleave capability of target during attaching it to region.

According to the CXL specification (section 8.2.4.20 CXL HDM Decoder

We should specify the revision of the spec in case future section number changes.

thanks, will fix.

Quoted 13 lines
Quoted 6 lines

Capability Structure), bits 11 and 12 within the 'CXL HDM Decoder Capability Register' indicate the capability to establish interleaving in 3, 6, 12, and 16 ways. If these bits are not set, the target cannot be attached to a region utilizing such interleave ways.

Quoted 6 lines

Additionally, bits 8 and 9 in the same register represent the capability of the bits used for interleaving in the address, Linux tracks this in the cxl_port interleave_mask.

Regarding 'Decoder Protection':
If IW is less than 8 (for interleave ways of 2, 4, 8, 16), the

Quoted 7 lines

First use of 'IW'. I suggest using 'eIW' and clarify with something like:

encoded Interleave Ways (eIW)

Should do the same for IG as well below.

Once defined, eIW and eIG can be used.

thanks, will fix.

Quoted 129 lines
Quoted 37 lines

interleave bits start at bit position IG + 8 and end at IG + IW + 8 - 1.

If the IW is greater than or equal to 8 (for interleave ways of 6, 12), the interleave bits start at bit position IG + 8 and end at IG + IW - 1.

When the interleave ways is 1 or 3, all the bits of HPA are used, the interleave bits are none, the following check is ignored.

If the interleave mask is insufficient to cover the required interleave bits, the target cannot be attached to the region.

Fixes 384e624bb211 ("cxl/region: Attach endpoint decoders")

---

V5[5] -> V6:
-- fix some typo.
-- update comment.
-- set rc when check faild in cxl_port_attach_region().

V4[4] -> V5:
-- update comment.
-- add nr_targets check while attaching a port to switch.
-- delete passthrough flag and allow all the capabilities for passthrough
decoders.

V3[3] -> V4:
-- update comment.
-- optimize the code.
-- add a passthrough flag to mark the passthrough decoder.

V2[2] -> V3:
-- revert ig_cap_mask to interleave_mask.
-- fix the interleave bits check logical.

Quoted 8 lines

V1[1] -> V2:
-- rename interleave_mask to ig_cap_mask. -- add a check for interleave granularity. -- update commit.
-- move hdm caps init to parse_hdm_decoder_caps().

[1]

https://lore.kernel.org/linux-cxl/[email protected]

Quoted 3 lines

[2]

https://lore.kernel.org/linux-cxl/[email protected]

Quoted 3 lines

[3]

https://lore.kernel.org/linux-cxl/[email protected]

Quoted 3 lines

[4]

https://lore.kernel.org/linux-cxl/[email protected]

Quoted 3 lines

[5]

https://lore.kernel.org/linux-cxl/[email protected]

Quoted 3 lines
---
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89

+++++++++++++++++++++++++++++++++++++++

drivers/cxl/core/hdm.c int devm_cxl_add_passthrough_decoder(struct cxl_port

Quoted 9 lines
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +0 -0
index 7d97790b893d..5b7dff19bbfa 100644
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port

*port)

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm

Quoted 12 lines
drivers/cxl/core/hdm.c +5 -0
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
 
 	cxlsd = cxl_switch_decoder_alloc(port, 1);
 	if (IS_ERR(cxlsd))
@@ -79,6 +84,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm

*cxlhdm)

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm) · line 87

Quoted 4 lines
drivers/cxl/core/hdm.c +1 -0
 		cxlhdm->interleave_mask |= GENMASK(11, 8);
 	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
 		cxlhdm->interleave_mask |= GENMASK(14, 12);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);

Should define a mask for this. Probably should have defined masks for all of the open coded BIT() sets.

drivers/cxl/core/hdm.c static void parse_hdm_decoder_caps(struct cxl_hdm · line 88

Quoted 1 line
drivers/cxl/core/hdm.c +1 -0
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY,

hdm_cap))

Quoted 15 lines
drivers/cxl/core/hdm.c +3 -0
+		cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
+	if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
+		cxlhdm->iw_cap_mask |= BIT(16);
 }
 
 static bool should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)
drivers/cxl/core/region.c +1 -0
index 5c186e0a39b9..2716678df5db 100644
@@ -1101,6 +1101,26 @@ static int cxl_port_attach_region(struct cxl_port *port,
 	}
 	cxld = cxl_rr->decoder;
 
+	/*

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

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+	 * the number of targets should not exceed the target_count
+	 * of the decoder
+	 */
+	if (is_switch_decoder(&cxld->dev)) {
+		struct cxl_switch_decoder *cxlsd;
+
+		cxlsd = to_cxl_switch_decoder(&cxld->dev);
+		if (cxl_rr->nr_targets > cxlsd->nr_targets) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s %s add: %s:%s @ %d overflows

targets: %d\n",

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

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+				dev_name(port->uport_dev),

dev_name(&port->dev),

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

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+				dev_name(&cxld->dev),

dev_name(&cxlmd->dev),

Quoted 10 lines

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

Quoted 7 lines
drivers/cxl/core/region.c +7 -0
+				dev_name(&cxled->cxld.dev), pos,
+				cxlsd->nr_targets);
+			rc = -ENXIO;
+			goto out_erase;
+		}
+	}
+

Should this be a seaprate patch? Seems to be outside the purpose of interleave capability check.

in this case, the cxl_rr->nr_targets is increasing, it cannot represent interleave ways, and the granularity is unknown, so I think it is better to do this check here.

Quoted 19 lines

drivers/cxl/core/region.c static int check_last_peer(struct

Quoted 5 lines
drivers/cxl/core/region.c +0 -0
 	rc = cxl_rr_ep_add(cxl_rr, cxled);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
@@ -1210,6 +1230,57 @@ static int check_last_peer(struct

cxl_endpoint_decoder *cxled,

Quoted 13 lines
drivers/cxl/core/region.c +10 -0
 	return 0;
 }
 
+static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
+{
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+	unsigned int interleave_mask;
+	u8 eiw;
+	u16 eig;
+	int rc, high_pos, low_pos;
+
+	rc = ways_to_eiw(iw, &eiw);
Quoted 8 lines

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

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+	if (rc)
+		return rc;
+
+	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
+		return -ENXIO;

This test can be moved above the ways_to_eiw() call? No need to do ways_to_eiw() if this fails?

in my opinion, the interleave ways should be valid first.

Quoted 11 lines

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

Quoted 8 lines
drivers/cxl/core/region.c +7 -0
+
+	rc = granularity_to_eig(ig, &eig);
+	if (rc)
+		return rc;
+
+	/*
+	 * Per CXL specification (8.2.4.20.13 Decoder Protection in r3.1),

A nit but it's probably easier to read if you specify the spec revision first

	Per CXL specification r3.1 (8.2.4.20.13...),

thanks, will fix.

Quoted 92 lines

DJ

Quoted 54 lines
drivers/cxl/core/region.c +40 -0
+	 * if eiw < 8:
+	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
+	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
+	 *
+	 *   when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
+	 *   interleave bits are none.
+	 *
+	 * if eiw >= 8:
+	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
+	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
+	 *
+	 *   when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
+	 *   interleave bits are none.
+	 */
+	if (eiw == 0 || eiw == 8)
+		return 0;
+
+	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_mask & ~cxlhdm->interleave_mask)
+		return -ENXIO;
+
+	return 0;
+}
+
 static int cxl_port_setup_targets(struct cxl_port *port,
 				  struct cxl_region *cxlr,
 				  struct cxl_endpoint_decoder *cxled)
@@ -1360,6 +1431,15 @@ static int cxl_port_setup_targets(struct cxl_port *port,
 			return -ENXIO;
 		}
 	} else {
+		rc = check_interleave_cap(cxld, iw, ig);
+		if (rc) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s iw: %d ig: %d is not supported\n",
+				dev_name(port->uport_dev),
+				dev_name(&port->dev), iw, ig);
+			return rc;
+		}
+
 		cxld->interleave_ways = iw;
 		cxld->interleave_granularity = ig;
 		cxld->hpa_range = (struct range) {
@@ -1796,6 +1876,15 @@ static int cxl_region_attach(struct cxl_region *cxlr,
 	struct cxl_dport *dport;
 	int rc = -ENXIO;
 
+	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
+				  p->interleave_granularity);

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

Quoted 10 lines
drivers/cxl/core/region.c +7 -0
+	if (rc) {
+		dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
+			dev_name(&cxled->cxld.dev), p->interleave_ways,
+			p->interleave_granularity);
+		return rc;
+	}
+
 	if (cxled->mode != cxlr->mode) {
 		dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
 			dev_name(&cxled->cxld.dev), cxlr->mode,

cxled->mode);

Quoted 25 lines
drivers/cxl/cxl.h +2 -0
index 036d17db68e0..dc8e46a1fe82 100644
@@ -45,6 +45,8 @@
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
 #define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
 #define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define   CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
+#define   CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4
 #define   CXL_HDM_DECODER_ENABLE BIT(1)
 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
drivers/cxl/cxlmem.h +1 -0
index 36cee9c30ceb..6b8cf20ff375 100644
@@ -853,6 +853,7 @@ struct cxl_hdm {
 	unsigned int decoder_count;
 	unsigned int target_count;
 	unsigned int interleave_mask;
+	unsigned long iw_cap_mask;
 	struct cxl_port *port;
 };
 
#5 lore raw

Xingtao Yao (Fujitsu) wrote:
[..]

Quoted 34 lines
Quoted 24 lines
Quoted 9 lines
drivers/cxl/core/region.c +1 -0
index 5c186e0a39b9..2716678df5db 100644
@@ -1101,6 +1101,26 @@ static int cxl_port_attach_region(struct cxl_port *port,
 	}
 	cxld = cxl_rr->decoder;
 
+	/*

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

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+	 * the number of targets should not exceed the target_count
+	 * of the decoder
+	 */
+	if (is_switch_decoder(&cxld->dev)) {
+		struct cxl_switch_decoder *cxlsd;
+
+		cxlsd = to_cxl_switch_decoder(&cxld->dev);
+		if (cxl_rr->nr_targets > cxlsd->nr_targets) {
+			dev_dbg(&cxlr->dev,
+				"%s:%s %s add: %s:%s @ %d overflows

targets: %d\n",

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

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+				dev_name(port->uport_dev),

dev_name(&port->dev),

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

Quoted 1 line
drivers/cxl/core/region.c +1 -0
+				dev_name(&cxld->dev),

dev_name(&cxlmd->dev),

Quoted 10 lines

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

Quoted 7 lines
drivers/cxl/core/region.c +7 -0
+				dev_name(&cxled->cxld.dev), pos,
+				cxlsd->nr_targets);
+			rc = -ENXIO;
+			goto out_erase;
+		}
+	}
+

Should this be a seaprate patch? Seems to be outside the purpose of interleave capability check.

Quoted 2 lines

in this case, the cxl_rr->nr_targets is increasing, it cannot represent interleave ways, and the granularity is unknown, so I think it is better to do this check here.

Right, the decoder's target count *is* a capability, so might as well include all the capability checks in this fix.

Quoted 28 lines
Quoted 20 lines

drivers/cxl/core/region.c static int check_last_peer(struct

Quoted 6 lines
drivers/cxl/core/region.c +0 -0
 	rc = cxl_rr_ep_add(cxl_rr, cxled);
 	if (rc) {
 		dev_dbg(&cxlr->dev,
@@ -1210,6 +1230,57 @@ static int check_last_peer(struct

cxl_endpoint_decoder *cxled,

Quoted 13 lines
drivers/cxl/core/region.c +10 -0
 	return 0;
 }
 
+static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
+{
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+	unsigned int interleave_mask;
+	u8 eiw;
+	u16 eig;
+	int rc, high_pos, low_pos;
+
+	rc = ways_to_eiw(iw, &eiw);
Quoted 8 lines

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

Quoted 5 lines
drivers/cxl/core/region.c +5 -0
+	if (rc)
+		return rc;
+
+	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
+		return -ENXIO;

This test can be moved above the ways_to_eiw() call? No need to do ways_to_eiw() if this fails?

Quoted 1 line

in my opinion, the interleave ways should be valid first.

The check can just be dropped altogether becuase it is redundant with earlier checks of the same:

cxl_port_setup_targets():
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cxl/core/region.c#n1297

interleave_ways_store():
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cxl/core/region.c#n498

Reviewed-by on v6 1/1
#6 lore raw
In reply to Yao Xingtao
Quoted 88 lines · Yao Xingtao

Since interleave capability is not verified, if the interleave capability of a target does not match the region need, committing decoder should have failed at the device end.

In order to checkout this error as quickly as possible, driver needs to check the interleave capability of target during attaching it to region.

According to the CXL specification (section 8.2.4.20 CXL HDM Decoder Capability Structure), bits 11 and 12 within the 'CXL HDM Decoder Capability Register' indicate the capability to establish interleaving in 3, 6, 12, and 16 ways. If these bits are not set, the target cannot be attached to a region utilizing such interleave ways.

Additionally, bits 8 and 9 in the same register represent the capability of the bits used for interleaving in the address, Linux tracks this in the cxl_port interleave_mask.

Regarding 'Decoder Protection':
If IW is less than 8 (for interleave ways of 2, 4, 8, 16), the interleave bits start at bit position IG + 8 and end at IG + IW + 8 - 1.

If the IW is greater than or equal to 8 (for interleave ways of 6, 12), the interleave bits start at bit position IG + 8 and end at IG + IW - 1.

When the interleave ways is 1 or 3, all the bits of HPA are used, the interleave bits are none, the following check is ignored.

If the interleave mask is insufficient to cover the required interleave bits, the target cannot be attached to the region.

Fixes 384e624bb211 ("cxl/region: Attach endpoint decoders")

---

V5[5] -> V6:
-- fix some typo.
-- update comment.
-- set rc when check faild in cxl_port_attach_region().

V4[4] -> V5:
-- update comment.
-- add nr_targets check while attaching a port to switch.
-- delete passthrough flag and allow all the capabilities for passthrough
decoders.

V3[3] -> V4:
-- update comment.
-- optimize the code.
-- add a passthrough flag to mark the passthrough decoder.

V2[2] -> V3:
-- revert ig_cap_mask to interleave_mask.
-- fix the interleave bits check logical.

V1[1] -> V2:
-- rename interleave_mask to ig_cap_mask. -- add a check for interleave granularity. -- update commit.
-- move hdm caps init to parse_hdm_decoder_caps().

[1]
https://lore.kernel.org/linux-cxl/[email protected]

[2]
https://lore.kernel.org/linux-cxl/[email protected]

[3]
https://lore.kernel.org/linux-cxl/[email protected]

[4]
https://lore.kernel.org/linux-cxl/[email protected]

[5]
https://lore.kernel.org/linux-cxl/[email protected]

---
 drivers/cxl/core/hdm.c    | 10 +++++
 drivers/cxl/core/region.c | 89 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  2 +
 drivers/cxl/cxlmem.h      |  1 +
 4 files changed, 102 insertions(+)
drivers/cxl/core/hdm.c +0 -0
index 7d97790b893d..5b7dff19bbfa 100644

drivers/cxl/core/hdm.c int devm_cxl_add_passthrough_decoder(struct cxl_port *port) · line 57

Quoted 6 lines
drivers/cxl/core/hdm.c +3 -0
@@ -52,6 +52,11 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
 	struct cxl_dport *dport = NULL;
 	int single_port_map[1];
 	unsigned long index;
+	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
+
+	/* allow all the interleave capabilities for passthrough decoder */

This comment is incomplete / misleading:

drivers/cxl/core/hdm.c int devm_cxl_add_passthrough_decoder(struct cxl_port *port) · line 59

Quoted 2 lines
drivers/cxl/core/hdm.c +2 -0
+	cxlhdm->interleave_mask = GENMASK(14, 8);
+	cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);

...because this is not all of the possible interleaves.

The precision here is not helpful because it makes it seem as if any of these checks actually matter for a passthrough decoder. So I would be happier if the comment and the code said:

	/*
	 * Capability checks are moot for passthrough decoders, support any and
	 * all possibilities
	 */
	cxlhdm->interleave_mask = ~0U;
	cxlhdm->iw_cap_mask = ~0UL;
Quoted 18 lines
drivers/cxl/cxl.h +2 -0
index 036d17db68e0..dc8e46a1fe82 100644
@@ -45,6 +45,8 @@
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
 #define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
 #define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define   CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
+#define   CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
 #define CXL_HDM_DECODER_CTRL_OFFSET 0x4
 #define   CXL_HDM_DECODER_ENABLE BIT(1)
 #define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
drivers/cxl/cxlmem.h +0 -0
index 36cee9c30ceb..6b8cf20ff375 100644

drivers/cxl/cxlmem.h struct cxl_hdm { · line 856

Quoted 4 lines
drivers/cxl/cxlmem.h +1 -0
@@ -853,6 +853,7 @@ struct cxl_hdm {
 	unsigned int decoder_count;
 	unsigned int target_count;
 	unsigned int interleave_mask;
+	unsigned long iw_cap_mask;

Given the code assumes that the reader knows that each bit is identical to a supported interleave value it would be useful to document that assumption here and add some kdoc, something like:

/**
* struct cxl_hdm - HDM Decoder registers and cached / decoded capabilities
* @regs: mapped registers, see devm_cxl_setup_hdm()
* @decoder_count: number of decoders for this port
* @target_count: for switch decoders, max downstream port targets
* @interleave_mask: interleave granularity capabilty, see check_interleave_cap()
* @iw_cap_mask: bitmask of supported interleave ways, see check_interleave_cap() */

...with the above changes you can add: