1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/platform_device.h>
3 #include <linux/memregion.h>
4 #include <linux/module.h>
5 #include <linux/pfn_t.h>
9 static bool region_idle;
10 module_param_named(region_idle, region_idle, bool, 0644);
12 static int dax_hmem_probe(struct platform_device *pdev)
14 unsigned long flags = IORESOURCE_DAX_KMEM;
15 struct device *dev = &pdev->dev;
16 struct dax_region *dax_region;
17 struct memregion_info *mri;
18 struct dev_dax_data data;
21 * @region_idle == true indicates that an administrative agent
22 * wants to manipulate the range partitioning before the devices
23 * are created, so do not send them to the dax_kmem driver by
29 mri = dev->platform_data;
30 dax_region = alloc_dax_region(dev, pdev->id, &mri->range,
31 mri->target_node, PMD_SIZE, flags);
35 data = (struct dev_dax_data) {
36 .dax_region = dax_region,
38 .size = region_idle ? 0 : range_len(&mri->range),
41 return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
44 static struct platform_driver dax_hmem_driver = {
45 .probe = dax_hmem_probe,
51 static void release_memregion(void *data)
53 memregion_free((long) data);
56 static void release_hmem(void *pdev)
58 platform_device_unregister(pdev);
61 static int hmem_register_device(struct device *host, int target_nid,
62 const struct resource *res)
64 struct platform_device *pdev;
65 struct memregion_info info;
69 if (IS_ENABLED(CONFIG_CXL_REGION) &&
70 region_intersects(res->start, resource_size(res), IORESOURCE_MEM,
71 IORES_DESC_CXL) != REGION_DISJOINT) {
72 dev_dbg(host, "deferring range to CXL: %pr\n", res);
76 rc = region_intersects(res->start, resource_size(res), IORESOURCE_MEM,
77 IORES_DESC_SOFT_RESERVED);
78 if (rc != REGION_INTERSECTS)
81 id = memregion_alloc(GFP_KERNEL);
83 dev_err(host, "memregion allocation failure for %pr\n", res);
86 rc = devm_add_action_or_reset(host, release_memregion, (void *) id);
90 pdev = platform_device_alloc("hmem", id);
92 dev_err(host, "device allocation failure for %pr\n", res);
96 pdev->dev.numa_node = numa_map_to_online_node(target_nid);
97 info = (struct memregion_info) {
98 .target_node = target_nid,
104 rc = platform_device_add_data(pdev, &info, sizeof(info));
106 dev_err(host, "memregion_info allocation failure for %pr\n",
111 rc = platform_device_add(pdev);
113 dev_err(host, "%s add failed for %pr\n", dev_name(&pdev->dev),
118 return devm_add_action_or_reset(host, release_hmem, pdev);
121 platform_device_put(pdev);
125 static int dax_hmem_platform_probe(struct platform_device *pdev)
127 return walk_hmem_resources(&pdev->dev, hmem_register_device);
130 static struct platform_driver dax_hmem_platform_driver = {
131 .probe = dax_hmem_platform_probe,
133 .name = "hmem_platform",
137 static __init int dax_hmem_init(void)
141 rc = platform_driver_register(&dax_hmem_platform_driver);
145 rc = platform_driver_register(&dax_hmem_driver);
147 platform_driver_unregister(&dax_hmem_platform_driver);
152 static __exit void dax_hmem_exit(void)
154 platform_driver_unregister(&dax_hmem_driver);
155 platform_driver_unregister(&dax_hmem_platform_driver);
158 module_init(dax_hmem_init);
159 module_exit(dax_hmem_exit);
161 /* Allow for CXL to define its own dax regions */
162 #if IS_ENABLED(CONFIG_CXL_REGION)
163 #if IS_MODULE(CONFIG_CXL_ACPI)
164 MODULE_SOFTDEP("pre: cxl_acpi");
168 MODULE_ALIAS("platform:hmem*");
169 MODULE_ALIAS("platform:hmem_platform*");
170 MODULE_LICENSE("GPL v2");
171 MODULE_AUTHOR("Intel Corporation");