Blame SOURCES/0116-cxl-list-Add-decoder-support.patch

26ccd9
From 46564977afb733a92526c688fe106e19b139ddfc Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Sun, 23 Jan 2022 16:54:27 -0800
26ccd9
Subject: [PATCH 116/217] cxl/list: Add decoder support
26ccd9
26ccd9
Decoder objects exist at each level of a CXL port topology and map a
26ccd9
physical address range a set of interleaved ports at each level of the
26ccd9
hierarchy. Typically end users mostly care about the root-level decoders
26ccd9
which enumerate the potential CXL address space in the system, and the
26ccd9
endpoint decoders that indicate which address ranges a given device
26ccd9
contributes resources. Intermediate switch-level decoders are typically
26ccd9
only useful for debugging decode problems.
26ccd9
26ccd9
$ cxl list -D -d 3.1 -u
26ccd9
{
26ccd9
  "decoder":"decoder3.1",
26ccd9
  "resource":"0x8030000000",
26ccd9
  "size":"512.00 MiB (536.87 MB)",
26ccd9
  "volatile_capable":true
26ccd9
}
26ccd9
26ccd9
Link: https://lore.kernel.org/r/164298566760.3021641.3999006903066004615.stgit@dwillia2-desk3.amr.corp.intel.com
26ccd9
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 .clang-format                    |   1 +
26ccd9
 Documentation/cxl/cxl-list.txt   |  18 ++-
26ccd9
 Documentation/cxl/lib/libcxl.txt |  70 ++++++++-
26ccd9
 cxl/filter.c                     | 194 +++++++++++++++++++++++--
26ccd9
 cxl/filter.h                     |   2 +
26ccd9
 cxl/json.c                       |  62 ++++++++
26ccd9
 cxl/json.h                       |   2 +
26ccd9
 cxl/lib/libcxl.c                 | 239 ++++++++++++++++++++++++++++---
26ccd9
 cxl/lib/libcxl.sym               |  14 ++
26ccd9
 cxl/lib/private.h                |  31 +++-
26ccd9
 cxl/libcxl.h                     |  28 ++++
26ccd9
 cxl/list.c                       |   9 +-
26ccd9
 util/json.h                      |   1 +
26ccd9
 13 files changed, 643 insertions(+), 28 deletions(-)
26ccd9
26ccd9
diff --git a/.clang-format b/.clang-format
26ccd9
index 106bc5e..16e28ac 100644
26ccd9
--- a/.clang-format
26ccd9
+++ b/.clang-format
26ccd9
@@ -80,6 +80,7 @@ ForEachMacros:
26ccd9
   - 'cxl_memdev_foreach'
26ccd9
   - 'cxl_bus_foreach'
26ccd9
   - 'cxl_port_foreach'
26ccd9
+  - 'cxl_decoder_foreach'
26ccd9
   - 'cxl_endpoint_foreach'
26ccd9
   - 'daxctl_dev_foreach'
26ccd9
   - 'daxctl_mapping_foreach'
26ccd9
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
26ccd9
index bac27c7..84872b9 100644
26ccd9
--- a/Documentation/cxl/cxl-list.txt
26ccd9
+++ b/Documentation/cxl/cxl-list.txt
26ccd9
@@ -5,7 +5,7 @@ cxl-list(1)
26ccd9
 
26ccd9
 NAME
26ccd9
 ----
26ccd9
-cxl-list - List CXL capable memory devices, and their attributes in json.
26ccd9
+cxl-list - List platform CXL objects, and their attributes, in json.
26ccd9
 
26ccd9
 SYNOPSIS
26ccd9
 --------
26ccd9
@@ -29,6 +29,10 @@ The potential top-level array names and their nesting properties are:
26ccd9
 "endpoints":: nest under ports or buses (if ports are not emitted)
26ccd9
 "memdevs":: nest under endpoints or ports (if endpoints are not
26ccd9
    emitted) or buses (if endpoints and ports are not emitted)
26ccd9
+"root decoders":: nest under buses
26ccd9
+"port decoders":: nest under ports, or buses (if ports are not emitted)
26ccd9
+"endpoint decoders":: nest under endpoints, or ports (if endpoints are
26ccd9
+   not emitted) or buses (if endpoints and ports are not emitted)
26ccd9
 
26ccd9
 Filters can by specifed as either a single identidier, a space separated
26ccd9
 quoted string, or a comma separated list. When multiple filter
26ccd9
@@ -254,6 +258,18 @@ OPTIONS
26ccd9
 	Specify CXL endpoint device name(s), or device id(s) to filter
26ccd9
 	the emitted endpoint(s).
26ccd9
 
26ccd9
+-D::
26ccd9
+--decoders::
26ccd9
+	Include decoder objects (CXL Memory decode capability instances
26ccd9
+	in buses, ports, and endpoints) in the listing.
26ccd9
+
26ccd9
+-d::
26ccd9
+--decoder::
26ccd9
+	Specify CXL decoder device name(s), device id(s), or decoder type names
26ccd9
+	to filter the emitted decoder(s). The format for a decoder name is
26ccd9
+	"decoder<port_id>.<instance_id>". The possible decoder type names are
26ccd9
+	"root", "switch", or "endpoint", similar to the port filter syntax.
26ccd9
+
26ccd9
 --debug::
26ccd9
 	If the cxl tool was built with debug enabled, turn on debug
26ccd9
 	messages.
26ccd9
diff --git a/Documentation/cxl/lib/libcxl.txt b/Documentation/cxl/lib/libcxl.txt
26ccd9
index 49edb71..73af3d0 100644
26ccd9
--- a/Documentation/cxl/lib/libcxl.txt
26ccd9
+++ b/Documentation/cxl/lib/libcxl.txt
26ccd9
@@ -179,6 +179,7 @@ struct cxl_bus *cxl_bus_get_first(struct cxl_ctx *ctx);
26ccd9
 struct cxl_bus *cxl_bus_get_next(struct cxl_bus *bus);
26ccd9
 struct cxl_ctx *cxl_bus_get_ctx(struct cxl_bus *bus);
26ccd9
 struct cxl_bus *cxl_memdev_get_bus(struct cxl_memdev *memdev);
26ccd9
+struct cxl_bus *cxl_port_get_bus(struct cxl_port *port);
26ccd9
 struct cxl_bus *cxl_endpoint_get_bus(struct cxl_endpoint *endpoint);
26ccd9
 
26ccd9
 #define cxl_bus_foreach(ctx, bus)                                           \
26ccd9
@@ -215,9 +216,9 @@ struct cxl_port *cxl_bus_get_port(struct cxl_bus *bus);
26ccd9
 struct cxl_port *cxl_port_get_first(struct cxl_port *parent);
26ccd9
 struct cxl_port *cxl_port_get_next(struct cxl_port *port);
26ccd9
 struct cxl_port *cxl_port_get_parent(struct cxl_port *port);
26ccd9
-struct cxl_bus *cxl_port_get_bus(struct cxl_port *port);
26ccd9
 struct cxl_ctx *cxl_port_get_ctx(struct cxl_port *port);
