Skip to contentSkip to conversation

CXL Region driver

PATCH Superseded Ben Widawsky 3 participants 70 messages Jan 28, 2022 – Feb 24, 2022 Page 2 of 2

Subsystems (5)
lore
Share & tools
lore
#57 lore raw

Re: [PATCH v3 09/14] cxl/region: Add infrastructure for decoder programming

In reply to Ben Widawsky
Quoted 6 lines · On Thu, 27 Jan 2022 16:27:02 -0800 Ben Widawsky <[email protected]> wrote:

There are 3 steps in handling region programming once it has been configured by userspace.
1. Sanitize the parameters against the system.
2. Collect decoder resources from the topology
3. Program decoder resources

Quoted 7 lines

The infrastructure added here addresses #2. Two new APIs are introduced to allow collecting and returning decoder resources. Additionally the infrastructure includes two lists managed by the region driver, a staged list, and a commit list. The staged list contains those collected in step #2, and the commit list are all the decoders programmed in step #3.

Minor comments inline.

Quoted 46 lines

static void cxld_unregister(void *dev)
{

drivers/cxl/core/port.c +0 -0
 	device_unregister(dev);
drivers/cxl/core/region.c +2 -0
index 784e4ba25128..a62d48454a56 100644
@@ -440,6 +440,8 @@ struct cxl_region *cxl_alloc_region(struct cxl_decoder *cxld, int id)
 	if (!cxlr)
 		return ERR_PTR(-ENOMEM);
 
+	INIT_LIST_HEAD(&cxlr->staged_list);
+	INIT_LIST_HEAD(&cxlr->commit_list);
 	cxlr->id = id;
 
 	return cxlr;
drivers/cxl/cxl.h +4 -0
index ed984465b59c..8ace6cca0776 100644
@@ -35,6 +35,8 @@
 #define   CXL_CM_CAP_CAP_ID_HDM 0x5
 #define   CXL_CM_CAP_CAP_HDM_VERSION 1
 
+#define CXL_DECODER_MAX_INSTANCES 10
+
 /* HDM decoders CXL 2.0 8.2.5.12 CXL HDM Decoder Capability Structure */
 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
 #define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
@@ -265,6 +267,7 @@ enum cxl_decoder_type {
  * @target_lock: coordinate coherent reads of the target list
  * @region_ida: allocator for region ids.
  * @address_space: Used/free address space for regions.
+ * @region_link: This decoder's place on either the staged, or commit list.
  * @nr_targets: number of elements in @target
  * @target: active ordered target list in current decoder configuration
  */
@@ -282,6 +285,7 @@ struct cxl_decoder {
 	seqlock_t target_lock;
 	struct ida region_ida;
 	struct gen_pool *address_space;
+	struct list_head region_link;
 	int nr_targets;
 	struct cxl_dport *target[];
 };

drivers/cxl/cxl.h struct cxl_nvdimm { · line 333

Quoted 4 lines
drivers/cxl/cxl.h +1 -0
@@ -326,6 +330,7 @@ struct cxl_nvdimm {
  * @id: id for port device-name
  * @dports: cxl_dport instances referenced by decoders
  * @endpoints: cxl_ep instances, endpoints that are a descendant of this port
+ * @region_link: this port's node on the region's list of ports

docs but no field in the structure...

Quoted 13 lines
drivers/cxl/cxl.h +2 -0
  * @decoder_ida: allocator for decoder ids
  * @component_reg_phys: component register capability base address (optional)
  * @dead: last ep has been removed, force port re-creation
@@ -396,6 +401,8 @@ struct cxl_port *find_cxl_root(struct device *dev);
 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
 int cxl_bus_rescan(void);
 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd);
+struct cxl_decoder *cxl_get_decoder(struct cxl_port *port);
+void cxl_put_decoder(struct cxl_decoder *cxld);
 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd);
 
 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,

drivers/cxl/cxl.h struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port, · line 416

Quoted 4 lines
drivers/cxl/cxl.h +1 -0
@@ -406,6 +413,7 @@ struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
 struct cxl_port *ep_find_cxl_port(struct cxl_memdev *cxlmd, unsigned int depth);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
+bool is_cxl_decoder(struct device *dev);

They are multiplying!! (see 2 lines down.)

Quoted 12 lines
drivers/cxl/cxl.h +0 -0
 bool is_root_decoder(struct device *dev);
 bool is_cxl_decoder(struct device *dev);
 struct cxl_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
drivers/cxl/region.c +0 -0
index d2f6c990c8a8..145d7bb02714 100644
@@ -359,21 +359,59 @@ static bool has_switch(const struct cxl_region *cxlr)
 	return false;
 }
 

...

Quoted 17 lines

static int bind_region(const struct cxl_region *cxlr)

drivers/cxl/region.c +2 -2
@@ -559,7 +648,7 @@ static int cxl_region_probe(struct device *dev)
 		return -ENXIO;
 	}
 
-	if (!rootd_valid(cxlr, rootd)) {
+	if (!rootd_valid(cxlr, rootd, true)) {
 		dev_err(dev, "Picked invalid rootd\n");
 		return -ENXIO;
 	}
@@ -574,14 +663,18 @@ static int cxl_region_probe(struct device *dev)
 
 	ret = collect_ep_decoders(cxlr);
 	if (ret)
-		return ret;
+		goto err;
 

drivers/cxl/region.c static int cxl_region_probe(struct device *dev) · line 673

Quoted 10 lines
drivers/cxl/region.c +4 -4
 	ret = bind_region(cxlr);
-	if (!ret) {
-		cxlr->active = true;
-		dev_info(dev, "Bound");
-	}
+	if (ret)
+		goto err;
 
+	cxlr->active = true;
+	dev_info(dev, "Bound");

Not keen on this being always printed in the logs. dev_dbg() perhaps with some more detail may be

Quoted 12 lines
drivers/cxl/region.c +4 -0
+	return 0;
+
+err:
+	cleanup_staged_decoders(cxlr);
 	return ret;
 }
 
drivers/cxl/region.h +0 -0
index 00a6dc729c26..fc15abaeb638 100644

drivers/cxl/region.h line 19

Quoted 6 lines
drivers/cxl/region.h +3 -0
@@ -14,6 +14,9 @@
  * @list: Node in decoder's region list.
  * @res: Resource this region carves out of the platform decode range.
  * @active: If the region has been activated.
+ * @staged_list: All decoders staged for programming.
+ * @commit_list: All decoders programmed for this region's parameters.
+ *

Why the blank line? If it makes sense should be in earlier patch and I'm not sure if kernel-doc allows blank lines in the list.

Quoted 12 lines
drivers/cxl/region.h +2 -0
  * @config: HDM decoder program config
  * @config.size: Size of the region determined from LSA or userspace.
  * @config.uuid: The UUID for this region.
@@ -27,6 +30,8 @@ struct cxl_region {
 	struct list_head list;
 	struct resource *res;
 	bool active;
+	struct list_head staged_list;
+	struct list_head commit_list;
 
 	struct {
 		u64 size;
#58 lore raw

Re: [PATCH v3 09/14] cxl/region: Add infrastructure for decoder programming

In reply to Ben Widawsky
Quoted 7 lines · Ben Widawsky

There are 3 steps in handling region programming once it has been configured by userspace.
1. Sanitize the parameters against the system.
2. Collect decoder resources from the topology
3. Program decoder resources

Quoted 5 lines

The infrastructure added here addresses #2. Two new APIs are introduced to allow collecting and returning decoder resources. Additionally the infrastructure includes two lists managed by the region driver, a staged list, and a commit list. The staged list contains those collected in step #2, and the commit list are all the decoders programmed in step #3.

I expect this patch will see significant rewrites with the ABI change to register endpoint decoders with regions. It's otherwise redundant to have a 'targets' array and then yet mosre linked lists to walk the decoders associated with a region.

#59 lore raw

PATCH v3 10/14 cxl/region: Collect host bridge decoders

In reply to Ben Widawsky

Part of host bridge verification in the CXL Type 3 Memory Device Software Guide calculates the host bridge interleave target list (6th step in the flow chart), ie. verification and state update are done in the same step. Host bridge verification is already in place, so go ahead and store the decoders with their target lists.

Switches are implemented in a separate patch.

---
 drivers/cxl/region.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/cxl/region.c +15 -2
index 145d7bb02714..b8982be13bfe 100644
@@ -428,6 +428,7 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 		return simple_config(cxlr, hbs[0]);
 
 	for (i = 0; i < hb_count; i++) {
+		struct cxl_decoder *cxld;
 		int idx, position_mask;
 		struct cxl_dport *rp;
 		struct cxl_port *hb;
@@ -486,6 +487,18 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 						"One or more devices are not connected to the correct Host Bridge Root Port\n");
 					goto err;
 				}
+
+				if (!state_update)
+					continue;
+
+				if (dev_WARN_ONCE(&cxld->dev,
+						  port_grouping >= cxld->nr_targets,
+						  "Invalid port grouping %d/%d\n",
+						  port_grouping, cxld->nr_targets))
+					goto err;
+
+				cxld->interleave_ways++;
+				cxld->target[port_grouping] = get_rp(ep);
 			}
 		}
 	}
