Skip to content

Commit 84328c5

Browse files
sailer1205davejiang
authored andcommitted
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. Per CXL specification r3.1(8.2.4.20.1 CXL HDM Decoder Capability Register), bits 11 and 12 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 represent the capability of the bits used for interleaving in the address, Linux tracks this in the cxl_port interleave_mask. Per CXL specification r3.1(8.2.4.20.13 Decoder Protection): eIW means encoded Interleave Ways. eIG means encoded Interleave Granularity. in HPA: if eIW is 0 or 8 (interleave ways: 1, 3), all the bits of HPA are used, the interleave bits are none, the following check is ignored. if eIW is less than 8 (interleave ways: 2, 4, 8, 16), the interleave bits start at bit position eIG + 8 and end at eIG + eIW + 8 - 1. if eIW is greater than 8 (interleave ways: 6, 12), the interleave bits start at bit position eIG + 8 and end at eIG + eIW - 1. if the interleave mask is insufficient to cover the required interleave bits, the target cannot be attached to the region. Fixes: 384e624 ("cxl/region: Attach endpoint decoders") Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Link: https://patch.msgid.link/20240614084755.59503-2-yaoxt.fnst@fujitsu.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 285f2a0 commit 84328c5

5 files changed

Lines changed: 111 additions & 0 deletions

File tree

drivers/cxl/core/hdm.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ int devm_cxl_add_passthrough_decoder(struct cxl_port *port)
5252
struct cxl_dport *dport = NULL;
5353
int single_port_map[1];
5454
unsigned long index;
55+
struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
56+
57+
/*
58+
* Capability checks are moot for passthrough decoders, support
59+
* any and all possibilities.
60+
*/
61+
cxlhdm->interleave_mask = ~0U;
62+
cxlhdm->iw_cap_mask = ~0UL;
5563

5664
cxlsd = cxl_switch_decoder_alloc(port, 1);
5765
if (IS_ERR(cxlsd))
@@ -79,6 +87,11 @@ static void parse_hdm_decoder_caps(struct cxl_hdm *cxlhdm)
7987
cxlhdm->interleave_mask |= GENMASK(11, 8);
8088
if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap))
8189
cxlhdm->interleave_mask |= GENMASK(14, 12);
90+
cxlhdm->iw_cap_mask = BIT(1) | BIT(2) | BIT(4) | BIT(8);
91+
if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY, hdm_cap))
92+
cxlhdm->iw_cap_mask |= BIT(3) | BIT(6) | BIT(12);
93+
if (FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_16_WAY, hdm_cap))
94+
cxlhdm->iw_cap_mask |= BIT(16);
8295
}
8396

8497
static bool should_emulate_decoders(struct cxl_endpoint_dvsec_info *info)

drivers/cxl/core/region.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,26 @@ static int cxl_port_attach_region(struct cxl_port *port,
11011101
}
11021102
cxld = cxl_rr->decoder;
11031103

