Skip to contentSkip to conversation

cxl: add region management

PATCH In review Vishal Verma 2 participants 13 messages Aug 15, 2022

Subsystems (4)
lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

v3 cover cxl: add region management

Changes since v2[1]:
- Collect review tags
- Fix -r description in the cxl-list man page (Dan)
- Fix typo + some wordsmithing in man libcxl(3) (Dan)
- s/cxl_ep_decoder_get.../cxl_decoder_get.../ (Dan)
- Move regions invalidation from patch 10 to patch 5 (Dan)
- Consolidate mode string to enum in a common helper (Dan)
- Fix a mid-series compile breakage with printing memdev json in the region listing.
- Remove the --ep-decoders option (Dan)
- Use a decoder->stale_regions list for stashing duplicate regions during region creation. Add the necessary helpers around freeing these.
- Don't free regions to invalidate them, use stale_regions instead (Dan)
- Fix a couple of bugs in the create-region unit test where the regions array wasn't used correctly leading to weird failures
- Fix max_available_extent json listing to be a size instead of hex
- Add an equals case to the region ordering compare helper (Dan)
- Change the max extent calculations to explicitly add/subtract 1 as needed (Dan)

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

---

Introduce the first cut at a 'cxl create-region' command, which uses the ABI merged in the kernel in [2], and builds on the create-region foundation laid out in [3].

This introduces libcxl objects for regions and their memdev mappings, adds listing support for them, and libcxl APIs to create, enable, disable, and destroy regions. These are consumed by respective cxl-cli commands to perform the same region management operations.

The 'cxl create-region' command deserves a bit more detail. Its current version can be considered a fundamental 'plumbing' implementation. It allows, and in some cases, requires the user to specify the parameters for the region being created. Future enhancements to this will allow a simple 'cxl create-region' invocation to automatically pick suitable root decoders, memdev endpoints, and interleave settings to create a maximally sized and interleaved region.

Today, the create-region command requires the following from the user:

-d <root decoder> : the root decoder (CFMWS window) under which to create the region

<targets>: the memdev (-m) targets that will form the new region

It can pick the following settings automatically (though the user can also override these if desired):

-w : interleave ways - picked based on the number of targets supplied

-g : interleave granularity - selects the interleave granularity advertised by the root decoder

-t : type of region - defaults to pmem. The ram (volatile) type is not supported yet.

-s : size of region - deduced based on sizes of the specified targets

[2]: https://git.kernel.org/torvalds/c/c235698355fa94df7073b51befda7d4be00a0e23
[3]: https://lore.kernel.org/linux-cxl/165781810717.1555691.1411727384567016588.stgit@dwillia2-xfh.jf.intel.com/

Vishal Verma (11):
libcxl: add a depth attribute to cxl_port cxl/port: Consolidate the debug option in cxl-port man pages cxl/memdev: refactor decoder mode string parsing
libcxl: Introduce libcxl region and mapping objects
cxl-cli: add region listing support
libcxl: add low level APIs for region creation
cxl: add a 'create-region' command
cxl: add commands to {enable,disable,destroy}-region cxl/list: make memdevs and regions the default listing
test: add a cxl-create-region test
cxl/decoder: add a max_available_extent attribute

 Documentation/cxl/bus-option.txt         |   5 +
 Documentation/cxl/cxl-create-region.txt  | 112 +++
 Documentation/cxl/cxl-destroy-region.txt |  41 ++
 Documentation/cxl/cxl-disable-port.txt   |   5 +-
 Documentation/cxl/cxl-disable-region.txt |  36 +
 Documentation/cxl/cxl-enable-port.txt    |   5 +-
 Documentation/cxl/cxl-enable-region.txt  |  36 +
 Documentation/cxl/cxl-list.txt           |  13 +-
 Documentation/cxl/debug-option.txt       |   4 +
 Documentation/cxl/decoder-option.txt     |   6 +
 Documentation/cxl/lib/libcxl.txt         |  69 ++
 Documentation/cxl/region-description.txt |   7 +
 cxl/lib/private.h                        |  39 +
 cxl/lib/libcxl.c                         | 902 ++++++++++++++++++++++-
 cxl/builtin.h                            |   4 +
 cxl/filter.h                             |   6 +
 cxl/json.h                               |   5 +
 cxl/libcxl.h                             |  80 +-
 cxl/cxl.c                                |   4 +
 cxl/filter.c                             | 158 +++-
 cxl/json.c                               | 131 ++++
 cxl/list.c                               |  26 +-
 cxl/memdev.c                             |  11 +-
 cxl/region.c                             | 753 +++++++++++++++++++
 .clang-format                            |   2 +
 Documentation/cxl/meson.build            |   7 +
 cxl/lib/libcxl.sym                       |  37 +
 cxl/meson.build                          |   1 +
 test/cxl-create-region.sh                | 125 ++++
 test/meson.build                         |   2 +
 30 files changed, 2580 insertions(+), 52 deletions(-)
 create mode 100644 Documentation/cxl/bus-option.txt
 create mode 100644 Documentation/cxl/cxl-create-region.txt
 create mode 100644 Documentation/cxl/cxl-destroy-region.txt
 create mode 100644 Documentation/cxl/cxl-disable-region.txt
 create mode 100644 Documentation/cxl/cxl-enable-region.txt
 create mode 100644 Documentation/cxl/debug-option.txt
 create mode 100644 Documentation/cxl/decoder-option.txt
 create mode 100644 Documentation/cxl/region-description.txt
 create mode 100644 cxl/region.c
 create mode 100644 test/cxl-create-region.sh
Signature · 1 line
2.37.1
author maintainer
#1 lore raw

PATCH v3 1/11 libcxl: add a depth attribute to cxl_port

Add a depth attribute to the cxl_port structure, that can be used for calculating its distance from the root port, and will be needed for interleave granularity calculations during region creation.

---
 cxl/lib/private.h | 1 +
 cxl/lib/libcxl.c  | 1 +
 2 files changed, 2 insertions(+)
