Blame SOURCES/0184-cxl-memdev-Add-reserve-free-dpa-commands.patch

26ccd9
From 6624f4fdf7ba43039111c996dfd3982b4fdc43bc Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Thu, 14 Jul 2022 10:02:44 -0700
26ccd9
Subject: [PATCH 184/217] cxl/memdev: Add {reserve,free}-dpa commands
26ccd9
26ccd9
Add helper commands for managing allocations of DPA (device physical
26ccd9
address) capacity on a set of CXL memory devices.
26ccd9
26ccd9
The main convenience this command affords is automatically picking the next
26ccd9
decoder to allocate per-memdev.
26ccd9
26ccd9
For example, to allocate 256MiB from all endpoints that are covered by a
26ccd9
given root decoder, and collect those resulting endpoint-decoders into an
26ccd9
array:
26ccd9
26ccd9
  readarray -t mem < <(cxl list -M -d $decoder | jq -r ".[].memdev")
26ccd9
  readarray -t endpoint < <(cxl reserve-dpa -t pmem ${mem[*]} -s $((256<<20)) |
26ccd9
                            jq -r ".[] | .decoder.decoder")
26ccd9
26ccd9
Link: https://lore.kernel.org/r/165781816425.1555691.17958897857798325111.stgit@dwillia2-xfh.jf.intel.com
26ccd9
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
[vishal: fix typo pointed out by Jonathan]
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 .clang-format                         |   1 +
26ccd9
 Documentation/cxl/cxl-free-dpa.txt    |  53 +++++
26ccd9
 Documentation/cxl/cxl-reserve-dpa.txt |  67 ++++++
26ccd9
 Documentation/cxl/lib/libcxl.txt      |   2 +
26ccd9
 Documentation/cxl/meson.build         |   2 +
26ccd9
 cxl/builtin.h                         |   2 +
26ccd9
 cxl/cxl.c                             |   2 +
26ccd9
 cxl/filter.c                          |   4 +-
26ccd9
 cxl/filter.h                          |   2 +
26ccd9
 cxl/lib/libcxl.c                      |  86 ++++++++
26ccd9
 cxl/lib/libcxl.sym                    |   4 +
26ccd9
 cxl/libcxl.h                          |   9 +
26ccd9
 cxl/memdev.c                          | 280 +++++++++++++++++++++++++-
26ccd9
 13 files changed, 511 insertions(+), 3 deletions(-)
26ccd9
 create mode 100644 Documentation/cxl/cxl-free-dpa.txt
26ccd9
 create mode 100644 Documentation/cxl/cxl-reserve-dpa.txt
26ccd9
26ccd9
diff --git a/.clang-format b/.clang-format
26ccd9
index 6aabcb6..7254a1b 100644
26ccd9
--- a/.clang-format
26ccd9
+++ b/.clang-format
26ccd9
@@ -81,6 +81,7 @@ ForEachMacros:
26ccd9
   - 'cxl_bus_foreach'
26ccd9
   - 'cxl_port_foreach'
26ccd9
   - 'cxl_decoder_foreach'
26ccd9
+  - 'cxl_decoder_foreach_reverse'
26ccd9
   - 'cxl_target_foreach'
26ccd9
   - 'cxl_dport_foreach'
26ccd9
   - 'cxl_endpoint_foreach'