@@ -538,7 +551,7 @@ static bool rootd_valid(const struct cxl_region *cxlr,
 
 struct rootd_context {
 	const struct cxl_region *cxlr;
-	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
+	const struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
 	int count;
 };
 
@@ -564,7 +577,7 @@ static struct cxl_decoder *find_rootd(const struct cxl_region *cxlr,
 	struct rootd_context ctx;
 	struct device *ret;
 
-	ctx.cxlr = cxlr;
+	ctx.cxlr = (struct cxl_region *)cxlr;
 
 	ret = device_find_child((struct device *)&root->dev, &ctx, rootd_match);
 	if (ret)
Signature · 1 line
2.35.0
#60 lore raw

Re: [PATCH v3 10/14] cxl/region: Collect host bridge decoders

Quoted 9 lines · On Thu, 27 Jan 2022 16:27:03 -0800 Ben Widawsky <[email protected]> wrote:

Part of host bridge verification in the CXL Type 3 Memory Device Software Guide calculates the host bridge interleave target list (6th step in the flow chart), ie. verification and state update are done in the same step. Host bridge verification is already in place, so go ahead and store the decoders with their target lists.

Switches are implemented in a separate patch.

Looks like a little bit of code got in here that I think belongs in a different patch.

Quoted 45 lines
---
 drivers/cxl/region.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/cxl/region.c +14 -1
index 145d7bb02714..b8982be13bfe 100644
@@ -428,6 +428,7 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 		return simple_config(cxlr, hbs[0]);
 
 	for (i = 0; i < hb_count; i++) {
+		struct cxl_decoder *cxld;
 		int idx, position_mask;
 		struct cxl_dport *rp;
 		struct cxl_port *hb;
@@ -486,6 +487,18 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 						"One or more devices are not connected to the correct Host Bridge Root Port\n");
 					goto err;
 				}
+
+				if (!state_update)
+					continue;
+
+				if (dev_WARN_ONCE(&cxld->dev,
+						  port_grouping >= cxld->nr_targets,
+						  "Invalid port grouping %d/%d\n",
+						  port_grouping, cxld->nr_targets))
+					goto err;
+
+				cxld->interleave_ways++;
+				cxld->target[port_grouping] = get_rp(ep);
 			}
 		}
 	}
@@ -538,7 +551,7 @@ static bool rootd_valid(const struct cxl_region *cxlr,
 
 struct rootd_context {
 	const struct cxl_region *cxlr;
-	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
+	const struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
 	int count;
 };
 

drivers/cxl/region.c static struct cxl_decoder *find_rootd(const struct cxl_region *cxlr, · line 580

Quoted 5 lines
drivers/cxl/region.c +1 -1
@@ -564,7 +577,7 @@ static struct cxl_decoder *find_rootd(const struct cxl_region *cxlr,
 	struct rootd_context ctx;
 	struct device *ret;
 
-	ctx.cxlr = cxlr;
+	ctx.cxlr = (struct cxl_region *)cxlr;

Why is this here? If it's needed, that need doesn't seem to have come in as part of this patch.

Quoted 3 lines
drivers/cxl/region.c +0 -0
 	ret = device_find_child((struct device *)&root->dev, &ctx, rootd_match);
 	if (ret)
#61 lore raw

Re: [PATCH v3 10/14] cxl/region: Collect host bridge decoders

In reply to Ben Widawsky
Quoted 32 lines · Ben Widawsky

Part of host bridge verification in the CXL Type 3 Memory Device Software Guide calculates the host bridge interleave target list (6th step in the flow chart), ie. verification and state update are done in the same step. Host bridge verification is already in place, so go ahead and store the decoders with their target lists.

Switches are implemented in a separate patch.

---
 drivers/cxl/region.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/cxl/region.c +3 -0
index 145d7bb02714..b8982be13bfe 100644
@@ -428,6 +428,7 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
                return simple_config(cxlr, hbs[0]);
 
        for (i = 0; i < hb_count; i++) {
+               struct cxl_decoder *cxld;
                int idx, position_mask;
                struct cxl_dport *rp;
                struct cxl_port *hb;
@@ -486,6 +487,18 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
                                                "One or more devices are not connected to the correct Host Bridge Root Port\n");
                                        goto err;
                                }
+
+                               if (!state_update)

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 501

Quoted 10 lines
drivers/cxl/region.c +10 -0
+                                       continue;
+
+                               if (dev_WARN_ONCE(&cxld->dev,
+                                                 port_grouping >= cxld->nr_targets,
+                                                 "Invalid port grouping %d/%d\n",
+                                                 port_grouping, cxld->nr_targets))
+                                       goto err;
+
+                               cxld->interleave_ways++;
+                               cxld->target[port_grouping] = get_rp(ep);

There is not enough context in the changelog to understand what this code is doing, but I do want to react to all this caching of objects without references. I'd prefer helpers that walk the device that are already synced with device_del() events than worry about these caches and when to invalidate their references.

Quoted 13 lines
drivers/cxl/region.c +1 -1
                        }
                }
        }
