CXL regions may wish not to auto-configure their memory as dax kmem,
but the current plumbing defaults all cxl-created dax devices to the
kmem driver.  This exposes them to hotplug policy, even if the user
intends to use the memory as a dax device.

Add plumbing to allow CXL drivers to select whether a DAX region should
default to kmem (DAXDRV_KMEM_TYPE) or device (DAXDRV_DEVICE_TYPE).

Add a 'dax_driver' field to struct cxl_dax_region and update
devm_cxl_add_dax_region() to take a dax_driver_type parameter.

In drivers/dax/cxl.c, the IORESOURCE_DAX_KMEM flag used by dax driver
matching code is now set conditionally based on dax_region->dax_driver.

Exports `enum dax_driver_type` to linux/dax.h for use in the cxl driver.

All current callers pass DAXDRV_KMEM_TYPE for backward compatibility.

Cc: John Groves <[email protected]>
Signed-off-by: Gregory Price <[email protected]>
---
 drivers/cxl/core/core.h   | 1 +
 drivers/cxl/core/region.c | 6 ++++--
 drivers/cxl/cxl.h         | 2 ++
 drivers/dax/bus.h         | 6 +-----
 drivers/dax/cxl.c         | 6 +++++-
 include/linux/dax.h       | 5 +++++
 6 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/drivers/cxl/core/core.h b/drivers/cxl/core/core.h
index 1fb66132b777..dd987ef2def5 100644
--- a/drivers/cxl/core/core.h
+++ b/drivers/cxl/core/core.h
@@ -6,6 +6,7 @@
 
 #include <cxl/mailbox.h>
 #include <linux/rwsem.h>
+#include <linux/dax.h>
 
 extern const struct device_type cxl_nvdimm_bridge_type;
 extern const struct device_type cxl_nvdimm_type;
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index eef5d5fe3f95..e4097c464ed3 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3450,7 +3450,8 @@ static void cxlr_dax_unregister(void *_cxlr_dax)
        device_unregister(&cxlr_dax->dev);
 }
 
-static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
+static int devm_cxl_add_dax_region(struct cxl_region *cxlr,
+                                  enum dax_driver_type dax_driver)
 {
        struct cxl_dax_region *cxlr_dax;
        struct device *dev;
@@ -3461,6 +3462,7 @@ static int devm_cxl_add_dax_region(struct cxl_region 
*cxlr)
                return PTR_ERR(cxlr_dax);
 
        cxlr_dax->online_type = mhp_get_default_online_type();
+       cxlr_dax->dax_driver = dax_driver;
        dev = &cxlr_dax->dev;
        rc = dev_set_name(dev, "dax_region%d", cxlr->id);
        if (rc)
@@ -3994,7 +3996,7 @@ static int cxl_region_probe(struct device *dev)
                                        p->res->start, p->res->end, cxlr,
                                        is_system_ram) > 0)
                        return 0;
-               return devm_cxl_add_dax_region(cxlr);
+               return devm_cxl_add_dax_region(cxlr, DAXDRV_KMEM_TYPE);
        default:
                dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
                        cxlr->mode);
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 07d57d13f4c7..c06a239c0008 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -12,6 +12,7 @@
 #include <linux/node.h>
 #include <linux/io.h>
 #include <linux/range.h>
+#include <linux/dax.h>
 
 extern const struct nvdimm_security_ops *cxl_security_ops;
 
@@ -592,6 +593,7 @@ struct cxl_dax_region {
        struct cxl_region *cxlr;
        struct range hpa_range;
        int online_type; /* MMOP_ value for kmem driver */
+       enum dax_driver_type dax_driver;
 };
 
 /**
diff --git a/drivers/dax/bus.h b/drivers/dax/bus.h
index 4ac92a4edfe7..9144593b4029 100644
--- a/drivers/dax/bus.h
+++ b/drivers/dax/bus.h
@@ -2,6 +2,7 @@
 /* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
 #ifndef __DAX_BUS_H__
 #define __DAX_BUS_H__
+#include <linux/dax.h>
 #include <linux/device.h>
 #include <linux/range.h>
 
@@ -29,11 +30,6 @@ struct dev_dax_data {
 
 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data);
 
-enum dax_driver_type {
-       DAXDRV_KMEM_TYPE,
-       DAXDRV_DEVICE_TYPE,
-};
-
 struct dax_device_driver {
        struct device_driver drv;
        struct list_head ids;
diff --git a/drivers/dax/cxl.c b/drivers/dax/cxl.c
index 856a0cd24f3b..b13ecc2f9806 100644
--- a/drivers/dax/cxl.c
+++ b/drivers/dax/cxl.c
@@ -11,14 +11,18 @@ static int cxl_dax_region_probe(struct device *dev)
        struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
        int nid = phys_to_target_node(cxlr_dax->hpa_range.start);
        struct cxl_region *cxlr = cxlr_dax->cxlr;
+       unsigned long flags = 0;
        struct dax_region *dax_region;
        struct dev_dax_data data;
 
+       if (cxlr_dax->dax_driver == DAXDRV_KMEM_TYPE)
+               flags |= IORESOURCE_DAX_KMEM;
+
        if (nid == NUMA_NO_NODE)
                nid = memory_add_physaddr_to_nid(cxlr_dax->hpa_range.start);
 
        dax_region = alloc_dax_region(dev, cxlr->id, &cxlr_dax->hpa_range, nid,
-                                     PMD_SIZE, IORESOURCE_DAX_KMEM);
+                                     PMD_SIZE, flags);
        if (!dax_region)
                return -ENOMEM;
 
diff --git a/include/linux/dax.h b/include/linux/dax.h
index bf103f317cac..e62f92d0ace1 100644
--- a/include/linux/dax.h
+++ b/include/linux/dax.h
@@ -19,6 +19,11 @@ enum dax_access_mode {
        DAX_RECOVERY_WRITE,
 };
 
+enum dax_driver_type {
+       DAXDRV_KMEM_TYPE,
+       DAXDRV_DEVICE_TYPE,
+};
+
 struct dax_operations {
        /*
         * direct_access: translate a device-relative
-- 
2.52.0


Reply via email to