xref: /linux/drivers/dax/hmem/hmem.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
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/dax.h>
6 #include "../bus.h"
7 
8 static bool region_idle;
9 module_param_named(region_idle, region_idle, bool, 0644);
10 
11 static int dax_hmem_probe(struct platform_device *pdev)
12 {
13 	unsigned long flags = IORESOURCE_DAX_KMEM;
14 	struct device *dev = &pdev->dev;
15 	struct dax_region *dax_region;
16 	struct memregion_info *mri;
17 	struct dev_dax_data data;
18 
19 	/*
20 	 * @region_idle == true indicates that an administrative agent
21 	 * wants to manipulate the range partitioning before the devices
22 	 * are created, so do not send them to the dax_kmem driver by
23 	 * default.
24 	 */
25 	if (region_idle)
26 		flags = 0;
27 
28 	mri = dev->platform_data;
29 	dax_region = alloc_dax_region(dev, pdev->id, &mri->range,
30 				      mri->target_node, PMD_SIZE, flags);
31 	if (!dax_region)
32 		return -ENOMEM;
33 
34 	data = (struct dev_dax_data) {
35 		.dax_region = dax_region,
36 		.id = -1,
37 		.size = region_idle ? 0 : range_len(&mri->range),
38 		.memmap_on_memory = false,
39 	};
40 
41 	return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
42 }
43 
44 static struct platform_driver dax_hmem_driver = {
45 	.probe = dax_hmem_probe,
46 	.driver = {
47 		.name = "hmem",
48 	},
49 };
50 
51 static void release_memregion(void *data)
52 {
53 	memregion_free((long) data);
54 }
55 
56 static void release_hmem(void *pdev)
57 {
58 	platform_device_unregister(pdev);
59 }
60 
61 static int hmem_register_device(struct device *host, int target_nid,
62 				const struct resource *res)
63 {
64 	struct platform_device *pdev;
65 	struct memregion_info info;
66 	long id;
67 	int rc;
68 
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);
73 		return 0;
74 	}
75 
76 	rc = region_intersects_soft_reserve(res->start, resource_size(res));
77 	if (rc != REGION_INTERSECTS)
78 		return 0;
79 
80 	/* TODO: Add Soft-Reserved memory back to iomem */
81 
82 	id = memregion_alloc(GFP_KERNEL);
83 	if (id < 0) {
84 		dev_err(host, "memregion allocation failure for %pr\n", res);
85 		return -ENOMEM;
86 	}
87 	rc = devm_add_action_or_reset(host, release_memregion, (void *) id);
88 	if (rc)
89 		return rc;
90 
91 	pdev = platform_device_alloc("hmem", id);
92 	if (!pdev) {
93 		dev_err(host, "device allocation failure for %pr\n", res);
94 		return -ENOMEM;
95 	}
96 
97 	pdev->dev.numa_node = numa_map_to_online_node(target_nid);
98 	info = (struct memregion_info) {
99 		.target_node = target_nid,
100 		.range = {
101 			.start = res->start,
102 			.end = res->end,
103 		},
104 	};
105 	rc = platform_device_add_data(pdev, &info, sizeof(info));
106 	if (rc < 0) {
107 		dev_err(host, "memregion_info allocation failure for %pr\n",
108 		       res);
109 		goto out_put;
110 	}
111 
112 	rc = platform_device_add(pdev);
113 	if (rc < 0) {
114 		dev_err(host, "%s add failed for %pr\n", dev_name(&pdev->dev),
115 			res);
116 		goto out_put;
117 	}
118 
119 	return devm_add_action_or_reset(host, release_hmem, pdev);
120 
121 out_put:
122 	platform_device_put(pdev);
123 	return rc;
124 }
125 
126 static int dax_hmem_platform_probe(struct platform_device *pdev)
127 {
128 	return walk_hmem_resources(&pdev->dev, hmem_register_device);
129 }
130 
131 static struct platform_driver dax_hmem_platform_driver = {
132 	.probe = dax_hmem_platform_probe,
133 	.driver = {
134 		.name = "hmem_platform",
135 	},
136 };
137 
138 static __init int dax_hmem_init(void)
139 {
140 	int rc;
141 
142 	rc = platform_driver_register(&dax_hmem_platform_driver);
143 	if (rc)
144 		return rc;
145 
146 	rc = platform_driver_register(&dax_hmem_driver);
147 	if (rc)
148 		platform_driver_unregister(&dax_hmem_platform_driver);
149 
150 	return rc;
151 }
152 
153 static __exit void dax_hmem_exit(void)
154 {
155 	platform_driver_unregister(&dax_hmem_driver);
156 	platform_driver_unregister(&dax_hmem_platform_driver);
157 }
158 
159 module_init(dax_hmem_init);
160 module_exit(dax_hmem_exit);
161 
162 /* Allow for CXL to define its own dax regions */
163 #if IS_ENABLED(CONFIG_CXL_REGION)
164 #if IS_MODULE(CONFIG_CXL_ACPI)
165 MODULE_SOFTDEP("pre: cxl_acpi");
166 #endif
167 #endif
168 
169 MODULE_ALIAS("platform:hmem*");
170 MODULE_ALIAS("platform:hmem_platform*");
171 MODULE_DESCRIPTION("HMEM DAX: direct access to 'specific purpose' memory");
172 MODULE_LICENSE("GPL v2");
173 MODULE_AUTHOR("Intel Corporation");
174