Blame SOURCES/0194-libcxl-add-low-level-APIs-for-region-creation.patch

26ccd9
From cafe4b2d4970b0d7f2193abb9cb32f58c03cbe3b Mon Sep 17 00:00:00 2001
26ccd9
From: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
Date: Mon, 15 Aug 2022 13:22:09 -0600
26ccd9
Subject: [PATCH 194/217] libcxl: add low level APIs for region creation
26ccd9
26ccd9
Add libcxl APIs to create a region under a given root decoder, and to
26ccd9
set different attributes for the new region. These allow setting the
26ccd9
size, interleave_ways, interleave_granularity, uuid, and the target
26ccd9
devices for the newly minted cxl_region object.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/20220815192214.545800-7-vishal.l.verma@intel.com
26ccd9
Cc: Dan Williams <dan.j.williams@intel.com>
26ccd9
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 Documentation/cxl/lib/libcxl.txt |  69 ++++++
26ccd9
 cxl/lib/libcxl.c                 | 381 ++++++++++++++++++++++++++++++-
26ccd9
 cxl/lib/libcxl.sym               |  16 ++
26ccd9
 cxl/lib/private.h                |   2 +
26ccd9
 cxl/libcxl.h                     |  23 +-
26ccd9
 5 files changed, 488 insertions(+), 3 deletions(-)
26ccd9
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index 7a38ce4..50b0c9c 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -508,6 +508,75 @@ device to represent the root of a PCI device hierarchy. The
26ccd9
 cxl_target_get_physical_node() helper returns the device name of that
26ccd9
 companion object in the PCI hierarchy.
26ccd9
 
26ccd9
+==== REGIONS
26ccd9
+A CXL region is composed of one or more slices of CXL memdevs, with configurable
26ccd9
+interleave settings - both the number of interleave ways, and the interleave
26ccd9
+granularity. In terms of hierarchy, it is the child of a CXL root decoder. A root
26ccd9
+decoder (recall that this corresponds to an ACPI CEDT.CFMWS 'window'), may have
26ccd9
+multiple child regions, but a region is strictly tied to one root decoder.
26ccd9
+
26ccd9
+The slices that compose a region are called mappings. A mapping is a
26ccd9
+tuple of 'memdev', 'endpoint decoder', and the 'position'.
26ccd9
+
26ccd9
+===== REGION: Enumeration
26ccd9
+----
26ccd9
+struct cxl_region *cxl_region_get_first(struct cxl_decoder *decoder);
26ccd9
+struct cxl_region *cxl_region_get_next(struct cxl_region *region);
26ccd9
+
26ccd9
+#define cxl_region_foreach(decoder, region)                                    \
26ccd9
+	for (region = cxl_region_get_first(decoder); region != NULL;           \
26ccd9
+	     region = cxl_region_get_next(region))
26ccd9
+
26ccd9
+#define cxl_region_foreach_safe(decoder, region, _region)                      \
26ccd9
+	for (region = cxl_region_get_first(decoder),                           \
26ccd9
+	     _region = region ? cxl_region_get_next(region) : NULL;            \
26ccd9
+	     region != NULL;                                                   \
26ccd9
+	     region = _region,                                                 \
26ccd9
+	     _region = _region ? cxl_region_get_next(_region) : NULL)
26ccd9
+----
26ccd9
+
26ccd9
+===== REGION: Attributes
26ccd9
+----
26ccd9
+int cxl_region_get_id(struct cxl_region *region);
26ccd9
+const char *cxl_region_get_devname(struct cxl_region *region);
26ccd9
+void cxl_region_get_uuid(struct cxl_region *region, uuid_t uu);
26ccd9
+unsigned long long cxl_region_get_size(struct cxl_region *region);
26ccd9
+unsigned long long cxl_region_get_resource(struct cxl_region *region);
26ccd9
+unsigned int cxl_region_get_interleave_ways(struct cxl_region *region);
26ccd9
+unsigned int cxl_region_get_interleave_granularity(struct cxl_region *region);
26ccd9
+struct cxl_decoder *cxl_region_get_target_decoder(struct cxl_region *region,
26ccd9
+						  int position);
26ccd9
+int cxl_region_set_size(struct cxl_region *region, unsigned long long size);
26ccd9
+int cxl_region_set_uuid(struct cxl_region *region, uuid_t uu);
26ccd9
+int cxl_region_set_interleave_ways(struct cxl_region *region,
26ccd9
+				   unsigned int ways);
26ccd9
+int cxl_region_set_interleave_granularity(struct cxl_region *region,
26ccd9
+					  unsigned int granularity);
26ccd9
+int cxl_region_set_target(struct cxl_region *region, int position,
26ccd9
+			  struct cxl_decoder *decoder);
26ccd9
+int cxl_region_clear_target(struct cxl_region *region, int position);
26ccd9
+int cxl_region_clear_all_targets(struct cxl_region *region);
26ccd9
+int cxl_region_decode_commit(struct cxl_region *region);
26ccd9
+int cxl_region_decode_reset(struct cxl_region *region);
26ccd9
+----
26ccd9
+
26ccd9
+A region's resource attribute is the Host Physical Address at which the region's
26ccd9
+address space starts. The region's address space is a subset of the parent root
26ccd9
+decoder's address space.
26ccd9
+
26ccd9
+The interleave ways is the number of component memdevs participating in the
26ccd9
+region.
26ccd9
+
26ccd9
+The interleave granularity depends on the root decoder's granularity, and must
26ccd9
+follow the interleave math rules defined in the CXL spec.
26ccd9
+
26ccd9
+Regions have a list of targets 0..N, which are programmed with the name of an
26ccd9
+endpoint decoder under each participating memdev.
26ccd9
+
26ccd9
+The 'decode_commit' and 'decode_reset' attributes reserve and free DPA space
26ccd9
+on a given memdev by allocating an endpoint decoder, and programming it based
26ccd9
+on the region's interleave geometry.
26ccd9
+
26ccd9
 include::../../copyright.txt[]
