Blame SOURCES/0192-libcxl-Introduce-libcxl-region-and-mapping-objects.patch

2eb93d
From d25dc6d7956bc022d7e4c4453416c52368df291d Mon Sep 17 00:00:00 2001
2eb93d
From: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
Date: Mon, 15 Aug 2022 13:22:07 -0600
2eb93d
Subject: [PATCH 192/217] libcxl: Introduce libcxl region and mapping objects
2eb93d
2eb93d
Add a cxl_region object to libcxl that represents a CXL region. CXL
2eb93d
regions are made up of one or more cxl_memdev 'targets'. The
2eb93d
relationship between a target and a region is conveyed with a
2eb93d
cxl_memdev_mapping object.
2eb93d
2eb93d
CXL regions are childeren of root decoders, and are organized as such.
2eb93d
Mapping objects are childeren of a CXL region.  Introduce the two
2eb93d
classes of objects themselves, and common accessors related to them.
2eb93d
2eb93d
Link: https://lore.kernel.org/r/20220815192214.545800-5-vishal.l.verma@intel.com
2eb93d
Cc: Dan Williams <dan.j.williams@intel.com>
2eb93d
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 .clang-format      |   2 +
2eb93d
 cxl/lib/libcxl.c   | 442 ++++++++++++++++++++++++++++++++++++++++++++-
2eb93d
 cxl/lib/libcxl.sym |  20 ++
2eb93d
 cxl/lib/private.h  |  35 ++++
2eb93d
 cxl/libcxl.h       |  41 +++++
2eb93d
 5 files changed, 530 insertions(+), 10 deletions(-)
2eb93d
2eb93d
diff --git a/.clang-format b/.clang-format
2eb93d
index 7254a1b..b6169e1 100644
2eb93d
--- a/.clang-format
2eb93d
+++ b/.clang-format
2eb93d
@@ -86,6 +86,8 @@ ForEachMacros:
2eb93d
   - 'cxl_dport_foreach'
2eb93d
   - 'cxl_endpoint_foreach'
2eb93d
   - 'cxl_port_foreach_all'
2eb93d
+  - 'cxl_region_foreach'
2eb93d
+  - 'cxl_region_foreach_safe'
2eb93d
   - 'daxctl_dev_foreach'
2eb93d
   - 'daxctl_mapping_foreach'
2eb93d
   - 'daxctl_region_foreach'
2eb93d
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
2eb93d
index 145c6ba..ad98188 100644
2eb93d
--- a/cxl/lib/libcxl.c
2eb93d
+++ b/cxl/lib/libcxl.c
2eb93d
@@ -79,6 +79,38 @@ static void free_target(struct cxl_target *target, struct list_head *head)
2eb93d
 	free(target);
2eb93d
 }
2eb93d
 
2eb93d
+static void free_region(struct cxl_region *region, struct list_head *head)
2eb93d
+{
2eb93d
+	struct cxl_memdev_mapping *mapping, *_m;
2eb93d
+
2eb93d
+	list_for_each_safe(&region->mappings, mapping, _m, list) {
2eb93d
+		list_del_from(&region->mappings, &mapping->list);
2eb93d
+		free(mapping);
2eb93d
+	}
2eb93d
+	if (head)
2eb93d
+		list_del_from(head, &region->list);
2eb93d
+	kmod_module_unref(region->module);
2eb93d
+	free(region->dev_buf);
2eb93d
+	free(region->dev_path);
2eb93d
+	free(region);
2eb93d
+}
2eb93d
+
2eb93d
+static void free_stale_regions(struct cxl_decoder *decoder)
2eb93d
+{
2eb93d
+	struct cxl_region *region, *_r;
2eb93d
+
2eb93d
+	list_for_each_safe(&decoder->stale_regions, region, _r, list)
2eb93d
+		free_region(region, &decoder->stale_regions);
2eb93d
+}
2eb93d
+
2eb93d
+static void free_regions(struct cxl_decoder *decoder)
2eb93d
+{
2eb93d
+	struct cxl_region *region, *_r;
2eb93d
+
2eb93d
+	list_for_each_safe(&decoder->regions, region, _r, list)
2eb93d
+		free_region(region, &decoder->regions);
2eb93d
+}
2eb93d
+
2eb93d
 static void free_decoder(struct cxl_decoder *decoder, struct list_head *head)