@@ -538,7 +551,7 @@ static bool rootd_valid(const struct cxl_region *cxlr,
 
 struct rootd_context {
        const struct cxl_region *cxlr;
-       struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
+       const struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
        int count;
 };
 

drivers/cxl/region.c static struct cxl_decoder *find_rootd(const struct cxl_region *cxlr, · line 580

Quoted 5 lines
drivers/cxl/region.c +1 -1
@@ -564,7 +577,7 @@ static struct cxl_decoder *find_rootd(const struct cxl_region *cxlr,
        struct rootd_context ctx;
        struct device *ret;
 
-       ctx.cxlr = cxlr;
+       ctx.cxlr = (struct cxl_region *)cxlr;

If const requires casting then don't use const.

Quoted 6 lines
drivers/cxl/region.c +0 -1
        ret = device_find_child((struct device *)&root->dev, &ctx, rootd_match);
        if (ret)
--

2.35.0

#62 lore raw

PATCH v3 11/14 cxl/region: Add support for single switch level

In reply to Ben Widawsky

CXL switches have HDM decoders just like host bridges and endpoints. Their programming works in a similar fashion.

The spec does not prohibit multiple levels of switches, however, those are not implemented at this time.

---
 drivers/cxl/cxl.h    |  5 ++++
 drivers/cxl/region.c | 61 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/cxl/cxl.h +5 -0
index 8ace6cca0776..d70d8c85d05f 100644
@@ -96,6 +96,11 @@ static inline u8 cxl_to_ig(u16 g)
 	return ilog2(g) - 8;
 }
 
+static inline int cxl_to_ways(u8 ways)
+{
+	return 1 << ways;
+}
+
 static inline bool cxl_is_interleave_ways_valid(int iw)
 {
 	switch (iw) {
drivers/cxl/region.c +59 -2

93 lines not on this page · raw message

Signature · 1 line
2.35.0
#63 lore raw

Re: [PATCH v3 11/14] cxl/region: Add support for single switch level

Quoted 7 lines · On Thu, 27 Jan 2022 16:27:04 -0800 Ben Widawsky <[email protected]> wrote:

CXL switches have HDM decoders just like host bridges and endpoints. Their programming works in a similar fashion.

The spec does not prohibit multiple levels of switches, however, those are not implemented at this time.

One trivial comment inline, but it's end of day here so I've not taken as deeper look at this as I probably will at some later date.

Thanks,

Jonathan

Quoted 10 lines
---
 drivers/cxl/cxl.h    |  5 ++++
 drivers/cxl/region.c | 61 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/cxl/cxl.h +0 -0
index 8ace6cca0776..d70d8c85d05f 100644

drivers/cxl/cxl.h static inline u8 cxl_to_ig(u16 g) · line 101

Quoted 6 lines
drivers/cxl/cxl.h +3 -0
@@ -96,6 +96,11 @@ static inline u8 cxl_to_ig(u16 g)
 	return ilog2(g) - 8;
 }
 
+static inline int cxl_to_ways(u8 ways)
+{
+	return 1 << ways;

This special case of cxl_to_interleave_ways probably needs some documentation or a name that makes it clear why it is special.

Quoted 107 lines
drivers/cxl/cxl.h +2 -0
+}
+
 static inline bool cxl_is_interleave_ways_valid(int iw)
 {
 	switch (iw) {
drivers/cxl/region.c +59 -2
index b8982be13bfe..f748060733dd 100644
@@ -359,6 +359,23 @@ static bool has_switch(const struct cxl_region *cxlr)
 	return false;
 }
 
+static bool has_multi_switch(const struct cxl_region *cxlr)
+{
+	struct cxl_memdev *ep;
+	int i;
+
+	for_each_cxl_endpoint(ep, cxlr, i)
+		if (ep->port->depth > 3)
+			return true;
+
+	return false;
+}
+
+static struct cxl_port *get_switch(struct cxl_memdev *ep)
+{
+	return to_cxl_port(ep->port->dev.parent);
+}
+
 static struct cxl_decoder *get_decoder(struct cxl_region *cxlr,
 				       struct cxl_port *p)
 {
@@ -409,6 +426,8 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 				      const struct cxl_decoder *rootd,
 				      bool state_update)
 {
+	const int region_ig = cxl_to_ig(cxlr->config.interleave_granularity);
+	const int region_eniw = cxl_to_eniw(cxlr->config.interleave_ways);
 	const int num_root_ports = get_num_root_ports(cxlr);
 	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
 	struct cxl_decoder *cxld, *c;
@@ -416,8 +435,12 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 	hb_count = get_unique_hostbridges(cxlr, hbs);
 
-	/* TODO: Switch support */
-	if (has_switch(cxlr))
+	/* TODO: support multiple levels of switches */
+	if (has_multi_switch(cxlr))
+		return false;
+
+	/* TODO: x3 interleave for switches is hard. */
+	if (has_switch(cxlr) && !is_power_of_2(region_ways(cxlr)))
 		return false;
 
 	/*
@@ -470,8 +493,14 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 		list_for_each_entry(rp, &hb->dports, list) {
 			struct cxl_memdev *ep;
 			int port_grouping = -1;
+			int target_ndx;
 
 			for_each_cxl_endpoint_hb(ep, cxlr, hb, idx) {
+				struct cxl_decoder *switch_cxld;
+				struct cxl_dport *target;
+				struct cxl_port *switch_port;
+				bool found = false;
+
 				if (get_rp(ep) != rp)
 					continue;
 
@@ -499,6 +528,34 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 				cxld->interleave_ways++;
 				cxld->target[port_grouping] = get_rp(ep);
+
+				/*
+				 * At least one switch is connected here if the endpoint
+				 * has a depth > 2
+				 */
+				if (ep->port->depth == 2)
+					continue;
+
+				/* Check the staged list to see if this
+				 * port has already been added
+				 */
+				switch_port = get_switch(ep);
+				list_for_each_entry(switch_cxld, &cxlr->staged_list, region_link) {
+					if (to_cxl_port(switch_cxld->dev.parent) == switch_port)
+						found = true;
+				}
+
+				if (found) {
+					target = cxl_find_dport_by_dev(switch_port, ep->dev.parent->parent);
+					switch_cxld->target[target_ndx++] = target;
+					continue;
+				}
+
+				target_ndx = 0;
+
+				switch_cxld = get_decoder(cxlr, switch_port);
+				switch_cxld->interleave_ways++;
+				switch_cxld->interleave_granularity = cxl_to_ways(region_ig + region_eniw);
 			}
 		}
 	}
#64 lore raw

Re: [PATCH v3 11/14] cxl/region: Add support for single switch level

In reply to Ben Widawsky
Quoted 7 lines · On Thu, 27 Jan 2022 16:27:04 -0800 Ben Widawsky <[email protected]> wrote:

CXL switches have HDM decoders just like host bridges and endpoints. Their programming works in a similar fashion.

The spec does not prohibit multiple levels of switches, however, those are not implemented at this time.

Hi Ben,

I'm still hammering away at trying to bring up qemu switch emulation. Even though I know you are reworking this, seems only sensible to point out issues when I hit them. If no longer relevant you can ignore them!

With these bits and a few other minor tweaks the decoders now look to be right - I just need to wire up the QEMU side so that I don't get hardware exceptions on actually reading and writing once the region is bound :)

Thanks,

J

Quoted 74 lines
---
 drivers/cxl/cxl.h    |  5 ++++
 drivers/cxl/region.c | 61 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/cxl/cxl.h +5 -0
index 8ace6cca0776..d70d8c85d05f 100644
@@ -96,6 +96,11 @@ static inline u8 cxl_to_ig(u16 g)
 	return ilog2(g) - 8;
 }
 
+static inline int cxl_to_ways(u8 ways)
+{
+	return 1 << ways;
+}
+
 static inline bool cxl_is_interleave_ways_valid(int iw)
 {
 	switch (iw) {
drivers/cxl/region.c +25 -2
index b8982be13bfe..f748060733dd 100644
@@ -359,6 +359,23 @@ static bool has_switch(const struct cxl_region *cxlr)
 	return false;
 }
 
+static bool has_multi_switch(const struct cxl_region *cxlr)
+{
+	struct cxl_memdev *ep;
+	int i;
+
+	for_each_cxl_endpoint(ep, cxlr, i)
+		if (ep->port->depth > 3)
+			return true;
+
+	return false;
+}
+
+static struct cxl_port *get_switch(struct cxl_memdev *ep)
+{
+	return to_cxl_port(ep->port->dev.parent);
+}
+
 static struct cxl_decoder *get_decoder(struct cxl_region *cxlr,
 				       struct cxl_port *p)
 {
@@ -409,6 +426,8 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 				      const struct cxl_decoder *rootd,
 				      bool state_update)
 {
+	const int region_ig = cxl_to_ig(cxlr->config.interleave_granularity);
+	const int region_eniw = cxl_to_eniw(cxlr->config.interleave_ways);
 	const int num_root_ports = get_num_root_ports(cxlr);
 	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
 	struct cxl_decoder *cxld, *c;
@@ -416,8 +435,12 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 	hb_count = get_unique_hostbridges(cxlr, hbs);
 
-	/* TODO: Switch support */
-	if (has_switch(cxlr))
+	/* TODO: support multiple levels of switches */
+	if (has_multi_switch(cxlr))
+		return false;
+
+	/* TODO: x3 interleave for switches is hard. */
+	if (has_switch(cxlr) && !is_power_of_2(region_ways(cxlr)))
 		return false;
 
 	/*

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 496

Quoted 4 lines
drivers/cxl/region.c +1 -0
@@ -470,8 +493,14 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 		list_for_each_entry(rp, &hb->dports, list) {
 			struct cxl_memdev *ep;
 			int port_grouping = -1;
+			int target_ndx;

As things currently stand, with a switch connected below a single port of a host bridge (4 type 3 off the switch) this will program the HB decoder to have 4 targets, all routed to the switch USP.

There is an argument that this is correct but its not what I'd expect. I'd expect to see 1 target only. It's not a problem for small cases, but with enough rp and switches we can run out of targets.

Quoted 19 lines
drivers/cxl/region.c +10 -0
 			for_each_cxl_endpoint_hb(ep, cxlr, hb, idx) {
+				struct cxl_decoder *switch_cxld;
+				struct cxl_dport *target;
+				struct cxl_port *switch_port;
+				bool found = false;
+
 				if (get_rp(ep) != rp)
 					continue;
 
@@ -499,6 +528,34 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 				cxld->interleave_ways++;
 				cxld->target[port_grouping] = get_rp(ep);
+
+				/*
+				 * At least one switch is connected here if the endpoint
+				 * has a depth > 2
+				 */

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 545

Quoted 10 lines
drivers/cxl/region.c +10 -0
+				if (ep->port->depth == 2)
+					continue;
+
+				/* Check the staged list to see if this
+				 * port has already been added
+				 */
+				switch_port = get_switch(ep);
+				list_for_each_entry(switch_cxld, &cxlr->staged_list, region_link) {
+					if (to_cxl_port(switch_cxld->dev.parent) == switch_port)
+						found = true;

break;

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 558

Quoted 13 lines
drivers/cxl/region.c +13 -0
+				}
+
+				if (found) {
+					target = cxl_find_dport_by_dev(switch_port, ep->dev.parent->parent);
+					switch_cxld->target[target_ndx++] = target;
+					continue;
+				}
+
+				target_ndx = 0;
+
+				switch_cxld = get_decoder(cxlr, switch_port);
+				switch_cxld->interleave_ways++;
+				switch_cxld->interleave_granularity = cxl_to_ways(region_ig + region_eniw);

I'm not following this. Perhaps comment on why this particular maths? I was assuming the switch interleave granularity would that of the region as the switch is last level of decode.

Need to do the equivalent here of what you do in the if (found) or the first target is missed. Also need to be updating interleave_ways only in the found path, not here (as the default is 1)

Quoted 3 lines
drivers/cxl/region.c +0 -0
 			}
 		}
 	}