26ccd9
diff --git a/Documentation/cxl/cxl-free-dpa.txt b/Documentation/cxl/cxl-free-dpa.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..73fb048
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-free-dpa.txt
26ccd9
@@ -0,0 +1,53 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-free-dpa(1)
26ccd9
+===============
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-free-dpa - release device-physical address space
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl free-dpa' <mem0> [<mem1>..<memN>] [<options>]
26ccd9
+
26ccd9
+The CXL region provisioning process proceeds in multiple steps. One of
26ccd9
+the steps is identifying and reserving the DPA span that each member of
26ccd9
+the interleave-set (region) contributes in advance of attaching that
26ccd9
+allocation to a region. For development, test, and debug purposes this
26ccd9
+command is a helper to find the last allocated decoder on a device and
26ccd9
+zero-out / free its DPA allocation.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+<memory device(s)>::
26ccd9
+include::memdev-option.txt[]
26ccd9
+
26ccd9
+-d::
26ccd9
+--decoder::
26ccd9
+	Specify the decoder to free. The CXL specification
26ccd9
+	mandates that DPA must be released in the reverse order it was
26ccd9
+	allocated. See linkcxl:cxl-reserve-dpa[1]
26ccd9
+
26ccd9
+-t::
26ccd9
+--type::
26ccd9
+	Constrain the search for "last allocated decoder" to decoders targeting
26ccd9
+	the given partition.
26ccd9
+
26ccd9
+-f::
26ccd9
+--force::
26ccd9
+	The kernel enforces CXL DPA ordering constraints on deallocation events,
26ccd9
+	and the tool anticipates those and fails operations that are expected to
26ccd9
+	fail without sending them to the kernel. For test purposes, continue to
26ccd9
+	attempt "expected to fail" operations to exercise the driver.
26ccd9
+
26ccd9
+-v::
26ccd9
+	Turn on verbose debug messages in the library (if libcxl was built with
26ccd9
+	logging and debug enabled).
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-reserve-dpa[1]
26ccd9
diff --git a/Documentation/cxl/cxl-reserve-dpa.txt b/Documentation/cxl/cxl-reserve-dpa.txt
26ccd9
new file mode 100644
26ccd9
index 0000000..5e79ef2
26ccd9
--- /dev/null
26ccd9
+++ b/Documentation/cxl/cxl-reserve-dpa.txt
26ccd9
@@ -0,0 +1,67 @@
26ccd9
+// SPDX-License-Identifier: GPL-2.0
26ccd9
+
26ccd9
+cxl-reserve-dpa(1)
26ccd9
+==================
26ccd9
+
26ccd9
+NAME
26ccd9
+----
26ccd9
+cxl-reserve-dpa - allocate device-physical address space
26ccd9
+
26ccd9
+SYNOPSIS
26ccd9
+--------
26ccd9
+[verse]
26ccd9
+'cxl reserve-dpa' <mem0> [<mem1>..<memN>] [<options>]
26ccd9
+
26ccd9
+The CXL region provisioning process proceeds in multiple steps. One of
26ccd9
+the steps is identifying and reserving the DPA span that each member of
26ccd9
+the interleave-set (region) contributes in advance of attaching that
26ccd9
+allocation to a region. For development, test, and debug purposes this
26ccd9
+command is a helper to find the next available decoder on endpoint
26ccd9
+(memdev) and mark a span of DPA as busy.
26ccd9
+
26ccd9
+OPTIONS
26ccd9
+-------
26ccd9
+<memory device(s)>::
26ccd9
+include::memdev-option.txt[]
26ccd9
+
26ccd9
+-d::
26ccd9
+--decoder::
26ccd9
+	Specify the decoder to attempt the allocation. The CXL specification
26ccd9
+	mandates that allocations must be ordered by DPA and decoder instance.
26ccd9
+	I.e. the lowest DPA allocation on the device is covered by decoder0, and
26ccd9
+	the last / highest DPA allocation is covered by the last decoder. This
26ccd9
+	ordering is enforced by the kernel. By default the tool picks the 'next
26ccd9
+	available' decoder.
26ccd9
+
26ccd9
+-t::
26ccd9
+--type::
26ccd9
+	Select the partition for the allocation. CXL devices implement a
26ccd9
+	partition that divdes 'ram' and 'pmem' capacity, where 'pmem' capacity
26ccd9
+	consumes the higher DPA capacity above the partition boundary. The type
26ccd9
+	defaults to 'pmem'. Note that given CXL DPA allocation constraints, once
26ccd9
+	any 'pmem' allocation is established then all remaining 'ram' capacity
26ccd9
+	becomes reserved (skipped).
26ccd9
+
26ccd9
+-f::
26ccd9
+--force::
26ccd9
+	The kernel enforces CXL DPA allocation ordering constraints, and
26ccd9
+	the tool anticipates those and fails operations that are expected to
26ccd9
+	fail without sending them to the kernel. For test purposes, continue to
26ccd9
+	attempt "expected to fail" operations to exercise the driver.
26ccd9
+
26ccd9
+-s::
26ccd9
+--size::
26ccd9
+	Specify the size of the allocation. This option supports the suffixes
26ccd9
+	"k" or "K" for KiB, "m" or "M" for MiB, "g" or "G" for GiB and "t" or
26ccd9
+	"T" for TiB. This defaults to "all available capacity of the specified
26ccd9
+	type".
26ccd9
+
26ccd9
+-v::
26ccd9
+	Turn on verbose debug messages in the library (if libcxl was built with
26ccd9
+	logging and debug enabled).
26ccd9
+
26ccd9
+include::../copyright.txt[]
26ccd9
+
26ccd9
+SEE ALSO
26ccd9
+--------
26ccd9
+linkcxl:cxl-free-dpa[1]
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index 90fe338..7a38ce4 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -394,6 +394,7 @@ unsigned long long cxl_decoder_get_resource(struct cxl_decoder *decoder);
26ccd9
 unsigned long long cxl_decoder_get_size(struct cxl_decoder *decoder);
