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>
15 #include <linux/seq_buf.h>
17 #include <asm/plpar_wrappers.h>
19 #define BIND_ANY_ADDR (~0ul)
21 #define PAPR_SCM_DIMM_CMD_MASK \
22 ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
23 (1ul << ND_CMD_GET_CONFIG_DATA) | \
24 (1ul << ND_CMD_SET_CONFIG_DATA))
26 /* DIMM health bitmap bitmap indicators */
27 /* SCM device is unable to persist memory contents */
28 #define PAPR_PMEM_UNARMED (1ULL << (63 - 0))
29 /* SCM device failed to persist memory contents */
30 #define PAPR_PMEM_SHUTDOWN_DIRTY (1ULL << (63 - 1))
31 /* SCM device contents are persisted from previous IPL */
32 #define PAPR_PMEM_SHUTDOWN_CLEAN (1ULL << (63 - 2))
33 /* SCM device contents are not persisted from previous IPL */
34 #define PAPR_PMEM_EMPTY (1ULL << (63 - 3))
35 /* SCM device memory life remaining is critically low */
36 #define PAPR_PMEM_HEALTH_CRITICAL (1ULL << (63 - 4))
37 /* SCM device will be garded off next IPL due to failure */
38 #define PAPR_PMEM_HEALTH_FATAL (1ULL << (63 - 5))
39 /* SCM contents cannot persist due to current platform health status */
40 #define PAPR_PMEM_HEALTH_UNHEALTHY (1ULL << (63 - 6))
41 /* SCM device is unable to persist memory contents in certain conditions */
42 #define PAPR_PMEM_HEALTH_NON_CRITICAL (1ULL << (63 - 7))
43 /* SCM device is encrypted */
44 #define PAPR_PMEM_ENCRYPTED (1ULL << (63 - 8))
45 /* SCM device has been scrubbed and locked */
46 #define PAPR_PMEM_SCRUBBED_AND_LOCKED (1ULL << (63 - 9))
48 /* Bits status indicators for health bitmap indicating unarmed dimm */
49 #define PAPR_PMEM_UNARMED_MASK (PAPR_PMEM_UNARMED | \
50 PAPR_PMEM_HEALTH_UNHEALTHY)
52 /* Bits status indicators for health bitmap indicating unflushed dimm */
53 #define PAPR_PMEM_BAD_SHUTDOWN_MASK (PAPR_PMEM_SHUTDOWN_DIRTY)
55 /* Bits status indicators for health bitmap indicating unrestored dimm */
56 #define PAPR_PMEM_BAD_RESTORE_MASK (PAPR_PMEM_EMPTY)
58 /* Bit status indicators for smart event notification */
59 #define PAPR_PMEM_SMART_EVENT_MASK (PAPR_PMEM_HEALTH_CRITICAL | \
60 PAPR_PMEM_HEALTH_FATAL | \
61 PAPR_PMEM_HEALTH_UNHEALTHY)
63 /* private struct associated with each region */
64 struct papr_scm_priv {
65 struct platform_device *pdev;
66 struct device_node *dn;
75 struct nvdimm_bus_descriptor bus_desc;
76 struct nvdimm_bus *bus;
77 struct nvdimm *nvdimm;
79 struct nd_region *region;
80 struct nd_interleave_set nd_set;
82 /* Protect dimm health data from concurrent read/writes */
83 struct mutex health_mutex;
85 /* Last time the health information of the dimm was updated */
86 unsigned long lasthealth_jiffies;
88 /* Health information for the dimm */
92 static int drc_pmem_bind(struct papr_scm_priv *p)
94 unsigned long ret[PLPAR_HCALL_BUFSIZE];
100 * When the hypervisor cannot map all the requested memory in a single
101 * hcall it returns H_BUSY and we call again with the token until
102 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
103 * leave the system in an undefined state, so we wait.
108 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
109 p->blocks, BIND_ANY_ADDR, token);
114 } while (rc == H_BUSY);
119 p->bound_addr = saved;
120 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
121 p->drc_index, (unsigned long)saved);
125 static void drc_pmem_unbind(struct papr_scm_priv *p)
127 unsigned long ret[PLPAR_HCALL_BUFSIZE];
131 dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
133 /* NB: unbind has the same retry requirements as drc_pmem_bind() */
136 /* Unbind of all SCM resources associated with drcIndex */
137 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
138 p->drc_index, token);
141 /* Check if we are stalled for some time */
142 if (H_IS_LONG_BUSY(rc)) {
143 msleep(get_longbusy_msecs(rc));
145 } else if (rc == H_BUSY) {
149 } while (rc == H_BUSY);
152 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
154 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
160 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
162 unsigned long start_addr;
163 unsigned long end_addr;
164 unsigned long ret[PLPAR_HCALL_BUFSIZE];
168 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
174 /* Make sure the full region is bound. */
175 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
176 p->drc_index, p->blocks - 1);
181 if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
184 p->bound_addr = start_addr;
185 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
189 dev_info(&p->pdev->dev,
190 "Failed to query, trying an unbind followed by bind");
192 return drc_pmem_bind(p);
196 * Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
197 * health information.
199 static int __drc_pmem_query_health(struct papr_scm_priv *p)
201 unsigned long ret[PLPAR_HCALL_BUFSIZE];
204 /* issue the hcall */
205 rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);
206 if (rc != H_SUCCESS) {
207 dev_err(&p->pdev->dev,
208 "Failed to query health information, Err:%ld\n", rc);
212 p->lasthealth_jiffies = jiffies;
213 p->health_bitmap = ret[0] & ret[1];
215 dev_dbg(&p->pdev->dev,
216 "Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",
222 /* Min interval in seconds for assuming stable dimm health */
223 #define MIN_HEALTH_QUERY_INTERVAL 60
225 /* Query cached health info and if needed call drc_pmem_query_health */
226 static int drc_pmem_query_health(struct papr_scm_priv *p)
228 unsigned long cache_timeout;
231 /* Protect concurrent modifications to papr_scm_priv */
232 rc = mutex_lock_interruptible(&p->health_mutex);
236 /* Jiffies offset for which the health data is assumed to be same */
237 cache_timeout = p->lasthealth_jiffies +
238 msecs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL * 1000);
240 /* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */
241 if (time_after(jiffies, cache_timeout))
242 rc = __drc_pmem_query_health(p);
244 /* Assume cached health data is valid */
247 mutex_unlock(&p->health_mutex);
251 static int papr_scm_meta_get(struct papr_scm_priv *p,
252 struct nd_cmd_get_config_data_hdr *hdr)
254 unsigned long data[PLPAR_HCALL_BUFSIZE];
255 unsigned long offset, data_offset;
259 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
262 for (len = hdr->in_length; len; len -= read) {
264 data_offset = hdr->in_length - len;
265 offset = hdr->in_offset + data_offset;
276 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
279 if (ret == H_PARAMETER) /* bad DRC index */
282 return -EINVAL; /* other invalid parameter */
286 *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
289 *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
293 *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
297 *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
304 static int papr_scm_meta_set(struct papr_scm_priv *p,
305 struct nd_cmd_set_config_hdr *hdr)
307 unsigned long offset, data_offset;
313 if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
316 for (len = hdr->in_length; len; len -= wrote) {
318 data_offset = hdr->in_length - len;
319 offset = hdr->in_offset + data_offset;
322 data = *(uint64_t *)(hdr->in_buf + data_offset);
323 data_be = cpu_to_be64(data);
325 } else if (len >= 4) {
326 data = *(uint32_t *)(hdr->in_buf + data_offset);
328 data_be = cpu_to_be32(data);
330 } else if (len >= 2) {
331 data = *(uint16_t *)(hdr->in_buf + data_offset);
333 data_be = cpu_to_be16(data);
336 data_be = *(uint8_t *)(hdr->in_buf + data_offset);
341 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
342 offset, data_be, wrote);
343 if (ret == H_PARAMETER) /* bad DRC index */
346 return -EINVAL; /* other invalid parameter */
352 static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,
353 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
354 unsigned int buf_len, int *cmd_rc)
356 struct nd_cmd_get_config_size *get_size_hdr;
357 struct papr_scm_priv *p;
359 /* Only dimm-specific calls are supported atm */
363 p = nvdimm_provider_data(nvdimm);
366 case ND_CMD_GET_CONFIG_SIZE:
369 get_size_hdr->status = 0;
370 get_size_hdr->max_xfer = 8;
371 get_size_hdr->config_size = p->metadata_size;
375 case ND_CMD_GET_CONFIG_DATA:
376 *cmd_rc = papr_scm_meta_get(p, buf);
379 case ND_CMD_SET_CONFIG_DATA:
380 *cmd_rc = papr_scm_meta_set(p, buf);
387 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
392 static ssize_t flags_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct nvdimm *dimm = to_nvdimm(dev);
396 struct papr_scm_priv *p = nvdimm_provider_data(dimm);
401 rc = drc_pmem_query_health(p);
405 /* Copy health_bitmap locally, check masks & update out buffer */
406 health = READ_ONCE(p->health_bitmap);
408 seq_buf_init(&s, buf, PAGE_SIZE);
409 if (health & PAPR_PMEM_UNARMED_MASK)
410 seq_buf_printf(&s, "not_armed ");
412 if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)
413 seq_buf_printf(&s, "flush_fail ");
415 if (health & PAPR_PMEM_BAD_RESTORE_MASK)
416 seq_buf_printf(&s, "restore_fail ");
418 if (health & PAPR_PMEM_ENCRYPTED)
419 seq_buf_printf(&s, "encrypted ");
421 if (health & PAPR_PMEM_SMART_EVENT_MASK)
422 seq_buf_printf(&s, "smart_notify ");
424 if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)
425 seq_buf_printf(&s, "scrubbed locked ");
427 if (seq_buf_used(&s))
428 seq_buf_printf(&s, "\n");
430 return seq_buf_used(&s);
432 DEVICE_ATTR_RO(flags);
434 /* papr_scm specific dimm attributes */
435 static struct attribute *papr_nd_attributes[] = {
436 &dev_attr_flags.attr,
440 static struct attribute_group papr_nd_attribute_group = {
442 .attrs = papr_nd_attributes,
445 static const struct attribute_group *papr_nd_attr_groups[] = {
446 &papr_nd_attribute_group,
450 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
452 struct device *dev = &p->pdev->dev;
453 struct nd_mapping_desc mapping;
454 struct nd_region_desc ndr_desc;
455 unsigned long dimm_flags;
456 int target_nid, online_nid;
458 p->bus_desc.ndctl = papr_scm_ndctl;
459 p->bus_desc.module = THIS_MODULE;
460 p->bus_desc.of_node = p->pdev->dev.of_node;
461 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
463 if (!p->bus_desc.provider_name)
466 p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
468 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
469 kfree(p->bus_desc.provider_name);
474 set_bit(NDD_LABELING, &dimm_flags);
476 p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,
477 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
479 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
483 if (nvdimm_bus_check_dimm_count(p->bus, 1))
486 /* now add the region */
488 memset(&mapping, 0, sizeof(mapping));
489 mapping.nvdimm = p->nvdimm;
491 mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
493 memset(&ndr_desc, 0, sizeof(ndr_desc));
494 target_nid = dev_to_node(&p->pdev->dev);
495 online_nid = numa_map_to_online_node(target_nid);
496 ndr_desc.numa_node = online_nid;
497 ndr_desc.target_node = target_nid;
498 ndr_desc.res = &p->res;
499 ndr_desc.of_node = p->dn;
500 ndr_desc.provider_data = p;
501 ndr_desc.mapping = &mapping;
502 ndr_desc.num_mappings = 1;
503 ndr_desc.nd_set = &p->nd_set;
506 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
508 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
509 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
512 dev_err(dev, "Error registering region %pR from %pOF\n",
513 ndr_desc.res, p->dn);
516 if (target_nid != online_nid)
517 dev_info(dev, "Region registered with target node %d and online node %d",
518 target_nid, online_nid);
522 err: nvdimm_bus_unregister(p->bus);
523 kfree(p->bus_desc.provider_name);
527 static int papr_scm_probe(struct platform_device *pdev)
529 struct device_node *dn = pdev->dev.of_node;
530 u32 drc_index, metadata_size;
531 u64 blocks, block_size;
532 struct papr_scm_priv *p;
533 const char *uuid_str;
537 /* check we have all the required DT properties */
538 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
539 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
543 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
544 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
548 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
549 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
553 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
554 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
559 p = kzalloc(sizeof(*p), GFP_KERNEL);
563 /* Initialize the dimm mutex */
564 mutex_init(&p->health_mutex);
566 /* optional DT properties */
567 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
570 p->drc_index = drc_index;
571 p->block_size = block_size;
573 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
575 /* We just need to ensure that set cookies are unique across */
576 uuid_parse(uuid_str, (uuid_t *) uuid);
578 * cookie1 and cookie2 are not really little endian
579 * we store a little endian representation of the
580 * uuid str so that we can compare this with the label
581 * area cookie irrespective of the endian config with which
582 * the kernel is built.
584 p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
585 p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
588 p->metadata_size = metadata_size;
591 /* request the hypervisor to bind this region to somewhere in memory */
592 rc = drc_pmem_bind(p);
594 /* If phyp says drc memory still bound then force unbound and retry */
596 rc = drc_pmem_query_n_bind(p);
598 if (rc != H_SUCCESS) {
599 dev_err(&p->pdev->dev, "bind err: %d\n", rc);
604 /* setup the resource for the newly bound range */
605 p->res.start = p->bound_addr;
606 p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
607 p->res.name = pdev->name;
608 p->res.flags = IORESOURCE_MEM;
610 rc = papr_scm_nvdimm_init(p);
614 platform_set_drvdata(pdev, p);
618 err2: drc_pmem_unbind(p);
623 static int papr_scm_remove(struct platform_device *pdev)
625 struct papr_scm_priv *p = platform_get_drvdata(pdev);
627 nvdimm_bus_unregister(p->bus);
629 kfree(p->bus_desc.provider_name);
635 static const struct of_device_id papr_scm_match[] = {
636 { .compatible = "ibm,pmemory" },
640 static struct platform_driver papr_scm_driver = {
641 .probe = papr_scm_probe,
642 .remove = papr_scm_remove,
645 .of_match_table = papr_scm_match,
649 module_platform_driver(papr_scm_driver);
650 MODULE_DEVICE_TABLE(of, papr_scm_match);
651 MODULE_LICENSE("GPL");
652 MODULE_AUTHOR("IBM Corporation");