#65 lore raw

Re: [PATCH v3 11/14] cxl/region: Add support for single switch level

Quoted 12 lines · On Tue, 15 Feb 2022 16:10:14 +0000 Jonathan Cameron…
Quoted 7 lines · On Thu, 27 Jan 2022 16:27:04 -0800 Ben Widawsky <[email protected]> wrote:

CXL switches have HDM decoders just like host bridges and endpoints. Their programming works in a similar fashion.

The spec does not prohibit multiple levels of switches, however, those are not implemented at this time.

Hi Ben,

Quoted 8 lines

I'm still hammering away at trying to bring up qemu switch emulation. Even though I know you are reworking this, seems only sensible to point out issues when I hit them. If no longer relevant you can ignore them!

With these bits and a few other minor tweaks the decoders now look to be right - I just need to wire up the QEMU side so that I don't get hardware exceptions on actually reading and writing once the region is bound :)

QEMU emulation of a very basic CXL switch at:
https://gitlab.com/jic23/qemu/-/commits/cxl-v7-draft-for-test

Hope that is helpful for testing next version of this. I've been testing with a 4 port switch and 4 type 3 devices and with a few hacks/fixes as mentioned in review feedback it all works (well devmem2 gives me what I expect. :)

Jonathan

Quoted 146 lines

Thanks,

J

Quoted 74 lines
---
 drivers/cxl/cxl.h    |  5 ++++
 drivers/cxl/region.c | 61 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/cxl/cxl.h +5 -0
index 8ace6cca0776..d70d8c85d05f 100644
@@ -96,6 +96,11 @@ static inline u8 cxl_to_ig(u16 g)
 	return ilog2(g) - 8;
 }
 
+static inline int cxl_to_ways(u8 ways)
+{
+	return 1 << ways;
+}
+
 static inline bool cxl_is_interleave_ways_valid(int iw)
 {
 	switch (iw) {
drivers/cxl/region.c +25 -2
index b8982be13bfe..f748060733dd 100644
@@ -359,6 +359,23 @@ static bool has_switch(const struct cxl_region *cxlr)
 	return false;
 }
 
+static bool has_multi_switch(const struct cxl_region *cxlr)
+{
+	struct cxl_memdev *ep;
+	int i;
+
+	for_each_cxl_endpoint(ep, cxlr, i)
+		if (ep->port->depth > 3)
+			return true;
+
+	return false;
+}
+
+static struct cxl_port *get_switch(struct cxl_memdev *ep)
+{
+	return to_cxl_port(ep->port->dev.parent);
+}
+
 static struct cxl_decoder *get_decoder(struct cxl_region *cxlr,
 				       struct cxl_port *p)
 {
@@ -409,6 +426,8 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 				      const struct cxl_decoder *rootd,
 				      bool state_update)
 {
+	const int region_ig = cxl_to_ig(cxlr->config.interleave_granularity);
+	const int region_eniw = cxl_to_eniw(cxlr->config.interleave_ways);
 	const int num_root_ports = get_num_root_ports(cxlr);
 	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
 	struct cxl_decoder *cxld, *c;
@@ -416,8 +435,12 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 	hb_count = get_unique_hostbridges(cxlr, hbs);
 
-	/* TODO: Switch support */
-	if (has_switch(cxlr))
+	/* TODO: support multiple levels of switches */
+	if (has_multi_switch(cxlr))
+		return false;
+
+	/* TODO: x3 interleave for switches is hard. */
+	if (has_switch(cxlr) && !is_power_of_2(region_ways(cxlr)))
 		return false;
 
 	/*

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 496

Quoted 4 lines
drivers/cxl/region.c +1 -0
@@ -470,8 +493,14 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 		list_for_each_entry(rp, &hb->dports, list) {
 			struct cxl_memdev *ep;
 			int port_grouping = -1;
+			int target_ndx;  

As things currently stand, with a switch connected below a single port of a host bridge (4 type 3 off the switch) this will program the HB decoder to have 4 targets, all routed to the switch USP.

There is an argument that this is correct but its not what I'd expect. I'd expect to see 1 target only. It's not a problem for small cases, but with enough rp and switches we can run out of targets.

Quoted 19 lines
drivers/cxl/region.c +10 -0
 			for_each_cxl_endpoint_hb(ep, cxlr, hb, idx) {
+				struct cxl_decoder *switch_cxld;
+				struct cxl_dport *target;
+				struct cxl_port *switch_port;
+				bool found = false;
+
 				if (get_rp(ep) != rp)
 					continue;
 
@@ -499,6 +528,34 @@ static bool region_hb_rp_config_valid(struct cxl_region *cxlr,
 
 				cxld->interleave_ways++;
 				cxld->target[port_grouping] = get_rp(ep);
+
+				/*
+				 * At least one switch is connected here if the endpoint
+				 * has a depth > 2
+				 */

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 545

Quoted 10 lines
drivers/cxl/region.c +10 -0
+				if (ep->port->depth == 2)
+					continue;
+
+				/* Check the staged list to see if this
+				 * port has already been added
+				 */
+				switch_port = get_switch(ep);
+				list_for_each_entry(switch_cxld, &cxlr->staged_list, region_link) {
+					if (to_cxl_port(switch_cxld->dev.parent) == switch_port)
+						found = true;  

break;

drivers/cxl/region.c static bool region_hb_rp_config_valid(struct cxl_region *cxlr, · line 558

Quoted 13 lines
drivers/cxl/region.c +13 -0
+				}
+
+				if (found) {
+					target = cxl_find_dport_by_dev(switch_port, ep->dev.parent->parent);
+					switch_cxld->target[target_ndx++] = target;
+					continue;
+				}
+
+				target_ndx = 0;
+
+				switch_cxld = get_decoder(cxlr, switch_port);
+				switch_cxld->interleave_ways++;
+				switch_cxld->interleave_granularity = cxl_to_ways(region_ig + region_eniw);  

I'm not following this. Perhaps comment on why this particular maths? I was assuming the switch interleave granularity would that of the region as the switch is last level of decode.

Need to do the equivalent here of what you do in the if (found) or the first target is missed. Also need to be updating interleave_ways only in the found path, not here (as the default is 1)

Quoted 3 lines
drivers/cxl/region.c +0 -0
 			}
 		}
 	}  