26ccd9
 unsigned long long cxl_decoder_get_dpa_resource(struct cxl_decoder *decoder);
26ccd9
 unsigned long long cxl_decoder_get_dpa_size(struct cxl_decoder *decoder);
26ccd9
+int cxl_decoder_set_dpa_size(struct cxl_decoder *decoder, unsigned long long size);
26ccd9
 const char *cxl_decoder_get_devname(struct cxl_decoder *decoder);
26ccd9
 int cxl_decoder_get_id(struct cxl_decoder *decoder);
26ccd9
 int cxl_decoder_get_nr_targets(struct cxl_decoder *decoder);
26ccd9
@@ -413,6 +414,7 @@ enum cxl_decoder_mode {
26ccd9
 	CXL_DECODER_MODE_RAM,
26ccd9
 };
26ccd9
 enum cxl_decoder_mode cxl_decoder_get_mode(struct cxl_decoder *decoder);
26ccd9
+int cxl_decoder_set_mode(struct cxl_decoder *decoder, enum cxl_decoder_mode mode);
26ccd9
 
26ccd9
 bool cxl_decoder_is_pmem_capable(struct cxl_decoder *decoder);
26ccd9
 bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder);
26ccd9
diff --git a/Documentation/cxl/meson.build b/Documentation/cxl/meson.build
26ccd9
index 974a5a4..d019dfc 100644
26ccd9
--- a/Documentation/cxl/meson.build
26ccd9
+++ b/Documentation/cxl/meson.build
26ccd9
@@ -36,6 +36,8 @@ cxl_manpages = [
26ccd9
   'cxl-disable-port.txt',
26ccd9
   'cxl-disable-bus.txt',
26ccd9
   'cxl-set-partition.txt',
26ccd9
+  'cxl-reserve-dpa.txt',
26ccd9
+  'cxl-free-dpa.txt',
26ccd9
 ]
26ccd9
 
26ccd9
 foreach man : cxl_manpages
