1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2023 Intel Corporation. All rights reserved. */ 3 #include <linux/module.h> 4 #include <linux/dax.h> 5 6 #include "../cxl/cxl.h" 7 #include "bus.h" 8 9 static int cxl_dax_region_probe(struct device *dev) 10 { 11 struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev); 12 int nid = phys_to_target_node(cxlr_dax->hpa_range.start); 13 struct cxl_region *cxlr = cxlr_dax->cxlr; 14 struct dax_region *dax_region; 15 struct dev_dax_data data; 16 17 if (nid == NUMA_NO_NODE) 18 nid = memory_add_physaddr_to_nid(cxlr_dax->hpa_range.start); 19 20 dax_region = alloc_dax_region(dev, cxlr->id, &cxlr_dax->hpa_range, nid, 21 PMD_SIZE, IORESOURCE_DAX_KMEM); 22 if (!dax_region) 23 return -ENOMEM; 24 25 data = (struct dev_dax_data) { 26 .dax_region = dax_region, 27 .id = -1, 28 .size = range_len(&cxlr_dax->hpa_range), 29 .memmap_on_memory = true, 30 }; 31 32 return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data)); 33 } 34 35 static struct cxl_driver cxl_dax_region_driver = { 36 .name = "cxl_dax_region", 37 .probe = cxl_dax_region_probe, 38 .id = CXL_DEVICE_DAX_REGION, 39 .drv = { 40 .suppress_bind_attrs = true, 41 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 42 }, 43 }; 44 45 static void cxl_dax_region_driver_register(struct work_struct *work) 46 { 47 dax_hmem_flush_work(); 48 cxl_driver_register(&cxl_dax_region_driver); 49 } 50 51 static DECLARE_WORK(cxl_dax_region_driver_work, cxl_dax_region_driver_register); 52 53 static int __init cxl_dax_region_init(void) 54 { 55 /* 56 * Need to resolve a race with dax_hmem wanting to drive regions 57 * instead of CXL 58 */ 59 queue_work(system_long_wq, &cxl_dax_region_driver_work); 60 return 0; 61 } 62 module_init(cxl_dax_region_init); 63 64 static void __exit cxl_dax_region_exit(void) 65 { 66 flush_work(&cxl_dax_region_driver_work); 67 cxl_driver_unregister(&cxl_dax_region_driver); 68 } 69 module_exit(cxl_dax_region_exit); 70 71 MODULE_ALIAS_CXL(CXL_DEVICE_DAX_REGION); 72 MODULE_DESCRIPTION("CXL DAX: direct access to CXL regions"); 73 MODULE_LICENSE("GPL"); 74 MODULE_AUTHOR("Intel Corporation"); 75 MODULE_IMPORT_NS("CXL"); 76