GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / powerpc / platforms / pseries / papr_scm.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #define pr_fmt(fmt)     "papr-scm: " fmt
4
5 #include <linux/of.h>
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
16 #include <asm/plpar_wrappers.h>
17
18 #define BIND_ANY_ADDR (~0ul)
19
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))
24
25 struct papr_scm_priv {
26         struct platform_device *pdev;
27         struct device_node *dn;
28         uint32_t drc_index;
29         uint64_t blocks;
30         uint64_t block_size;
31         int metadata_size;
32         bool is_volatile;
33
34         uint64_t bound_addr;
35
36         struct nvdimm_bus_descriptor bus_desc;
37         struct nvdimm_bus *bus;
38         struct nvdimm *nvdimm;
39         struct resource res;
40         struct nd_region *region;
41         struct nd_interleave_set nd_set;
42 };
43
44 static int drc_pmem_bind(struct papr_scm_priv *p)
45 {
46         unsigned long ret[PLPAR_HCALL_BUFSIZE];
47         uint64_t saved = 0;
48         uint64_t token;
49         int64_t rc;
50
51         /*
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.
56          */
57         token = 0;
58
59         do {
60                 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
61                                 p->blocks, BIND_ANY_ADDR, token);
62                 token = ret[0];
63                 if (!saved)
64                         saved = ret[1];
65                 cond_resched();
66         } while (rc == H_BUSY);
67
68         if (rc)
69                 return rc;
70
71         p->bound_addr = saved;
72         dev_dbg(&p->pdev->dev, "bound drc 0x%x to %pR\n", p->drc_index, &p->res);
73         return rc;
74 }
75
76 static void drc_pmem_unbind(struct papr_scm_priv *p)
77 {
78         unsigned long ret[PLPAR_HCALL_BUFSIZE];
79         uint64_t token = 0;
80         int64_t rc;
81
82         dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
83
84         /* NB: unbind has the same retry requirements as drc_pmem_bind() */
85         do {
86
87                 /* Unbind of all SCM resources associated with drcIndex */
88                 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
89                                  p->drc_index, token);
90                 token = ret[0];
91
92                 /* Check if we are stalled for some time */
93                 if (H_IS_LONG_BUSY(rc)) {
94                         msleep(get_longbusy_msecs(rc));
95                         rc = H_BUSY;
96                 } else if (rc == H_BUSY) {
97                         cond_resched();
98                 }
99
100         } while (rc == H_BUSY);
101
102         if (rc)
103                 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
104         else
105                 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
106                         p->drc_index);
107
108         return;
109 }
110
111 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
112 {
113         unsigned long start_addr;
114         unsigned long end_addr;
115         unsigned long ret[PLPAR_HCALL_BUFSIZE];
116         int64_t rc;
117
118
119         rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
120                          p->drc_index, 0);
121         if (rc)
122                 goto err_out;
123         start_addr = ret[0];
124
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);
128         if (rc)
129                 goto err_out;
130         end_addr = ret[0];
131
132         if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
133                 goto err_out;
134
135         p->bound_addr = start_addr;
136         dev_dbg(&p->pdev->dev, "bound drc 0x%x to %pR\n", p->drc_index, &p->res);
137         return rc;
138
139 err_out:
140         dev_info(&p->pdev->dev,
141                  "Failed to query, trying an unbind followed by bind");
142         drc_pmem_unbind(p);
143         return drc_pmem_bind(p);
144 }
145
146
147 static int papr_scm_meta_get(struct papr_scm_priv *p,
148                              struct nd_cmd_get_config_data_hdr *hdr)
149 {
150         unsigned long data[PLPAR_HCALL_BUFSIZE];
151         unsigned long offset, data_offset;
152         int len, read;
153         int64_t ret;
154
155         if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
156                 return -EINVAL;
157
158         for (len = hdr->in_length; len; len -= read) {
159
160                 data_offset = hdr->in_length - len;
161                 offset = hdr->in_offset + data_offset;
162
163                 if (len >= 8)
164                         read = 8;
165                 else if (len >= 4)
166                         read = 4;
167                 else if (len >= 2)
168                         read = 2;
169                 else
170                         read = 1;
171
172                 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
173                                   offset, read);
174
175                 if (ret == H_PARAMETER) /* bad DRC index */
176                         return -ENODEV;
177                 if (ret)
178                         return -EINVAL; /* other invalid parameter */
179
180                 switch (read) {
181                 case 8:
182                         *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
183                         break;
184                 case 4:
185                         *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
186                         break;
187
188                 case 2:
189                         *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
190                         break;
191
192                 case 1:
193                         *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
194                         break;
195                 }
196         }
197         return 0;
198 }
199
200 static int papr_scm_meta_set(struct papr_scm_priv *p,
201                              struct nd_cmd_set_config_hdr *hdr)
202 {
203         unsigned long offset, data_offset;
204         int len, wrote;
205         unsigned long data;
206         __be64 data_be;
207         int64_t ret;
208
209         if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
210                 return -EINVAL;
211
212         for (len = hdr->in_length; len; len -= wrote) {
213
214                 data_offset = hdr->in_length - len;
215                 offset = hdr->in_offset + data_offset;
216
217                 if (len >= 8) {
218                         data = *(uint64_t *)(hdr->in_buf + data_offset);
219                         data_be = cpu_to_be64(data);
220                         wrote = 8;
221                 } else if (len >= 4) {
222                         data = *(uint32_t *)(hdr->in_buf + data_offset);
223                         data &= 0xffffffff;
224                         data_be = cpu_to_be32(data);
225                         wrote = 4;
226                 } else if (len >= 2) {
227                         data = *(uint16_t *)(hdr->in_buf + data_offset);
228                         data &= 0xffff;
229                         data_be = cpu_to_be16(data);
230                         wrote = 2;
231                 } else {
232                         data_be = *(uint8_t *)(hdr->in_buf + data_offset);
233                         data_be &= 0xff;
234                         wrote = 1;
235                 }
236
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 */
240                         return -ENODEV;
241                 if (ret)
242                         return -EINVAL; /* other invalid parameter */
243         }
244
245         return 0;
246 }
247
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)
250 {
251         struct nd_cmd_get_config_size *get_size_hdr;
252         struct papr_scm_priv *p;
253
254         /* Only dimm-specific calls are supported atm */
255         if (!nvdimm)
256                 return -EINVAL;
257
258         p = nvdimm_provider_data(nvdimm);
259
260         switch (cmd) {
261         case ND_CMD_GET_CONFIG_SIZE:
262                 get_size_hdr = buf;
263
264                 get_size_hdr->status = 0;
265                 get_size_hdr->max_xfer = 8;
266                 get_size_hdr->config_size = p->metadata_size;
267                 *cmd_rc = 0;
268                 break;
269
270         case ND_CMD_GET_CONFIG_DATA:
271                 *cmd_rc = papr_scm_meta_get(p, buf);
272                 break;
273
274         case ND_CMD_SET_CONFIG_DATA:
275                 *cmd_rc = papr_scm_meta_set(p, buf);
276                 break;
277
278         default:
279                 return -EINVAL;
280         }
281
282         dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
283
284         return 0;
285 }
286
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,
292         NULL,
293 };
294
295 static const struct attribute_group *bus_attr_groups[] = {
296         &nvdimm_bus_attribute_group,
297         NULL,
298 };
299
300 static const struct attribute_group *papr_scm_dimm_groups[] = {
301         &nvdimm_attribute_group,
302         &nd_device_attribute_group,
303         NULL,
304 };
305
306 static inline int papr_scm_node(int node)
307 {
308         int min_dist = INT_MAX, dist;
309         int nid, min_node;
310
311         if ((node == NUMA_NO_NODE) || node_online(node))
312                 return node;
313
314         min_node = first_online_node;
315         for_each_online_node(nid) {
316                 dist = node_distance(node, nid);
317                 if (dist < min_dist) {
318                         min_dist = dist;
319                         min_node = nid;
320                 }
321         }
322         return min_node;
323 }
324
325 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
326 {
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;
332
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);
338
339         if (!p->bus_desc.provider_name)
340                 return -ENOMEM;
341
342         p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
343         if (!p->bus) {
344                 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
345                 kfree(p->bus_desc.provider_name);
346                 return -ENXIO;
347         }
348
349         dimm_flags = 0;
350         set_bit(NDD_ALIASING, &dimm_flags);
351
352         p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
353                                 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
354         if (!p->nvdimm) {
355                 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
356                 goto err;
357         }
358
359         if (nvdimm_bus_check_dimm_count(p->bus, 1))
360                 goto err;
361
362         /* now add the region */
363
364         memset(&mapping, 0, sizeof(mapping));
365         mapping.nvdimm = p->nvdimm;
366         mapping.start = 0;
367         mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
368
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);
382
383         if (p->is_volatile)
384                 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
385         else
386                 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
387         if (!p->region) {
388                 dev_err(dev, "Error registering region %pR from %pOF\n",
389                                 ndr_desc.res, p->dn);
390                 goto err;
391         }
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);
395
396         return 0;
397
398 err:    nvdimm_bus_unregister(p->bus);
399         kfree(p->bus_desc.provider_name);
400         return -ENXIO;
401 }
402
403 static int papr_scm_probe(struct platform_device *pdev)
404 {
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;
410         u64 uuid[2];
411         int rc;
412
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);
416                 return -ENODEV;
417         }
418
419         if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
420                 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
421                 return -ENODEV;
422         }
423
424         if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
425                 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
426                 return -ENODEV;
427         }
428
429         if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
430                 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
431                 return -ENODEV;
432         }
433
434
435         p = kzalloc(sizeof(*p), GFP_KERNEL);
436         if (!p)
437                 return -ENOMEM;
438
439         /* optional DT properties */
440         of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
441
442         p->dn = dn;
443         p->drc_index = drc_index;
444         p->block_size = block_size;
445         p->blocks = blocks;
446         p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
447
448         /* We just need to ensure that set cookies are unique across */
449         uuid_parse(uuid_str, (uuid_t *) uuid);
450         /*
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.
456          */
457         p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
458         p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
459
460         /* might be zero */
461         p->metadata_size = metadata_size;
462         p->pdev = pdev;
463
464         /* request the hypervisor to bind this region to somewhere in memory */
465         rc = drc_pmem_bind(p);
466
467         /* If phyp says drc memory still bound then force unbound and retry */
468         if (rc == H_OVERLAP)
469                 rc = drc_pmem_query_n_bind(p);
470
471         if (rc != H_SUCCESS) {
472                 dev_err(&p->pdev->dev, "bind err: %d\n", rc);
473                 rc = -ENXIO;
474                 goto err;
475         }
476
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;
482
483         rc = papr_scm_nvdimm_init(p);
484         if (rc)
485                 goto err2;
486
487         platform_set_drvdata(pdev, p);
488
489         return 0;
490
491 err2:   drc_pmem_unbind(p);
492 err:    kfree(p);
493         return rc;
494 }
495
496 static int papr_scm_remove(struct platform_device *pdev)
497 {
498         struct papr_scm_priv *p = platform_get_drvdata(pdev);
499
500         nvdimm_bus_unregister(p->bus);
501         drc_pmem_unbind(p);
502         kfree(p->bus_desc.provider_name);
503         kfree(p);
504
505         return 0;
506 }
507
508 static const struct of_device_id papr_scm_match[] = {
509         { .compatible = "ibm,pmemory" },
510         { },
511 };
512
513 static struct platform_driver papr_scm_driver = {
514         .probe = papr_scm_probe,
515         .remove = papr_scm_remove,
516         .driver = {
517                 .name = "papr_scm",
518                 .owner = THIS_MODULE,
519                 .of_match_table = papr_scm_match,
520         },
521 };
522
523 module_platform_driver(papr_scm_driver);
524 MODULE_DEVICE_TABLE(of, papr_scm_match);
525 MODULE_LICENSE("GPL");
526 MODULE_AUTHOR("IBM Corporation");