26ccd9
diff --git a/cxl/builtin.h b/cxl/builtin.h
26ccd9
index a437bc3..9e6fc62 100644
26ccd9
--- a/cxl/builtin.h
26ccd9
+++ b/cxl/builtin.h
26ccd9
@@ -12,6 +12,8 @@ int cmd_init_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_check_labels(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_disable_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_enable_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_reserve_dpa(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
+int cmd_free_dpa(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_disable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_enable_port(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
 int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx);
26ccd9
diff --git a/cxl/cxl.c b/cxl/cxl.c
26ccd9
index aa4ce61..ef4cda9 100644
26ccd9
--- a/cxl/cxl.c
26ccd9
+++ b/cxl/cxl.c
26ccd9
@@ -66,6 +66,8 @@ static struct cmd_struct commands[] = {
26ccd9
 	{ "write-labels", .c_fn = cmd_write_labels },
26ccd9
 	{ "disable-memdev", .c_fn = cmd_disable_memdev },
26ccd9
 	{ "enable-memdev", .c_fn = cmd_enable_memdev },
26ccd9
+	{ "reserve-dpa", .c_fn = cmd_reserve_dpa },
26ccd9
+	{ "free-dpa", .c_fn = cmd_free_dpa },
26ccd9
 	{ "disable-port", .c_fn = cmd_disable_port },
26ccd9
 	{ "enable-port", .c_fn = cmd_enable_port },
26ccd9
 	{ "set-partition", .c_fn = cmd_set_partition },
26ccd9
diff --git a/cxl/filter.c b/cxl/filter.c
26ccd9
index 2f88a9d..e5fab19 100644
26ccd9
--- a/cxl/filter.c
26ccd9
+++ b/cxl/filter.c
26ccd9
@@ -380,8 +380,8 @@ struct cxl_port *util_cxl_port_filter_by_memdev(struct cxl_port *port,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
-static struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,
26ccd9
-						   const char *__ident)
26ccd9
+struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,
26ccd9
+					    const char *__ident)
26ccd9
 {
26ccd9
 	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
 	int pid, did;
26ccd9
diff --git a/cxl/filter.h b/cxl/filter.h
26ccd9
index 9557943..c913daf 100644
26ccd9
--- a/cxl/filter.h
26ccd9
+++ b/cxl/filter.h
26ccd9
@@ -50,6 +50,8 @@ struct cxl_target *util_cxl_target_filter_by_memdev(struct cxl_target *target,
26ccd9
 struct cxl_dport *util_cxl_dport_filter_by_memdev(struct cxl_dport *dport,
26ccd9
 						  const char *ident,
26ccd9
 						  const char *serial);
26ccd9
+struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,
26ccd9
+					    const char *__ident);
26ccd9
 int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *param);
26ccd9
 bool cxl_filter_has(const char *needle, const char *__filter);
26ccd9
 #endif /* _CXL_UTIL_FILTER_H_ */
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index b802e5d..e52896f 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -1121,6 +1121,20 @@ CXL_EXPORT struct cxl_decoder *cxl_decoder_get_next(struct cxl_decoder *decoder)
26ccd9
 	return list_next(&port->decoders, decoder, list);
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT struct cxl_decoder *cxl_decoder_get_last(struct cxl_port *port)
26ccd9
+{
26ccd9
+	cxl_decoders_init(port);
26ccd9
+
26ccd9
+	return list_tail(&port->decoders, struct cxl_decoder, list);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_decoder *cxl_decoder_get_prev(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	struct cxl_port *port = decoder->port;
26ccd9
+
26ccd9
+	return list_prev(&port->decoders, decoder, list);
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT struct cxl_ctx *cxl_decoder_get_ctx(struct cxl_decoder *decoder)
26ccd9
 {
26ccd9
 	return decoder->ctx;
26ccd9
@@ -1176,6 +1190,78 @@ cxl_decoder_get_dpa_size(struct cxl_decoder *decoder)
26ccd9
 	return decoder->dpa_size;
26ccd9
 }
26ccd9
 
26ccd9
+CXL_EXPORT int cxl_decoder_set_dpa_size(struct cxl_decoder *decoder,
26ccd9
+					unsigned long long size)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
26ccd9
+	char *path = decoder->dev_buf;
26ccd9
+	int len = decoder->buf_len, rc;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (!cxl_port_is_endpoint(port)) {
26ccd9
+		err(ctx, "%s: not an endpoint decoder\n",
26ccd9
+		    cxl_decoder_get_devname(decoder));
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/dpa_size", decoder->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n",
26ccd9
+		    cxl_decoder_get_devname(decoder));
26ccd9
+		return -ENOMEM;
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
+	decoder->dpa_size = size;
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_decoder_set_mode(struct cxl_decoder *decoder,
26ccd9
+				    enum cxl_decoder_mode mode)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	struct cxl_ctx *ctx = cxl_decoder_get_ctx(decoder);
26ccd9
+	char *path = decoder->dev_buf;
26ccd9
+	int len = decoder->buf_len, rc;
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+
26ccd9
+	if (!cxl_port_is_endpoint(port)) {
26ccd9
+		err(ctx, "%s: not an endpoint decoder\n",
26ccd9
+		    cxl_decoder_get_devname(decoder));
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	switch (mode) {
26ccd9
+	case CXL_DECODER_MODE_PMEM:
26ccd9
+		sprintf(buf, "pmem");
26ccd9
+		break;
26ccd9
+	case CXL_DECODER_MODE_RAM:
26ccd9
+		sprintf(buf, "ram");
26ccd9
+		break;
26ccd9
+	default:
26ccd9
+		err(ctx, "%s: unsupported mode: %d\n",
26ccd9
+		    cxl_decoder_get_devname(decoder), mode);
26ccd9
+		return -EINVAL;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (snprintf(path, len, "%s/mode", decoder->dev_path) >= len) {
26ccd9
+		err(ctx, "%s: buffer too small!\n",
26ccd9
+		    cxl_decoder_get_devname(decoder));
26ccd9
+		return -ENOMEM;
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = sysfs_write_attr(ctx, path, buf);
26ccd9
+	if (rc < 0)
26ccd9
+		return rc;
26ccd9
+
26ccd9
+	decoder->mode = mode;
26ccd9
+	return 0;
26ccd9
+}
26ccd9
+
26ccd9
 CXL_EXPORT enum cxl_decoder_mode
26ccd9
 cxl_decoder_get_mode(struct cxl_decoder *decoder)
26ccd9
 {
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index 88c5a7e..7712de0 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -173,4 +173,8 @@ global:
26ccd9
 	cxl_decoder_get_dpa_resource;
26ccd9
 	cxl_decoder_get_dpa_size;
26ccd9
 	cxl_decoder_get_mode;
26ccd9
+	cxl_decoder_get_last;
26ccd9
+	cxl_decoder_get_prev;
26ccd9
+	cxl_decoder_set_dpa_size;
26ccd9
+	cxl_decoder_set_mode;
26ccd9
 } LIBCXL_2;
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index 1436dc4..33a216e 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -139,6 +139,7 @@ enum cxl_decoder_mode {
26ccd9
 	CXL_DECODER_MODE_PMEM,
26ccd9
 	CXL_DECODER_MODE_RAM,
26ccd9
 };
26ccd9
+
26ccd9
 static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
26ccd9
 {
26ccd9
 	static const char *names[] = {
26ccd9
@@ -154,6 +155,10 @@ static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
26ccd9
 }
26ccd9
 
26ccd9
 enum cxl_decoder_mode cxl_decoder_get_mode(struct cxl_decoder *decoder);
26ccd9
+int cxl_decoder_set_mode(struct cxl_decoder *decoder,
26ccd9
+			 enum cxl_decoder_mode mode);
26ccd9
+int cxl_decoder_set_dpa_size(struct cxl_decoder *decoder,
26ccd9
+			     unsigned long long size);
26ccd9
 const char *cxl_decoder_get_devname(struct cxl_decoder *decoder);
26ccd9
 struct cxl_target *cxl_decoder_get_target_by_memdev(struct cxl_decoder *decoder,
26ccd9
 						    struct cxl_memdev *memdev);
26ccd9
@@ -182,6 +187,10 @@ bool cxl_decoder_is_locked(struct cxl_decoder *decoder);
26ccd9
 	for (decoder = cxl_decoder_get_first(port); decoder != NULL;           \
26ccd9
 	     decoder = cxl_decoder_get_next(decoder))
26ccd9
 
26ccd9
+#define cxl_decoder_foreach_reverse(port, decoder)                             \
26ccd9
+	for (decoder = cxl_decoder_get_last(port); decoder != NULL;           \
26ccd9
+	     decoder = cxl_decoder_get_prev(decoder))
26ccd9
+
26ccd9
 struct cxl_target;
26ccd9
 struct cxl_target *cxl_target_get_first(struct cxl_decoder *decoder);
26ccd9
 struct cxl_target *cxl_target_get_next(struct cxl_target *target);
26ccd9
diff --git a/cxl/memdev.c b/cxl/memdev.c
26ccd9
index 1cecad2..e42f554 100644
26ccd9
--- a/cxl/memdev.c
26ccd9
+++ b/cxl/memdev.c
26ccd9
@@ -33,6 +33,7 @@ static struct parameters {
26ccd9
 	bool align;
26ccd9
 	const char *type;
26ccd9
 	const char *size;
26ccd9
+	const char *decoder_filter;
26ccd9
 } param;
26ccd9
 
26ccd9
 static struct log_ctx ml;
26ccd9
@@ -71,6 +72,19 @@ OPT_STRING('s', "size",  &param.size, "size",			\
26ccd9
 OPT_BOOLEAN('a', "align",  &param.align,			\
26ccd9
 	"auto-align --size per device's requirement")
26ccd9
 
26ccd9
+#define RESERVE_DPA_OPTIONS()                                          \
26ccd9
+OPT_STRING('s', "size", &param.size, "size",                           \
26ccd9
+	   "size in bytes (Default: all available capacity)")
26ccd9
+
26ccd9
+#define DPA_OPTIONS()                                          \
26ccd9
+OPT_STRING('d', "decoder", &param.decoder_filter,              \
26ccd9
+   "decoder instance id",                                      \
26ccd9
+   "override the automatic decoder selection"),                \
26ccd9
+OPT_STRING('t', "type", &param.type, "type",                   \
26ccd9
+	   "'pmem' or 'ram' (volatile) (Default: 'pmem')"),    \
26ccd9
+OPT_BOOLEAN('f', "force", &param.force,                        \
26ccd9
+	    "Attempt 'expected to fail' operations")
26ccd9
+
26ccd9
 static const struct option read_options[] = {
26ccd9
 	BASE_OPTIONS(),
26ccd9
 	LABEL_OPTIONS(),
26ccd9
@@ -108,6 +122,242 @@ static const struct option set_partition_options[] = {
26ccd9
 	OPT_END(),
26ccd9
 };
26ccd9
 
26ccd9
+static const struct option reserve_dpa_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	RESERVE_DPA_OPTIONS(),
26ccd9
+	DPA_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+static const struct option free_dpa_options[] = {
26ccd9
+	BASE_OPTIONS(),
26ccd9
+	DPA_OPTIONS(),
26ccd9
+	OPT_END(),
26ccd9
+};
26ccd9
+
26ccd9
+enum reserve_dpa_mode {
26ccd9
+	DPA_ALLOC,
26ccd9
+	DPA_FREE,
26ccd9
+};
26ccd9
+
26ccd9
+static int __reserve_dpa(struct cxl_memdev *memdev,
26ccd9
+			 enum reserve_dpa_mode alloc_mode,
26ccd9
+			 struct action_context *actx)
26ccd9
+{
26ccd9
+	struct cxl_decoder *decoder, *auto_target = NULL, *target = NULL;
26ccd9
+	struct cxl_endpoint *endpoint = cxl_memdev_get_endpoint(memdev);
26ccd9
+	const char *devname = cxl_memdev_get_devname(memdev);
26ccd9
+	unsigned long long avail_dpa, size;
26ccd9
+	enum cxl_decoder_mode mode;
26ccd9
+	struct cxl_port *port;
26ccd9
+	char buf[256];
26ccd9
+	int rc;
26ccd9
+
26ccd9
+	if (param.type) {
26ccd9
+		if (strcmp(param.type, "ram") == 0)
26ccd9
+			mode = CXL_DECODER_MODE_RAM;
26ccd9
+		else if (strcmp(param.type, "volatile") == 0)
26ccd9
+			mode = CXL_DECODER_MODE_RAM;
26ccd9
+		else if (strcmp(param.type, "ram") == 0)
26ccd9
+			mode = CXL_DECODER_MODE_RAM;
26ccd9
+		else if (strcmp(param.type, "pmem") == 0)
26ccd9
+			mode = CXL_DECODER_MODE_PMEM;
26ccd9
+		else {
26ccd9
+			log_err(&ml, "%s: unsupported type: %s\n", devname,
26ccd9
+				param.type);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+	} else
26ccd9
+		mode = CXL_DECODER_MODE_RAM;
26ccd9
+
26ccd9
+	if (!endpoint) {
26ccd9
+		log_err(&ml, "%s: CXL operation disabled\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	port = cxl_endpoint_get_port(endpoint);
26ccd9
+
26ccd9
+	if (mode == CXL_DECODER_MODE_RAM)
26ccd9
+		avail_dpa = cxl_memdev_get_ram_size(memdev);
26ccd9
+	else
26ccd9
+		avail_dpa = cxl_memdev_get_pmem_size(memdev);
26ccd9
+
26ccd9
+	cxl_decoder_foreach(port, decoder) {
26ccd9
+		size = cxl_decoder_get_dpa_size(decoder);
26ccd9
+		if (size == ULLONG_MAX)
26ccd9
+			continue;
26ccd9
+		if (cxl_decoder_get_mode(decoder) != mode)
26ccd9
+			continue;
26ccd9
+
26ccd9
+		if (size > avail_dpa) {
26ccd9
+			log_err(&ml, "%s: capacity accounting error\n",
26ccd9
+				devname);
26ccd9
+			return -ENXIO;
26ccd9
+		}
26ccd9
+		avail_dpa -= size;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!param.size)
26ccd9
+		if (alloc_mode == DPA_ALLOC) {
26ccd9
+			size = avail_dpa;
26ccd9
+			if (!avail_dpa) {
26ccd9
+				log_err(&ml, "%s: no available capacity\n",
26ccd9
+					devname);
26ccd9
+				return -ENOSPC;
26ccd9
+			}
26ccd9
+		} else
26ccd9
+			size = 0;
26ccd9
+	else {
26ccd9
+		size = parse_size64(param.size);
26ccd9
+		if (size == ULLONG_MAX) {
26ccd9
+			log_err(&ml, "%s: failed to parse size option '%s'\n",
26ccd9
+				devname, param.size);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+		if (size > avail_dpa) {
26ccd9
+			log_err(&ml, "%s: '%s' exceeds available capacity\n",
26ccd9
+				devname, param.size);
26ccd9
+			if (!param.force)
26ccd9
+				return -ENOSPC;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	/*
26ccd9
+	 * Find next free decoder, assumes cxl_decoder_foreach() is in
26ccd9
+	 * hardware instance-id order
26ccd9
+	 */
26ccd9
+	if (alloc_mode == DPA_ALLOC)
26ccd9
+		cxl_decoder_foreach(port, decoder) {
26ccd9
+			/* first 0-dpa_size is our target */
26ccd9
+			if (cxl_decoder_get_dpa_size(decoder) == 0) {
26ccd9
+				auto_target = decoder;
26ccd9
+				break;
26ccd9
+			}
26ccd9
+		}
26ccd9
+	else
26ccd9
+		cxl_decoder_foreach_reverse(port, decoder) {
26ccd9
+			/* nothing to free? */
26ccd9
+			if (!cxl_decoder_get_dpa_size(decoder))
26ccd9
+				continue;
26ccd9
+			/*
26ccd9
+			 * Active decoders can't be freed, and by definition all
26ccd9
+			 * previous decoders must also be active
26ccd9
+			 */
26ccd9
+			if (cxl_decoder_get_size(decoder))
26ccd9
+				break;
26ccd9
+			/* first dpa_size > 0 + disabled decoder is our target */
26ccd9
+			if (cxl_decoder_get_dpa_size(decoder) < ULLONG_MAX) {
26ccd9
+				auto_target = decoder;
26ccd9
+				break;
26ccd9
+			}
26ccd9
+		}
26ccd9
+
26ccd9
+	if (param.decoder_filter) {
26ccd9
+		unsigned long id;
26ccd9
+		char *end;
26ccd9
+
26ccd9
+		id = strtoul(param.decoder_filter, &end, 0);
26ccd9
+		/* allow for standalone ordinal decoder ids */
26ccd9
+		if (*end == '\0')
26ccd9
+			rc = snprintf(buf, sizeof(buf), "decoder%d.%ld",
26ccd9
+				      cxl_port_get_id(port), id);
26ccd9
+		else
26ccd9
+			rc = snprintf(buf, sizeof(buf), "%s",
26ccd9
+				      param.decoder_filter);
26ccd9
+
26ccd9
+		if (rc >= (int)sizeof(buf)) {
26ccd9
+			log_err(&ml, "%s: decoder filter '%s' too long\n",
26ccd9
+				devname, param.decoder_filter);
26ccd9
+			return -EINVAL;
26ccd9
+		}
26ccd9
+
26ccd9
+		if (alloc_mode == DPA_ALLOC)
26ccd9
+			cxl_decoder_foreach(port, decoder) {
26ccd9
+				target = util_cxl_decoder_filter(decoder, buf);
26ccd9
+				if (target)
26ccd9
+					break;
26ccd9
+			}
26ccd9
+		else
26ccd9
+			cxl_decoder_foreach_reverse(port, decoder) {
26ccd9
+				target = util_cxl_decoder_filter(decoder, buf);
26ccd9
+				if (target)
26ccd9
+					break;
26ccd9
+			}
26ccd9
+
26ccd9
+		if (!target) {
26ccd9
+			log_err(&ml, "%s: no match for decoder: '%s'\n",
26ccd9
+				devname, param.decoder_filter);
26ccd9
+			return -ENXIO;
26ccd9
+		}
26ccd9
+
26ccd9
+		if (target != auto_target) {
26ccd9
+			log_err(&ml, "%s: %s is out of sequence\n", devname,
26ccd9
+				cxl_decoder_get_devname(target));
26ccd9
+			if (!param.force)
26ccd9
+				return -EINVAL;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (!target)
26ccd9
+		target = auto_target;
26ccd9
+
26ccd9
+	if (!target) {
26ccd9
+		log_err(&ml, "%s: no suitable decoder found\n", devname);
26ccd9
+		return -ENXIO;
26ccd9
+	}
26ccd9
+
26ccd9
+	if (cxl_decoder_get_mode(target) != mode) {
26ccd9
+		rc = cxl_decoder_set_dpa_size(target, 0);
26ccd9
+		if (rc) {
26ccd9
+			log_err(&ml,
26ccd9
+				"%s: %s: failed to clear allocation to set mode\n",
26ccd9
+				devname, cxl_decoder_get_devname(target));
26ccd9
+			return rc;
26ccd9
+		}
26ccd9
+		rc = cxl_decoder_set_mode(target, mode);
26ccd9
+		if (rc) {
26ccd9
+			log_err(&ml, "%s: %s: failed to set %s mode\n", devname,
26ccd9
+				cxl_decoder_get_devname(target),
26ccd9
+				mode == CXL_DECODER_MODE_PMEM ? "pmem" : "ram");
26ccd9
+			return rc;
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	rc = cxl_decoder_set_dpa_size(target, size);
26ccd9
+	if (rc)
26ccd9
+		log_err(&ml, "%s: %s: failed to set dpa allocation\n", devname,
26ccd9
+			cxl_decoder_get_devname(target));
26ccd9
+	else {
26ccd9
+		struct json_object *jdev, *jdecoder;
26ccd9
+		unsigned long flags = 0;
26ccd9
+
26ccd9
+		if (actx->f_out == stdout && isatty(1))
26ccd9
+			flags |= UTIL_JSON_HUMAN;
26ccd9
+		jdev = util_cxl_memdev_to_json(memdev, flags);
26ccd9
+		jdecoder = util_cxl_decoder_to_json(target, flags);
26ccd9
+		if (!jdev || !jdecoder) {
26ccd9
+			json_object_put(jdev);
26ccd9
+			json_object_put(jdecoder);
26ccd9
+		} else {
26ccd9
+			json_object_object_add(jdev, "decoder", jdecoder);
26ccd9
+			json_object_array_add(actx->jdevs, jdev);
26ccd9
+		}
26ccd9
+	}
26ccd9
+	return rc;
26ccd9
+}
26ccd9
+
26ccd9
+static int action_reserve_dpa(struct cxl_memdev *memdev,
26ccd9
+			      struct action_context *actx)
26ccd9
+{
26ccd9
+	return __reserve_dpa(memdev, DPA_ALLOC, actx);
26ccd9
+}
26ccd9
+
26ccd9
+static int action_free_dpa(struct cxl_memdev *memdev,
26ccd9
+			   struct action_context *actx)
26ccd9
+{
26ccd9
+	return __reserve_dpa(memdev, DPA_FREE, actx);
26ccd9
+}
26ccd9
+
26ccd9
 static int action_disable(struct cxl_memdev *memdev, struct action_context *actx)
26ccd9
 {
26ccd9
 	if (!cxl_memdev_is_enabled(memdev))
26ccd9
@@ -452,7 +702,8 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
 		err++;
26ccd9
 	}
26ccd9
 
26ccd9
-	if (action == action_setpartition)
26ccd9
+	if (action == action_setpartition || action == action_reserve_dpa ||
26ccd9
+	    action == action_free_dpa)
26ccd9
 		actx.jdevs = json_object_new_array();
26ccd9
 
26ccd9
 	if (err == argc) {
26ccd9
@@ -495,6 +746,8 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
 	count = 0;
26ccd9
 
26ccd9
 	for (i = 0; i < argc; i++) {
26ccd9
+		bool found = false;
26ccd9
+
26ccd9
 		cxl_memdev_foreach(ctx, memdev) {
26ccd9
 			const char *memdev_filter = NULL;
26ccd9
 			const char *serial_filter = NULL;
26ccd9
@@ -507,6 +760,7 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
 			if (!util_cxl_memdev_filter(memdev, memdev_filter,
26ccd9
 						    serial_filter))
26ccd9
 				continue;
26ccd9
+			found = true;
26ccd9
 
26ccd9
 			if (action == action_write) {
26ccd9
 				single = memdev;
26ccd9
@@ -519,6 +773,8 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
26ccd9
 			else if (rc && !err)
26ccd9
 				err = rc;
26ccd9
 		}
26ccd9
+		if (!found)
26ccd9
+			log_info(&ml, "no memdev matches %s\n", argv[i]);
26ccd9
 	}
26ccd9
 	rc = err;
26ccd9
 
26ccd9
@@ -622,3 +878,25 @@ int cmd_set_partition(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
 
26ccd9
 	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
 }
26ccd9
+
26ccd9
+int cmd_reserve_dpa(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(
26ccd9
+		argc, argv, ctx, action_reserve_dpa, reserve_dpa_options,
26ccd9
+		"cxl reserve-dpa <mem0> [<mem1>..<memn>] [<options>]");
26ccd9
+	log_info(&ml, "reservation completed on %d mem device%s\n",
26ccd9
+		 count >= 0 ? count : 0, count > 1 ? "s" : "");
26ccd9
+
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
+
26ccd9
+int cmd_free_dpa(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
+{
26ccd9
+	int count = memdev_action(
26ccd9
+		argc, argv, ctx, action_free_dpa, free_dpa_options,
26ccd9
+		"cxl free-dpa <mem0> [<mem1>..<memn>] [<options>]");
26ccd9
+	log_info(&ml, "reservation release completed on %d mem device%s\n",
26ccd9
+		 count >= 0 ? count : 0, count > 1 ? "s" : "");
26ccd9
+
26ccd9
+	return count >= 0 ? 0 : EXIT_FAILURE;
26ccd9
+}
26ccd9
-- 
26ccd9
2.27.0
26ccd9