2eb93d
 {
2eb93d
 	struct cxl_target *target, *_t;
2eb93d
@@ -87,6 +119,8 @@ static void free_decoder(struct cxl_decoder *decoder, struct list_head *head)
2eb93d
 		list_del_from(head, &decoder->list);
2eb93d
 	list_for_each_safe(&decoder->targets, target, _t, list)
2eb93d
 		free_target(target, &decoder->targets);
2eb93d
+	free_regions(decoder);
2eb93d
+	free_stale_regions(decoder);
2eb93d
 	free(decoder->dev_buf);
2eb93d
 	free(decoder->dev_path);
2eb93d
 	free(decoder);
2eb93d
@@ -304,6 +338,402 @@ CXL_EXPORT void cxl_set_log_priority(struct cxl_ctx *ctx, int priority)
2eb93d
 	ctx->ctx.log_priority = priority;
2eb93d
 }
2eb93d
 
2eb93d
+static int is_enabled(const char *drvpath)
2eb93d
+{
2eb93d
+	struct stat st;
2eb93d
+
2eb93d
+	if (lstat(drvpath, &st) < 0 || !S_ISLNK(st.st_mode))
2eb93d
+		return 0;
2eb93d
+	else
2eb93d
+		return 1;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT int cxl_region_is_enabled(struct cxl_region *region)
2eb93d
+{
2eb93d
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
2eb93d
+	char *path = region->dev_buf;
2eb93d
+	int len = region->buf_len;
2eb93d
+
2eb93d
+	if (snprintf(path, len, "%s/driver", region->dev_path) >= len) {
2eb93d
+		err(ctx, "%s: buffer too small!\n", cxl_region_get_devname(region));
2eb93d
+		return 0;
2eb93d
+	}
2eb93d
+
2eb93d
+	return is_enabled(path);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT int cxl_region_disable(struct cxl_region *region)
2eb93d
+{
2eb93d
+	const char *devname = cxl_region_get_devname(region);
2eb93d
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
2eb93d
+
2eb93d
+	util_unbind(region->dev_path, ctx);
2eb93d
+
2eb93d
+	if (cxl_region_is_enabled(region)) {
2eb93d
+		err(ctx, "%s: failed to disable\n", devname);
2eb93d
+		return -EBUSY;
2eb93d
+	}
2eb93d
+
2eb93d
+	dbg(ctx, "%s: disabled\n", devname);
2eb93d
+
2eb93d
+	return 0;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT int cxl_region_enable(struct cxl_region *region)
2eb93d
+{
2eb93d
+	const char *devname = cxl_region_get_devname(region);
2eb93d
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
2eb93d
+	char *path = region->dev_buf;
2eb93d
+	int len = region->buf_len;
2eb93d
+	char buf[SYSFS_ATTR_SIZE];
2eb93d
+	u64 resource = ULLONG_MAX;
2eb93d
+
2eb93d
+	if (cxl_region_is_enabled(region))
2eb93d
+		return 0;
2eb93d
+
2eb93d
+	util_bind(devname, region->module, "cxl", ctx);
2eb93d
+
2eb93d
+	if (!cxl_region_is_enabled(region)) {
2eb93d
+		err(ctx, "%s: failed to enable\n", devname);
2eb93d
+		return -ENXIO;
2eb93d
+	}
2eb93d
+
2eb93d
+	/*
2eb93d
+	 * Currently 'resource' is the only attr that may change after enabling.
2eb93d
+	 * Just refresh it here. If there are additional resources that need
2eb93d
+	 * to be refreshed here later, split these out into a common helper
2eb93d
+	 * for this and add_cxl_region()
2eb93d
+	 */
2eb93d
+	if (snprintf(path, len, "%s/resource", region->dev_path) >= len) {
2eb93d
+		err(ctx, "%s: buffer too small!\n", devname);
2eb93d
+		return 0;
2eb93d
+	}
2eb93d
+
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) == 0)
2eb93d
+		resource = strtoull(buf, NULL, 0);
2eb93d
+
2eb93d
+	if (resource < ULLONG_MAX)
2eb93d
+		region->start = resource;
2eb93d
+
2eb93d
+	dbg(ctx, "%s: enabled\n", devname);
2eb93d
+
2eb93d
+	return 0;
2eb93d
+}
2eb93d
+
2eb93d
+static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
2eb93d
+{
2eb93d
+	const char *devname = devpath_to_devname(cxlregion_base);
2eb93d
+	char *path = calloc(1, strlen(cxlregion_base) + 100);
2eb93d
+	struct cxl_region *region, *region_dup, *_r;
2eb93d
+	struct cxl_decoder *decoder = parent;
2eb93d
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
2eb93d
+	char buf[SYSFS_ATTR_SIZE];
2eb93d
+	u64 resource = ULLONG_MAX;
2eb93d
+
2eb93d
+	dbg(ctx, "%s: base: \'%s\'\n", devname, cxlregion_base);
2eb93d
+
2eb93d
+	if (!path)
2eb93d
+		return NULL;
2eb93d
+
2eb93d
+	region = calloc(1, sizeof(*region));
2eb93d
+	if (!region)
2eb93d
+		goto err;
2eb93d
+
2eb93d
+	region->id = id;
2eb93d
+	region->ctx = ctx;
2eb93d
+	region->decoder = decoder;
2eb93d
+	list_head_init(&region->mappings);
2eb93d
+
2eb93d
+	region->dev_path = strdup(cxlregion_base);
2eb93d
+	if (!region->dev_path)
2eb93d
+		goto err;
2eb93d
+
2eb93d
+	region->dev_buf = calloc(1, strlen(cxlregion_base) + 50);
2eb93d
+	if (!region->dev_buf)
2eb93d
+		goto err;
2eb93d
+	region->buf_len = strlen(cxlregion_base) + 50;
2eb93d
+
2eb93d
+	sprintf(path, "%s/size", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
+		region->size = ULLONG_MAX;
2eb93d
+	else
2eb93d
+		region->size = strtoull(buf, NULL, 0);
2eb93d
+
2eb93d
+	sprintf(path, "%s/resource", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) == 0)
2eb93d
+		resource = strtoull(buf, NULL, 0);
2eb93d
+
2eb93d
+	if (resource < ULLONG_MAX)
2eb93d
+		region->start = resource;
2eb93d
+
2eb93d
+	sprintf(path, "%s/uuid", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
+		goto err;
2eb93d
+	if (strlen(buf) && uuid_parse(buf, region->uuid) < 0) {
2eb93d
+		dbg(ctx, "%s:%s\n", path, buf);
2eb93d
+		goto err;
2eb93d
+	}
2eb93d
+
2eb93d
+	sprintf(path, "%s/interleave_granularity", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
+		region->interleave_granularity = UINT_MAX;
2eb93d
+	else
2eb93d
+		region->interleave_granularity = strtoul(buf, NULL, 0);
2eb93d
+
2eb93d
+	sprintf(path, "%s/interleave_ways", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
+		region->interleave_ways = UINT_MAX;
2eb93d
+	else
2eb93d
+		region->interleave_ways = strtoul(buf, NULL, 0);
2eb93d
+
2eb93d
+	sprintf(path, "%s/commit", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) < 0)
2eb93d
+		region->decode_state = CXL_DECODE_UNKNOWN;
2eb93d
+	else
2eb93d
+		region->decode_state = strtoul(buf, NULL, 0);
2eb93d
+
2eb93d
+	sprintf(path, "%s/modalias", cxlregion_base);
2eb93d
+	if (sysfs_read_attr(ctx, path, buf) == 0)
2eb93d
+		region->module = util_modalias_to_module(ctx, buf);
2eb93d
+
2eb93d
+	cxl_region_foreach_safe(decoder, region_dup, _r)
2eb93d
+		if (region_dup->id == region->id) {
2eb93d
+			list_del_from(&decoder->regions, &region_dup->list);
2eb93d
+			list_add_tail(&decoder->stale_regions,
2eb93d
+				      &region_dup->list);
2eb93d
+			break;
2eb93d
+		}
2eb93d
+
2eb93d
+	list_add(&decoder->regions, &region->list);
2eb93d
+
2eb93d
+	return region;
2eb93d
+err:
2eb93d
+	free(region->dev_path);
2eb93d
+	free(region->dev_buf);
2eb93d
+	free(region);
2eb93d
+	free(path);
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
+static void cxl_regions_init(struct cxl_decoder *decoder)
2eb93d
+{
2eb93d
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
2eb93d
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
2eb93d
+
2eb93d
+	if (decoder->regions_init)
2eb93d
+		return;
2eb93d
+
2eb93d
+	/* Only root port decoders may have child regions */
2eb93d
+	if (!cxl_port_is_root(port))
2eb93d
+		return;
2eb93d
+
2eb93d
+	decoder->regions_init = 1;
2eb93d
+
2eb93d
+	sysfs_device_parse(ctx, decoder->dev_path, "region", decoder,
2eb93d
+			   add_cxl_region);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_region *cxl_region_get_first(struct cxl_decoder *decoder)
2eb93d
+{
2eb93d
+	cxl_regions_init(decoder);
2eb93d
+
2eb93d
+	return list_top(&decoder->regions, struct cxl_region, list);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_region *cxl_region_get_next(struct cxl_region *region)
2eb93d
+{
2eb93d
+	struct cxl_decoder *decoder = region->decoder;
2eb93d
+
2eb93d
+	return list_next(&decoder->regions, region, list);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_ctx *cxl_region_get_ctx(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->ctx;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_decoder *cxl_region_get_decoder(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->decoder;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT int cxl_region_get_id(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->id;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT const char *cxl_region_get_devname(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return devpath_to_devname(region->dev_path);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT void cxl_region_get_uuid(struct cxl_region *region, uuid_t uu)
2eb93d
+{
2eb93d
+	memcpy(uu, region->uuid, sizeof(uuid_t));
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT unsigned long long cxl_region_get_size(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->size;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT unsigned long long cxl_region_get_resource(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->start;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT unsigned int
2eb93d
+cxl_region_get_interleave_ways(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->interleave_ways;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT int cxl_region_decode_is_committed(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return (region->decode_state == CXL_DECODE_COMMIT) ? 1 : 0;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT unsigned int
2eb93d
+cxl_region_get_interleave_granularity(struct cxl_region *region)
2eb93d
+{
2eb93d
+	return region->interleave_granularity;
2eb93d
+}
2eb93d
+
2eb93d
+static struct cxl_decoder *__cxl_port_match_decoder(struct cxl_port *port,
2eb93d
+						    const char *ident)
2eb93d
+{
2eb93d
+	struct cxl_decoder *decoder;
2eb93d
+
2eb93d
+	cxl_decoder_foreach(port, decoder)
2eb93d
+		if (strcmp(cxl_decoder_get_devname(decoder), ident) == 0)
2eb93d
+			return decoder;
2eb93d
+
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
+static struct cxl_decoder *cxl_port_find_decoder(struct cxl_port *port,
2eb93d
+						 const char *ident)
2eb93d
+{
2eb93d
+	struct cxl_decoder *decoder;
2eb93d
+	struct cxl_endpoint *ep;
2eb93d
+
2eb93d
+	/* First, check decoders directly under @port */
2eb93d
+	decoder = __cxl_port_match_decoder(port, ident);
2eb93d
+	if (decoder)
2eb93d
+		return decoder;
2eb93d
+
2eb93d
+	/* Next, iterate over the endpoints under @port */
2eb93d
+	cxl_endpoint_foreach(port, ep) {
2eb93d
+		decoder = __cxl_port_match_decoder(cxl_endpoint_get_port(ep),
2eb93d
+						   ident);
2eb93d
+		if (decoder)
2eb93d
+			return decoder;
2eb93d
+	}
2eb93d
+
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
+static struct cxl_decoder *cxl_decoder_get_by_name(struct cxl_ctx *ctx,
2eb93d
+						   const char *ident)
2eb93d
+{
2eb93d
+	struct cxl_bus *bus;
2eb93d
+
2eb93d
+	cxl_bus_foreach(ctx, bus) {
2eb93d
+		struct cxl_decoder *decoder;
2eb93d
+		struct cxl_port *port, *top;
2eb93d
+
2eb93d
+		port = cxl_bus_get_port(bus);
2eb93d
+		decoder = cxl_port_find_decoder(port, ident);
2eb93d
+		if (decoder)
2eb93d
+			return decoder;
2eb93d
+
2eb93d
+		top = port;
2eb93d
+		cxl_port_foreach_all (top, port) {
2eb93d
+			decoder = cxl_port_find_decoder(port, ident);
2eb93d
+			if (decoder)
2eb93d
+				return decoder;
2eb93d
+		}
2eb93d
+	}
2eb93d
+
2eb93d
+	return NULL;
2eb93d
+}
2eb93d
+
2eb93d
+static void cxl_mappings_init(struct cxl_region *region)
2eb93d
+{
2eb93d
+	const char *devname = cxl_region_get_devname(region);
2eb93d
+	struct cxl_ctx *ctx = cxl_region_get_ctx(region);
2eb93d
+	char *mapping_path, buf[SYSFS_ATTR_SIZE];
2eb93d
+	unsigned int i;
2eb93d
+
2eb93d
+	if (region->mappings_init)
2eb93d
+		return;
2eb93d
+	region->mappings_init = 1;
2eb93d
+
2eb93d
+	mapping_path = calloc(1, strlen(region->dev_path) + 100);
2eb93d
+	if (!mapping_path) {
2eb93d
+		err(ctx, "%s: allocation failure\n", devname);
2eb93d
+		return;
2eb93d
+	}
2eb93d
+
2eb93d
+	for (i = 0; i < region->interleave_ways; i++) {
2eb93d
+		struct cxl_memdev_mapping *mapping;
2eb93d
+		struct cxl_decoder *decoder;
2eb93d
+
2eb93d
+		sprintf(mapping_path, "%s/target%d", region->dev_path, i);
2eb93d
+		if (sysfs_read_attr(ctx, mapping_path, buf) < 0) {
2eb93d
+			err(ctx, "%s: failed to read target%d\n", devname, i);
2eb93d
+			continue;
2eb93d
+		}
2eb93d
+
2eb93d
+		decoder = cxl_decoder_get_by_name(ctx, buf);
2eb93d
+		if (!decoder) {
2eb93d
+			err(ctx, "%s target%d: %s lookup failure\n",
2eb93d
+			    devname, i, buf);
2eb93d
+			continue;
2eb93d
+		}
2eb93d
+
2eb93d
+		mapping = calloc(1, sizeof(*mapping));
2eb93d
+		if (!mapping) {
2eb93d
+			err(ctx, "%s target%d: allocation failure\n", devname, i);
2eb93d
+			continue;
2eb93d
+		}
2eb93d
+
2eb93d
+		mapping->region = region;
2eb93d
+		mapping->decoder = decoder;
2eb93d
+		mapping->position = i;
2eb93d
+		list_add(&region->mappings, &mapping->list);
2eb93d
+	}
2eb93d
+	free(mapping_path);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_memdev_mapping *
2eb93d
+cxl_mapping_get_first(struct cxl_region *region)
2eb93d
+{
2eb93d
+	cxl_mappings_init(region);
2eb93d
+
2eb93d
+	return list_top(&region->mappings, struct cxl_memdev_mapping, list);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_memdev_mapping *
2eb93d
+cxl_mapping_get_next(struct cxl_memdev_mapping *mapping)
2eb93d
+{
2eb93d
+	struct cxl_region *region = mapping->region;
2eb93d
+
2eb93d
+	return list_next(&region->mappings, mapping, list);
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT struct cxl_decoder *
2eb93d
+cxl_mapping_get_decoder(struct cxl_memdev_mapping *mapping)
2eb93d
+{
2eb93d
+	return mapping->decoder;
2eb93d
+}
2eb93d
+
2eb93d
+CXL_EXPORT unsigned int
2eb93d
+cxl_mapping_get_position(struct cxl_memdev_mapping *mapping)
2eb93d
+{
2eb93d
+	return mapping->position;
2eb93d
+}
2eb93d
+
2eb93d
 static void *add_cxl_pmem(void *parent, int id, const char *br_base)
2eb93d
 {
2eb93d
 	const char *devname = devpath_to_devname(br_base);
2eb93d
@@ -681,16 +1111,6 @@ CXL_EXPORT size_t cxl_memdev_get_label_size(struct cxl_memdev *memdev)
2eb93d
 	return memdev->lsa_size;
2eb93d
 }
2eb93d
 
2eb93d
-static int is_enabled(const char *drvpath)
2eb93d
-{
2eb93d
-	struct stat st;
2eb93d
-
2eb93d
-	if (lstat(drvpath, &st) < 0 || !S_ISLNK(st.st_mode))
2eb93d
-		return 0;
2eb93d
-	else
2eb93d
-		return 1;
2eb93d
-}
2eb93d
-
2eb93d
 CXL_EXPORT int cxl_memdev_is_enabled(struct cxl_memdev *memdev)
2eb93d
 {
2eb93d
 	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
2eb93d
@@ -940,6 +1360,8 @@ static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
2eb93d
 	decoder->ctx = ctx;
2eb93d
 	decoder->port = port;
2eb93d
 	list_head_init(&decoder->targets);
2eb93d
+	list_head_init(&decoder->regions);
2eb93d
+	list_head_init(&decoder->stale_regions);
2eb93d
 
2eb93d
 	decoder->dev_path = strdup(cxldecoder_base);
2eb93d
 	if (!decoder->dev_path)
2eb93d
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
2eb93d
index 7712de0..e410298 100644
2eb93d
--- a/cxl/lib/libcxl.sym
2eb93d
+++ b/cxl/lib/libcxl.sym
2eb93d
@@ -177,4 +177,24 @@ global:
2eb93d
 	cxl_decoder_get_prev;
2eb93d
 	cxl_decoder_set_dpa_size;
2eb93d
 	cxl_decoder_set_mode;
2eb93d
+	cxl_region_get_first;
2eb93d
+	cxl_region_get_next;
2eb93d
+	cxl_region_decode_is_committed;
2eb93d
+	cxl_region_is_enabled;
2eb93d
+	cxl_region_disable;
2eb93d
+	cxl_region_enable;
2eb93d
+	cxl_region_get_ctx;
2eb93d
+	cxl_region_get_decoder;
2eb93d
+	cxl_region_get_id;
2eb93d
+	cxl_region_get_devname;
2eb93d
+	cxl_region_get_uuid;
2eb93d
+	cxl_region_get_size;
2eb93d
+	cxl_region_get_resource;
2eb93d
+	cxl_region_get_interleave_ways;
2eb93d
+	cxl_region_get_interleave_granularity;
2eb93d
+	cxl_mapping_get_first;
2eb93d
+	cxl_mapping_get_next;
2eb93d
+	cxl_mapping_get_decoder;
2eb93d
+	cxl_mapping_get_region;
2eb93d
+	cxl_mapping_get_position;
2eb93d
 } LIBCXL_2;
2eb93d
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
2eb93d
index 832a815..5e2fdd5 100644
2eb93d
--- a/cxl/lib/private.h
2eb93d
+++ b/cxl/lib/private.h
2eb93d
@@ -116,7 +116,42 @@ struct cxl_decoder {
2eb93d
 	bool accelmem_capable;
2eb93d
 	bool locked;
2eb93d
 	enum cxl_decoder_target_type target_type;
2eb93d
+	int regions_init;
2eb93d
 	struct list_head targets;
2eb93d
+	struct list_head regions;
2eb93d
+	struct list_head stale_regions;
2eb93d
+};
2eb93d
+
2eb93d
+enum cxl_decode_state {
2eb93d
+	CXL_DECODE_UNKNOWN = -1,
2eb93d
+	CXL_DECODE_RESET = 0,
2eb93d
+	CXL_DECODE_COMMIT,
2eb93d
+};
2eb93d
+
2eb93d
+struct cxl_region {
2eb93d
+	struct cxl_decoder *decoder;
2eb93d
+	struct list_node list;
2eb93d
+	int mappings_init;
2eb93d
+	struct cxl_ctx *ctx;
2eb93d
+	void *dev_buf;
2eb93d
+	size_t buf_len;
2eb93d
+	char *dev_path;
2eb93d
+	int id;
2eb93d
+	uuid_t uuid;
2eb93d
+	u64 start;
2eb93d
+	u64 size;
2eb93d
+	unsigned int interleave_ways;
2eb93d
+	unsigned int interleave_granularity;
2eb93d
+	enum cxl_decode_state decode_state;
2eb93d
+	struct kmod_module *module;
2eb93d
+	struct list_head mappings;
2eb93d
+};
2eb93d
+
2eb93d
+struct cxl_memdev_mapping {
2eb93d
+	struct cxl_region *region;
2eb93d
+	struct cxl_decoder *decoder;
2eb93d
+	unsigned int position;
2eb93d
+	struct list_node list;
2eb93d
 };
2eb93d
 
2eb93d
 enum cxl_cmd_query_status {
2eb93d
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
2eb93d
index c1f8d14..19d94e4 100644
2eb93d
--- a/cxl/libcxl.h
2eb93d
+++ b/cxl/libcxl.h
2eb93d
@@ -237,6 +237,47 @@ int cxl_memdev_is_enabled(struct cxl_memdev *memdev);
2eb93d
 	for (endpoint = cxl_endpoint_get_first(port); endpoint != NULL;        \
2eb93d
 	     endpoint = cxl_endpoint_get_next(endpoint))
2eb93d
 
2eb93d
+struct cxl_region;
2eb93d
+struct cxl_region *cxl_region_get_first(struct cxl_decoder *decoder);
2eb93d
+struct cxl_region *cxl_region_get_next(struct cxl_region *region);
2eb93d
+int cxl_region_decode_is_committed(struct cxl_region *region);
2eb93d
+int cxl_region_is_enabled(struct cxl_region *region);
2eb93d
+int cxl_region_disable(struct cxl_region *region);
2eb93d
+int cxl_region_enable(struct cxl_region *region);
2eb93d
+struct cxl_ctx *cxl_region_get_ctx(struct cxl_region *region);
2eb93d
+struct cxl_decoder *cxl_region_get_decoder(struct cxl_region *region);
2eb93d
+int cxl_region_get_id(struct cxl_region *region);
2eb93d
+const char *cxl_region_get_devname(struct cxl_region *region);
2eb93d
+void cxl_region_get_uuid(struct cxl_region *region, uuid_t uu);
2eb93d
+unsigned long long cxl_region_get_size(struct cxl_region *region);
2eb93d
+unsigned long long cxl_region_get_resource(struct cxl_region *region);
2eb93d
+unsigned int cxl_region_get_interleave_ways(struct cxl_region *region);
2eb93d
+unsigned int cxl_region_get_interleave_granularity(struct cxl_region *region);
2eb93d
+
2eb93d
+#define cxl_region_foreach(decoder, region)                                    \
2eb93d
+	for (region = cxl_region_get_first(decoder); region != NULL;           \
2eb93d
+	     region = cxl_region_get_next(region))
2eb93d
+
2eb93d
+#define cxl_region_foreach_safe(decoder, region, _region)                      \
2eb93d
+	for (region = cxl_region_get_first(decoder),                           \
2eb93d
+	     _region = region ? cxl_region_get_next(region) : NULL;            \
2eb93d
+	     region != NULL;                                                   \
2eb93d
+	     region = _region,                                                 \
2eb93d
+	     _region = _region ? cxl_region_get_next(_region) : NULL)
2eb93d
+
2eb93d
+struct cxl_memdev_mapping;
2eb93d
+struct cxl_memdev_mapping *cxl_mapping_get_first(struct cxl_region *region);
2eb93d
+struct cxl_memdev_mapping *
2eb93d
+cxl_mapping_get_next(struct cxl_memdev_mapping *mapping);
2eb93d
+struct cxl_decoder *cxl_mapping_get_decoder(struct cxl_memdev_mapping *mapping);
2eb93d
+struct cxl_region *cxl_mapping_get_region(struct cxl_memdev_mapping *mapping);
2eb93d
+unsigned int cxl_mapping_get_position(struct cxl_memdev_mapping *mapping);
2eb93d
+
2eb93d
+#define cxl_mapping_foreach(region, mapping) \
2eb93d
+        for (mapping = cxl_mapping_get_first(region); \
2eb93d
+             mapping != NULL; \
2eb93d
+             mapping = cxl_mapping_get_next(mapping))
2eb93d
+
2eb93d
 struct cxl_cmd;
2eb93d
 const char *cxl_cmd_get_devname(struct cxl_cmd *cmd);
2eb93d
 struct cxl_cmd *cxl_cmd_new_raw(struct cxl_memdev *memdev, int opcode);
2eb93d
-- 
2eb93d
2.27.0
2eb93d