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>
14 #include <linux/delay.h>
16 #include <asm/plpar_wrappers.h>
18 #define BIND_ANY_ADDR (~0ul)
20 #define PAPR_SCM_DIMM_CMD_MASK \
21 ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
22 (1ul << ND_CMD_GET_CONFIG_DATA) | \
23 (1ul << ND_CMD_SET_CONFIG_DATA))
25 struct papr_scm_priv {
26 struct platform_device *pdev;
27 struct device_node *dn;
36 struct nvdimm_bus_descriptor bus_desc;
37 struct nvdimm_bus *bus;
38 struct nvdimm *nvdimm;
40 struct nd_region *region;
41 struct nd_interleave_set nd_set;
44 static int drc_pmem_bind(struct papr_scm_priv *p)
46 unsigned long ret[PLPAR_HCALL_BUFSIZE];
52 * When the hypervisor cannot map all the requested memory in a single
53 * hcall it returns H_BUSY and we call again with the token until
54 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
55 * leave the system in an undefined state, so we wait.
60 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
61 p->blocks, BIND_ANY_ADDR, token);
66 } while (rc == H_BUSY);
71 p->bound_addr = saved;
72 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
73 p->drc_index, (unsigned long)saved);
77 static void drc_pmem_unbind(struct papr_scm_priv *p)
79 unsigned long ret[PLPAR_HCALL_BUFSIZE];
83 dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
85 /* NB: unbind has the same retry requirements as drc_pmem_bind() */
88 /* Unbind of all SCM resources associated with drcIndex */
89 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
93 /* Check if we are stalled for some time */
94 if (H_IS_LONG_BUSY(rc)) {
95 msleep(get_longbusy_msecs(rc));
97 } else if (rc == H_BUSY) {
101 } while (rc == H_BUSY);
104 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
106 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
112 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
114 unsigned long start_addr;
115 unsigned long end_addr;
116 unsigned long ret[PLPAR_HCALL_BUFSIZE];
120 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
126 /* Make sure the full region is bound. */
127 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
128 p->drc_index, p->blocks - 1);
133 if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
136 p->bound_addr = start_addr;
137 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
141 dev_info(&p->pdev->dev,
142 "Failed to query, trying an unbind followed by bind");
144 return drc_pmem_bind(p);
148 static int papr_scm_meta_get(struct papr_scm_priv *p,
149 struct nd_cmd_get_config_data_hdr *hdr)
151 unsigned long data[PLPAR_HCALL_BUFSIZE];
152 unsigned long offset, data_offset;
156 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
159 for (len = hdr->in_length; len; len -= read) {
161 data_offset = hdr->in_length - len;
162 offset = hdr->in_offset + data_offset;
173 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
176 if (ret == H_PARAMETER) /* bad DRC index */
179 return -EINVAL; /* other invalid parameter */
183 *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
186 *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
190 *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
194 *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
201 static int papr_scm_meta_set(struct papr_scm_priv *p,
202 struct nd_cmd_set_config_hdr *hdr)
204 unsigned long offset, data_offset;
210 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
213 for (len = hdr->in_length; len; len -= wrote) {
215 data_offset = hdr->in_length - len;
216 offset = hdr->in_offset + data_offset;
219 data = *(uint64_t *)(hdr->in_buf + data_offset);
220 data_be = cpu_to_be64(data);
222 } else if (len >= 4) {
223 data = *(uint32_t *)(hdr->in_buf + data_offset);
225 data_be = cpu_to_be32(data);
227 } else if (len >= 2) {
228 data = *(uint16_t *)(hdr->in_buf + data_offset);
230 data_be = cpu_to_be16(data);
233 data_be = *(uint8_t *)(hdr->in_buf + data_offset);
238 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
239 offset, data_be, wrote);
240 if (ret == H_PARAMETER) /* bad DRC index */
243 return -EINVAL; /* other invalid parameter */
249 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
250 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
252 struct nd_cmd_get_config_size *get_size_hdr;
253 struct papr_scm_priv *p;
255 /* Only dimm-specific calls are supported atm */
259 p = nvdimm_provider_data(nvdimm);
262 case ND_CMD_GET_CONFIG_SIZE:
265 get_size_hdr->status = 0;
266 get_size_hdr->max_xfer = 8;
267 get_size_hdr->config_size = p->metadata_size;
271 case ND_CMD_GET_CONFIG_DATA:
272 *cmd_rc = papr_scm_meta_get(p, buf);
275 case ND_CMD_SET_CONFIG_DATA:
276 *cmd_rc = papr_scm_meta_set(p, buf);
283 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
288 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
290 struct device *dev = &p->pdev->dev;
291 struct nd_mapping_desc mapping;
292 struct nd_region_desc ndr_desc;
293 unsigned long dimm_flags;
294 int target_nid, online_nid;
296 p->bus_desc.ndctl = papr_scm_ndctl;
297 p->bus_desc.module = THIS_MODULE;
298 p->bus_desc.of_node = p->pdev->dev.of_node;
299 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
301 if (!p->bus_desc.provider_name)
304 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
306 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
307 kfree(p->bus_desc.provider_name);
312 set_bit(NDD_LABELING, &dimm_flags);
314 p->nvdimm = nvdimm_create(p->bus, p, NULL, dimm_flags,
315 PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
317 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
321 if (nvdimm_bus_check_dimm_count(p->bus, 1))
324 /* now add the region */
326 memset(&mapping, 0, sizeof(mapping));
327 mapping.nvdimm = p->nvdimm;
329 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
331 memset(&ndr_desc, 0, sizeof(ndr_desc));
332 target_nid = dev_to_node(&p->pdev->dev);
333 online_nid = numa_map_to_online_node(target_nid);
334 ndr_desc.numa_node = online_nid;
335 ndr_desc.target_node = target_nid;
336 ndr_desc.res = &p->res;
337 ndr_desc.of_node = p->dn;
338 ndr_desc.provider_data = p;
339 ndr_desc.mapping = &mapping;
340 ndr_desc.num_mappings = 1;
341 ndr_desc.nd_set = &p->nd_set;
344 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
346 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
347 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
350 dev_err(dev, "Error registering region %pR from %pOF\n",
351 ndr_desc.res, p->dn);
354 if (target_nid != online_nid)
355 dev_info(dev, "Region registered with target node %d and online node %d",
356 target_nid, online_nid);
360 err: nvdimm_bus_unregister(p->bus);
361 kfree(p->bus_desc.provider_name);
365 static int papr_scm_probe(struct platform_device *pdev)
367 struct device_node *dn = pdev->dev.of_node;
368 u32 drc_index, metadata_size;
369 u64 blocks, block_size;
370 struct papr_scm_priv *p;
371 const char *uuid_str;
375 /* check we have all the required DT properties */
376 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
377 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
381 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
382 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
386 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
387 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
391 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
392 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
397 p = kzalloc(sizeof(*p), GFP_KERNEL);
401 /* optional DT properties */
402 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
405 p->drc_index = drc_index;
406 p->block_size = block_size;
408 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
410 /* We just need to ensure that set cookies are unique across */
411 uuid_parse(uuid_str, (uuid_t *) uuid);
413 * cookie1 and cookie2 are not really little endian
414 * we store a little endian representation of the
415 * uuid str so that we can compare this with the label
416 * area cookie irrespective of the endian config with which
417 * the kernel is built.
419 p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
420 p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
423 p->metadata_size = metadata_size;
426 /* request the hypervisor to bind this region to somewhere in memory */
427 rc = drc_pmem_bind(p);
429 /* If phyp says drc memory still bound then force unbound and retry */
431 rc = drc_pmem_query_n_bind(p);
433 if (rc != H_SUCCESS) {
434 dev_err(&p->pdev->dev, "bind err: %d\n", rc);
439 /* setup the resource for the newly bound range */
440 p->res.start = p->bound_addr;
441 p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
442 p->res.name = pdev->name;
443 p->res.flags = IORESOURCE_MEM;
445 rc = papr_scm_nvdimm_init(p);
449 platform_set_drvdata(pdev, p);
453 err2: drc_pmem_unbind(p);
458 static int papr_scm_remove(struct platform_device *pdev)
460 struct papr_scm_priv *p = platform_get_drvdata(pdev);
462 nvdimm_bus_unregister(p->bus);
464 kfree(p->bus_desc.provider_name);
470 static const struct of_device_id papr_scm_match[] = {
471 { .compatible = "ibm,pmemory" },
475 static struct platform_driver papr_scm_driver = {
476 .probe = papr_scm_probe,
477 .remove = papr_scm_remove,
480 .of_match_table = papr_scm_match,
484 module_platform_driver(papr_scm_driver);
485 MODULE_DEVICE_TABLE(of, papr_scm_match);
486 MODULE_LICENSE("GPL");
487 MODULE_AUTHOR("IBM Corporation");