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 %pR\n", p->drc_index, &p->res);
76 static void drc_pmem_unbind(struct papr_scm_priv *p)
78 unsigned long ret[PLPAR_HCALL_BUFSIZE];
82 dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
84 /* NB: unbind has the same retry requirements as drc_pmem_bind() */
87 /* Unbind of all SCM resources associated with drcIndex */
88 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
92 /* Check if we are stalled for some time */
93 if (H_IS_LONG_BUSY(rc)) {
94 msleep(get_longbusy_msecs(rc));
96 } else if (rc == H_BUSY) {
100 } while (rc == H_BUSY);
103 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
105 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
111 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
113 unsigned long start_addr;
114 unsigned long end_addr;
115 unsigned long ret[PLPAR_HCALL_BUFSIZE];
119 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
125 /* Make sure the full region is bound. */
126 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
127 p->drc_index, p->blocks - 1);
132 if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
135 p->bound_addr = start_addr;
136 dev_dbg(&p->pdev->dev, "bound drc 0x%x to %pR\n", p->drc_index, &p->res);
140 dev_info(&p->pdev->dev,
141 "Failed to query, trying an unbind followed by bind");
143 return drc_pmem_bind(p);
147 static int papr_scm_meta_get(struct papr_scm_priv *p,
148 struct nd_cmd_get_config_data_hdr *hdr)
150 unsigned long data[PLPAR_HCALL_BUFSIZE];
151 unsigned long offset, data_offset;
155 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
158 for (len = hdr->in_length; len; len -= read) {
160 data_offset = hdr->in_length - len;
161 offset = hdr->in_offset + data_offset;
172 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
175 if (ret == H_PARAMETER) /* bad DRC index */
178 return -EINVAL; /* other invalid parameter */
182 *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
185 *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
189 *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
193 *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
200 static int papr_scm_meta_set(struct papr_scm_priv *p,
201 struct nd_cmd_set_config_hdr *hdr)
203 unsigned long offset, data_offset;
209 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
212 for (len = hdr->in_length; len; len -= wrote) {
214 data_offset = hdr->in_length - len;
215 offset = hdr->in_offset + data_offset;
218 data = *(uint64_t *)(hdr->in_buf + data_offset);
219 data_be = cpu_to_be64(data);
221 } else if (len >= 4) {
222 data = *(uint32_t *)(hdr->in_buf + data_offset);
224 data_be = cpu_to_be32(data);
226 } else if (len >= 2) {
227 data = *(uint16_t *)(hdr->in_buf + data_offset);
229 data_be = cpu_to_be16(data);
232 data_be = *(uint8_t *)(hdr->in_buf + data_offset);
237 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
238 offset, data_be, wrote);
239 if (ret == H_PARAMETER) /* bad DRC index */
242 return -EINVAL; /* other invalid parameter */
248 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
249 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
251 struct nd_cmd_get_config_size *get_size_hdr;
252 struct papr_scm_priv *p;
254 /* Only dimm-specific calls are supported atm */
258 p = nvdimm_provider_data(nvdimm);
261 case ND_CMD_GET_CONFIG_SIZE:
264 get_size_hdr->status = 0;
265 get_size_hdr->max_xfer = 8;
266 get_size_hdr->config_size = p->metadata_size;
270 case ND_CMD_GET_CONFIG_DATA:
271 *cmd_rc = papr_scm_meta_get(p, buf);
274 case ND_CMD_SET_CONFIG_DATA:
275 *cmd_rc = papr_scm_meta_set(p, buf);
282 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
287 static const struct attribute_group *region_attr_groups[] = {
288 &nd_region_attribute_group,
289 &nd_device_attribute_group,
290 &nd_mapping_attribute_group,
291 &nd_numa_attribute_group,
295 static const struct attribute_group *bus_attr_groups[] = {
296 &nvdimm_bus_attribute_group,
300 static const struct attribute_group *papr_scm_dimm_groups[] = {
301 &nvdimm_attribute_group,
302 &nd_device_attribute_group,
306 static inline int papr_scm_node(int node)
308 int min_dist = INT_MAX, dist;
311 if ((node == NUMA_NO_NODE) || node_online(node))
314 min_node = first_online_node;
315 for_each_online_node(nid) {
316 dist = node_distance(node, nid);
317 if (dist < min_dist) {
325 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
327 struct device *dev = &p->pdev->dev;
328 struct nd_mapping_desc mapping;
329 struct nd_region_desc ndr_desc;
330 unsigned long dimm_flags;
331 int target_nid, online_nid;
333 p->bus_desc.ndctl = papr_scm_ndctl;
334 p->bus_desc.module = THIS_MODULE;
335 p->bus_desc.of_node = p->pdev->dev.of_node;
336 p->bus_desc.attr_groups = bus_attr_groups;
337 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
339 if (!p->bus_desc.provider_name)
342 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
344 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
345 kfree(p->bus_desc.provider_name);
350 set_bit(NDD_ALIASING, &dimm_flags);
352 p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
353 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
355 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
359 if (nvdimm_bus_check_dimm_count(p->bus, 1))
362 /* now add the region */
364 memset(&mapping, 0, sizeof(mapping));
365 mapping.nvdimm = p->nvdimm;
367 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
369 memset(&ndr_desc, 0, sizeof(ndr_desc));
370 ndr_desc.attr_groups = region_attr_groups;
371 target_nid = dev_to_node(&p->pdev->dev);
372 online_nid = papr_scm_node(target_nid);
373 ndr_desc.numa_node = online_nid;
374 ndr_desc.target_node = target_nid;
375 ndr_desc.res = &p->res;
376 ndr_desc.of_node = p->dn;
377 ndr_desc.provider_data = p;
378 ndr_desc.mapping = &mapping;
379 ndr_desc.num_mappings = 1;
380 ndr_desc.nd_set = &p->nd_set;
381 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
384 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
386 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
388 dev_err(dev, "Error registering region %pR from %pOF\n",
389 ndr_desc.res, p->dn);
392 if (target_nid != online_nid)
393 dev_info(dev, "Region registered with target node %d and online node %d",
394 target_nid, online_nid);
398 err: nvdimm_bus_unregister(p->bus);
399 kfree(p->bus_desc.provider_name);
403 static int papr_scm_probe(struct platform_device *pdev)
405 struct device_node *dn = pdev->dev.of_node;
406 u32 drc_index, metadata_size;
407 u64 blocks, block_size;
408 struct papr_scm_priv *p;
409 const char *uuid_str;
413 /* check we have all the required DT properties */
414 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
415 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
419 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
420 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
424 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
425 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
429 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
430 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
435 p = kzalloc(sizeof(*p), GFP_KERNEL);
439 /* optional DT properties */
440 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
443 p->drc_index = drc_index;
444 p->block_size = block_size;
446 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
448 /* We just need to ensure that set cookies are unique across */
449 uuid_parse(uuid_str, (uuid_t *) uuid);
451 * cookie1 and cookie2 are not really little endian
452 * we store a little endian representation of the
453 * uuid str so that we can compare this with the label
454 * area cookie irrespective of the endian config with which
455 * the kernel is built.
457 p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
458 p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
461 p->metadata_size = metadata_size;
464 /* request the hypervisor to bind this region to somewhere in memory */
465 rc = drc_pmem_bind(p);
467 /* If phyp says drc memory still bound then force unbound and retry */
469 rc = drc_pmem_query_n_bind(p);
471 if (rc != H_SUCCESS) {
472 dev_err(&p->pdev->dev, "bind err: %d\n", rc);
477 /* setup the resource for the newly bound range */
478 p->res.start = p->bound_addr;
479 p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
480 p->res.name = pdev->name;
481 p->res.flags = IORESOURCE_MEM;
483 rc = papr_scm_nvdimm_init(p);
487 platform_set_drvdata(pdev, p);
491 err2: drc_pmem_unbind(p);
496 static int papr_scm_remove(struct platform_device *pdev)
498 struct papr_scm_priv *p = platform_get_drvdata(pdev);
500 nvdimm_bus_unregister(p->bus);
502 kfree(p->bus_desc.provider_name);
508 static const struct of_device_id papr_scm_match[] = {
509 { .compatible = "ibm,pmemory" },
513 static struct platform_driver papr_scm_driver = {
514 .probe = papr_scm_probe,
515 .remove = papr_scm_remove,
518 .owner = THIS_MODULE,
519 .of_match_table = papr_scm_match,
523 module_platform_driver(papr_scm_driver);
524 MODULE_DEVICE_TABLE(of, papr_scm_match);
525 MODULE_LICENSE("GPL");
526 MODULE_AUTHOR("IBM Corporation");