26ccd9
 const char *cxl_port_get_host(struct cxl_port *port);
26ccd9
+struct cxl_port *cxl_decoder_get_port(struct cxl_decoder *decoder);
26ccd9
 
26ccd9
 #define cxl_port_foreach(parent, port)                                      \
26ccd9
        for (port = cxl_port_get_first(parent); port != NULL;                \
26ccd9
@@ -284,6 +285,73 @@ int cxl_endpoint_get_id(struct cxl_endpoint *endpoint);
26ccd9
 int cxl_endpoint_is_enabled(struct cxl_endpoint *endpoint);
26ccd9
 ----
26ccd9
 
26ccd9
+DECODERS
26ccd9
+--------
26ccd9
+Decoder objects are associated with the "HDM Decoder Capability"
26ccd9
+published in Port devices and CXL capable PCIe endpoints. The kernel
26ccd9
+additionally models platform firmware described CXL memory ranges (like
26ccd9
+the ACPI CEDT.CFMWS) as static decoder objects. They route System
26ccd9
+Physical Addresses through a port topology to an endpoint decoder that
26ccd9
+does the final translation from SPA to DPA (system-physical-address to
26ccd9
+device-local-physical-address).
26ccd9
+
26ccd9
+=== DECODER: Enumeration
26ccd9
+----
26ccd9
+struct cxl_decoder *cxl_decoder_get_first(struct cxl_port *port);
26ccd9
+struct cxl_decoder *cxl_decoder_get_next(struct cxl_decoder *decoder);
26ccd9
+struct cxl_ctx *cxl_decoder_get_ctx(struct cxl_decoder *decoder);
26ccd9
+
26ccd9
+#define cxl_decoder_foreach(port, decoder)                                  \
26ccd9
+       for (decoder = cxl_decoder_get_first(port); decoder != NULL;         \
26ccd9
+            decoder = cxl_decoder_get_next(decoder))
26ccd9
+----
26ccd9
+The definition of a CXL port in libcxl is an object that hosts one or
26ccd9
+more CXL decoder objects.
26ccd9
+
26ccd9
+=== DECODER: Attributes
26ccd9
+----
26ccd9
+unsigned long long cxl_decoder_get_resource(struct cxl_decoder *decoder);
26ccd9
+unsigned long long cxl_decoder_get_size(struct cxl_decoder *decoder);
26ccd9
+const char *cxl_decoder_get_devname(struct cxl_decoder *decoder);
26ccd9
+int cxl_decoder_get_id(struct cxl_decoder *decoder);
26ccd9
+
26ccd9
+enum cxl_decoder_target_type {
26ccd9
+       CXL_DECODER_TTYPE_UNKNOWN,
26ccd9
+       CXL_DECODER_TTYPE_EXPANDER,
26ccd9
+       CXL_DECODER_TTYPE_ACCELERATOR,
26ccd9
+};
26ccd9
+
26ccd9
+cxl_decoder_get_target_type(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_pmem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_mem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_accelmem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_locked(struct cxl_decoder *decoder);
26ccd9
+----
26ccd9
+The kernel protects the enumeration of the physical address layout of
26ccd9
+the system. Without CAP_SYS_ADMIN cxl_decoder_get_resource() returns
26ccd9
+ULLONG_MAX to indicate that the address information was not retrievable.
26ccd9
+Otherwise, cxl_decoder_get_resource() returns the currently programmed
26ccd9
+value of the base of the decoder's decode range. A zero-sized decoder
26ccd9
+indicates a disabled decoder.
26ccd9
+
26ccd9
+Root level decoders only support limited set of memory types in their
26ccd9
+address range. The cxl_decoder_is_<memtype>_capable() helpers identify
26ccd9
+what is supported. Switch level decoders, in contrast are capable of
26ccd9
+routing any memory type, i.e. they just forward along the memory type
26ccd9
+support from their parent port. Endpoint decoders follow the
26ccd9
+capabilities of their host memory device.
26ccd9
+
26ccd9
+The capabilities of a decoder are not to be confused with their type /
26ccd9
+mode.  The type ultimately depends on the endpoint. For example an
26ccd9
+accelerator requires all decoders in its ancestry to be set to
26ccd9
+CXL_DECODER_TTYPE_ACCELERATOR, and conversely plain memory expander
26ccd9
+devices require CXL_DECODER_TTYPE_EXPANDER.
26ccd9
+
26ccd9
+Platform firmware may setup the CXL decode hierarchy before the OS
26ccd9
+boots, and may additionally require that the OS not change the decode
26ccd9
+settings. This property is indicated by the cxl_decoder_is_locked() API.
26ccd9
+
26ccd9
 include::../../copyright.txt[]
26ccd9
 
26ccd9
 SEE ALSO
26ccd9
diff --git a/cxl/filter.c b/cxl/filter.c
26ccd9
index 6dc61a1..dc052f6 100644
26ccd9
--- a/cxl/filter.c
26ccd9
+++ b/cxl/filter.c
26ccd9
@@ -171,6 +171,17 @@ util_cxl_endpoint_filter_by_port(struct cxl_endpoint *endpoint,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static struct cxl_decoder *
26ccd9
+util_cxl_decoder_filter_by_port(struct cxl_decoder *decoder, const char *ident,
26ccd9
+				enum cxl_port_filter_mode mode)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+
26ccd9
+	if (util_cxl_port_filter(port, ident, mode))
26ccd9
+		return decoder;
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
 static struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus,
26ccd9
 					   const char *__ident)
26ccd9
 {
26ccd9
@@ -233,6 +244,16 @@ static struct cxl_port *util_cxl_port_filter_by_bus(struct cxl_port *port,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static struct cxl_decoder *
26ccd9
+util_cxl_decoder_filter_by_bus(struct cxl_decoder *decoder, const char *__ident)
26ccd9
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+
26ccd9
+	if (!util_cxl_port_filter_by_bus(port, __ident))
26ccd9
+		return NULL;
26ccd9
+	return decoder;
26ccd9
+}
26ccd9
+
26ccd9
 static struct cxl_memdev *
26ccd9
 util_cxl_memdev_serial_filter(struct cxl_memdev *memdev, const char *__serials)
26ccd9
 {
26ccd9
@@ -357,6 +378,49 @@ static 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
+{
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	int pid, did;
26ccd9
+	char *ident, *save;
26ccd9
+	const char *name;
26ccd9
+
26ccd9
+	if (!__ident)
26ccd9
+		return decoder;
26ccd9
+
26ccd9
+	ident = strdup(__ident);
26ccd9
+	if (!ident)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	for (name = strtok_r(ident, which_sep(__ident), &save); name;
26ccd9
+	     name = strtok_r(NULL, which_sep(__ident), &save)) {
26ccd9
+		if (strcmp(name, "all") == 0)
26ccd9
+			break;
26ccd9
+
26ccd9
+		if (strcmp(name, "root") == 0 && cxl_port_is_root(port))
26ccd9
+			break;
26ccd9
+		if (strcmp(name, "switch") == 0 && cxl_port_is_switch(port))
26ccd9
+			break;
26ccd9
+		if (strcmp(name, "endpoint") == 0 && cxl_port_is_endpoint(port))
26ccd9
+			break;
26ccd9
+
26ccd9
+		if ((sscanf(name, "%d.%d", &pid, &did) == 2 ||
26ccd9
+		     sscanf(name, "decoder%d.%d", &pid, &did) == 2) &&
26ccd9
+		    cxl_port_get_id(port) == pid &&
26ccd9
+		    cxl_decoder_get_id(decoder) == did)
26ccd9
+			break;
26ccd9
+
26ccd9
+		if (strcmp(name, cxl_decoder_get_devname(decoder)) == 0)
26ccd9
+			break;
26ccd9
+	}
26ccd9
+
26ccd9
+	free(ident);
26ccd9
+	if (name)
26ccd9
+		return decoder;
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
 static unsigned long params_to_flags(struct cxl_filter_params *param)
26ccd9
 {
26ccd9
 	unsigned long flags = 0;
26ccd9
@@ -440,15 +504,44 @@ static struct json_object *pick_array(struct json_object *child,
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static void walk_decoders(struct cxl_port *port, struct cxl_filter_params *p,
26ccd9
+			  struct json_object *jdecoders, unsigned long flags)
26ccd9
+{
26ccd9
+	struct cxl_decoder *decoder;
26ccd9
+
26ccd9
+	cxl_decoder_foreach(port, decoder) {
26ccd9
+		struct json_object *jdecoder;
26ccd9
+
26ccd9
+		if (!p->decoders)
26ccd9
+			continue;
26ccd9
+		if (!util_cxl_decoder_filter(decoder, p->decoder_filter))
26ccd9
+			continue;
26ccd9
+		if (!util_cxl_decoder_filter_by_bus(decoder, p->bus_filter))
26ccd9
+			continue;
26ccd9
+		if (!util_cxl_decoder_filter_by_port(decoder, p->port_filter,
26ccd9
+						     pf_mode(p)))
26ccd9
+			continue;
26ccd9
+		if (!p->idle && cxl_decoder_get_size(decoder) == 0)
26ccd9
+			continue;
26ccd9
+		jdecoder = util_cxl_decoder_to_json(decoder, flags);
26ccd9
+		if (!decoder) {
26ccd9
+			dbg(p, "decoder object allocation failure\n");
26ccd9
+			continue;
26ccd9
+		}
26ccd9
+		json_object_array_add(jdecoders, jdecoder);
26ccd9
+	}
26ccd9
+}
26ccd9
+
26ccd9
 static void walk_endpoints(struct cxl_port *port, struct cxl_filter_params *p,
26ccd9
 			   struct json_object *jeps, struct json_object *jdevs,
26ccd9
-			   unsigned long flags)
26ccd9
+			   struct json_object *jdecoders, unsigned long flags)
26ccd9
 {
26ccd9
 	struct cxl_endpoint *endpoint;
26ccd9
 
26ccd9
 	cxl_endpoint_foreach(port, endpoint) {
26ccd9
 		struct cxl_port *ep_port = cxl_endpoint_get_port(endpoint);
26ccd9
 		const char *devname = cxl_endpoint_get_devname(endpoint);
26ccd9
+		struct json_object *jchilddecoders = NULL;
26ccd9
 		struct json_object *jendpoint = NULL;
26ccd9
 		struct cxl_memdev *memdev;
26ccd9
 
26ccd9
@@ -495,14 +588,31 @@ static void walk_endpoints(struct cxl_port *port, struct cxl_filter_params *p,
26ccd9
 			else
26ccd9
 				json_object_array_add(jdevs, jobj);
26ccd9
 		}
26ccd9
+
26ccd9
+		if (p->decoders && p->endpoints) {
26ccd9
+			jchilddecoders = json_object_new_array();
26ccd9
+			if (!jchilddecoders) {
26ccd9
+				err(p,
26ccd9
+				    "%s: failed to enumerate child decoders\n",
26ccd9
+				    devname);
26ccd9
+				continue;
26ccd9
+			}
26ccd9
+		}
26ccd9
+
26ccd9
+		if (!p->decoders)
26ccd9
+			continue;
26ccd9
+		walk_decoders(cxl_endpoint_get_port(endpoint), p,
26ccd9
+			      pick_array(jchilddecoders, jdecoders), flags);
26ccd9
+		cond_add_put_array_suffix(jendpoint, "decoders", devname,
26ccd9
+					  jchilddecoders);
26ccd9
 	}
26ccd9
 }
26ccd9
 
26ccd9
-static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
-			     struct cxl_filter_params *p,
26ccd9
-			     struct json_object *jports,
26ccd9
-			     struct json_object *jeps,
26ccd9
-			     struct json_object *jdevs, unsigned long flags)
26ccd9
+static void
26ccd9
+walk_child_ports(struct cxl_port *parent_port, struct cxl_filter_params *p,
26ccd9
+		 struct json_object *jports, struct json_object *jportdecoders,
26ccd9
+		 struct json_object *jeps, struct json_object *jepdecoders,
26ccd9
+		 struct json_object *jdevs, unsigned long flags)
26ccd9
 {
26ccd9
 	struct cxl_port *port;
26ccd9
 
26ccd9
@@ -512,6 +622,7 @@ static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
 		struct json_object *jchilddevs = NULL;
26ccd9
 		struct json_object *jchildports = NULL;
26ccd9
 		struct json_object *jchildeps = NULL;
26ccd9
+		struct json_object *jchilddecoders = NULL;
26ccd9
 
26ccd9
 		if (!util_cxl_port_filter_by_memdev(port, p->memdev_filter,
26ccd9
 						    p->serial_filter))
26ccd9
@@ -555,19 +666,37 @@ static void walk_child_ports(struct cxl_port *parent_port,
26ccd9
 					continue;
26ccd9
 				}
26ccd9
 			}
26ccd9
+
26ccd9
+			if (p->decoders) {
26ccd9
+				jchilddecoders = json_object_new_array();
26ccd9
+				if (!jchilddecoders) {
26ccd9
+					err(p,
26ccd9
+					    "%s: failed to enumerate child decoders\n",
26ccd9
+					    devname);
26ccd9
+					continue;
26ccd9
+				}
26ccd9
+			}
26ccd9
 		}
26ccd9
 
26ccd9
 walk_children:
26ccd9
-		if (p->endpoints || p->memdevs)
26ccd9
+		if (p->endpoints || p->memdevs || p->decoders)
26ccd9
 			walk_endpoints(port, p, pick_array(jchildeps, jeps),
26ccd9
-				       pick_array(jchilddevs, jdevs), flags);
26ccd9
+				       pick_array(jchilddevs, jdevs),
26ccd9
+				       pick_array(jchilddecoders, jepdecoders),
26ccd9
+				       flags);
26ccd9
 
26ccd9
+		walk_decoders(port, p,
26ccd9
+			      pick_array(jchilddecoders, jportdecoders), flags);
26ccd9
 		walk_child_ports(port, p, pick_array(jchildports, jports),
26ccd9
+				 pick_array(jchilddecoders, jportdecoders),
26ccd9
 				 pick_array(jchildeps, jeps),
26ccd9
+				 pick_array(jchilddecoders, jepdecoders),
26ccd9
 				 pick_array(jchilddevs, jdevs), flags);
26ccd9
 		cond_add_put_array_suffix(jport, "ports", devname, jchildports);
26ccd9
 		cond_add_put_array_suffix(jport, "endpoints", devname,
26ccd9
 					  jchildeps);
26ccd9
+		cond_add_put_array_suffix(jport, "decoders", devname,
26ccd9
+					  jchilddecoders);
26ccd9
 		cond_add_put_array_suffix(jport, "memdevs", devname,
26ccd9
 					  jchilddevs);
26ccd9
 	}
26ccd9
@@ -578,6 +707,9 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	struct json_object *jdevs = NULL, *jbuses = NULL, *jports = NULL;
26ccd9
 	struct json_object *jplatform = json_object_new_array();
26ccd9
 	unsigned long flags = params_to_flags(p);
26ccd9
+	struct json_object *jportdecoders = NULL;
26ccd9
+	struct json_object *jbusdecoders = NULL;
26ccd9
+	struct json_object *jepdecoders = NULL;
26ccd9
 	struct json_object *janondevs = NULL;
26ccd9
 	struct json_object *jeps = NULL;
26ccd9
 	struct cxl_memdev *memdev;
26ccd9
@@ -609,6 +741,18 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	if (!jdevs)
26ccd9
 		goto err;
26ccd9
 
26ccd9
+	jbusdecoders = json_object_new_array();
26ccd9
+	if (!jbusdecoders)
26ccd9
+		goto err;
26ccd9
+
26ccd9
+	jportdecoders = json_object_new_array();
26ccd9
+	if (!jportdecoders)
26ccd9
+		goto err;
26ccd9
+
26ccd9
+	jepdecoders = json_object_new_array();
26ccd9
+	if (!jepdecoders)
26ccd9
+		goto err;
26ccd9
+
26ccd9
 	dbg(p, "walk memdevs\n");
26ccd9
 	cxl_memdev_foreach(ctx, memdev) {
26ccd9
 		struct json_object *janondev;
26ccd9
@@ -633,6 +777,7 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 	dbg(p, "walk buses\n");
26ccd9
 	cxl_bus_foreach(ctx, bus) {
26ccd9
 		struct json_object *jbus = NULL;
26ccd9
+		struct json_object *jchilddecoders = NULL;
26ccd9
 		struct json_object *jchildports = NULL;
26ccd9
 		struct json_object *jchilddevs = NULL;
26ccd9
 		struct json_object *jchildeps = NULL;
26ccd9
@@ -681,15 +826,33 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
26ccd9
 					continue;
26ccd9
 				}
26ccd9
 			}
26ccd9
+			if (p->decoders) {
26ccd9
+				jchilddecoders = json_object_new_array();
26ccd9
+				if (!jchilddecoders) {
26ccd9
+					err(p,
26ccd9
+					    "%s: failed to enumerate child decoders\n",
26ccd9
+					    devname);
26ccd9
+					continue;
26ccd9
+				}
26ccd9
+			}
26ccd9
+
26ccd9
 		}
26ccd9
 walk_children:
26ccd9
+		dbg(p, "walk decoders\n");
26ccd9
+		walk_decoders(port, p, pick_array(jchilddecoders, jbusdecoders),
26ccd9
+			      flags);
26ccd9
+
26ccd9
 		dbg(p, "walk ports\n");
26ccd9
 		walk_child_ports(port, p, pick_array(jchildports, jports),
26ccd9
+				 pick_array(jchilddecoders, jportdecoders),
26ccd9
 				 pick_array(jchildeps, jeps),
26ccd9
+				 pick_array(jchilddecoders, jepdecoders),
26ccd9
 				 pick_array(jchilddevs, jdevs), flags);
26ccd9
 		cond_add_put_array_suffix(jbus, "ports", devname, jchildports);
26ccd9
 		cond_add_put_array_suffix(jbus, "endpoints", devname,
26ccd9
 					  jchildeps);
26ccd9
+		cond_add_put_array_suffix(jbus, "decoders", devname,
26ccd9
+					  jchilddecoders);
26ccd9
 		cond_add_put_array_suffix(jbus, "memdevs", devname, jchilddevs);
26ccd9
 	}
