1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) "papr-scm: " fmt
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/ioport.h>
9 #include <linux/slab.h>
10 #include <linux/ndctl.h>
11 #include <linux/sched.h>
12 #include <linux/libnvdimm.h>
13 #include <linux/platform_device.h>
15 #include <asm/plpar_wrappers.h>
17 #define BIND_ANY_ADDR (~0ul)
19 #define PAPR_SCM_DIMM_CMD_MASK \
20 ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
21 (1ul << ND_CMD_GET_CONFIG_DATA) | \
22 (1ul << ND_CMD_SET_CONFIG_DATA))
24 struct papr_scm_priv {
25 struct platform_device *pdev;
26 struct device_node *dn;
34 struct nvdimm_bus_descriptor bus_desc;
35 struct nvdimm_bus *bus;
36 struct nvdimm *nvdimm;
38 struct nd_region *region;
39 struct nd_interleave_set nd_set;
42 static int drc_pmem_bind(struct papr_scm_priv *p)
44 unsigned long ret[PLPAR_HCALL_BUFSIZE];
48 * When the hypervisor cannot map all the requested memory in a single
49 * hcall it returns H_BUSY and we call again with the token until
50 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
51 * leave the system in an undefined state, so we wait.
56 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
57 p->blocks, BIND_ANY_ADDR, token);
58 token = be64_to_cpu(ret[0]);
60 } while (rc == H_BUSY);
63 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
67 p->bound_addr = be64_to_cpu(ret[1]);
69 dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
74 static int drc_pmem_unbind(struct papr_scm_priv *p)
76 unsigned long ret[PLPAR_HCALL_BUFSIZE];
81 /* NB: unbind has the same retry requirements mentioned above */
83 rc = plpar_hcall(H_SCM_UNBIND_MEM, ret, p->drc_index,
84 p->bound_addr, p->blocks, token);
85 token = be64_to_cpu(ret);
87 } while (rc == H_BUSY);
90 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
95 static int papr_scm_meta_get(struct papr_scm_priv *p,
96 struct nd_cmd_get_config_data_hdr *hdr)
98 unsigned long data[PLPAR_HCALL_BUFSIZE];
101 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
104 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
107 if (ret == H_PARAMETER) /* bad DRC index */
110 return -EINVAL; /* other invalid parameter */
112 hdr->out_buf[0] = data[0] & 0xff;
117 static int papr_scm_meta_set(struct papr_scm_priv *p,
118 struct nd_cmd_set_config_hdr *hdr)
122 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
125 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA,
126 p->drc_index, hdr->in_offset, hdr->in_buf[0], 1);
128 if (ret == H_PARAMETER) /* bad DRC index */
131 return -EINVAL; /* other invalid parameter */
136 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
137 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
139 struct nd_cmd_get_config_size *get_size_hdr;
140 struct papr_scm_priv *p;
142 /* Only dimm-specific calls are supported atm */
146 p = nvdimm_provider_data(nvdimm);
149 case ND_CMD_GET_CONFIG_SIZE:
152 get_size_hdr->status = 0;
153 get_size_hdr->max_xfer = 1;
154 get_size_hdr->config_size = p->metadata_size;
158 case ND_CMD_GET_CONFIG_DATA:
159 *cmd_rc = papr_scm_meta_get(p, buf);
162 case ND_CMD_SET_CONFIG_DATA:
163 *cmd_rc = papr_scm_meta_set(p, buf);
170 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
175 static const struct attribute_group *region_attr_groups[] = {
176 &nd_region_attribute_group,
177 &nd_device_attribute_group,
178 &nd_mapping_attribute_group,
179 &nd_numa_attribute_group,
183 static const struct attribute_group *bus_attr_groups[] = {
184 &nvdimm_bus_attribute_group,
188 static const struct attribute_group *papr_scm_dimm_groups[] = {
189 &nvdimm_attribute_group,
190 &nd_device_attribute_group,
194 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
196 struct device *dev = &p->pdev->dev;
197 struct nd_mapping_desc mapping;
198 struct nd_region_desc ndr_desc;
199 unsigned long dimm_flags;
201 p->bus_desc.ndctl = papr_scm_ndctl;
202 p->bus_desc.module = THIS_MODULE;
203 p->bus_desc.of_node = p->pdev->dev.of_node;
204 p->bus_desc.attr_groups = bus_attr_groups;
205 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
207 if (!p->bus_desc.provider_name)
210 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
212 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
217 set_bit(NDD_ALIASING, &dimm_flags);
219 p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
220 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
222 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
226 /* now add the region */
228 memset(&mapping, 0, sizeof(mapping));
229 mapping.nvdimm = p->nvdimm;
231 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
233 memset(&ndr_desc, 0, sizeof(ndr_desc));
234 ndr_desc.attr_groups = region_attr_groups;
235 ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
236 ndr_desc.res = &p->res;
237 ndr_desc.of_node = p->dn;
238 ndr_desc.provider_data = p;
239 ndr_desc.mapping = &mapping;
240 ndr_desc.num_mappings = 1;
241 ndr_desc.nd_set = &p->nd_set;
242 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
244 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
246 dev_err(dev, "Error registering region %pR from %pOF\n",
247 ndr_desc.res, p->dn);
253 err: nvdimm_bus_unregister(p->bus);
254 kfree(p->bus_desc.provider_name);
258 static int papr_scm_probe(struct platform_device *pdev)
260 uint32_t drc_index, metadata_size, unit_cap[2];
261 struct device_node *dn = pdev->dev.of_node;
262 struct papr_scm_priv *p;
265 /* check we have all the required DT properties */
266 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
267 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
271 if (of_property_read_u32_array(dn, "ibm,unit-capacity", unit_cap, 2)) {
272 dev_err(&pdev->dev, "%pOF: missing unit-capacity!\n", dn);
276 p = kzalloc(sizeof(*p), GFP_KERNEL);
280 /* optional DT properties */
281 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
284 p->drc_index = drc_index;
285 p->block_size = unit_cap[0];
286 p->blocks = unit_cap[1];
289 p->metadata_size = metadata_size;
292 /* request the hypervisor to bind this region to somewhere in memory */
293 rc = drc_pmem_bind(p);
297 /* setup the resource for the newly bound range */
298 p->res.start = p->bound_addr;
299 p->res.end = p->bound_addr + p->blocks * p->block_size;
300 p->res.name = pdev->name;
301 p->res.flags = IORESOURCE_MEM;
303 rc = papr_scm_nvdimm_init(p);
307 platform_set_drvdata(pdev, p);
311 err2: drc_pmem_unbind(p);
316 static int papr_scm_remove(struct platform_device *pdev)
318 struct papr_scm_priv *p = platform_get_drvdata(pdev);
320 nvdimm_bus_unregister(p->bus);
327 static const struct of_device_id papr_scm_match[] = {
328 { .compatible = "ibm,pmemory" },
332 static struct platform_driver papr_scm_driver = {
333 .probe = papr_scm_probe,
334 .remove = papr_scm_remove,
337 .owner = THIS_MODULE,
338 .of_match_table = papr_scm_match,
342 module_platform_driver(papr_scm_driver);
343 MODULE_DEVICE_TABLE(of, papr_scm_match);
344 MODULE_LICENSE("GPL");
345 MODULE_AUTHOR("IBM Corporation");