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;
35 struct nvdimm_bus_descriptor bus_desc;
36 struct nvdimm_bus *bus;
37 struct nvdimm *nvdimm;
39 struct nd_region *region;
40 struct nd_interleave_set nd_set;
43 static int drc_pmem_bind(struct papr_scm_priv *p)
45 unsigned long ret[PLPAR_HCALL_BUFSIZE];
50 * When the hypervisor cannot map all the requested memory in a single
51 * hcall it returns H_BUSY and we call again with the token until
52 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
53 * leave the system in an undefined state, so we wait.
58 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
59 p->blocks, BIND_ANY_ADDR, token);
64 } while (rc == H_BUSY);
67 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
71 p->bound_addr = saved;
73 dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
78 static int drc_pmem_unbind(struct papr_scm_priv *p)
80 unsigned long ret[PLPAR_HCALL_BUFSIZE];
85 /* NB: unbind has the same retry requirements mentioned above */
87 rc = plpar_hcall(H_SCM_UNBIND_MEM, ret, p->drc_index,
88 p->bound_addr, p->blocks, token);
91 } while (rc == H_BUSY);
94 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
99 static int papr_scm_meta_get(struct papr_scm_priv *p,
100 struct nd_cmd_get_config_data_hdr *hdr)
102 unsigned long data[PLPAR_HCALL_BUFSIZE];
105 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
108 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
111 if (ret == H_PARAMETER) /* bad DRC index */
114 return -EINVAL; /* other invalid parameter */
116 hdr->out_buf[0] = data[0] & 0xff;
121 static int papr_scm_meta_set(struct papr_scm_priv *p,
122 struct nd_cmd_set_config_hdr *hdr)
126 if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
129 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA,
130 p->drc_index, hdr->in_offset, hdr->in_buf[0], 1);
132 if (ret == H_PARAMETER) /* bad DRC index */
135 return -EINVAL; /* other invalid parameter */
140 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
141 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
143 struct nd_cmd_get_config_size *get_size_hdr;
144 struct papr_scm_priv *p;
146 /* Only dimm-specific calls are supported atm */
150 p = nvdimm_provider_data(nvdimm);
153 case ND_CMD_GET_CONFIG_SIZE:
156 get_size_hdr->status = 0;
157 get_size_hdr->max_xfer = 1;
158 get_size_hdr->config_size = p->metadata_size;
162 case ND_CMD_GET_CONFIG_DATA:
163 *cmd_rc = papr_scm_meta_get(p, buf);
166 case ND_CMD_SET_CONFIG_DATA:
167 *cmd_rc = papr_scm_meta_set(p, buf);
174 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
179 static const struct attribute_group *region_attr_groups[] = {
180 &nd_region_attribute_group,
181 &nd_device_attribute_group,
182 &nd_mapping_attribute_group,
183 &nd_numa_attribute_group,
187 static const struct attribute_group *bus_attr_groups[] = {
188 &nvdimm_bus_attribute_group,
192 static const struct attribute_group *papr_scm_dimm_groups[] = {
193 &nvdimm_attribute_group,
194 &nd_device_attribute_group,
198 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
200 struct device *dev = &p->pdev->dev;
201 struct nd_mapping_desc mapping;
202 struct nd_region_desc ndr_desc;
203 unsigned long dimm_flags;
205 p->bus_desc.ndctl = papr_scm_ndctl;
206 p->bus_desc.module = THIS_MODULE;
207 p->bus_desc.of_node = p->pdev->dev.of_node;
208 p->bus_desc.attr_groups = bus_attr_groups;
209 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
211 if (!p->bus_desc.provider_name)
214 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
216 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
221 set_bit(NDD_ALIASING, &dimm_flags);
223 p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
224 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
226 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
230 if (nvdimm_bus_check_dimm_count(p->bus, 1))
233 /* now add the region */
235 memset(&mapping, 0, sizeof(mapping));
236 mapping.nvdimm = p->nvdimm;
238 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
240 memset(&ndr_desc, 0, sizeof(ndr_desc));
241 ndr_desc.attr_groups = region_attr_groups;
242 ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
243 ndr_desc.target_node = ndr_desc.numa_node;
244 ndr_desc.res = &p->res;
245 ndr_desc.of_node = p->dn;
246 ndr_desc.provider_data = p;
247 ndr_desc.mapping = &mapping;
248 ndr_desc.num_mappings = 1;
249 ndr_desc.nd_set = &p->nd_set;
250 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
253 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
255 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
257 dev_err(dev, "Error registering region %pR from %pOF\n",
258 ndr_desc.res, p->dn);
264 err: nvdimm_bus_unregister(p->bus);
265 kfree(p->bus_desc.provider_name);
269 static int papr_scm_probe(struct platform_device *pdev)
271 struct device_node *dn = pdev->dev.of_node;
272 u32 drc_index, metadata_size;
273 u64 blocks, block_size;
274 struct papr_scm_priv *p;
275 const char *uuid_str;
279 /* check we have all the required DT properties */
280 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
281 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
285 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
286 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
290 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
291 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
295 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
296 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
301 p = kzalloc(sizeof(*p), GFP_KERNEL);
305 /* optional DT properties */
306 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
309 p->drc_index = drc_index;
310 p->block_size = block_size;
312 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
314 /* We just need to ensure that set cookies are unique across */
315 uuid_parse(uuid_str, (uuid_t *) uuid);
316 p->nd_set.cookie1 = uuid[0];
317 p->nd_set.cookie2 = uuid[1];
320 p->metadata_size = metadata_size;
323 /* request the hypervisor to bind this region to somewhere in memory */
324 rc = drc_pmem_bind(p);
328 /* setup the resource for the newly bound range */
329 p->res.start = p->bound_addr;
330 p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
331 p->res.name = pdev->name;
332 p->res.flags = IORESOURCE_MEM;
334 rc = papr_scm_nvdimm_init(p);
338 platform_set_drvdata(pdev, p);
342 err2: drc_pmem_unbind(p);
347 static int papr_scm_remove(struct platform_device *pdev)
349 struct papr_scm_priv *p = platform_get_drvdata(pdev);
351 nvdimm_bus_unregister(p->bus);
358 static const struct of_device_id papr_scm_match[] = {
359 { .compatible = "ibm,pmemory" },
363 static struct platform_driver papr_scm_driver = {
364 .probe = papr_scm_probe,
365 .remove = papr_scm_remove,
368 .owner = THIS_MODULE,
369 .of_match_table = papr_scm_match,
373 module_platform_driver(papr_scm_driver);
374 MODULE_DEVICE_TABLE(of, papr_scm_match);
375 MODULE_LICENSE("GPL");
376 MODULE_AUTHOR("IBM Corporation");