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
8static bool region_idle;
9module_param_named(region_idle, region_idle, bool, 0644);
10
11static 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(parent: dev, region_id: pdev->id, range: &mri->range,
30 target_node: 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(range: &mri->range),
38 .memmap_on_memory = false,
39 };
40
41 return PTR_ERR_OR_ZERO(ptr: devm_create_dev_dax(data: &data));
42}
43
44static struct platform_driver dax_hmem_driver = {
45 .probe = dax_hmem_probe,
46 .driver = {
47 .name = "hmem",
48 },
49};
50
51static void release_memregion(void *data)
52{
53 memregion_free(id: (long) data);
54}
55
56static void release_hmem(void *pdev)
57{
58 platform_device_unregister(pdev);
59}
60
61static 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(offset: res->start, size: resource_size(res), IORESOURCE_MEM,
71 desc: 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(offset: res->start, size: resource_size(res), IORESOURCE_MEM,
77 desc: IORES_DESC_SOFT_RESERVED);
78 if (rc != REGION_INTERSECTS)
79 return 0;
80
81 id = memregion_alloc(GFP_KERNEL);
82 if (id < 0) {
83 dev_err(host, "memregion allocation failure for %pr\n", res);
84 return -ENOMEM;
85 }
86 rc = devm_add_action_or_reset(host, release_memregion, (void *) id);
87 if (rc)
88 return rc;
89
90 pdev = platform_device_alloc(name: "hmem", id);
91 if (!pdev) {
92 dev_err(host, "device allocation failure for %pr\n", res);
93 return -ENOMEM;
94 }
95
96 pdev->dev.numa_node = numa_map_to_online_node(target_nid);
97 info = (struct memregion_info) {
98 .target_node = target_nid,
99 .range = {
100 .start = res->start,
101 .end = res->end,
102 },
103 };
104 rc = platform_device_add_data(pdev, data: &info, size: sizeof(info));
105 if (rc < 0) {
106 dev_err(host, "memregion_info allocation failure for %pr\n",
107 res);
108 goto out_put;
109 }
110
111 rc = platform_device_add(pdev);
112 if (rc < 0) {
113 dev_err(host, "%s add failed for %pr\n", dev_name(&pdev->dev),
114 res);
115 goto out_put;
116 }
117
118 return devm_add_action_or_reset(host, release_hmem, pdev);
119
120out_put:
121 platform_device_put(pdev);
122 return rc;
123}
124
125static int dax_hmem_platform_probe(struct platform_device *pdev)
126{
127 return walk_hmem_resources(dev: &pdev->dev, fn: hmem_register_device);
128}
129
130static struct platform_driver dax_hmem_platform_driver = {
131 .probe = dax_hmem_platform_probe,
132 .driver = {
133 .name = "hmem_platform",
134 },
135};
136
137static __init int dax_hmem_init(void)
138{
139 int rc;
140
141 rc = platform_driver_register(&dax_hmem_platform_driver);
142 if (rc)
143 return rc;
144
145 rc = platform_driver_register(&dax_hmem_driver);
146 if (rc)
147 platform_driver_unregister(&dax_hmem_platform_driver);
148
149 return rc;
150}
151
152static __exit void dax_hmem_exit(void)
153{
154 platform_driver_unregister(&dax_hmem_driver);
155 platform_driver_unregister(&dax_hmem_platform_driver);
156}
157
158module_init(dax_hmem_init);
159module_exit(dax_hmem_exit);
160
161/* Allow for CXL to define its own dax regions */
162#if IS_ENABLED(CONFIG_CXL_REGION)
163#if IS_MODULE(CONFIG_CXL_ACPI)
164MODULE_SOFTDEP("pre: cxl_acpi");
165#endif
166#endif
167
168MODULE_ALIAS("platform:hmem*");
169MODULE_ALIAS("platform:hmem_platform*");
170MODULE_DESCRIPTION("HMEM DAX: direct access to 'specific purpose' memory");
171MODULE_LICENSE("GPL v2");
172MODULE_AUTHOR("Intel Corporation");
173

source code of linux/drivers/dax/hmem/hmem.c