#66 lore raw

PATCH v3 12/14 cxl: Program decoders for regions

In reply to Ben Widawsky

Configure and commit the HDM decoders for the region. Since the region driver already was able to walk the topology and build the list of needed decoders, all that was needed to finish region setup was to actually write the HDM decoder MMIO.

CXL regions appear as linear addresses in the system's physical address space. CXL memory devices comprise the storage for the region. In order for traffic to be properly routed to the memory devices in the region, a set of Host-manged Device Memory decoders must be present. The decoders are a piece of hardware defined in the CXL specification.

---
Changes since v2:
- Fix unwind issue in bind_region introduced in v2

---
 drivers/cxl/core/hdm.c | 209 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |   3 +
 drivers/cxl/region.c   |  72 +++++++++++---
 3 files changed, 272 insertions(+), 12 deletions(-)
drivers/cxl/core/hdm.c +209 -0
index a28369f264da..66c08d69f7a6 100644
@@ -268,3 +268,212 @@ int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm)
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_decoders, CXL);
+
+#define COMMIT_TIMEOUT_MS 10
+static int wait_for_commit(struct cxl_decoder *cxld)
+{
+	const unsigned long end = jiffies + msecs_to_jiffies(COMMIT_TIMEOUT_MS);
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	void __iomem *hdm_decoder;
+	struct cxl_hdm *cxlhdm;
+	u32 ctrl;
+
+	cxlhdm = dev_get_drvdata(&port->dev);
+	hdm_decoder = cxlhdm->regs.hdm_decoder;
+
+	while (1) {
+		ctrl = readl(hdm_decoder +
+			     CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+		if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+			break;
+
+		if (time_after(jiffies, end)) {
+			dev_err(&cxld->dev, "HDM decoder commit timeout %x\n",
+				ctrl);
+			return -ETIMEDOUT;
+		}
+		if ((ctrl & CXL_HDM_DECODER0_CTRL_COMMIT_ERROR) != 0) {
+			dev_err(&cxld->dev, "HDM decoder commit error %x\n",
+				ctrl);
+			return -ENXIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * cxl_commit_decoder() - Program a configured cxl_decoder
+ * @cxld: The preconfigured cxl decoder.
+ *
+ * A cxl decoder that is to be committed should have been earmarked as enabled.
+ * This mechanism acts as a soft reservation on the decoder.
+ *
+ * Returns 0 if commit was successful, negative error code otherwise.
+ */
+int cxl_commit_decoder(struct cxl_decoder *cxld)
+{
+	u32 ctrl, tl_lo, tl_hi, base_lo, base_hi, size_lo, size_hi;
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	void __iomem *hdm_decoder;
+	struct cxl_hdm *cxlhdm;
+	int rc;
+
+	/*
+	 * Decoder flags are entirely software controlled and therefore this
+	 * case is purely a driver bug.
+	 */
+	if (dev_WARN_ONCE(&port->dev, (cxld->flags & CXL_DECODER_F_ENABLE) != 0,
+			  "Invalid %s enable state\n", dev_name(&cxld->dev)))
+		return -ENXIO;
+
+	cxlhdm = dev_get_drvdata(&port->dev);
+	hdm_decoder = cxlhdm->regs.hdm_decoder;
+	ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+	/*
+	 * A decoder that's currently active cannot be changed without the
+	 * system being quiesced. While the driver should prevent against this,
+	 * for a variety of reasons the hardware might not be in sync with the
+	 * hardware and so, do not splat on error.
+	 */
+	size_hi = readl(hdm_decoder +
+			CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+	size_lo =
+		readl(hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+	if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl) &&
+	    (size_lo + size_hi)) {
+		dev_err(&port->dev, "Tried to change an active decoder (%s)\n",
+			dev_name(&cxld->dev));
+		return -EBUSY;
+	}
+
+	u32p_replace_bits(&ctrl, cxl_to_ig(cxld->interleave_granularity),
+			  CXL_HDM_DECODER0_CTRL_IG_MASK);
+	u32p_replace_bits(&ctrl, cxl_to_eniw(cxld->interleave_ways),
+			  CXL_HDM_DECODER0_CTRL_IW_MASK);
+	u32p_replace_bits(&ctrl, 1, CXL_HDM_DECODER0_CTRL_COMMIT);
+
+	/* TODO: set based on type */
+	u32p_replace_bits(&ctrl, 1, CXL_HDM_DECODER0_CTRL_TYPE);
+
+	base_lo = GENMASK(31, 28) & lower_32_bits(cxld->decoder_range.start);
+	base_hi = upper_32_bits(cxld->decoder_range.start);
+
+	size_lo = GENMASK(31, 28) & (u32)(range_len(&cxld->decoder_range));
+	size_hi = upper_32_bits(range_len(&cxld->decoder_range) >> 32);
+
+	if (cxld->nr_targets > 0) {
+		tl_hi = 0;
+
+		tl_lo = FIELD_PREP(GENMASK(7, 0), cxld->target[0]->port_id);
+
+		if (cxld->interleave_ways > 1)
+			tl_lo |= FIELD_PREP(GENMASK(15, 8),
+					    cxld->target[1]->port_id);
+		if (cxld->interleave_ways > 2)
+			tl_lo |= FIELD_PREP(GENMASK(23, 16),
+					    cxld->target[2]->port_id);
+		if (cxld->interleave_ways > 3)
+			tl_lo |= FIELD_PREP(GENMASK(31, 24),
+					    cxld->target[3]->port_id);
+		if (cxld->interleave_ways > 4)
+			tl_hi |= FIELD_PREP(GENMASK(7, 0),
+					    cxld->target[4]->port_id);
+		if (cxld->interleave_ways > 5)
+			tl_hi |= FIELD_PREP(GENMASK(15, 8),
+					    cxld->target[5]->port_id);
+		if (cxld->interleave_ways > 6)
+			tl_hi |= FIELD_PREP(GENMASK(23, 16),
+					    cxld->target[6]->port_id);
+		if (cxld->interleave_ways > 7)
+			tl_hi |= FIELD_PREP(GENMASK(31, 24),
+					    cxld->target[7]->port_id);
+
+		writel(tl_hi, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+		writel(tl_lo, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));
+	} else {
+		/* Zero out skip list for devices */
+		writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+		writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));
+	}
+
+	writel(size_hi,
+	       hdm_decoder + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+	writel(size_lo,
+	       hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+	writel(base_hi,
+	       hdm_decoder + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(cxld->id));
+	writel(base_lo,
+	       hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(cxld->id));
+	writel(ctrl, hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+	rc = wait_for_commit(cxld);
+	if (rc)
+		return rc;
+
+	cxld->flags |= CXL_DECODER_F_ENABLE;
+
+#define DPORT_TL_STR "%d %d %d %d %d %d %d %d"
+#define DPORT(i)                                                               \
+	(cxld->nr_targets && cxld->interleave_ways > (i)) ?                    \
+		cxld->target[(i)]->port_id :                                   \
+		      -1
+#define DPORT_TL                                                               \
+	DPORT(0), DPORT(1), DPORT(2), DPORT(3), DPORT(4), DPORT(5), DPORT(6),  \
+		DPORT(7)
+
+	dev_dbg(&cxld->dev,
+		"%s (depth %d)\n\tBase %pa\n\tSize %llu\n\tIG %u (%ub)\n\tENIW %u (x%u)\n\tTargetList: \n" DPORT_TL_STR,
+		dev_name(&port->dev), port->depth, &cxld->decoder_range.start,
+		range_len(&cxld->decoder_range),
+		cxl_to_ig(cxld->interleave_granularity),
+		cxld->interleave_granularity,
+		cxl_to_eniw(cxld->interleave_ways), cxld->interleave_ways,
+		DPORT_TL);
+#undef DPORT_TL
+#undef DPORT
+#undef DPORT_TL_STR
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cxl_commit_decoder);
+
+/**
+ * cxl_disable_decoder() - Disables a decoder
+ * @cxld: The active cxl decoder.
+ *
+ * CXL decoders (as of 2.0 spec) have no way to deactivate them other than to
+ * set the size of the HDM to 0. This function will clear all registers, and if
+ * the decoder is active, commit the 0'd out registers.
+ */
+void cxl_disable_decoder(struct cxl_decoder *cxld)
+{
+	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+	void __iomem *hdm_decoder;
+	struct cxl_hdm *cxlhdm;
+	u32 ctrl;
+
+	cxlhdm = dev_get_drvdata(&port->dev);
+	hdm_decoder = cxlhdm->regs.hdm_decoder;
+	ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+	if (dev_WARN_ONCE(&port->dev, (cxld->flags & CXL_DECODER_F_ENABLE) == 0,
+			  "Invalid decoder enable state\n"))
+		return;
+
+	cxld->flags &= ~CXL_DECODER_F_ENABLE;
+
+	/* There's no way to "uncommit" a committed decoder, only 0 size it */
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(cxld->id));
+	writel(0, hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(cxld->id));
+
+	/* If the device isn't actually active, just zero out all the fields */
+	if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+		writel(CXL_HDM_DECODER0_CTRL_COMMIT,
+		       hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+}
+EXPORT_SYMBOL_GPL(cxl_disable_decoder);
drivers/cxl/cxl.h +3 -0
index d70d8c85d05f..f9dab312ed26 100644
@@ -55,6 +55,7 @@
 #define   CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
 #define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
 #define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)
 #define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
 #define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
 #define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
@@ -416,6 +417,8 @@ struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
 struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
 					const struct device *dev);
 struct cxl_port *ep_find_cxl_port(struct cxl_memdev *cxlmd, unsigned int depth);
+int cxl_commit_decoder(struct cxl_decoder *cxld);
+void cxl_disable_decoder(struct cxl_decoder *cxld);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_cxl_decoder(struct device *dev);
drivers/cxl/region.c +60 -12

90 lines not on this page · raw message

Signature · 1 line
2.35.0
#67 lore raw

Re: [PATCH v3 12/14] cxl: Program decoders for regions

Quoted 35 lines · Ben Widawsky

Configure and commit the HDM decoders for the region. Since the region driver already was able to walk the topology and build the list of needed decoders, all that was needed to finish region setup was to actually write the HDM decoder MMIO.

CXL regions appear as linear addresses in the system's physical address space. CXL memory devices comprise the storage for the region. In order for traffic to be properly routed to the memory devices in the region, a set of Host-manged Device Memory decoders must be present. The decoders are a piece of hardware defined in the CXL specification.

---
Changes since v2:
- Fix unwind issue in bind_region introduced in v2

---
 drivers/cxl/core/hdm.c | 209 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h      |   3 +
 drivers/cxl/region.c   |  72 +++++++++++---
 3 files changed, 272 insertions(+), 12 deletions(-)
drivers/cxl/core/hdm.c +4 -0
index a28369f264da..66c08d69f7a6 100644
@@ -268,3 +268,212 @@ int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm)
        return 0;
 }
 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_decoders, CXL);
+
+#define COMMIT_TIMEOUT_MS 10
+static int wait_for_commit(struct cxl_decoder *cxld)
+{

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 284

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+       const unsigned long end = jiffies + msecs_to_jiffies(COMMIT_TIMEOUT_MS);
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       void __iomem *hdm_decoder;
+       struct cxl_hdm *cxlhdm;
+       u32 ctrl;
+
+       cxlhdm = dev_get_drvdata(&port->dev);
+       hdm_decoder = cxlhdm->regs.hdm_decoder;
+
+       while (1) {

A live wait is too expensive. Also, given region programming is writing multiple decoders it would seem to make sense to amortize the waiting over multiple decoders. I.e. a flow like:

program all decoders
commit all decoders
10ms wait
check all decoder for commit timeouts

Quoted 58 lines
drivers/cxl/core/hdm.c +58 -0
+               ctrl = readl(hdm_decoder +
+                            CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+               if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+                       break;
+
+               if (time_after(jiffies, end)) {
+                       dev_err(&cxld->dev, "HDM decoder commit timeout %x\n",
+                               ctrl);
+                       return -ETIMEDOUT;
+               }
+               if ((ctrl & CXL_HDM_DECODER0_CTRL_COMMIT_ERROR) != 0) {
+                       dev_err(&cxld->dev, "HDM decoder commit error %x\n",
+                               ctrl);
+                       return -ENXIO;
+               }
+       }
+
+       return 0;
+}
+
+/**
+ * cxl_commit_decoder() - Program a configured cxl_decoder
+ * @cxld: The preconfigured cxl decoder.
+ *
+ * A cxl decoder that is to be committed should have been earmarked as enabled.
+ * This mechanism acts as a soft reservation on the decoder.
+ *
+ * Returns 0 if commit was successful, negative error code otherwise.
+ */
+int cxl_commit_decoder(struct cxl_decoder *cxld)
+{
+       u32 ctrl, tl_lo, tl_hi, base_lo, base_hi, size_lo, size_hi;
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       void __iomem *hdm_decoder;
+       struct cxl_hdm *cxlhdm;
+       int rc;
+
+       /*
+        * Decoder flags are entirely software controlled and therefore this
+        * case is purely a driver bug.
+        */
+       if (dev_WARN_ONCE(&port->dev, (cxld->flags & CXL_DECODER_F_ENABLE) != 0,
+                         "Invalid %s enable state\n", dev_name(&cxld->dev)))
+               return -ENXIO;
+
+       cxlhdm = dev_get_drvdata(&port->dev);
+       hdm_decoder = cxlhdm->regs.hdm_decoder;
+       ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+       /*
+        * A decoder that's currently active cannot be changed without the
+        * system being quiesced. While the driver should prevent against this,
+        * for a variety of reasons the hardware might not be in sync with the
+        * hardware and so, do not splat on error.
+        */
+       size_hi = readl(hdm_decoder +
+                       CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+       size_lo =

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 352

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+               readl(hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+       if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl) &&
+           (size_lo + size_hi)) {
+               dev_err(&port->dev, "Tried to change an active decoder (%s)\n",
+                       dev_name(&cxld->dev));
+               return -EBUSY;
+       }
+
+       u32p_replace_bits(&ctrl, cxl_to_ig(cxld->interleave_granularity),
+                         CXL_HDM_DECODER0_CTRL_IG_MASK);

Usage of u32p_replace_bits() makes me wonder what is worth preserving in the old control value? In this case the code is completely overwriting the old value so it can just do typical updates for a @ctrl variable initialized to zero at the start. Otherwise a comment is needed as to what fields in the current @ctrl need to be preserved after programming granularity, ways, type, and commit.

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 363

Quoted 11 lines
drivers/cxl/core/hdm.c +11 -0
+       u32p_replace_bits(&ctrl, cxl_to_eniw(cxld->interleave_ways),
+                         CXL_HDM_DECODER0_CTRL_IW_MASK);
+       u32p_replace_bits(&ctrl, 1, CXL_HDM_DECODER0_CTRL_COMMIT);
+
+       /* TODO: set based on type */
+       u32p_replace_bits(&ctrl, 1, CXL_HDM_DECODER0_CTRL_TYPE);
+
+       base_lo = GENMASK(31, 28) & lower_32_bits(cxld->decoder_range.start);
+       base_hi = upper_32_bits(cxld->decoder_range.start);
+
+       size_lo = GENMASK(31, 28) & (u32)(range_len(&cxld->decoder_range));

Why the cast vs just lower_32_bits(range_len(&cxld->decoder_range))?

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 364

Quoted 1 line
drivers/cxl/core/hdm.c +1 -0
+       size_hi = upper_32_bits(range_len(&cxld->decoder_range) >> 32);

Isn't this always 0? I would expect upper_32_bits() without the shift.

Quoted 22 lines
drivers/cxl/core/hdm.c +22 -0
+
+       if (cxld->nr_targets > 0) {
+               tl_hi = 0;
+
+               tl_lo = FIELD_PREP(GENMASK(7, 0), cxld->target[0]->port_id);
+
+               if (cxld->interleave_ways > 1)
+                       tl_lo |= FIELD_PREP(GENMASK(15, 8),
+                                           cxld->target[1]->port_id);
+               if (cxld->interleave_ways > 2)
+                       tl_lo |= FIELD_PREP(GENMASK(23, 16),
+                                           cxld->target[2]->port_id);
+               if (cxld->interleave_ways > 3)
+                       tl_lo |= FIELD_PREP(GENMASK(31, 24),
+                                           cxld->target[3]->port_id);
+               if (cxld->interleave_ways > 4)
+                       tl_hi |= FIELD_PREP(GENMASK(7, 0),
+                                           cxld->target[4]->port_id);
+               if (cxld->interleave_ways > 5)
+                       tl_hi |= FIELD_PREP(GENMASK(15, 8),
+                                           cxld->target[5]->port_id);
+               if (cxld->interleave_ways > 6)

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 396

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+                       tl_hi |= FIELD_PREP(GENMASK(23, 16),
+                                           cxld->target[6]->port_id);
+               if (cxld->interleave_ways > 7)
+                       tl_hi |= FIELD_PREP(GENMASK(31, 24),
+                                           cxld->target[7]->port_id);
+
+               writel(tl_hi, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+               writel(tl_lo, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));
+       } else {
+               /* Zero out skip list for devices */

Seems unwieldy to mix root and downstream port decoder programming in the same function as endpoint decoder programming. Let's keep them separate. They can of course share helpers, but I don't want the mental strain of reading this function and wondering what context it is being called, that should be evident from the function name.

Quoted 5 lines
drivers/cxl/core/hdm.c +5 -0
+               writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+               writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));
+       }
+
+       writel(size_hi,

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 411

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+              hdm_decoder + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+       writel(size_lo,
+              hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+       writel(base_hi,
+              hdm_decoder + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(cxld->id));
+       writel(base_lo,
+              hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(cxld->id));
+       writel(ctrl, hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+       rc = wait_for_commit(cxld);

per above, do you think it's workable to have a common wait for all decoders at least per level of the hierarcy, i.e.:

program root decoders, wait;
program switch decoders, wait;
program endpoint decoders, wait;

Quoted 15 lines
drivers/cxl/core/hdm.c +15 -0
+       if (rc)
+               return rc;
+
+       cxld->flags |= CXL_DECODER_F_ENABLE;
+
+#define DPORT_TL_STR "%d %d %d %d %d %d %d %d"
+#define DPORT(i)                                                               \
+       (cxld->nr_targets && cxld->interleave_ways > (i)) ?                    \
+               cxld->target[(i)]->port_id :                                   \
+                     -1
+#define DPORT_TL                                                               \
+       DPORT(0), DPORT(1), DPORT(2), DPORT(3), DPORT(4), DPORT(5), DPORT(6),  \
+               DPORT(7)
+
+       dev_dbg(&cxld->dev,

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 436

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+               "%s (depth %d)\n\tBase %pa\n\tSize %llu\n\tIG %u (%ub)\n\tENIW %u (x%u)\n\tTargetList: \n" DPORT_TL_STR,
+               dev_name(&port->dev), port->depth, &cxld->decoder_range.start,
+               range_len(&cxld->decoder_range),
+               cxl_to_ig(cxld->interleave_granularity),
+               cxld->interleave_granularity,
+               cxl_to_eniw(cxld->interleave_ways), cxld->interleave_ways,
+               DPORT_TL);
+#undef DPORT_TL
+#undef DPORT
+#undef DPORT_TL_STR

It just seems like all of this data is available in sysfs, does the kernel need to print it?

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 439

Quoted 3 lines
drivers/cxl/core/hdm.c +3 -0
+       return 0;
+}
+EXPORT_SYMBOL_GPL(cxl_commit_decoder);

EXPOER_SYMBOL_NS_GPL

Quoted 12 lines
drivers/cxl/core/hdm.c +12 -0
+
+/**
+ * cxl_disable_decoder() - Disables a decoder
+ * @cxld: The active cxl decoder.
+ *
+ * CXL decoders (as of 2.0 spec) have no way to deactivate them other than to
+ * set the size of the HDM to 0. This function will clear all registers, and if
+ * the decoder is active, commit the 0'd out registers.
+ */
+void cxl_disable_decoder(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 461

Quoted 10 lines
drivers/cxl/core/hdm.c +10 -0
+       void __iomem *hdm_decoder;
+       struct cxl_hdm *cxlhdm;
+       u32 ctrl;
+
+       cxlhdm = dev_get_drvdata(&port->dev);
+       hdm_decoder = cxlhdm->regs.hdm_decoder;
+       ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+
+       if (dev_WARN_ONCE(&port->dev, (cxld->flags & CXL_DECODER_F_ENABLE) == 0,
+                         "Invalid decoder enable state\n"))

Why crash the kernel when trying to disable a disabled decoder?

Where does the "locked" check happen?

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 468

Quoted 7 lines
drivers/cxl/core/hdm.c +7 -0
+               return;
+
+       cxld->flags &= ~CXL_DECODER_F_ENABLE;
+
+       /* There's no way to "uncommit" a committed decoder, only 0 size it */
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_HIGH(cxld->id));
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_TL_LOW(cxld->id));

Shouldn't size be first just in case there are cycles still in flight?

drivers/cxl/core/hdm.c int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm) · line 474

Quoted 6 lines
drivers/cxl/core/hdm.c +6 -0
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(cxld->id));
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(cxld->id));
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(cxld->id));
+       writel(0, hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(cxld->id));
+
+       /* If the device isn't actually active, just zero out all the fields */