1104+
/*
1105+
* the number of targets should not exceed the target_count
1106+
* of the decoder
1107+
*/
1108+
if (is_switch_decoder(&cxld->dev)) {
1109+
struct cxl_switch_decoder *cxlsd;
1110+
1111+
cxlsd = to_cxl_switch_decoder(&cxld->dev);
1112+
if (cxl_rr->nr_targets > cxlsd->nr_targets) {
1113+
dev_dbg(&cxlr->dev,
1114+
"%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
1115+
dev_name(port->uport_dev), dev_name(&port->dev),
1116+
dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1117+
dev_name(&cxled->cxld.dev), pos,
1118+
cxlsd->nr_targets);
1119+
rc = -ENXIO;
1120+
goto out_erase;
1121+
}
1122+
}
1123+
11041124
rc = cxl_rr_ep_add(cxl_rr, cxled);
11051125
if (rc) {
11061126
dev_dbg(&cxlr->dev,
@@ -1210,6 +1230,50 @@ static int check_last_peer(struct cxl_endpoint_decoder *cxled,
12101230
return 0;
12111231
}
12121232

1233+
static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
1234+
{
1235+
struct cxl_port *port = to_cxl_port(cxld->dev.parent);
1236+
struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
1237+
unsigned int interleave_mask;
1238+
u8 eiw;
1239+
u16 eig;
1240+
int high_pos, low_pos;
1241+
1242+
if (!test_bit(iw, &cxlhdm->iw_cap_mask))
1243+
return -ENXIO;
1244+
/*
1245+
* Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
1246+
* if eiw < 8:
1247+
* DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
1248+
* DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1249+
*
1250+
* when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
1251+
* interleave bits are none.
1252+
*
1253+
* if eiw >= 8:
1254+
* DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
1255+
* DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1256+
*
1257+
* when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
1258+
* interleave bits are none.
1259+
*/
1260+
ways_to_eiw(iw, &eiw);
1261+
if (eiw == 0 || eiw == 8)
1262+
return 0;
1263+
1264+
granularity_to_eig(ig, &eig);
1265+
if (eiw > 8)
1266+
high_pos = eiw + eig - 1;
1267+
else
1268+
high_pos = eiw + eig + 7;
1269+
low_pos = eig + 8;
1270+
interleave_mask = GENMASK(high_pos, low_pos);
1271+
if (interleave_mask & ~cxlhdm->interleave_mask)
1272+
return -ENXIO;
1273+
1274+
return 0;
1275+
}
1276+
12131277
static int cxl_port_setup_targets(struct cxl_port *port,
12141278
struct cxl_region *cxlr,
12151279
struct cxl_endpoint_decoder *cxled)
@@ -1360,6 +1424,15 @@ static int cxl_port_setup_targets(struct cxl_port *port,
13601424
return -ENXIO;
13611425
}
13621426
} else {
1427+
rc = check_interleave_cap(cxld, iw, ig);
1428+
if (rc) {
1429+
dev_dbg(&cxlr->dev,
1430+
"%s:%s iw: %d ig: %d is not supported\n",
1431+
dev_name(port->uport_dev),
1432+
dev_name(&port->dev), iw, ig);
1433+
return rc;
1434+
}
1435+
13631436
cxld->interleave_ways = iw;
13641437
cxld->interleave_granularity = ig;
13651438
cxld->hpa_range = (struct range) {
@@ -1796,6 +1869,15 @@ static int cxl_region_attach(struct cxl_region *cxlr,
17961869
struct cxl_dport *dport;
17971870
int rc = -ENXIO;
17981871

1872+
rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
1873+
p->interleave_granularity);
1874+
if (rc) {
1875+
dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
1876+
dev_name(&cxled->cxld.dev), p->interleave_ways,
1877+
p->interleave_granularity);
1878+
return rc;
1879+
}
1880+
17991881
if (cxled->mode != cxlr->mode) {
18001882
dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
18011883
dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);

drivers/cxl/cxl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern const struct nvdimm_security_ops *cxl_security_ops;
4747
#define CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
4848
#define CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
4949
#define CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
50+
#define CXL_HDM_DECODER_INTERLEAVE_3_6_12_WAY BIT(11)
51+
#define CXL_HDM_DECODER_INTERLEAVE_16_WAY BIT(12)
5052
#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
5153
#define CXL_HDM_DECODER_ENABLE BIT(1)
5254
#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)

drivers/cxl/cxlmem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,21 @@ static inline void cxl_mem_active_dec(void)
851851

852852
int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd);
853853

854+
/**
855+
* struct cxl_hdm - HDM Decoder registers and cached / decoded capabilities
856+
* @regs: mapped registers, see devm_cxl_setup_hdm()
857+
* @decoder_count: number of decoders for this port
858+
* @target_count: for switch decoders, max downstream port targets
859+
* @interleave_mask: interleave granularity capability, see check_interleave_cap()
860+
* @iw_cap_mask: bitmask of supported interleave ways, see check_interleave_cap()
861+
* @port: mapped cxl_port, see devm_cxl_setup_hdm()
862+
*/
854863
struct cxl_hdm {
855864
struct cxl_component_regs regs;
856865
unsigned int decoder_count;
857866
unsigned int target_count;
858867
unsigned int interleave_mask;
868+
unsigned long iw_cap_mask;
859869
struct cxl_port *port;
860870
};
861871

tools/testing/cxl/test/cxl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,15 @@ static struct cxl_hdm *mock_cxl_setup_hdm(struct cxl_port *port,
630630
struct cxl_endpoint_dvsec_info *info)
631631
{
632632
struct cxl_hdm *cxlhdm = devm_kzalloc(&port->dev, sizeof(*cxlhdm), GFP_KERNEL);
633+
struct device *dev = &port->dev;
633634

634635
if (!cxlhdm)
635636
return ERR_PTR(-ENOMEM);
636637

637638
cxlhdm->port = port;
639+
cxlhdm->interleave_mask = ~0U;
640+
cxlhdm->iw_cap_mask = ~0UL;
641+
dev_set_drvdata(dev, cxlhdm);
638642
return cxlhdm;
639643
}
640644

0 commit comments

Comments
 (0)