[PATCH V2 15/20] cxl: Add a routine to find cxl root decoder on cxl bus using cxl port

From: Neeraj Kumar
Date: Wed Jul 30 2025 - 08:19:59 EST


Add cxl_find_root_decoder_by_port() to find root decoder on cxl bus.
It is used to find root decoder using cxl port.

Signed-off-by: Neeraj Kumar <s.neeraj@xxxxxxxxxxx>
---
drivers/cxl/core/port.c | 27 +++++++++++++++++++++++++++
drivers/cxl/cxl.h | 1 +
2 files changed, 28 insertions(+)

diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
index ba743e31f721..c1bb0e0286ed 100644
--- a/drivers/cxl/core/port.c
+++ b/drivers/cxl/core/port.c
@@ -524,6 +524,33 @@ struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev)
}
EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, "CXL");

+static int match_root_decoder(struct device *dev, const void *data)
+{
+ return is_root_decoder(dev);
+}
+
+/**
+ * cxl_find_root_decoder_by_port() - find a cxl root decoder on cxl bus
+ * @port: any descendant port in CXL port topology
+ */
+struct cxl_root_decoder *cxl_find_root_decoder_by_port(struct cxl_port *port)
+{
+ struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
+ struct device *dev;
+
+ if (!cxl_root)
+ return NULL;
+
+ dev = device_find_child(&cxl_root->port.dev, NULL, match_root_decoder);
+ if (!dev)
+ return NULL;
+
+ /* Release device ref taken via device_find_child() */
+ put_device(dev);
+ return to_cxl_root_decoder(dev);
+}
+EXPORT_SYMBOL_NS_GPL(cxl_find_root_decoder_by_port, "CXL");
+
static void cxl_ep_release(struct cxl_ep *ep)
{
put_device(ep->ep);
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 129db2e49aa7..e249372b642d 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -857,6 +857,7 @@ struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev);
bool is_cxl_nvdimm(struct device *dev);
int devm_cxl_add_nvdimm(struct cxl_port *parent_port, struct cxl_memdev *cxlmd);
struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port);
+struct cxl_root_decoder *cxl_find_root_decoder_by_port(struct cxl_port *port);

#ifdef CONFIG_CXL_REGION
bool is_cxl_pmem_region(struct device *dev);
--
2.34.1