cxl/lib/private.h +1 -0
index f6d4573..832a815 100644
@@ -66,6 +66,7 @@ struct cxl_port {
 	int decoders_init;
 	int dports_init;
 	int nr_dports;
+	int depth;
 	struct cxl_ctx *ctx;
 	struct cxl_bus *bus;
 	enum cxl_port_type type;
cxl/lib/libcxl.c +1 -0
index be6bc2c..946cd4b 100644
@@ -744,6 +744,7 @@ static int cxl_port_init(struct cxl_port *port, struct cxl_port *parent_port,
 	port->type = type;
 	port->parent = parent_port;
 	port->type = type;
+	port->depth = parent_port ? parent_port->depth + 1 : 0;
 
 	list_head_init(&port->child_ports);
 	list_head_init(&port->endpoints);
Signature · 1 line
2.37.1
author maintainer
#2 lore raw

PATCH v3 2/11 cxl/port: Consolidate the debug option in cxl-port man pages

In reply to Vishal Verma

In preparation for additional commands that implement the --debug option, consolidate the option description from the cxl-port man pages into an include.

The port man pages also mentioned the debug option requiring a build with debug enabled, which wasn't true - so remove that part.

---
 Documentation/cxl/cxl-disable-port.txt | 5 +----
 Documentation/cxl/cxl-enable-port.txt  | 5 +----
 Documentation/cxl/debug-option.txt     | 4 ++++
 Documentation/cxl/meson.build          | 1 +
 4 files changed, 7 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/cxl/debug-option.txt
Documentation/cxl/cxl-disable-port.txt +1 -4
index ac56f20..7a22efc 100644
@@ -30,10 +30,7 @@ OPTIONS
 	firmware and disabling an active device is akin to force removing memory
 	from a running system.
 
---debug::
-	If the cxl tool was built with debug disabled, turn on debug
-	messages.
-
+include::debug-option.txt[]
 
 include::../copyright.txt[]
 
Documentation/cxl/cxl-enable-port.txt +1 -4
index 9a37cef..50b53d1 100644
@@ -31,10 +31,7 @@ OPTIONS
 	memdev is only enabled after all CXL ports in its device topology
 	ancestry are enabled.
 
---debug::
-	If the cxl tool was built with debug enabled, turn on debug
-	messages.
-
+include::debug-option.txt[]
 
 include::../copyright.txt[]
 
Documentation/cxl/debug-option.txt new file +4 -0
new file mode 100644
index 0000000..70b922f
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+--debug::
+	Turn on additional debug messages including library debug.
Documentation/cxl/meson.build +1 -0
index d019dfc..423be90 100644
@@ -22,6 +22,7 @@ filedeps = [
   '../copyright.txt',
   'memdev-option.txt',
   'labels-options.txt',
+  'debug-option.txt',
 ]
 
 cxl_manpages = [
Signature · 1 line
2.37.1
author maintainer
#3 lore raw

PATCH v3 3/11 cxl/memdev: refactor decoder mode string parsing

In reply to Vishal Verma

In preparation for create-region to use a similar decoder mode string to enum operation, break out the mode string parsing into its own inline helper in libcxl.h, and call it from memdev.c:__reserve_dpa().

---
 cxl/libcxl.h | 13 +++++++++++++
 cxl/memdev.c | 11 ++---------
 2 files changed, 15 insertions(+), 9 deletions(-)
cxl/libcxl.h +13 -0
index 33a216e..c1f8d14 100644
@@ -4,6 +4,7 @@
 #define _LIBCXL_H_
 
 #include <stdarg.h>
+#include <string.h>
 #include <unistd.h>
 #include <stdbool.h>
 
@@ -154,6 +155,18 @@ static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
 	return names[mode];
 }
 
+static inline enum cxl_decoder_mode
+cxl_decoder_mode_from_ident(const char *ident)
+{
+	if (strcmp(ident, "ram") == 0)
+		return CXL_DECODER_MODE_RAM;
+	else if (strcmp(ident, "volatile") == 0)
+		return CXL_DECODER_MODE_RAM;
+	else if (strcmp(ident, "pmem") == 0)
+		return CXL_DECODER_MODE_PMEM;
+	return CXL_DECODER_MODE_NONE;
+}
+
 enum cxl_decoder_mode cxl_decoder_get_mode(struct cxl_decoder *decoder);
 int cxl_decoder_set_mode(struct cxl_decoder *decoder,
 			 enum cxl_decoder_mode mode);
cxl/memdev.c +2 -9
index e42f554..0b3ad02 100644
@@ -154,15 +154,8 @@ static int __reserve_dpa(struct cxl_memdev *memdev,
 	int rc;
 
 	if (param.type) {
-		if (strcmp(param.type, "ram") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "volatile") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "ram") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "pmem") == 0)
-			mode = CXL_DECODER_MODE_PMEM;
-		else {
+		mode = cxl_decoder_mode_from_ident(param.type);
+		if (mode == CXL_DECODER_MODE_NONE) {
 			log_err(&ml, "%s: unsupported type: %s\n", devname,
 				param.type);
 			return -EINVAL;
Signature · 1 line
2.37.1
Reviewed-by on v3 3/11
#4 lore raw

RE: [ndctl PATCH v3 03/11] cxl/memdev: refactor decoder mode string parsing

Quoted 6 lines · Vishal Verma

In preparation for create-region to use a similar decoder mode string to enum operation, break out the mode string parsing into its own inline helper in libcxl.h, and call it from memdev.c:__reserve_dpa().

LGTM

author maintainer
#5 lore raw

PATCH v3 4/11 libcxl: Introduce libcxl region and mapping objects

In reply to Vishal Verma

Add a cxl_region object to libcxl that represents a CXL region. CXL regions are made up of one or more cxl_memdev 'targets'. The relationship between a target and a region is conveyed with a cxl_memdev_mapping object.

CXL regions are childeren of root decoders, and are organized as such. Mapping objects are childeren of a CXL region. Introduce the two classes of objects themselves, and common accessors related to them.

---
 cxl/lib/private.h  |  35 ++++
 cxl/lib/libcxl.c   | 442 ++++++++++++++++++++++++++++++++++++++++++++-
 cxl/libcxl.h       |  41 +++++
 .clang-format      |   2 +
 cxl/lib/libcxl.sym |  20 ++
 5 files changed, 530 insertions(+), 10 deletions(-)
cxl/lib/private.h +35 -0
index 832a815..5e2fdd5 100644
@@ -116,7 +116,42 @@ struct cxl_decoder {
 	bool accelmem_capable;
 	bool locked;
 	enum cxl_decoder_target_type target_type;
+	int regions_init;
 	struct list_head targets;
+	struct list_head regions;
+	struct list_head stale_regions;
+};
+
+enum cxl_decode_state {
+	CXL_DECODE_UNKNOWN = -1,
+	CXL_DECODE_RESET = 0,
+	CXL_DECODE_COMMIT,
+};
+
+struct cxl_region {
+	struct cxl_decoder *decoder;
+	struct list_node list;
+	int mappings_init;
+	struct cxl_ctx *ctx;
+	void *dev_buf;
+	size_t buf_len;
+	char *dev_path;
+	int id;
+	uuid_t uuid;
+	u64 start;
+	u64 size;
+	unsigned int interleave_ways;
+	unsigned int interleave_granularity;
+	enum cxl_decode_state decode_state;
+	struct kmod_module *module;
+	struct list_head mappings;
+};
+
+struct cxl_memdev_mapping {
+	struct cxl_region *region;
+	struct cxl_decoder *decoder;
+	unsigned int position;
+	struct list_node list;
 };
 
 enum cxl_cmd_query_status {
cxl/lib/libcxl.c +432 -10

472 lines not on this page · raw message

cxl/libcxl.h +41 -0

47 lines not on this page · raw message

.clang-format +2 -0
index 7254a1b..b6169e1 100644
@@ -86,6 +86,8 @@ ForEachMacros:
   - 'cxl_dport_foreach'
   - 'cxl_endpoint_foreach'
   - 'cxl_port_foreach_all'
+  - 'cxl_region_foreach'
+  - 'cxl_region_foreach_safe'
   - 'daxctl_dev_foreach'
   - 'daxctl_mapping_foreach'
   - 'daxctl_region_foreach'
cxl/lib/libcxl.sym +20 -0
index 7712de0..e410298 100644
@@ -177,4 +177,24 @@ global:
 	cxl_decoder_get_prev;
 	cxl_decoder_set_dpa_size;
 	cxl_decoder_set_mode;
+	cxl_region_get_first;
+	cxl_region_get_next;
+	cxl_region_decode_is_committed;
+	cxl_region_is_enabled;
+	cxl_region_disable;
+	cxl_region_enable;
+	cxl_region_get_ctx;
+	cxl_region_get_decoder;
+	cxl_region_get_id;
+	cxl_region_get_devname;
+	cxl_region_get_uuid;
+	cxl_region_get_size;
+	cxl_region_get_resource;
+	cxl_region_get_interleave_ways;
+	cxl_region_get_interleave_granularity;
+	cxl_mapping_get_first;
+	cxl_mapping_get_next;
+	cxl_mapping_get_decoder;
+	cxl_mapping_get_region;
+	cxl_mapping_get_position;
 } LIBCXL_2;
Signature · 1 line
2.37.1
author maintainer
#6 lore raw

PATCH v3 5/11 cxl-cli: add region listing support

In reply to Vishal Verma

Add cxl_region -> json and cxl_mapping -> json emitter helpers, and teach cxl_filter_walk about cxl_regions. With these in place, 'cxl-list' can now emit json objects for CXL regions. They can be top-level objects if requested by themselves, or nested under root-decoders, if listed along with decoders. Allow a plain 'cxl list' command to imply '--regions'.

---
 Documentation/cxl/cxl-list.txt |  13 ++-
 cxl/filter.h                   |   4 +
 cxl/json.h                     |   5 ++
 cxl/filter.c                   | 158 +++++++++++++++++++++++++++++++--
 cxl/json.c                     | 114 ++++++++++++++++++++++++
 cxl/list.c                     |  25 +++---
 6 files changed, 295 insertions(+), 24 deletions(-)
Documentation/cxl/cxl-list.txt +11 -2
index f6aba0c..f952e4d 100644
@@ -309,8 +309,9 @@ OPTIONS
 
 -T::
 --targets::
-	Extend decoder listings with downstream port target information, and /
-	or port and bus listings with the downstream port information.
+	Extend decoder listings with downstream port target information, port
+	and bus listings with the downstream port information, and / or regions
+	with mapping information.
 ----
 # cxl list -BTu -b ACPI.CXL
 {
@@ -327,6 +328,14 @@ OPTIONS
 }
 ----
 
+-R::
+--regions::
+	Include region objects in the listing.
+
+-r::
+--region::
+	Specify CXL region device name(s), or device id(s), to filter the listing.
+
 --debug::
 	If the cxl tool was built with debug enabled, turn on debug
 	messages.
cxl/filter.h +4 -0
index c913daf..609433c 100644
@@ -13,9 +13,11 @@ struct cxl_filter_params {
 	const char *port_filter;
 	const char *endpoint_filter;
 	const char *decoder_filter;
+	const char *region_filter;
 	bool single;
 	bool endpoints;
 	bool decoders;
+	bool regions;
 	bool targets;
 	bool memdevs;
 	bool ports;
@@ -33,6 +35,8 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
 struct cxl_port *util_cxl_port_filter_by_memdev(struct cxl_port *port,
 						const char *ident,
 						const char *serial);
+struct cxl_region *util_cxl_region_filter(struct cxl_region *region,
+					    const char *__ident);
 
 enum cxl_port_filter_mode {
 	CXL_PF_SINGLE,
cxl/json.h +5 -0
index 9a5a845..eb7572b 100644
@@ -15,6 +15,11 @@ struct json_object *util_cxl_endpoint_to_json(struct cxl_endpoint *endpoint,
 					      unsigned long flags);
 struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
 					     unsigned long flags);
+struct json_object *util_cxl_region_to_json(struct cxl_region *region,
+					     unsigned long flags);
+void util_cxl_mappings_append_json(struct json_object *jregion,
+				  struct cxl_region *region,
+				  unsigned long flags);
 void util_cxl_targets_append_json(struct json_object *jdecoder,
 				  struct cxl_decoder *decoder,
 				  const char *ident, const char *serial,
cxl/filter.c +149 -9

255 lines not on this page · raw message

cxl/json.c +114 -0

120 lines not on this page · raw message

cxl/list.c +12 -13
index 1b5f583..88ca9d9 100644
@@ -41,7 +41,10 @@ static const struct option options[] = {
 	OPT_BOOLEAN('D', "decoders", &param.decoders,
 		    "include CXL decoder info"),
 	OPT_BOOLEAN('T', "targets", &param.targets,
-		    "include CXL target data with decoders or ports"),
+		    "include CXL target data with decoders, ports, or regions"),
+	OPT_STRING('r', "region", &param.region_filter, "region name",
+		   "filter by CXL region name(s)"),
+	OPT_BOOLEAN('R', "regions", &param.regions, "include CXL regions"),
 	OPT_BOOLEAN('i', "idle", &param.idle, "include disabled devices"),
 	OPT_BOOLEAN('u', "human", &param.human,
 		    "use human friendly number formats"),
@@ -58,7 +61,7 @@ static const struct option options[] = {
 static int num_list_flags(void)
 {
 	return !!param.memdevs + !!param.buses + !!param.ports +
-	       !!param.endpoints + !!param.decoders;
+	       !!param.endpoints + !!param.decoders + !!param.regions;
 }
 
 int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
@@ -92,18 +95,14 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
 			param.endpoints = true;
 		if (param.decoder_filter)
 			param.decoders = true;
-		if (num_list_flags() == 0) {
-			/*
-			 * TODO: We likely want to list regions by default if
-			 * nothing was explicitly asked for. But until we have
-			 * region support, print this error asking for devices
-			 * explicitly.  Once region support is added, this TODO
-			 * can be removed.
-			 */
-			error("please specify entities to list, e.g. using -m/-M\n");
-			usage_with_options(u, options);
-		}
 		param.single = true;
+		if (param.region_filter)
+			param.regions = true;
+	}
+
+	/* List regions by default */
+	if (num_list_flags() == 0) {
+		param.regions = true;
 	}
 
 	log_init(&param.ctx, "cxl list", "CXL_LIST_LOG");
Signature · 1 line
2.37.1
author maintainer
#7 lore raw

PATCH v3 6/11 libcxl: add low level APIs for region creation

In reply to Vishal Verma

Add libcxl APIs to create a region under a given root decoder, and to set different attributes for the new region. These allow setting the size, interleave_ways, interleave_granularity, uuid, and the target devices for the newly minted cxl_region object.

---
 Documentation/cxl/lib/libcxl.txt |  69 ++++++
 cxl/lib/private.h                |   2 +
 cxl/lib/libcxl.c                 | 381 ++++++++++++++++++++++++++++++-
 cxl/libcxl.h                     |  23 +-
 cxl/lib/libcxl.sym               |  16 ++
 5 files changed, 488 insertions(+), 3 deletions(-)
Documentation/cxl/lib/libcxl.txt +69 -0
index 7a38ce4..50b0c9c 100644
@@ -508,6 +508,75 @@ device to represent the root of a PCI device hierarchy. The
 cxl_target_get_physical_node() helper returns the device name of that
 companion object in the PCI hierarchy.
 
+==== REGIONS
+A CXL region is composed of one or more slices of CXL memdevs, with configurable
+interleave settings - both the number of interleave ways, and the interleave
+granularity. In terms of hierarchy, it is the child of a CXL root decoder. A root
+decoder (recall that this corresponds to an ACPI CEDT.CFMWS 'window'), may have
+multiple child regions, but a region is strictly tied to one root decoder.
+
+The slices that compose a region are called mappings. A mapping is a
+tuple of 'memdev', 'endpoint decoder', and the 'position'.
+
+===== REGION: Enumeration
+----
+struct cxl_region *cxl_region_get_first(struct cxl_decoder *decoder);
+struct cxl_region *cxl_region_get_next(struct cxl_region *region);
+
+#define cxl_region_foreach(decoder, region)                                    \
+	for (region = cxl_region_get_first(decoder); region != NULL;           \
+	     region = cxl_region_get_next(region))
+
+#define cxl_region_foreach_safe(decoder, region, _region)                      \
+	for (region = cxl_region_get_first(decoder),                           \
+	     _region = region ? cxl_region_get_next(region) : NULL;            \
+	     region != NULL;                                                   \
+	     region = _region,                                                 \
+	     _region = _region ? cxl_region_get_next(_region) : NULL)
+----
+
+===== REGION: Attributes
+----
+int cxl_region_get_id(struct cxl_region *region);
+const char *cxl_region_get_devname(struct cxl_region *region);
+void cxl_region_get_uuid(struct cxl_region *region, uuid_t uu);
+unsigned long long cxl_region_get_size(struct cxl_region *region);
+unsigned long long cxl_region_get_resource(struct cxl_region *region);
+unsigned int cxl_region_get_interleave_ways(struct cxl_region *region);
+unsigned int cxl_region_get_interleave_granularity(struct cxl_region *region);
+struct cxl_decoder *cxl_region_get_target_decoder(struct cxl_region *region,
+						  int position);
+int cxl_region_set_size(struct cxl_region *region, unsigned long long size);
+int cxl_region_set_uuid(struct cxl_region *region, uuid_t uu);
+int cxl_region_set_interleave_ways(struct cxl_region *region,
+				   unsigned int ways);
+int cxl_region_set_interleave_granularity(struct cxl_region *region,
+					  unsigned int granularity);
+int cxl_region_set_target(struct cxl_region *region, int position,
+			  struct cxl_decoder *decoder);
+int cxl_region_clear_target(struct cxl_region *region, int position);
+int cxl_region_clear_all_targets(struct cxl_region *region);
+int cxl_region_decode_commit(struct cxl_region *region);
+int cxl_region_decode_reset(struct cxl_region *region);
+----
+
+A region's resource attribute is the Host Physical Address at which the region's
+address space starts. The region's address space is a subset of the parent root
+decoder's address space.
+
+The interleave ways is the number of component memdevs participating in the
+region.
+
+The interleave granularity depends on the root decoder's granularity, and must
+follow the interleave math rules defined in the CXL spec.
+
+Regions have a list of targets 0..N, which are programmed with the name of an
+endpoint decoder under each participating memdev.
+
+The 'decode_commit' and 'decode_reset' attributes reserve and free DPA space
+on a given memdev by allocating an endpoint decoder, and programming it based
+on the region's interleave geometry.
+
 include::../../copyright.txt[]
 
 SEE ALSO
cxl/lib/private.h +2 -0
index 5e2fdd5..8bc9620 100644
@@ -110,6 +110,8 @@ struct cxl_decoder {
 	int nr_targets;
 	int id;
 	enum cxl_decoder_mode mode;
+	unsigned int interleave_ways;
+	unsigned int interleave_granularity;
 	bool pmem_capable;
 	bool volatile_capable;
 	bool mem_capable;
cxl/lib/libcxl.c +379 -2

423 lines not on this page · raw message

cxl/libcxl.h +22 -1
index 19d94e4..69d9c09 100644
@@ -195,7 +195,13 @@ bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder);
 bool cxl_decoder_is_mem_capable(struct cxl_decoder *decoder);
 bool cxl_decoder_is_accelmem_capable(struct cxl_decoder *decoder);
 bool cxl_decoder_is_locked(struct cxl_decoder *decoder);
-
+unsigned int
+cxl_decoder_get_interleave_granularity(struct cxl_decoder *decoder);
+unsigned int cxl_decoder_get_interleave_ways(struct cxl_decoder *decoder);
+struct cxl_region *cxl_decoder_create_pmem_region(struct cxl_decoder *decoder);
+struct cxl_decoder *cxl_decoder_get_by_name(struct cxl_ctx *ctx,
+					    const char *ident);
+struct cxl_memdev *cxl_decoder_get_memdev(struct cxl_decoder *decoder);
 #define cxl_decoder_foreach(port, decoder)                                     \
 	for (decoder = cxl_decoder_get_first(port); decoder != NULL;           \
 	     decoder = cxl_decoder_get_next(decoder))
@@ -244,6 +250,7 @@ int cxl_region_decode_is_committed(struct cxl_region *region);
 int cxl_region_is_enabled(struct cxl_region *region);
 int cxl_region_disable(struct cxl_region *region);
 int cxl_region_enable(struct cxl_region *region);
+int cxl_region_delete(struct cxl_region *region);
 struct cxl_ctx *cxl_region_get_ctx(struct cxl_region *region);
 struct cxl_decoder *cxl_region_get_decoder(struct cxl_region *region);
 int cxl_region_get_id(struct cxl_region *region);
@@ -253,6 +260,20 @@ unsigned long long cxl_region_get_size(struct cxl_region *region);
 unsigned long long cxl_region_get_resource(struct cxl_region *region);
 unsigned int cxl_region_get_interleave_ways(struct cxl_region *region);
 unsigned int cxl_region_get_interleave_granularity(struct cxl_region *region);
+struct cxl_decoder *cxl_region_get_target_decoder(struct cxl_region *region,
+						  int position);
+int cxl_region_set_size(struct cxl_region *region, unsigned long long size);
+int cxl_region_set_uuid(struct cxl_region *region, uuid_t uu);
+int cxl_region_set_interleave_ways(struct cxl_region *region,
+				   unsigned int ways);
+int cxl_region_set_interleave_granularity(struct cxl_region *region,
+					  unsigned int granularity);
+int cxl_region_set_target(struct cxl_region *region, int position,
+			  struct cxl_decoder *decoder);
+int cxl_region_clear_target(struct cxl_region *region, int position);
+int cxl_region_clear_all_targets(struct cxl_region *region);
+int cxl_region_decode_commit(struct cxl_region *region);
+int cxl_region_decode_reset(struct cxl_region *region);
 
 #define cxl_region_foreach(decoder, region)                                    \
 	for (region = cxl_region_get_first(decoder); region != NULL;           \
cxl/lib/libcxl.sym +16 -0
index e410298..cb23a0b 100644
@@ -140,6 +140,7 @@ global:
 	cxl_decoder_is_mem_capable;
 	cxl_decoder_is_accelmem_capable;
 	cxl_decoder_is_locked;
+	cxl_decoder_create_pmem_region;
 	cxl_target_get_first;
 	cxl_target_get_next;
 	cxl_target_get_decoder;
@@ -183,6 +184,7 @@ global:
 	cxl_region_is_enabled;
 	cxl_region_disable;
 	cxl_region_enable;
+	cxl_region_delete;
 	cxl_region_get_ctx;
 	cxl_region_get_decoder;
 	cxl_region_get_id;
@@ -192,9 +194,23 @@ global:
 	cxl_region_get_resource;
 	cxl_region_get_interleave_ways;
 	cxl_region_get_interleave_granularity;
+	cxl_region_get_target_decoder;
+	cxl_region_set_size;
+	cxl_region_set_uuid;
+	cxl_region_set_interleave_ways;
+	cxl_region_set_interleave_granularity;
+	cxl_region_set_target;
+	cxl_region_clear_target;
+	cxl_region_clear_all_targets;
+	cxl_region_decode_commit;
+	cxl_region_decode_reset;
 	cxl_mapping_get_first;
 	cxl_mapping_get_next;
 	cxl_mapping_get_decoder;
 	cxl_mapping_get_region;
 	cxl_mapping_get_position;
+	cxl_decoder_get_by_name;
+	cxl_decoder_get_memdev;
+	cxl_decoder_get_interleave_granularity;
+	cxl_decoder_get_interleave_ways;
 } LIBCXL_2;
Signature · 1 line
2.37.1
author maintainer
#8 lore raw

PATCH v3 7/11 cxl: add a 'create-region' command

In reply to Vishal Verma

Add a 'create-region' command to cxl-cli that walks the platform's CXL hierarchy to find an appropriate root decoder based on any options provided, and uses libcxl APIs to create a 'region' that is comprehended by libnvdimm and ndctl.

---
 Documentation/cxl/bus-option.txt         |   5 +
 Documentation/cxl/cxl-create-region.txt  | 112 +++++
 Documentation/cxl/region-description.txt |   7 +
 cxl/builtin.h                            |   1 +
 cxl/filter.h                             |   4 +-
 cxl/cxl.c                                |   1 +
 cxl/json.c                               |   9 +
 cxl/region.c                             | 550 +++++++++++++++++++++++
 Documentation/cxl/meson.build            |   2 +
 cxl/meson.build                          |   1 +
 10 files changed, 691 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/cxl/bus-option.txt
 create mode 100644 Documentation/cxl/cxl-create-region.txt
 create mode 100644 Documentation/cxl/region-description.txt
 create mode 100644 cxl/region.c
Documentation/cxl/bus-option.txt new file +5 -0
new file mode 100644
index 0000000..02e2f08
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+
+-b::
+--bus=::
+	Restrict the operation to the specified bus.
Documentation/cxl/cxl-create-region.txt new file +112 -0

112 lines not on this page · raw message

Documentation/cxl/region-description.txt new file +7 -0
new file mode 100644
index 0000000..d7e3077
@@ -0,0 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
+
+DESCRIPTION
+-----------
+A CXL region is composed of one or more slices of CXL memdevs, with configurable
+interleave settings - both the number of interleave ways, and the interleave
+granularity.
cxl/builtin.h +1 -0
index 9e6fc62..843bada 100644
@@ -18,4 +18,5 @@ int cmd_disable_port(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_disable_bus(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
 #endif /* _CXL_BUILTIN_H_ */
cxl/filter.h +3 -1
index 609433c..d22d8b1 100644
@@ -35,8 +35,10 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
 struct cxl_port *util_cxl_port_filter_by_memdev(struct cxl_port *port,
 						const char *ident,
 						const char *serial);
-struct cxl_region *util_cxl_region_filter(struct cxl_region *region,
+struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,
 					    const char *__ident);
+struct cxl_region *util_cxl_region_filter(struct cxl_region *region,
+					  const char *__ident);
 
 enum cxl_port_filter_mode {
 	CXL_PF_SINGLE,
cxl/cxl.c +1 -0
index ef4cda9..f0afcfe 100644
@@ -72,6 +72,7 @@ static struct cmd_struct commands[] = {
 	{ "enable-port", .c_fn = cmd_enable_port },
 	{ "set-partition", .c_fn = cmd_set_partition },
 	{ "disable-bus", .c_fn = cmd_disable_bus },
+	{ "create-region", .c_fn = cmd_create_region },
 };
 
 int main(int argc, const char **argv)
cxl/json.c +9 -0
index 70cf286..9dc99df 100644
@@ -547,6 +547,7 @@ void util_cxl_mappings_append_json(struct json_object *jregion,
 	cxl_mapping_foreach(region, mapping) {
 		struct json_object *jmapping;
 		struct cxl_decoder *decoder;
+		struct cxl_memdev *memdev;
 
 		jmapping = json_object_new_object();
 		if (!jmapping)
@@ -564,6 +565,14 @@ void util_cxl_mappings_append_json(struct json_object *jregion,
 		if (!decoder)
 			continue;
 
+		memdev = cxl_decoder_get_memdev(decoder);
+		if (memdev) {
+			devname = cxl_memdev_get_devname(memdev);
+			jobj = json_object_new_string(devname);
+			if (jobj)
+				json_object_object_add(jmapping, "memdev", jobj);
+		}
+
 		devname = cxl_decoder_get_devname(decoder);
 		jobj = json_object_new_string(devname);
 		if (jobj)
cxl/region.c new file +550 -0

550 lines not on this page · raw message

Documentation/cxl/meson.build +2 -0
index 423be90..340cdee 100644
@@ -23,6 +23,7 @@ filedeps = [
   'memdev-option.txt',
   'labels-options.txt',
   'debug-option.txt',
+  'region-description.txt',
 ]
 
 cxl_manpages = [
@@ -39,6 +40,7 @@ cxl_manpages = [
   'cxl-set-partition.txt',
   'cxl-reserve-dpa.txt',
   'cxl-free-dpa.txt',
+  'cxl-create-region.txt',
 ]
 
 foreach man : cxl_manpages
cxl/meson.build +1 -0
index d63dcb1..f2474aa 100644
@@ -3,6 +3,7 @@ cxl_src = [
   'list.c',
   'port.c',
   'bus.c',
+  'region.c',
   'memdev.c',
   'json.c',
   'filter.c',
Signature · 1 line
2.37.1
author maintainer
#9 lore raw

PATCH v3 8/11 cxl: add commands to {enable,disable,destroy}-region

In reply to Vishal Verma

With a template from cxl-create-region in place, add its friends:

cxl enable-region
cxl disable-region
cxl destroy-region

---
 Documentation/cxl/cxl-destroy-region.txt |  41 +++++
 Documentation/cxl/cxl-disable-region.txt |  36 +++++
 Documentation/cxl/cxl-enable-region.txt  |  36 +++++
 Documentation/cxl/decoder-option.txt     |   6 +
 cxl/builtin.h                            |   3 +
 cxl/cxl.c                                |   3 +
 cxl/region.c                             | 193 ++++++++++++++++++++++-
 Documentation/cxl/meson.build            |   4 +
 8 files changed, 321 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/cxl/cxl-destroy-region.txt
 create mode 100644 Documentation/cxl/cxl-disable-region.txt
 create mode 100644 Documentation/cxl/cxl-enable-region.txt
 create mode 100644 Documentation/cxl/decoder-option.txt
Documentation/cxl/cxl-destroy-region.txt new file +41 -0
new file mode 100644
index 0000000..74f4093
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+
+cxl-destroy-region(1)
+=====================
+
+NAME
+----
+cxl-destroy-region - destroy specified region(s).
+
+SYNOPSIS
+--------
+[verse]
+'cxl destroy-region <region> [<options>]'
+
+include::region-description.txt[]
+
+EXAMPLE
+-------
+----
+# cxl destroy-region all
+destroyed 2 regions
+----
+
+OPTIONS
+-------
+include::bus-option.txt[]
+
+-f::
+--force::
+	Force a destroy operation even if the region is active.
+	This will attempt to disable the region first.
+
+include::decoder-option.txt[]
+
+include::debug-option.txt[]
+
+include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-list[1], linkcxl:cxl-create-region[1]
Documentation/cxl/cxl-disable-region.txt new file +36 -0
new file mode 100644
index 0000000..6a39aee
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+cxl-disable-region(1)
+=====================
+
+NAME
+----
+cxl-disable-region - disable specified region(s).
+
+SYNOPSIS
+--------
+[verse]
+'cxl disable-region <region> [<options>]'
+
+include::region-description.txt[]
+
+EXAMPLE
+-------
+----
+# cxl disable-region all
+disabled 2 regions
+----
+
+OPTIONS
+-------
+include::bus-option.txt[]
+
+include::decoder-option.txt[]
+
+include::debug-option.txt[]
+
+include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-list[1], linkcxl:cxl-enable-region[1]
Documentation/cxl/cxl-enable-region.txt new file +36 -0
new file mode 100644
index 0000000..f6ef00f
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+cxl-enable-region(1)
+=====================
+
+NAME
+----
+cxl-enable-region - enable specified region(s).
+
+SYNOPSIS
+--------
+[verse]
+'cxl enable-region <region> [<options>]'
+
+include::region-description.txt[]
+
+EXAMPLE
+-------
+----
+# cxl enable-region all
+enabled 2 regions
+----
+
+OPTIONS
+-------
+include::bus-option.txt[]
+
+include::decoder-option.txt[]
+
+include::debug-option.txt[]
+
+include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-list[1], linkcxl:cxl-disable-region[1]
Documentation/cxl/decoder-option.txt new file +6 -0
new file mode 100644
index 0000000..e638d6e
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+
+-d::
+--decoder=::
+	The root decoder to limit the operation to. Only regions that are
+	children of the specified decoder will be acted upon.
cxl/builtin.h +3 -0
index 843bada..b28c221 100644
@@ -19,4 +19,7 @@ int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_disable_bus(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx);
 #endif /* _CXL_BUILTIN_H_ */
cxl/cxl.c +3 -0
index f0afcfe..dd1be7a 100644
@@ -73,6 +73,9 @@ static struct cmd_struct commands[] = {
 	{ "set-partition", .c_fn = cmd_set_partition },
 	{ "disable-bus", .c_fn = cmd_disable_bus },
 	{ "create-region", .c_fn = cmd_create_region },
+	{ "enable-region", .c_fn = cmd_enable_region },
+	{ "disable-region", .c_fn = cmd_disable_region },
+	{ "destroy-region", .c_fn = cmd_destroy_region },
 };
 
 int main(int argc, const char **argv)
cxl/region.c +192 -1

226 lines not on this page · raw message

Documentation/cxl/meson.build +4 -0
index 340cdee..147ea71 100644
@@ -24,6 +24,7 @@ filedeps = [
   'labels-options.txt',
   'debug-option.txt',
   'region-description.txt',
+  'decoder-option.txt',
 ]
 
 cxl_manpages = [
@@ -41,6 +42,9 @@ cxl_manpages = [
   'cxl-reserve-dpa.txt',
   'cxl-free-dpa.txt',
   'cxl-create-region.txt',
+  'cxl-disable-region.txt',
+  'cxl-enable-region.txt',
+  'cxl-destroy-region.txt',
 ]
 
 foreach man : cxl_manpages
Signature · 1 line
2.37.1
author maintainer
#10 lore raw

PATCH v3 9/11 cxl/list: make memdevs and regions the default listing

In reply to Vishal Verma

Instead of only listing regions by default (which can often be empty if no regions have been configured), change the default listing mode to both memdevs and regions. This will allow a plain 'cxl-list' to be a quick health check of whether all the expected memdevs have enumerated correctly, and see any regions that have been configured.

---
 cxl/list.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/list.c +2 -1
index 88ca9d9..5f604ec 100644
@@ -100,9 +100,10 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
 			param.regions = true;
 	}
 
-	/* List regions by default */
+	/* List regions and memdevs by default */
 	if (num_list_flags() == 0) {
 		param.regions = true;
+		param.memdevs = true;
 	}
 
 	log_init(&param.ctx, "cxl list", "CXL_LIST_LOG");
Signature · 1 line
2.37.1
author maintainer
#11 lore raw

PATCH v3 10/11 test: add a cxl-create-region test

In reply to Vishal Verma

Add a unit test to exercise the cxl-create-region command with different combinations of memdevs and decoders, using cxl_test based mocked devices.

---
 test/cxl-create-region.sh | 125 ++++++++++++++++++++++++++++++++++++++
 test/meson.build          |   2 +
 2 files changed, 127 insertions(+)
 create mode 100644 test/cxl-create-region.sh
test/cxl-create-region.sh new file +125 -0
new file mode 100644
index 0000000..66df38f
@@ -0,0 +1,125 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2022 Intel Corporation. All rights reserved.
+
+. $(dirname $0)/common
+
+rc=1
+
+set -ex
+
+trap 'err $LINENO' ERR
+
+check_prereq "jq"
+
+modprobe -r cxl_test
+modprobe cxl_test
+udevadm settle
+
+destroy_regions()
+{
+	if [[ "$*" ]]; then
+		$CXL destroy-region -f -b cxl_test "$@"
+	else
+		$CXL destroy-region -f -b cxl_test all
+	fi
+}
+
+create_x1_region()
+{
+	mem="$1"
+
+	# find a pmem capable root decoder for this mem
+	decoder=$($CXL list -b cxl_test -D -d root -m "$mem" |
+		  jq -r ".[] |
+		  select(.pmem_capable == true) |
+		  select(.nr_targets == 1) |
+		  .decoder")
+
+	if [[ ! $decoder ]]; then
+		echo "no suitable decoder found for $mem, skipping"
+		return
+	fi
+
+	# create region
+	region=$($CXL create-region -d "$decoder" -m "$mem" | jq -r ".region")
+
+	if [[ ! $region ]]; then
+		echo "create-region failed for $decoder / $mem"
+		err "$LINENO"
+	fi
+
+	# cycle disable/enable
+	$CXL disable-region --bus=cxl_test "$region"
+	$CXL enable-region --bus=cxl_test "$region"
+
+	# cycle destroying and creating the same region
+	destroy_regions "$region"
+	region=$($CXL create-region -d "$decoder" -m "$mem" | jq -r ".region")
+
+	if [[ ! $region ]]; then
+		echo "create-region failed for $decoder / $mem"
+		err "$LINENO"
+	fi
+	destroy_regions "$region"
+}
+
+create_subregions()
+{
+	slice=$((256 << 20))
+	mem="$1"
+
+	# find a pmem capable root decoder for this mem
+	decoder=$($CXL list -b cxl_test -D -d root -m "$mem" |
+		  jq -r ".[] |
+		  select(.pmem_capable == true) |
+		  select(.nr_targets == 1) |
+		  .decoder")
+
+	if [[ ! $decoder ]]; then
+		echo "no suitable decoder found for $mem, skipping"
+		return
+	fi
+
+	size="$($CXL list -m "$mem" | jq -r '.[].pmem_size')"
+	if [[ ! $size ]]; then
+		echo "$mem: unable to determine size"
+		err "$LINENO"
+	fi
+
+	num_regions=$((size / slice))
+
+	declare -a regions
+	for (( i = 0; i < num_regions; i++ )); do
+		regions[$i]=$($CXL create-region -d "$decoder" -m "$mem" -s "$slice" | jq -r ".region")
+		if [[ ! ${regions[$i]} ]]; then
+			echo "create sub-region failed for $decoder / $mem"
+			err "$LINENO"
+		fi
+		udevadm settle
+	done
+
+	echo "created $num_regions subregions:"
+	for (( i = 0; i < num_regions; i++ )); do
+		echo "${regions[$i]}"
+	done
+
+	for (( i = (num_regions - 1); i >= 0; i-- )); do
+		destroy_regions "${regions[$i]}"
+	done
+}
+
+# test reading labels directly through cxl-cli
+readarray -t mems < <("$CXL" list -b cxl_test -M | jq -r '.[].memdev')
+
+for mem in ${mems[@]}; do
+	create_x1_region "$mem"
+done
+
+# test multiple subregions under the same decoder, using slices of the same memdev
+# to test out back-to-back pmem DPA allocations on memdevs
+for mem in ${mems[@]}; do
+	create_subregions "$mem"
+done
+
+modprobe -r cxl_test
test/meson.build +2 -0
index b382f46..5953c28 100644
@@ -153,6 +153,7 @@ track_uuid = find_program('track-uuid.sh')
 cxl_topo = find_program('cxl-topology.sh')
 cxl_sysfs = find_program('cxl-region-sysfs.sh')
 cxl_labels = find_program('cxl-labels.sh')
+cxl_create_region = find_program('cxl-create-region.sh')
 
 tests = [
   [ 'libndctl',               libndctl,		  'ndctl' ],
@@ -180,6 +181,7 @@ tests = [
   [ 'cxl-topology.sh',	      cxl_topo,		  'cxl'   ],
   [ 'cxl-region-sysfs.sh',    cxl_sysfs,	  'cxl'   ],
   [ 'cxl-labels.sh',          cxl_labels,	  'cxl'   ],
+  [ 'cxl-create-region.sh',   cxl_create_region,  'cxl'   ],
 ]
 
 if get_option('destructive').enabled()
Signature · 1 line
2.37.1
author maintainer
#12 lore raw

PATCH v3 11/11 cxl/decoder: add a max_available_extent attribute

In reply to Vishal Verma

Add a max_available_extent attribute to cxl_decoder. In order to aid in its calculation, change the order of regions in the root decoder's list to be sorted by start HPA of the region.

Additionally, emit this attribute in decoder listings, and consult it for available space before creating a new region.

---
 cxl/lib/private.h  |  1 +
 cxl/lib/libcxl.c   | 84 +++++++++++++++++++++++++++++++++++++++++++++-
 cxl/libcxl.h       |  3 ++
 cxl/json.c         |  8 +++++
 cxl/region.c       | 14 +++++++-
 cxl/lib/libcxl.sym |  1 +
 6 files changed, 109 insertions(+), 2 deletions(-)
cxl/lib/private.h +1 -0
index 8bc9620..437eade 100644
@@ -104,6 +104,7 @@ struct cxl_decoder {
 	u64 size;
 	u64 dpa_resource;
 	u64 dpa_size;
+	u64 max_available_extent;
 	void *dev_buf;
 	size_t buf_len;
 	char *dev_path;
cxl/lib/libcxl.c +83 -1

114 lines not on this page · raw message

cxl/libcxl.h +3 -0
index 69d9c09..61c7fc4 100644
@@ -134,6 +134,9 @@ unsigned long long cxl_decoder_get_resource(struct cxl_decoder *decoder);
 unsigned long long cxl_decoder_get_size(struct cxl_decoder *decoder);
 unsigned long long cxl_decoder_get_dpa_resource(struct cxl_decoder *decoder);
 unsigned long long cxl_decoder_get_dpa_size(struct cxl_decoder *decoder);
+unsigned long long
+cxl_decoder_get_max_available_extent(struct cxl_decoder *decoder);
+
 enum cxl_decoder_mode {
 	CXL_DECODER_MODE_NONE,
 	CXL_DECODER_MODE_MIXED,
cxl/json.c +8 -0
index 9dc99df..9cec58b 100644
@@ -499,6 +499,14 @@ struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
 	}
 
 	if (cxl_port_is_root(port) && cxl_decoder_is_mem_capable(decoder)) {
+		size = cxl_decoder_get_max_available_extent(decoder);
+		if (size < ULLONG_MAX) {
+			jobj = util_json_object_size(size, flags);
+			if (jobj)
+				json_object_object_add(jdecoder,
+						       "max_available_extent",
+						       jobj);
+		}
 		if (cxl_decoder_is_pmem_capable(decoder)) {
 			jobj = json_object_new_boolean(true);
 			if (jobj)
cxl/region.c +13 -1
index b22d3c8..a30313c 100644
@@ -438,9 +438,9 @@ static int create_region(struct cxl_ctx *ctx, int *count,
 	struct json_object *jregion;
 	unsigned int i, granularity;
 	struct cxl_region *region;
+	u64 size, max_extent;
 	const char *devname;
 	uuid_t uuid;
-	u64 size;
 	int rc;
 
 	rc = create_region_validate_config(ctx, p);
@@ -455,6 +455,18 @@ static int create_region(struct cxl_ctx *ctx, int *count,
 		log_err(&rl, "%s: unable to determine region size\n", __func__);
 		return -ENXIO;
 	}
+	max_extent = cxl_decoder_get_max_available_extent(p->root_decoder);
+	if (max_extent == ULLONG_MAX) {
+		log_err(&rl, "%s: unable to determine max extent\n",
+			cxl_decoder_get_devname(p->root_decoder));
+		return -EINVAL;
+	}
+	if (size > max_extent) {
+		log_err(&rl,
+			"%s: region size %#lx exceeds max available space\n",
+			cxl_decoder_get_devname(p->root_decoder), size);
+		return -ENOSPC;
+	}
 
 	if (p->mode == CXL_DECODER_MODE_PMEM) {
 		region = cxl_decoder_create_pmem_region(p->root_decoder);
cxl/lib/libcxl.sym +1 -0
index cb23a0b..549f88d 100644
@@ -213,4 +213,5 @@ global:
 	cxl_decoder_get_memdev;
 	cxl_decoder_get_interleave_granularity;
 	cxl_decoder_get_interleave_ways;
+	cxl_decoder_get_max_available_extent;
 } LIBCXL_2;
Signature · 1 line
2.37.1