26ccd9
 
26ccd9
@@ -703,12 +866,24 @@ walk_children:
26ccd9
 		top_level_objs++;
26ccd9
 	if (json_object_array_length(jdevs))
26ccd9
 		top_level_objs++;
26ccd9
+	if (json_object_array_length(jbusdecoders))
26ccd9
+		top_level_objs++;
26ccd9
+	if (json_object_array_length(jportdecoders))
26ccd9
+		top_level_objs++;
26ccd9
+	if (json_object_array_length(jepdecoders))
26ccd9
+		top_level_objs++;
26ccd9
 
26ccd9
 	splice_array(p, janondevs, jplatform, "anon memdevs", top_level_objs > 1);
26ccd9
 	splice_array(p, jbuses, jplatform, "buses", top_level_objs > 1);
26ccd9
 	splice_array(p, jports, jplatform, "ports", top_level_objs > 1);
26ccd9
 	splice_array(p, jeps, jplatform, "endpoints", top_level_objs > 1);
26ccd9
 	splice_array(p, jdevs, jplatform, "memdevs", top_level_objs > 1);
26ccd9
+	splice_array(p, jbusdecoders, jplatform, "root decoders",
26ccd9
+		     top_level_objs > 1);
26ccd9
+	splice_array(p, jportdecoders, jplatform, "port decoders",
26ccd9
+		     top_level_objs > 1);
26ccd9
+	splice_array(p, jepdecoders, jplatform, "endpoint decoders",
26ccd9
+		     top_level_objs > 1);
26ccd9
 