Why not unconditionally try to commit the new size here? I'd be ok to keep this policy as you have it, but the comment needs to be changed to indicate "why?", not "what?" because the code answers the latter.

Quoted 31 lines
drivers/cxl/core/hdm.c +5 -0
+       if (FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               writel(CXL_HDM_DECODER0_CTRL_COMMIT,
+                      hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
+}
+EXPORT_SYMBOL_GPL(cxl_disable_decoder);
drivers/cxl/cxl.h +3 -0
index d70d8c85d05f..f9dab312ed26 100644
@@ -55,6 +55,7 @@
 #define   CXL_HDM_DECODER0_CTRL_LOCK BIT(8)
 #define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
 #define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)
 #define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
 #define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
 #define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
@@ -416,6 +417,8 @@ struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
 struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
                                        const struct device *dev);
 struct cxl_port *ep_find_cxl_port(struct cxl_memdev *cxlmd, unsigned int depth);
+int cxl_commit_decoder(struct cxl_decoder *cxld);
+void cxl_disable_decoder(struct cxl_decoder *cxld);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_cxl_decoder(struct device *dev);
drivers/cxl/region.c +0 -0
index f748060733dd..ac290677534d 100644

drivers/cxl/region.c static int collect_ep_decoders(struct cxl_region *cxlr) · line 681

