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];
49 * When the hypervisor cannot map all the requested memory in a single
50 * hcall it returns H_BUSY and we call again with the token until
51 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
52 * leave the system in an undefined state, so we wait.
57 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
58 p->blocks, BIND_ANY_ADDR, token);
63 } while (rc == H_BUSY);
66 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
70 p->bound_addr = saved;
72 dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
77 static int drc_pmem_unbind(struct papr_scm_priv *p)
79 unsigned long ret[PLPAR_HCALL_BUFSIZE];
84 /* NB: unbind has the same retry requirements mentioned above */
86 rc = plpar_hcall(H_SCM_UNBIND_MEM, ret, p->drc_index,
87 p->bound_addr, p->blocks, token);
90 } while (rc == H_BUSY);
93 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
98 static int papr_scm_meta_get(struct papr_scm_priv *p,
99 struct nd_cmd_get_config_data_hdr *hdr)
101 unsigned long data[PLPAR_HCALL_BUFSIZE];
104 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
107 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
110 if (ret == H_PARAMETER) /* bad DRC index */
113 return -EINVAL; /* other invalid parameter */
115 hdr->out_buf[0] = data[0] & 0xff;
120 static int papr_scm_meta_set(struct papr_scm_priv *p,
121 struct nd_cmd_set_config_hdr *hdr)
125 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
128 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA,
129 p->drc_index, hdr->in_offset, hdr->in_buf[0], 1);
131 if (ret == H_PARAMETER) /* bad DRC index */
134 return -EINVAL; /* other invalid parameter */
139 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
140 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
142 struct nd_cmd_get_config_size *get_size_hdr;
143 struct papr_scm_priv *p;
145 /* Only dimm-specific calls are supported atm */
149 p = nvdimm_provider_data(nvdimm);
152 case ND_CMD_GET_CONFIG_SIZE:
155 get_size_hdr->status = 0;
156 get_size_hdr->max_xfer = 1;
157 get_size_hdr->config_size = p->metadata_size;
161 case ND_CMD_GET_CONFIG_DATA:
162 *cmd_rc = papr_scm_meta_get(p, buf);
165 case ND_CMD_SET_CONFIG_DATA:
166 *cmd_rc = papr_scm_meta_set(p, buf);
173 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
178 static const struct attribute_group *region_attr_groups[] = {
179 &nd_region_attribute_group,
180 &nd_device_attribute_group,
181 &nd_mapping_attribute_group,
182 &nd_numa_attribute_group,
186 static const struct attribute_group *bus_attr_groups[] = {
187 &nvdimm_bus_attribute_group,
191 static const struct attribute_group *papr_scm_dimm_groups[] = {
192 &nvdimm_attribute_group,
193 &nd_device_attribute_group,
197 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
199 struct device *dev = &p->pdev->dev;
200 struct nd_mapping_desc mapping;
201 struct nd_region_desc ndr_desc;
202 unsigned long dimm_flags;
204 p->bus_desc.ndctl = papr_scm_ndctl;
205 p->bus_desc.module = THIS_MODULE;
206 p->bus_desc.of_node = p->pdev->dev.of_node;
207 p->bus_desc.attr_groups = bus_attr_groups;
208 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
210 if (!p->bus_desc.provider_name)
213 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
215 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
220 set_bit(NDD_ALIASING, &dimm_flags);
222 p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
223 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
225 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
229 if (nvdimm_bus_check_dimm_count(p->bus, 1))
232 /* now add the region */
234 memset(&mapping, 0, sizeof(mapping));
235 mapping.nvdimm = p->nvdimm;
237 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
239 memset(&ndr_desc, 0, sizeof(ndr_desc));
240 ndr_desc.attr_groups = region_attr_groups;
241 ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
242 ndr_desc.res = &p->res;
243 ndr_desc.of_node = p->dn;
244 ndr_desc.provider_data = p;
245 ndr_desc.mapping = &mapping;
246 ndr_desc.num_mappings = 1;
247 ndr_desc.nd_set = &p->nd_set;
248 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
250 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
252 dev_err(dev, "Error registering region %pR from %pOF\n",
253 ndr_desc.res, p->dn);
259 err: nvdimm_bus_unregister(p->bus);
260 kfree(p->bus_desc.provider_name);
264 static int papr_scm_probe(struct platform_device *pdev)
266 struct device_node *dn = pdev->dev.of_node;
267 u32 drc_index, metadata_size;
268 u64 blocks, block_size;
269 struct papr_scm_priv *p;
270 const char *uuid_str;
274 /* check we have all the required DT properties */
275 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
276 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
280 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
281 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
285 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
286 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
290 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
291 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
295 p = kzalloc(sizeof(*p), GFP_KERNEL);
299 /* optional DT properties */
300 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
303 p->drc_index = drc_index;
304 p->block_size = block_size;
307 /* We just need to ensure that set cookies are unique across */
308 uuid_parse(uuid_str, (uuid_t *) uuid);
309 p->nd_set.cookie1 = uuid[0];
310 p->nd_set.cookie2 = uuid[1];
313 p->metadata_size = metadata_size;
316 /* request the hypervisor to bind this region to somewhere in memory */
317 rc = drc_pmem_bind(p);
321 /* setup the resource for the newly bound range */
322 p->res.start = p->bound_addr;
323 p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
324 p->res.name = pdev->name;
325 p->res.flags = IORESOURCE_MEM;
327 rc = papr_scm_nvdimm_init(p);
331 platform_set_drvdata(pdev, p);
335 err2: drc_pmem_unbind(p);
340 static int papr_scm_remove(struct platform_device *pdev)
342 struct papr_scm_priv *p = platform_get_drvdata(pdev);
344 nvdimm_bus_unregister(p->bus);
351 static const struct of_device_id papr_scm_match[] = {
352 { .compatible = "ibm,pmemory" },
356 static struct platform_driver papr_scm_driver = {
357 .probe = papr_scm_probe,
358 .remove = papr_scm_remove,
361 .owner = THIS_MODULE,
362 .of_match_table = papr_scm_match,
366 module_platform_driver(papr_scm_driver);
367 MODULE_DEVICE_TABLE(of, papr_scm_match);
368 MODULE_LICENSE("GPL");
369 MODULE_AUTHOR("IBM Corporation");