26ccd9
 	util_display_json_array(stdout, jplatform, flags);
26ccd9
 
26ccd9
@@ -719,6 +894,9 @@ err:
26ccd9
 	json_object_put(jports);
26ccd9
 	json_object_put(jeps);
26ccd9
 	json_object_put(jdevs);
26ccd9
+	json_object_put(jbusdecoders);
26ccd9
+	json_object_put(jportdecoders);
26ccd9
+	json_object_put(jepdecoders);
26ccd9
 	json_object_put(jplatform);
26ccd9
 	return -ENOMEM;
26ccd9
 }
26ccd9
diff --git a/cxl/filter.h b/cxl/filter.h
26ccd9
index bbd341c..5d7bf45 100644
26ccd9
--- a/cxl/filter.h
26ccd9
+++ b/cxl/filter.h
26ccd9
@@ -12,8 +12,10 @@ struct cxl_filter_params {
26ccd9
 	const char *bus_filter;
26ccd9
 	const char *port_filter;
26ccd9
 	const char *endpoint_filter;
26ccd9
+	const char *decoder_filter;
26ccd9
 	bool single;
26ccd9
 	bool endpoints;
26ccd9
+	bool decoders;
26ccd9
 	bool memdevs;
26ccd9
 	bool ports;
26ccd9
 	bool buses;
26ccd9
diff --git a/cxl/json.c b/cxl/json.c
26ccd9
index 51918d6..548bc52 100644
26ccd9
--- a/cxl/json.c
26ccd9
+++ b/cxl/json.c
26ccd9
@@ -262,6 +262,68 @@ struct json_object *util_cxl_bus_to_json(struct cxl_bus *bus,
26ccd9
 	return jbus;
26ccd9
 }
26ccd9
 
26ccd9
+struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
+					     unsigned long flags)
26ccd9
+{
26ccd9
+	const char *devname = cxl_decoder_get_devname(decoder);
26ccd9
+	struct cxl_port *port = cxl_decoder_get_port(decoder);
26ccd9
+	struct json_object *jdecoder, *jobj;
26ccd9
+	u64 val;
26ccd9
+
26ccd9
+	jdecoder = json_object_new_object();
26ccd9
+	if (!jdecoder)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	jobj = json_object_new_string(devname);
26ccd9
+	if (jobj)
26ccd9
+		json_object_object_add(jdecoder, "decoder", jobj);
26ccd9
+
26ccd9
+	val = cxl_decoder_get_resource(decoder);
26ccd9
+	if (val < ULLONG_MAX) {
26ccd9
+		jobj = util_json_object_hex(val, flags);
26ccd9
+		if (jobj)
26ccd9
+			json_object_object_add(jdecoder, "resource", jobj);
26ccd9
+	}
26ccd9
+
26ccd9
+	val = cxl_decoder_get_size(decoder);
26ccd9
+	if (val < ULLONG_MAX) {
26ccd9
+		jobj = util_json_object_size(val, flags);
26ccd9
+		if (jobj)
26ccd9
+			json_object_object_add(jdecoder, "size", jobj);
26ccd9
+	}
26ccd9
+
26ccd9
+	if (val == 0) {
26ccd9
+		jobj = json_object_new_string("disabled");
26ccd9
+		if (jobj)
26ccd9
+			json_object_object_add(jdecoder, "state", jobj);
26ccd9
+	}
26ccd9
+
26ccd9
+	if (cxl_port_is_root(port) && cxl_decoder_is_mem_capable(decoder)) {
26ccd9
+		if (cxl_decoder_is_pmem_capable(decoder)) {
26ccd9
+			jobj = json_object_new_boolean(true);
26ccd9
+			if (jobj)
26ccd9
+				json_object_object_add(jdecoder, "pmem_capable",
26ccd9
+						       jobj);
26ccd9
+		}
26ccd9
+		if (cxl_decoder_is_volatile_capable(decoder)) {
26ccd9
+			jobj = json_object_new_boolean(true);
26ccd9
+			if (jobj)
26ccd9
+				json_object_object_add(
26ccd9
+					jdecoder, "volatile_capable", jobj);
26ccd9
+		}
26ccd9
+	}
26ccd9
+
26ccd9
+	if (cxl_port_is_root(port) &&
26ccd9
+	    cxl_decoder_is_accelmem_capable(decoder)) {
26ccd9
+		jobj = json_object_new_boolean(true);
26ccd9
+		if (jobj)
26ccd9
+			json_object_object_add(jdecoder, "accelmem_capable",
26ccd9
+					       jobj);
26ccd9
+	}
26ccd9
+
26ccd9
+	return jdecoder;
26ccd9
+}
26ccd9
+
26ccd9
 static struct json_object *__util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 						   const char *name_key,