Quoted 4 lines
drivers/cxl/region.c +0 -1
@@ -678,10 +678,52 @@ static int collect_ep_decoders(struct cxl_region *cxlr)
        return rc;
 }
 
-static int bind_region(const struct cxl_region *cxlr)

Try not to change the function signature twice in the same patch series, i.e. make it non-const at the outset... although I'm not sure this function survives the new ABI that is assigning decoders to regions as that would also impact the need to have these ->staged_list and ->commit_list trackers.

Quoted 29 lines
drivers/cxl/region.c +26 -2
+static int bind_region(struct cxl_region *cxlr)
 {
-       /* TODO: */
-       return 0;
+       struct cxl_decoder *cxld, *d;
+       int rc;
+
+       list_for_each_entry_safe(cxld, d, &cxlr->staged_list, region_link) {
+               rc = cxl_commit_decoder(cxld);
+               if (!rc) {
+                       list_move_tail(&cxld->region_link, &cxlr->commit_list);
+               } else {
+                       dev_dbg(&cxlr->dev, "Failed to commit %s\n",
+                               dev_name(&cxld->dev));
+                       break;
+               }
+       }
+
+       list_for_each_entry_safe(cxld, d, &cxlr->commit_list, region_link) {
+               if (rc) {
+                       cxl_disable_decoder(cxld);
+                       list_del(&cxld->region_link);
+               }
+       }
+
+       if (rc)
+               cleanup_staged_decoders(cxlr);
+
+       BUG_ON(!list_empty(&cxlr->staged_list));

drivers/cxl/region.c static int collect_ep_decoders(struct cxl_region *cxlr) · line 717

Quoted 10 lines
drivers/cxl/region.c +10 -0
+       return rc;
+}
+
+static void region_unregister(void *dev)
+{
+       struct cxl_region *region = to_cxl_region(dev);
+       struct cxl_decoder *cxld, *d;
+
+       if (dev_WARN_ONCE(dev, !list_empty(&region->staged_list),
+                         "Decoders still staged"))

If I am reading this correctly, why is it a programming error to unregister a region mid-programming? Certainly userspace could abort a create attempt and delete after adding some targets, but not others.

Quoted 51 lines
drivers/cxl/region.c +24 -10
+               cleanup_staged_decoders(region);
+
+       /* TODO: teardown the nd_region */
+
+       list_for_each_entry_safe(cxld, d, &region->commit_list, region_link) {
+               cxl_disable_decoder(cxld);
+               list_del(&cxld->region_link);
+               cxl_put_decoder(cxld);
+       }
 }
 
 static int cxl_region_probe(struct device *dev)