26ccd9
 
26ccd9
 SEE ALSO
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index ad98188..fd2ea4f 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -17,6 +17,7 @@
26ccd9
 #include <ccan/minmax/minmax.h>
26ccd9
 #include <ccan/array_size/array_size.h>
26ccd9
 #include <ccan/short_types/short_types.h>
26ccd9
+#include <ccan/container_of/container_of.h>
26ccd9
 
26ccd9
 #include <util/log.h>
26ccd9
 #include <util/list.h>
26ccd9
@@ -420,6 +421,40 @@ CXL_EXPORT int cxl_region_enable(struct cxl_region *region)
26ccd9
 	return 0;
26ccd9
 }
26ccd9
 
26ccd9
+static int cxl_region_delete_name(struct cxl_decoder *decoder,
26ccd9
+				  const char *devname)
26ccd9
+{
26ccd9
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
26ccd9
+	char *path = decoder->dev_buf;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	sprintf(path, "%s/delete_region", decoder->dev_path);
26ccd9
+	rc = sysfs_write_attr(ctx, path, devname);
26ccd9
+	if (rc != 0) {
26ccd9
+		err(ctx, "error deleting region: %s\n", strerror(-rc));
26ccd9
+		return rc;
26ccd9
+	}
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_delete(struct cxl_region *region)
26ccd9
+{
26ccd9
+	struct cxl_decoder *decoder = cxl_region_get_decoder(region);
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (cxl_region_is_enabled(region))
26ccd9
+		return -EBUSY;
26ccd9
+
26ccd9
+	rc = cxl_region_delete_name(decoder, devname);
26ccd9
+	if (rc != 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	decoder->regions_init = 0;
26ccd9
+	free_region(region, &decoder->regions);
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
 static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
26ccd9
 {
26ccd9
 	const char *devname = devpath_to_devname(cxlregion_base);
26ccd9
@@ -599,6 +634,258 @@ cxl_region_get_interleave_granularity(struct cxl_region *region)
26ccd9
 	return region->interleave_granularity;
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT struct cxl_decoder *
26ccd9
+cxl_region_get_target_decoder(struct cxl_region *region, int position)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	struct cxl_decoder *decoder;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/target%d", region->dev_path, position) >=
26ccd9
+	    len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = sysfs_read_attr(ctx, path, buf);
26ccd9
+	if (rc < 0) {
26ccd9
+		err(ctx, "%s: error reading target%d: %s\n", devname,
26ccd9
+		    position, strerror(-rc));
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	decoder = cxl_decoder_get_by_name(ctx, buf);
26ccd9
+	if (!decoder) {
26ccd9
+		err(ctx, "%s: error locating decoder for target%d\n", devname,
26ccd9
+		    position);
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+	return decoder;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_set_size(struct cxl_region *region,
26ccd9
+				   unsigned long long size)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (size == 0) {
26ccd9
+		dbg(ctx, "%s: cannot use %s to delete a region\n", __func__,
26ccd9
+		    devname);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/size", region->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	sprintf(buf, "%#llx\n", size);
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	region->size = size;
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_set_uuid(struct cxl_region *region, uuid_t uu)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	char uuid[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/uuid", region->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	uuid_unparse(uu, uuid);
26ccd9
+	rc = sysfs_write_attr(ctx, path, uuid);
26ccd9
+	if (rc != 0)
26ccd9
+		return rc;
26ccd9
+	memcpy(region->uuid, uu, sizeof(uuid_t));
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_set_interleave_ways(struct cxl_region *region,
26ccd9
+					      unsigned int ways)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/interleave_ways",
26ccd9
+		     region->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	sprintf(buf, "%u\n", ways);
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	region->interleave_ways = ways;
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_set_interleave_granularity(struct cxl_region *region,
26ccd9
+						     unsigned int granularity)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/interleave_granularity",
26ccd9
+		     region->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	sprintf(buf, "%u\n", granularity);
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	region->interleave_granularity = granularity;
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+static int region_write_target(struct cxl_region *region, int position,
26ccd9
+			       struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	const char *dec_name = "";
26ccd9
+
26ccd9
+	if (decoder)
26ccd9
+		dec_name = cxl_decoder_get_devname(decoder);
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/target%d", region->dev_path, position) >=
26ccd9
+	    len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = sysfs_write_attr(ctx, path, dec_name);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_set_target(struct cxl_region *region, int position,
26ccd9
+				     struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	if (!decoder)
26ccd9
+		return -ENXIO;
26ccd9
+
26ccd9
+	return region_write_target(region, position, decoder);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_clear_target(struct cxl_region *region, int position)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (cxl_region_is_enabled(region)) {
26ccd9
+		err(ctx, "%s: can't clear targets on an active region\n",
26ccd9
+		    devname);
26ccd9
+		return -EBUSY;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = region_write_target(region, position, NULL);
26ccd9
+	if (rc) {
26ccd9
+		err(ctx, "%s: error clearing target%d: %s\n",
26ccd9
+		    devname, position, strerror(-rc));
26ccd9
+		return rc;
26ccd9
+	}
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_clear_all_targets(struct cxl_region *region)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	unsigned int ways, i;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (cxl_region_is_enabled(region)) {
26ccd9
+		err(ctx, "%s: can't clear targets on an active region\n",
26ccd9
+		    devname);
26ccd9
+		return -EBUSY;
26ccd9
+	}
26ccd9
+
26ccd9
+	ways = cxl_region_get_interleave_ways(region);
26ccd9
+	if (ways == 0 || ways == UINT_MAX)
26ccd9
+		return -ENXIO;
26ccd9
+
26ccd9
+	for (i = 0; i < ways; i++) {
26ccd9
+		rc = region_write_target(region, i, NULL);
26ccd9
+		if (rc) {
26ccd9
+			err(ctx, "%s: error clearing target%d: %s\n",
26ccd9
+			    devname, i, strerror(-rc));
26ccd9
+			return rc;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+static int set_region_decode(struct cxl_region *region,
26ccd9
+			     enum cxl_decode_state decode_state)
26ccd9
+{
26ccd9
+	const char *devname = cxl_region_get_devname(region);
26ccd9
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
26ccd9
+	int len = region->buf_len, rc;
26ccd9
+	char *path = region->dev_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/commit", region->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	sprintf(buf, "%d\n", decode_state);
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	region->decode_state = decode_state;
26ccd9
+
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_decode_commit(struct cxl_region *region)
26ccd9
+{
26ccd9
+	return set_region_decode(region, CXL_DECODE_COMMIT);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_region_decode_reset(struct cxl_region *region)
26ccd9
+{
26ccd9
+	return set_region_decode(region, CXL_DECODE_RESET);
26ccd9
+}
26ccd9
+
26ccd9
 static struct cxl_decoder *__cxl_port_match_decoder(struct cxl_port *port,
26ccd9
 						    const char *ident)
26ccd9
 {
26ccd9
@@ -633,8 +920,8 @@ static struct cxl_decoder *cxl_port_find_decoder(struct cxl_port *port,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
-static struct cxl_decoder *cxl_decoder_get_by_name(struct cxl_ctx *ctx,
26ccd9
-						   const char *ident)
26ccd9
+CXL_EXPORT struct cxl_decoder *cxl_decoder_get_by_name(struct cxl_ctx *ctx,
26ccd9
+						       const char *ident)
26ccd9
 {
26ccd9
 	struct cxl_bus *bus;
26ccd9
 
26ccd9
@@ -1399,6 +1686,18 @@ static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
26ccd9
 	} else
26ccd9
 		decoder->mode = CXL_DECODER_MODE_NONE;
26ccd9
 
26ccd9
+	sprintf(path, "%s/interleave_granularity", cxldecoder_base);
26ccd9
+	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
+		decoder->interleave_granularity = UINT_MAX;
26ccd9
+	else
26ccd9
+		decoder->interleave_granularity = strtoul(buf, NULL, 0);
26ccd9
+
26ccd9
+	sprintf(path, "%s/interleave_ways", cxldecoder_base);
26ccd9
+	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
+		decoder->interleave_ways = UINT_MAX;
26ccd9
+	else
26ccd9
+		decoder->interleave_ways = strtoul(buf, NULL, 0);
26ccd9
+
26ccd9
 	switch (port->type) {
26ccd9
 	case CXL_PORT_ENDPOINT:
26ccd9
 		sprintf(path, "%s/dpa_resource", cxldecoder_base);
26ccd9
@@ -1731,6 +2030,66 @@ CXL_EXPORT bool cxl_decoder_is_locked(struct cxl_decoder *decoder)
26ccd9
 	return decoder->locked;
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT unsigned int
26ccd9
+cxl_decoder_get_interleave_granularity(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->interleave_granularity;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT unsigned int
26ccd9
+cxl_decoder_get_interleave_ways(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->interleave_ways;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_region *
26ccd9
+cxl_decoder_create_pmem_region(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
26ccd9
+	char *path = decoder->dev_buf;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+	struct cxl_region *region;
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	sprintf(path, "%s/create_pmem_region", decoder->dev_path);
26ccd9
+	rc = sysfs_read_attr(ctx, path, buf);
26ccd9
+	if (rc < 0) {
26ccd9
+		err(ctx, "failed to read new region name: %s\n",
26ccd9
+		    strerror(-rc));
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0) {
26ccd9
+		err(ctx, "failed to write new region name: %s\n",
26ccd9
+		    strerror(-rc));
26ccd9
+		return NULL;
26ccd9
+	}
26ccd9
+
26ccd9
+	/* Force a re-init of regions so that the new one can be discovered */
26ccd9
+	decoder->regions_init = 0;
26ccd9
+
26ccd9
+	/* create_region was successful, walk to the new region */
26ccd9
+	cxl_region_foreach(decoder, region) {
26ccd9
+		const char *devname = cxl_region_get_devname(region);
26ccd9
+
26ccd9
+		if (strcmp(devname, buf) == 0)
26ccd9
+			goto found;
26ccd9
+	}
26ccd9
+
26ccd9
+	/*
26ccd9
+	 * If walking to the region we just created failed, something has gone
26ccd9
+	 * very wrong. Attempt to delete it to avoid leaving a dangling region
26ccd9
+	 * id behind.
26ccd9
+	 */
26ccd9
+	err(ctx, "failed to add new region to libcxl\n");
26ccd9
+	cxl_region_delete_name(decoder, buf);
26ccd9
+	return NULL;
26ccd9
+
26ccd9
+ found:
26ccd9
+	return region;
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT int cxl_decoder_get_nr_targets(struct cxl_decoder *decoder)
26ccd9
 {
26ccd9
 	return decoder->nr_targets;
26ccd9
@@ -1741,6 +2100,24 @@ CXL_EXPORT const char *cxl_decoder_get_devname(struct cxl_decoder *decoder)
26ccd9
 	return devpath_to_devname(decoder->dev_path);
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT struct cxl_memdev *
26ccd9
+cxl_decoder_get_memdev(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	struct cxl_endpoint *ep;
26ccd9
+
26ccd9
+	if (!port)
26ccd9
+		return NULL;
26ccd9
+	if (!cxl_port_is_endpoint(port))
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	ep = container_of(port, struct cxl_endpoint, port);
26ccd9
+	if (!ep)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	return cxl_endpoint_get_memdev(ep);
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT struct cxl_target *cxl_target_get_first(struct cxl_decoder *decoder)
26ccd9
 {
26ccd9
 	return list_top(&decoder->targets, struct cxl_target, list);
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index e410298..cb23a0b 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -140,6 +140,7 @@ global:
26ccd9
 	cxl_decoder_is_mem_capable;
26ccd9
 	cxl_decoder_is_accelmem_capable;
26ccd9
 	cxl_decoder_is_locked;
26ccd9
+	cxl_decoder_create_pmem_region;
26ccd9
 	cxl_target_get_first;
26ccd9
 	cxl_target_get_next;
26ccd9
 	cxl_target_get_decoder;
26ccd9
@@ -183,6 +184,7 @@ global:
26ccd9
 	cxl_region_is_enabled;
26ccd9
 	cxl_region_disable;
26ccd9
 	cxl_region_enable;
26ccd9
+	cxl_region_delete;
26ccd9
 	cxl_region_get_ctx;
26ccd9
 	cxl_region_get_decoder;
26ccd9
 	cxl_region_get_id;
26ccd9
@@ -192,9 +194,23 @@ global:
26ccd9
 	cxl_region_get_resource;
26ccd9
 	cxl_region_get_interleave_ways;
26ccd9
 	cxl_region_get_interleave_granularity;
26ccd9
+	cxl_region_get_target_decoder;
26ccd9
+	cxl_region_set_size;
26ccd9
+	cxl_region_set_uuid;
26ccd9
+	cxl_region_set_interleave_ways;
26ccd9
+	cxl_region_set_interleave_granularity;
26ccd9
+	cxl_region_set_target;
26ccd9
+	cxl_region_clear_target;
26ccd9
+	cxl_region_clear_all_targets;
26ccd9
+	cxl_region_decode_commit;
26ccd9
+	cxl_region_decode_reset;
26ccd9
 	cxl_mapping_get_first;
26ccd9
 	cxl_mapping_get_next;
26ccd9
 	cxl_mapping_get_decoder;
26ccd9
 	cxl_mapping_get_region;
26ccd9
 	cxl_mapping_get_position;
26ccd9
+	cxl_decoder_get_by_name;
26ccd9
+	cxl_decoder_get_memdev;
26ccd9
+	cxl_decoder_get_interleave_granularity;
26ccd9
+	cxl_decoder_get_interleave_ways;
26ccd9
 } LIBCXL_2;
26ccd9
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
26ccd9
index 5e2fdd5..8bc9620 100644
26ccd9
--- a/cxl/lib/private.h
26ccd9
+++ b/cxl/lib/private.h
26ccd9
@@ -110,6 +110,8 @@ struct cxl_decoder {
26ccd9
 	int nr_targets;
26ccd9
 	int id;
26ccd9
 	enum cxl_decoder_mode mode;
26ccd9
+	unsigned int interleave_ways;
26ccd9
+	unsigned int interleave_granularity;
26ccd9
 	bool pmem_capable;
26ccd9
 	bool volatile_capable;
26ccd9
 	bool mem_capable;
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index 19d94e4..69d9c09 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -195,7 +195,13 @@ bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder);
26ccd9
 bool cxl_decoder_is_mem_capable(struct cxl_decoder *decoder);
26ccd9
 bool cxl_decoder_is_accelmem_capable(struct cxl_decoder *decoder);
26ccd9
 bool cxl_decoder_is_locked(struct cxl_decoder *decoder);
26ccd9
-
26ccd9
+unsigned int
26ccd9
+cxl_decoder_get_interleave_granularity(struct cxl_decoder *decoder);
26ccd9
+unsigned int cxl_decoder_get_interleave_ways(struct cxl_decoder *decoder);
26ccd9
+struct cxl_region *cxl_decoder_create_pmem_region(struct cxl_decoder *decoder);
26ccd9
+struct cxl_decoder *cxl_decoder_get_by_name(struct cxl_ctx *ctx,
26ccd9
+					    const char *ident);
26ccd9
+struct cxl_memdev *cxl_decoder_get_memdev(struct cxl_decoder *decoder);
26ccd9
 #define cxl_decoder_foreach(port, decoder)                                     \
26ccd9
 	for (decoder = cxl_decoder_get_first(port); decoder != NULL;           \
26ccd9
 	     decoder = cxl_decoder_get_next(decoder))
26ccd9
@@ -244,6 +250,7 @@ int cxl_region_decode_is_committed(struct cxl_region *region);
26ccd9
 int cxl_region_is_enabled(struct cxl_region *region);
26ccd9
 int cxl_region_disable(struct cxl_region *region);
26ccd9
 int cxl_region_enable(struct cxl_region *region);
26ccd9
+int cxl_region_delete(struct cxl_region *region);
26ccd9
 struct cxl_ctx *cxl_region_get_ctx(struct cxl_region *region);
26ccd9
 struct cxl_decoder *cxl_region_get_decoder(struct cxl_region *region);
26ccd9
 int cxl_region_get_id(struct cxl_region *region);
26ccd9
@@ -253,6 +260,20 @@ unsigned long long cxl_region_get_size(struct cxl_region *region);
26ccd9
 unsigned long long cxl_region_get_resource(struct cxl_region *region);
26ccd9
 unsigned int cxl_region_get_interleave_ways(struct cxl_region *region);
26ccd9
 unsigned int cxl_region_get_interleave_granularity(struct cxl_region *region);
26ccd9
+struct cxl_decoder *cxl_region_get_target_decoder(struct cxl_region *region,
26ccd9
+						  int position);
26ccd9
+int cxl_region_set_size(struct cxl_region *region, unsigned long long size);
26ccd9
+int cxl_region_set_uuid(struct cxl_region *region, uuid_t uu);
26ccd9
+int cxl_region_set_interleave_ways(struct cxl_region *region,
26ccd9
+				   unsigned int ways);
26ccd9
+int cxl_region_set_interleave_granularity(struct cxl_region *region,
26ccd9
+					  unsigned int granularity);
26ccd9
+int cxl_region_set_target(struct cxl_region *region, int position,
26ccd9
+			  struct cxl_decoder *decoder);
26ccd9
+int cxl_region_clear_target(struct cxl_region *region, int position);
26ccd9
+int cxl_region_clear_all_targets(struct cxl_region *region);
26ccd9
+int cxl_region_decode_commit(struct cxl_region *region);
26ccd9
+int cxl_region_decode_reset(struct cxl_region *region);
26ccd9
 
26ccd9
 #define cxl_region_foreach(decoder, region)                                    \
26ccd9
 	for (region = cxl_region_get_first(decoder); region != NULL;           \
26ccd9
-- 
26ccd9
2.27.0
26ccd9