26ccd9
 						   unsigned long flags)
26ccd9
diff --git a/cxl/json.h b/cxl/json.h
26ccd9
index 8f45190..fcca2e6 100644
26ccd9
--- a/cxl/json.h
26ccd9
+++ b/cxl/json.h
26ccd9
@@ -13,4 +13,6 @@ struct json_object *util_cxl_port_to_json(struct cxl_port *port,
26ccd9
 					  unsigned long flags);
26ccd9
 struct json_object *util_cxl_endpoint_to_json(struct cxl_endpoint *endpoint,
26ccd9
 					      unsigned long flags);
26ccd9
+struct json_object *util_cxl_decoder_to_json(struct cxl_decoder *decoder,
26ccd9
+					     unsigned long flags);
26ccd9
 #endif /* __CXL_UTIL_JSON_H__ */
26ccd9
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
26ccd9
index 2fdaf71..5e30923 100644
26ccd9
--- a/cxl/lib/libcxl.c
26ccd9
+++ b/cxl/lib/libcxl.c
26ccd9
@@ -63,16 +63,26 @@ static void free_memdev(struct cxl_memdev *memdev, struct list_head *head)
26ccd9
 	free(memdev->firmware_version);
26ccd9
 	free(memdev->dev_buf);
26ccd9
 	free(memdev->dev_path);
26ccd9
-	free(memdev->host);
26ccd9
+	free(memdev->host_path);
26ccd9
 	free(memdev);
26ccd9
 }
26ccd9
 
26ccd9
+static void free_decoder(struct cxl_decoder *decoder, struct list_head *head)
26ccd9
+{
26ccd9
+	if (head)
26ccd9
+		list_del_from(head, &decoder->list);
26ccd9
+	free(decoder->dev_buf);
26ccd9
+	free(decoder->dev_path);
26ccd9
+	free(decoder);
26ccd9
+}
26ccd9
+
26ccd9
 static void free_port(struct cxl_port *port, struct list_head *head);
26ccd9
 static void free_endpoint(struct cxl_endpoint *endpoint, struct list_head *head);
26ccd9
 static void __free_port(struct cxl_port *port, struct list_head *head)