@@ -732,20 +774,26 @@ static int cxl_region_probe(struct device *dev)
                put_device(&ours->dev);
 
        ret = collect_ep_decoders(cxlr);
-       if (ret)
-               goto err;
+       if (ret) {
+               cleanup_staged_decoders(cxlr);
+               return ret;
+       }
 
        ret = bind_region(cxlr);
-       if (ret)
-               goto err;
+       if (ret) {
+               /* bind_region should cleanup after itself */
+               if (dev_WARN_ONCE(dev, !list_empty(&cxlr->staged_list),
+                                 "Region bind failed to cleanup staged decoders\n"))
+                       cleanup_staged_decoders(cxlr);
+               if (dev_WARN_ONCE(dev, !list_empty(&cxlr->commit_list),
+                                 "Region bind failed to cleanup committed decoders\n"))
+                       region_unregister(&cxlr->dev);
+               return ret;
+       }
 
        cxlr->active = true;
        dev_info(dev, "Bound");
-       return 0;
-
-err:
-       cleanup_staged_decoders(cxlr);
-       return ret;
+       return devm_add_action_or_reset(dev, region_unregister, dev);
 }
 
 static struct cxl_driver cxl_region_driver = {
--

2.35.0

#68 lore raw

PATCH v3 13/14 cxl/pmem: Convert nvdimm bridge API to use dev

In reply to Ben Widawsky

The cxl_pmem driver specific cxl_nvdimm structure isn't a suitable parameter for an exported API that can be used by other drivers. Instead, use a dev structure, which should be woven into any caller using this API. This will allow for either the nvdimm's dev, or the memdev's dev to be used.

---
Changes since v2:
- Added kdoc to cxl_find_nvdimm_bridge()

---
 drivers/cxl/core/pmem.c | 12 +++++++++---
 drivers/cxl/cxl.h       |  2 +-
 drivers/cxl/pmem.c      |  2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
drivers/cxl/core/pmem.c +9 -3
index 8de240c4d96b..7e431667ade1 100644
@@ -62,10 +62,16 @@ static int match_nvdimm_bridge(struct device *dev, void *data)
 	return is_cxl_nvdimm_bridge(dev);
 }
 
-struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_nvdimm *cxl_nvd)
+/**
+ * cxl_find_nvdimm_bridge() - Find an nvdimm bridge for a given device
+ * @dev: The device to find a bridge for. This device must be in the part of the
+ *	 CXL topology which is being bridged.
+ *
+ * Return: bridge device that hosts cxl_nvdimm objects if found, else NULL.
+ */
+struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct device *dev)
 {
-	struct cxl_port *port = find_cxl_root(&cxl_nvd->dev);
-	struct device *dev;
+	struct cxl_port *port = find_cxl_root(dev);
 
 	if (!port)
 		return NULL;
drivers/cxl/cxl.h +1 -1
index f9dab312ed26..062654204eca 100644
@@ -479,7 +479,7 @@ struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
 bool is_cxl_nvdimm(struct device *dev);
 bool is_cxl_nvdimm_bridge(struct device *dev);
 int devm_cxl_add_nvdimm(struct device *host, struct cxl_memdev *cxlmd);
-struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_nvdimm *cxl_nvd);
+struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct device *dev);
 
 /*
  * Unit test builds overrides this to __weak, find the 'strong' version
drivers/cxl/pmem.c +1 -1
index 15ad666ab03e..fabdb0c6dbf2 100644
@@ -39,7 +39,7 @@ static int cxl_nvdimm_probe(struct device *dev)
 	struct nvdimm *nvdimm;
 	int rc;
 
-	cxl_nvb = cxl_find_nvdimm_bridge(cxl_nvd);
+	cxl_nvb = cxl_find_nvdimm_bridge(&cxl_nvd->dev);
 	if (!cxl_nvb)
 		return -ENXIO;
 
Signature · 1 line
2.35.0
#69 lore raw

PATCH v3 14/14 cxl/region: Create an nd_region

In reply to Ben Widawsky

LIBNVDIMM supports the creation of regions for both persistent and volatile memory ranges. The cxl_region driver is capable of handling the CXL side of region creation but will reuse LIBVDIMM for interfacing with the rest of the kernel.

TODO: CXL regions can go away. As a result the nd_region must also be torn down.

TODO2: Handle mappings. LIBNVDIMM is capable of being informed about which parts of devices contribute to a region and validating whether or not the region is configured properly. To do this properly requires tracking allocations per device.

---
Changes since v2:
- Check nvb is non-null
- Give a dev_dbg for non-existent nvdimm_bus

---
 drivers/cxl/Kconfig     |  3 ++-
 drivers/cxl/core/pmem.c | 16 ++++++++++++
 drivers/cxl/cxl.h       |  1 +
 drivers/cxl/region.c    | 58 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 1 deletion(-)
drivers/cxl/Kconfig +2 -1
index 742847503c16..054dc78d6f7d 100644
@@ -99,7 +99,8 @@ config CXL_PORT
 	tristate
 
 config CXL_REGION
-	default CXL_PORT
+	depends on CXL_PMEM
+	default CXL_BUS
 	tristate
 
 endif
drivers/cxl/core/pmem.c +16 -0
index 7e431667ade1..58dc6fba3130 100644
@@ -220,6 +220,22 @@ struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev)
 }
 EXPORT_SYMBOL_NS_GPL(to_cxl_nvdimm, CXL);
 
+static int match_cxl_nvdimm(struct device *dev, void *data)
+{
+	return is_cxl_nvdimm(dev);
+}
+
+struct cxl_nvdimm *cxl_find_nvdimm(struct cxl_memdev *cxlmd)
+{
+	struct device *dev;
+
+	dev = device_find_child(&cxlmd->dev, NULL, match_cxl_nvdimm);
+	if (!dev)
+		return NULL;
+	return to_cxl_nvdimm(dev);
+}
+EXPORT_SYMBOL_NS_GPL(cxl_find_nvdimm, CXL);
+
 static struct cxl_nvdimm *cxl_nvdimm_alloc(struct cxl_memdev *cxlmd)
 {
 	struct cxl_nvdimm *cxl_nvd;
drivers/cxl/cxl.h +1 -0
index 062654204eca..7eb8f36af30b 100644
@@ -480,6 +480,7 @@ bool is_cxl_nvdimm(struct device *dev);
 bool is_cxl_nvdimm_bridge(struct device *dev);
 int devm_cxl_add_nvdimm(struct device *host, struct cxl_memdev *cxlmd);
 struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct device *dev);
+struct cxl_nvdimm *cxl_find_nvdimm(struct cxl_memdev *cxlmd);
 
 /*
  * Unit test builds overrides this to __weak, find the 'strong' version
drivers/cxl/region.c +58 -0

70 lines not on this page · raw message

Signature · 1 line
2.35.0