anitazha / rpms / ndctl

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