26ccd9
 {
26ccd9
 	struct cxl_port *child, *_c;
26ccd9
 	struct cxl_endpoint *endpoint, *_e;
26ccd9
+	struct cxl_decoder *decoder, *_d;
26ccd9
 
26ccd9
 	if (head)
26ccd9
 		list_del_from(head, &port->list);
26ccd9
@@ -80,6 +90,8 @@ static void __free_port(struct cxl_port *port, struct list_head *head)
26ccd9
 		free_port(child, &port->child_ports);
26ccd9
 	list_for_each_safe(&port->endpoints, endpoint, _e, port.list)
26ccd9
 		free_endpoint(endpoint, &port->endpoints);
26ccd9
+	list_for_each_safe(&port->decoders, decoder, _d, list)
26ccd9
+		free_decoder(decoder, &port->decoders);
26ccd9
 	kmod_module_unref(port->module);
26ccd9
 	free(port->dev_buf);
26ccd9
 	free(port->dev_path);
26ccd9
@@ -298,9 +310,9 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	char *path = calloc(1, strlen(cxlmem_base) + 100);
26ccd9
 	struct cxl_ctx *ctx = parent;
26ccd9
 	struct cxl_memdev *memdev, *memdev_dup;
26ccd9
-	char *host, *rpath = NULL;
26ccd9
 	char buf[SYSFS_ATTR_SIZE];
26ccd9
 	struct stat st;
26ccd9
+	char *host;
26ccd9
 
26ccd9
 	if (!path)
26ccd9
 		return NULL;
26ccd9
@@ -358,21 +370,13 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	if (!memdev->dev_path)
26ccd9
 		goto err_read;
26ccd9
 
26ccd9
-	rpath = realpath(cxlmem_base, NULL);
26ccd9
-	if (!rpath)
26ccd9
+	memdev->host_path = realpath(cxlmem_base, NULL);
26ccd9
+	if (!memdev->host_path)
26ccd9
 		goto err_read;
26ccd9
-	host = strrchr(rpath, '/');
26ccd9
-	if (host) {
26ccd9
-		host[0] = '\0';
26ccd9
-		host = strrchr(rpath, '/');
26ccd9
-	}
26ccd9
+	host = strrchr(memdev->host_path, '/');
26ccd9
 	if (!host)
26ccd9
 		goto err_read;
26ccd9
-	memdev->host = strdup(host + 1);
26ccd9
-	if (!memdev->host)
26ccd9
-		goto err_read;
26ccd9
-	free(rpath);
26ccd9
-	rpath = NULL;
26ccd9
+	host[0] = '\0';
26ccd9
 
26ccd9
 	sprintf(path, "%s/firmware_version", cxlmem_base);
26ccd9
 	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
@@ -404,8 +408,8 @@ static void *add_cxl_memdev(void *parent, int id, const char *cxlmem_base)
26ccd9
 	free(memdev->firmware_version);
26ccd9
 	free(memdev->dev_buf);
26ccd9
 	free(memdev->dev_path);
26ccd9
+	free(memdev->host_path);
26ccd9
 	free(memdev);
26ccd9
-	free(rpath);
26ccd9
  err_dev:
26ccd9
 	free(path);
26ccd9
 	return NULL;
26ccd9
@@ -463,7 +467,7 @@ CXL_EXPORT const char *cxl_memdev_get_devname(struct cxl_memdev *memdev)
26ccd9
 
26ccd9
 CXL_EXPORT const char *cxl_memdev_get_host(struct cxl_memdev *memdev)
26ccd9
 {
26ccd9
-	return memdev->host;
26ccd9
+	return devpath_to_devname(memdev->host_path);
26ccd9
 }
26ccd9
 
26ccd9
 CXL_EXPORT struct cxl_bus *cxl_memdev_get_bus(struct cxl_memdev *memdev)
26ccd9
@@ -679,9 +683,11 @@ static int cxl_port_init(struct cxl_port *port, struct cxl_port *parent_port,
26ccd9
 	port->ctx = ctx;
26ccd9
 	port->type = type;
26ccd9
 	port->parent = parent_port;
26ccd9
+	port->type = type;
26ccd9
 
26ccd9
 	list_head_init(&port->child_ports);
26ccd9
 	list_head_init(&port->endpoints);
26ccd9
+	list_head_init(&port->decoders);
26ccd9
 
26ccd9
 	port->dev_path = strdup(cxlport_base);
26ccd9
 	if (!port->dev_path)
26ccd9
@@ -842,6 +848,207 @@ cxl_endpoint_get_memdev(struct cxl_endpoint *endpoint)
26ccd9
 	return NULL;
26ccd9
 }
26ccd9
 
26ccd9
+static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
26ccd9
+{
26ccd9
+	const char *devname = devpath_to_devname(cxldecoder_base);
26ccd9
+	char *path = calloc(1, strlen(cxldecoder_base) + 100);
26ccd9
+	struct cxl_decoder *decoder, *decoder_dup;
26ccd9
+	struct cxl_port *port = parent;
26ccd9
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
26ccd9
+	char buf[SYSFS_ATTR_SIZE];
26ccd9
+	size_t i;
26ccd9
+
26ccd9
+	dbg(ctx, "%s: base: \'%s\'\n", devname, cxldecoder_base);
26ccd9
+
26ccd9
+	if (!path)
26ccd9
+		return NULL;
26ccd9
+
26ccd9
+	decoder = calloc(1, sizeof(*decoder));
26ccd9
+	if (!decoder)
26ccd9
+		goto err;
26ccd9
+
26ccd9
+	decoder->id = id;
26ccd9
+	decoder->ctx = ctx;
26ccd9
+	decoder->port = port;
26ccd9
+
26ccd9
+	decoder->dev_path = strdup(cxldecoder_base);
26ccd9
+	if (!decoder->dev_path)
26ccd9
+		goto err;
26ccd9
+
26ccd9
+	decoder->dev_buf = calloc(1, strlen(cxldecoder_base) + 50);
26ccd9
+	if (!decoder->dev_buf)
26ccd9
+		goto err;
26ccd9
+	decoder->buf_len = strlen(cxldecoder_base) + 50;
26ccd9
+
26ccd9
+	sprintf(path, "%s/start", cxldecoder_base);
26ccd9
+	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
+		decoder->start = ULLONG_MAX;
26ccd9
+	else
26ccd9
+		decoder->start = strtoull(buf, NULL, 0);
26ccd9
+
26ccd9
+	sprintf(path, "%s/size", cxldecoder_base);
26ccd9
+	if (sysfs_read_attr(ctx, path, buf) < 0)
26ccd9
+		decoder->size = ULLONG_MAX;
26ccd9
+	else
26ccd9
+		decoder->size = strtoull(buf, NULL, 0);
26ccd9
+
26ccd9
+	switch (port->type) {
26ccd9
+	case CXL_PORT_SWITCH:
26ccd9
+	case CXL_PORT_ENDPOINT:
26ccd9
+		decoder->pmem_capable = true;
26ccd9
+		decoder->volatile_capable = true;
26ccd9
+		decoder->mem_capable = true;
26ccd9
+		decoder->accelmem_capable = true;
26ccd9
+		sprintf(path, "%s/locked", cxldecoder_base);
26ccd9
+		if (sysfs_read_attr(ctx, path, buf) == 0)
26ccd9
+			decoder->locked = !!strtoul(buf, NULL, 0);
26ccd9
+		sprintf(path, "%s/target_type", cxldecoder_base);
26ccd9
+		if (sysfs_read_attr(ctx, path, buf) == 0) {
26ccd9
+			if (strcmp(buf, "accelerator") == 0)
26ccd9
+				decoder->target_type =
26ccd9
+					CXL_DECODER_TTYPE_ACCELERATOR;
26ccd9
+			if (strcmp(buf, "expander") == 0)
26ccd9
+				decoder->target_type =
26ccd9
+					CXL_DECODER_TTYPE_EXPANDER;
26ccd9
+		}
26ccd9
+		break;
26ccd9
+	case CXL_PORT_ROOT: {
26ccd9
+		struct cxl_decoder_flag {
26ccd9
+			char *name;
26ccd9
+			bool *flag;
26ccd9
+		} flags[] = {
26ccd9
+			{ "cap_type2", &decoder->accelmem_capable },
26ccd9
+			{ "cap_type3", &decoder->mem_capable },
26ccd9
+			{ "cap_ram", &decoder->volatile_capable },
26ccd9
+			{ "cap_pmem", &decoder->pmem_capable },
26ccd9
+			{ "locked", &decoder->locked },
26ccd9
+		};
26ccd9
+
26ccd9
+		for (i = 0; i < ARRAY_SIZE(flags); i++) {
26ccd9
+			struct cxl_decoder_flag *flag = &flags[i];
26ccd9
+
26ccd9
+			sprintf(path, "%s/%s", cxldecoder_base, flag->name);
26ccd9
+			if (sysfs_read_attr(ctx, path, buf) == 0)
26ccd9
+				*(flag->flag) = !!strtoul(buf, NULL, 0);
26ccd9
+		}
26ccd9
+		break;
26ccd9
+	}
26ccd9
+	}
26ccd9
+
26ccd9
+	cxl_decoder_foreach(port, decoder_dup)
26ccd9
+		if (decoder_dup->id == decoder->id) {
26ccd9
+			free_decoder(decoder, NULL);
26ccd9
+			return decoder_dup;
26ccd9
+		}
26ccd9
+
26ccd9
+	list_add(&port->decoders, &decoder->list);
26ccd9
+
26ccd9
+	return decoder;
26ccd9
+err:
26ccd9
+	free(decoder->dev_path);
26ccd9
+	free(decoder->dev_buf);
26ccd9
+	free(decoder);
26ccd9
+	free(path);
26ccd9
+	return NULL;
26ccd9
+}
26ccd9
+
26ccd9
+static void cxl_decoders_init(struct cxl_port *port)
26ccd9
+{
26ccd9
+	struct cxl_ctx *ctx = cxl_port_get_ctx(port);
26ccd9
+	char *decoder_fmt;
26ccd9
+
26ccd9
+	if (port->decoders_init)
26ccd9
+		return;
26ccd9
+
26ccd9
+	if (asprintf(&decoder_fmt, "decoder%d.", cxl_port_get_id(port)) < 0) {
26ccd9
+		err(ctx, "%s: failed to add decoder(s)\n",
26ccd9
+		    cxl_port_get_devname(port));
26ccd9
+		return;
26ccd9
+	}
26ccd9
+
26ccd9
+	port->decoders_init = 1;
26ccd9
+
26ccd9
+	sysfs_device_parse(ctx, port->dev_path, decoder_fmt, port,
26ccd9
+			   add_cxl_decoder);
26ccd9
+
26ccd9
+	free(decoder_fmt);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_decoder *cxl_decoder_get_first(struct cxl_port *port)
26ccd9
+{
26ccd9
+	cxl_decoders_init(port);
26ccd9
+
26ccd9
+	return list_top(&port->decoders, struct cxl_decoder, list);
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_decoder *cxl_decoder_get_next(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	struct cxl_port *port = decoder->port;
26ccd9
+
26ccd9
+	return list_next(&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
+}
26ccd9
+
26ccd9
+CXL_EXPORT int cxl_decoder_get_id(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->id;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT struct cxl_port *cxl_decoder_get_port(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->port;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT unsigned long long cxl_decoder_get_resource(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->start;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT unsigned long long cxl_decoder_get_size(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->size;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT enum cxl_decoder_target_type
26ccd9
+cxl_decoder_get_target_type(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->target_type;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT bool cxl_decoder_is_pmem_capable(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->pmem_capable;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->volatile_capable;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT bool cxl_decoder_is_mem_capable(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->mem_capable;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT bool cxl_decoder_is_accelmem_capable(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->accelmem_capable;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT bool cxl_decoder_is_locked(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return decoder->locked;
26ccd9
+}
26ccd9
+
26ccd9
+CXL_EXPORT const char *cxl_decoder_get_devname(struct cxl_decoder *decoder)
26ccd9
+{
26ccd9
+	return devpath_to_devname(decoder->dev_path);
26ccd9
+}
26ccd9
+
26ccd9
 static void *add_cxl_port(void *parent, int id, const char *cxlport_base)
26ccd9
 {
26ccd9
 	const char *devname = devpath_to_devname(cxlport_base);
26ccd9
diff --git a/cxl/lib/libcxl.sym b/cxl/lib/libcxl.sym
26ccd9
index f235e99..22babb7 100644
26ccd9
--- a/cxl/lib/libcxl.sym
26ccd9
+++ b/cxl/lib/libcxl.sym
26ccd9
@@ -117,4 +117,18 @@ global:
26ccd9
 	cxl_memdev_get_bus;
26ccd9
 	cxl_memdev_disable_invalidate;
26ccd9
 	cxl_memdev_enable;
26ccd9
+	cxl_decoder_get_first;
26ccd9
+	cxl_decoder_get_next;
26ccd9
+	cxl_decoder_get_ctx;
26ccd9
+	cxl_decoder_get_id;
26ccd9
+	cxl_decoder_get_port;
26ccd9
+	cxl_decoder_get_resource;
26ccd9
+	cxl_decoder_get_size;
26ccd9
+	cxl_decoder_get_devname;
26ccd9
+	cxl_decoder_get_target_type;
26ccd9
+	cxl_decoder_is_pmem_capable;
26ccd9
+	cxl_decoder_is_volatile_capable;
26ccd9
+	cxl_decoder_is_mem_capable;
26ccd9
+	cxl_decoder_is_accelmem_capable;
26ccd9
+	cxl_decoder_is_locked;
26ccd9
 } LIBCXL_1;
26ccd9
diff --git a/cxl/lib/private.h b/cxl/lib/private.h
26ccd9
index c00bb36..1743a24 100644
26ccd9
--- a/cxl/lib/private.h
26ccd9
+++ b/cxl/lib/private.h
26ccd9
@@ -23,7 +23,7 @@ struct cxl_memdev {
26ccd9
 	int numa_node;
26ccd9
 	void *dev_buf;
26ccd9
 	size_t buf_len;
26ccd9
-	char *host;
26ccd9
+	char *host_path;
26ccd9
 	char *dev_path;
26ccd9
 	char *firmware_version;
26ccd9
 	struct cxl_ctx *ctx;
26ccd9
@@ -52,6 +52,7 @@ struct cxl_port {
26ccd9
 	char *uport;
26ccd9
 	int ports_init;
26ccd9
 	int endpoints_init;
26ccd9
+	int decoders_init;
26ccd9
 	struct cxl_ctx *ctx;
26ccd9
 	struct cxl_bus *bus;
26ccd9
 	enum cxl_port_type type;
26ccd9
@@ -60,6 +61,7 @@ struct cxl_port {
26ccd9
 	struct list_node list;
26ccd9
 	struct list_head child_ports;
26ccd9
 	struct list_head endpoints;
26ccd9
+	struct list_head decoders;
26ccd9
 };
26ccd9
 
26ccd9
 struct cxl_bus {
26ccd9
@@ -71,6 +73,33 @@ struct cxl_endpoint {
26ccd9
 	struct cxl_memdev *memdev;
26ccd9
 };
26ccd9
 
26ccd9
+struct cxl_target {
26ccd9
+	struct list_node list;
26ccd9
+	struct cxl_decoder *decoder;
26ccd9
+	char *dev_path;
26ccd9
+	int id, position;
26ccd9
+};
26ccd9
+
26ccd9
+struct cxl_decoder {
26ccd9
+	struct cxl_port *port;
26ccd9
+	struct list_node list;
26ccd9
+	struct cxl_ctx *ctx;
26ccd9
+	u64 start;
26ccd9
+	u64 size;
26ccd9
+	void *dev_buf;
26ccd9
+	size_t buf_len;
26ccd9
+	char *dev_path;
26ccd9
+	int nr_targets;
26ccd9
+	int id;
26ccd9
+	bool pmem_capable;
26ccd9
+	bool volatile_capable;
26ccd9
+	bool mem_capable;
26ccd9
+	bool accelmem_capable;
26ccd9
+	bool locked;
26ccd9
+	enum cxl_decoder_target_type target_type;
26ccd9
+	struct list_head targets;
26ccd9
+};
26ccd9
+
26ccd9
 enum cxl_cmd_query_status {
26ccd9
 	CXL_CMD_QUERY_NOT_RUN = 0,
26ccd9
 	CXL_CMD_QUERY_OK,
26ccd9
diff --git a/cxl/libcxl.h b/cxl/libcxl.h
26ccd9
index 53f68dd..439ed93 100644
26ccd9
--- a/cxl/libcxl.h
26ccd9
+++ b/cxl/libcxl.h
26ccd9
@@ -98,6 +98,34 @@ bool cxl_port_hosts_memdev(struct cxl_port *port, struct cxl_memdev *memdev);
26ccd9
 	for (port = cxl_port_get_first(parent); port != NULL;                  \
26ccd9
 	     port = cxl_port_get_next(port))
26ccd9
 
26ccd9
+struct cxl_decoder;
26ccd9
+struct cxl_decoder *cxl_decoder_get_first(struct cxl_port *port);
26ccd9
+struct cxl_decoder *cxl_decoder_get_next(struct cxl_decoder *decoder);
26ccd9
+unsigned long long cxl_decoder_get_resource(struct cxl_decoder *decoder);
26ccd9
+unsigned long long cxl_decoder_get_size(struct cxl_decoder *decoder);
26ccd9
+const char *cxl_decoder_get_devname(struct cxl_decoder *decoder);
26ccd9
+struct cxl_ctx *cxl_decoder_get_ctx(struct cxl_decoder *decoder);
26ccd9
+int cxl_decoder_get_id(struct cxl_decoder *decoder);
26ccd9
+struct cxl_port *cxl_decoder_get_port(struct cxl_decoder *decoder);
26ccd9
+
26ccd9
+enum cxl_decoder_target_type {
26ccd9
+	CXL_DECODER_TTYPE_UNKNOWN,
26ccd9
+	CXL_DECODER_TTYPE_EXPANDER,
26ccd9
+	CXL_DECODER_TTYPE_ACCELERATOR,
26ccd9
+};
26ccd9
+
26ccd9
+enum cxl_decoder_target_type
26ccd9
+cxl_decoder_get_target_type(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_pmem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_volatile_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_mem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_accelmem_capable(struct cxl_decoder *decoder);
26ccd9
+bool cxl_decoder_is_locked(struct cxl_decoder *decoder);
26ccd9
+
26ccd9
+#define cxl_decoder_foreach(port, decoder)                                     \
26ccd9
+	for (decoder = cxl_decoder_get_first(port); decoder != NULL;           \
26ccd9
+	     decoder = cxl_decoder_get_next(decoder))
26ccd9
+
26ccd9
 struct cxl_endpoint;
26ccd9
 struct cxl_endpoint *cxl_endpoint_get_first(struct cxl_port *parent);
26ccd9
 struct cxl_endpoint *cxl_endpoint_get_next(struct cxl_endpoint *endpoint);
26ccd9
diff --git a/cxl/list.c b/cxl/list.c
26ccd9
index b15e01c..d70192a 100644
26ccd9
--- a/cxl/list.c
26ccd9
+++ b/cxl/list.c
26ccd9
@@ -36,6 +36,11 @@ static const struct option options[] = {
26ccd9
 		   "filter by CXL endpoint device name(s)"),
26ccd9
 	OPT_BOOLEAN('E', "endpoints", &param.endpoints,
26ccd9
 		    "include CXL endpoint info"),
26ccd9
+	OPT_STRING('d', "decoder", &param.decoder_filter,
26ccd9
+		   "decoder device name",
26ccd9
+		   "filter by CXL decoder device name(s) / class"),
26ccd9
+	OPT_BOOLEAN('D', "decoders", &param.decoders,
26ccd9
+		    "include CXL decoder info"),
26ccd9
 	OPT_BOOLEAN('i', "idle", &param.idle, "include disabled devices"),
26ccd9
 	OPT_BOOLEAN('u', "human", &param.human,
26ccd9
 		    "use human friendly number formats "),
26ccd9
@@ -50,7 +55,7 @@ static const struct option options[] = {
26ccd9
 static int num_list_flags(void)
26ccd9
 {
26ccd9
 	return !!param.memdevs + !!param.buses + !!param.ports +
26ccd9
-	       !!param.endpoints;
26ccd9
+	       !!param.endpoints + !!param.decoders;
26ccd9
 }
26ccd9
 
26ccd9
 int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
@@ -82,6 +87,8 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
26ccd9
 			param.ports = true;
26ccd9
 		if (param.endpoint_filter)
26ccd9
 			param.endpoints = true;
26ccd9
+		if (param.decoder_filter)
26ccd9
+			param.decoders = true;
26ccd9
 		if (num_list_flags() == 0) {
26ccd9
 			/*
26ccd9
 			 * TODO: We likely want to list regions by default if
26ccd9
diff --git a/util/json.h b/util/json.h
26ccd9
index 061f0d4..e026df1 100644
26ccd9
--- a/util/json.h
26ccd9
+++ b/util/json.h
26ccd9
@@ -18,6 +18,7 @@ enum util_json_flags {
26ccd9
 	UTIL_JSON_FIRMWARE	= (1 << 8),
26ccd9
 	UTIL_JSON_DAX_MAPPINGS	= (1 << 9),
26ccd9
 	UTIL_JSON_HEALTH	= (1 << 10),
26ccd9
+	UTIL_JSON_TARGETS	= (1 << 11),
26ccd9
 };
26ccd9
 
26ccd9
 void util_display_json_array(FILE *f_out, struct json_object *jarray,
26ccd9
-- 
26ccd9
2.27.0
26ccd9