Merge branch 'sched/rt' into sched/core, to pick up -rt changes
[linux-2.6-block.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                 /* H_OVERLAP needs a separate error path */
70                 if (rc == H_OVERLAP)
71                         return -EBUSY;
72
73                 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
74                 return -ENXIO;
75         }
76
77         p->bound_addr = saved;
78
79         dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
80
81         return 0;
82 }
83
84 static int drc_pmem_unbind(struct papr_scm_priv *p)
85 {
86         unsigned long ret[PLPAR_HCALL_BUFSIZE];
87         uint64_t token = 0;
88         int64_t rc;
89
90         dev_dbg(&p->pdev->dev, "unbind drc %x\n", p->drc_index);
91
92         /* NB: unbind has the same retry requirements as drc_pmem_bind() */
93         do {
94
95                 /* Unbind of all SCM resources associated with drcIndex */
96                 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
97                                  p->drc_index, token);
98                 token = ret[0];
99
100                 /* Check if we are stalled for some time */
101                 if (H_IS_LONG_BUSY(rc)) {
102                         msleep(get_longbusy_msecs(rc));
103                         rc = H_BUSY;
104                 } else if (rc == H_BUSY) {
105                         cond_resched();
106                 }
107
108         } while (rc == H_BUSY);
109
110         if (rc)
111                 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
112         else
113                 dev_dbg(&p->pdev->dev, "unbind drc %x complete\n",
114                         p->drc_index);
115
116         return rc == H_SUCCESS ? 0 : -ENXIO;
117 }
118
119 static int papr_scm_meta_get(struct papr_scm_priv *p,
120                              struct nd_cmd_get_config_data_hdr *hdr)
121 {
122         unsigned long data[PLPAR_HCALL_BUFSIZE];
123         unsigned long offset, data_offset;
124         int len, read;
125         int64_t ret;
126
127         if ((hdr->in_offset + hdr->in_length) >= p->metadata_size)
128                 return -EINVAL;
129
130         for (len = hdr->in_length; len; len -= read) {
131
132                 data_offset = hdr->in_length - len;
133                 offset = hdr->in_offset + data_offset;
134
135                 if (len >= 8)
136                         read = 8;
137                 else if (len >= 4)
138                         read = 4;
139                 else if (len >= 2)
140                         read = 2;
141                 else
142                         read = 1;
143
144                 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
145                                   offset, read);
146
147                 if (ret == H_PARAMETER) /* bad DRC index */
148                         return -ENODEV;
149                 if (ret)
150                         return -EINVAL; /* other invalid parameter */
151
152                 switch (read) {
153                 case 8:
154                         *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
155                         break;
156                 case 4:
157                         *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
158                         break;
159
160                 case 2:
161                         *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
162                         break;
163
164                 case 1:
165                         *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
166                         break;
167                 }
168         }
169         return 0;
170 }
171
172 static int papr_scm_meta_set(struct papr_scm_priv *p,
173                              struct nd_cmd_set_config_hdr *hdr)
174 {
175         unsigned long offset, data_offset;
176         int len, wrote;
177         unsigned long data;
178         __be64 data_be;
179         int64_t ret;
180
181         if ((hdr->in_offset + hdr->in_length) >= p->metadata_size)
182                 return -EINVAL;
183
184         for (len = hdr->in_length; len; len -= wrote) {
185
186                 data_offset = hdr->in_length - len;
187                 offset = hdr->in_offset + data_offset;
188
189                 if (len >= 8) {
190                         data = *(uint64_t *)(hdr->in_buf + data_offset);
191                         data_be = cpu_to_be64(data);
192                         wrote = 8;
193                 } else if (len >= 4) {
194                         data = *(uint32_t *)(hdr->in_buf + data_offset);
195                         data &= 0xffffffff;
196                         data_be = cpu_to_be32(data);
197                         wrote = 4;
198                 } else if (len >= 2) {
199                         data = *(uint16_t *)(hdr->in_buf + data_offset);
200                         data &= 0xffff;
201                         data_be = cpu_to_be16(data);
202                         wrote = 2;
203                 } else {
204                         data_be = *(uint8_t *)(hdr->in_buf + data_offset);
205                         data_be &= 0xff;
206                         wrote = 1;
207                 }
208
209                 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
210                                          offset, data_be, wrote);
211                 if (ret == H_PARAMETER) /* bad DRC index */
212                         return -ENODEV;
213                 if (ret)
214                         return -EINVAL; /* other invalid parameter */
215         }
216
217         return 0;
218 }
219
220 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
221                 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
222 {
223         struct nd_cmd_get_config_size *get_size_hdr;
224         struct papr_scm_priv *p;
225
226         /* Only dimm-specific calls are supported atm */
227         if (!nvdimm)
228                 return -EINVAL;
229
230         p = nvdimm_provider_data(nvdimm);
231
232         switch (cmd) {
233         case ND_CMD_GET_CONFIG_SIZE:
234                 get_size_hdr = buf;
235
236                 get_size_hdr->status = 0;
237                 get_size_hdr->max_xfer = 8;
238                 get_size_hdr->config_size = p->metadata_size;
239                 *cmd_rc = 0;
240                 break;
241
242         case ND_CMD_GET_CONFIG_DATA:
243                 *cmd_rc = papr_scm_meta_get(p, buf);
244                 break;
245
246         case ND_CMD_SET_CONFIG_DATA:
247                 *cmd_rc = papr_scm_meta_set(p, buf);
248                 break;
249
250         default:
251                 return -EINVAL;
252         }
253
254         dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
255
256         return 0;
257 }
258
259 static const struct attribute_group *region_attr_groups[] = {
260         &nd_region_attribute_group,
261         &nd_device_attribute_group,
262         &nd_mapping_attribute_group,
263         &nd_numa_attribute_group,
264         NULL,
265 };
266
267 static const struct attribute_group *bus_attr_groups[] = {
268         &nvdimm_bus_attribute_group,
269         NULL,
270 };
271
272 static const struct attribute_group *papr_scm_dimm_groups[] = {
273         &nvdimm_attribute_group,
274         &nd_device_attribute_group,
275         NULL,
276 };
277
278 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
279 {
280         struct device *dev = &p->pdev->dev;
281         struct nd_mapping_desc mapping;
282         struct nd_region_desc ndr_desc;
283         unsigned long dimm_flags;
284
285         p->bus_desc.ndctl = papr_scm_ndctl;
286         p->bus_desc.module = THIS_MODULE;
287         p->bus_desc.of_node = p->pdev->dev.of_node;
288         p->bus_desc.attr_groups = bus_attr_groups;
289         p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
290
291         if (!p->bus_desc.provider_name)
292                 return -ENOMEM;
293
294         p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
295         if (!p->bus) {
296                 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
297                 return -ENXIO;
298         }
299
300         dimm_flags = 0;
301         set_bit(NDD_ALIASING, &dimm_flags);
302
303         p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
304                                 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
305         if (!p->nvdimm) {
306                 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
307                 goto err;
308         }
309
310         if (nvdimm_bus_check_dimm_count(p->bus, 1))
311                 goto err;
312
313         /* now add the region */
314
315         memset(&mapping, 0, sizeof(mapping));
316         mapping.nvdimm = p->nvdimm;
317         mapping.start = 0;
318         mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
319
320         memset(&ndr_desc, 0, sizeof(ndr_desc));
321         ndr_desc.attr_groups = region_attr_groups;
322         ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
323         ndr_desc.target_node = ndr_desc.numa_node;
324         ndr_desc.res = &p->res;
325         ndr_desc.of_node = p->dn;
326         ndr_desc.provider_data = p;
327         ndr_desc.mapping = &mapping;
328         ndr_desc.num_mappings = 1;
329         ndr_desc.nd_set = &p->nd_set;
330         set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
331
332         if (p->is_volatile)
333                 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
334         else
335                 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
336         if (!p->region) {
337                 dev_err(dev, "Error registering region %pR from %pOF\n",
338                                 ndr_desc.res, p->dn);
339                 goto err;
340         }
341
342         return 0;
343
344 err:    nvdimm_bus_unregister(p->bus);
345         kfree(p->bus_desc.provider_name);
346         return -ENXIO;
347 }
348
349 static int papr_scm_probe(struct platform_device *pdev)
350 {
351         struct device_node *dn = pdev->dev.of_node;
352         u32 drc_index, metadata_size;
353         u64 blocks, block_size;
354         struct papr_scm_priv *p;
355         const char *uuid_str;
356         u64 uuid[2];
357         int rc;
358
359         /* check we have all the required DT properties */
360         if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
361                 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
362                 return -ENODEV;
363         }
364
365         if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
366                 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
367                 return -ENODEV;
368         }
369
370         if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
371                 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
372                 return -ENODEV;
373         }
374
375         if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
376                 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
377                 return -ENODEV;
378         }
379
380
381         p = kzalloc(sizeof(*p), GFP_KERNEL);
382         if (!p)
383                 return -ENOMEM;
384
385         /* optional DT properties */
386         of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
387
388         p->dn = dn;
389         p->drc_index = drc_index;
390         p->block_size = block_size;
391         p->blocks = blocks;
392         p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
393
394         /* We just need to ensure that set cookies are unique across */
395         uuid_parse(uuid_str, (uuid_t *) uuid);
396         /*
397          * cookie1 and cookie2 are not really little endian
398          * we store a little endian representation of the
399          * uuid str so that we can compare this with the label
400          * area cookie irrespective of the endian config with which
401          * the kernel is built.
402          */
403         p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
404         p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
405
406         /* might be zero */
407         p->metadata_size = metadata_size;
408         p->pdev = pdev;
409
410         /* request the hypervisor to bind this region to somewhere in memory */
411         rc = drc_pmem_bind(p);
412
413         /* If phyp says drc memory still bound then force unbound and retry */
414         if (rc == -EBUSY) {
415                 dev_warn(&pdev->dev, "Retrying bind after unbinding\n");
416                 drc_pmem_unbind(p);
417                 rc = drc_pmem_bind(p);
418         }
419
420         if (rc)
421                 goto err;
422
423         /* setup the resource for the newly bound range */
424         p->res.start = p->bound_addr;
425         p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
426         p->res.name  = pdev->name;
427         p->res.flags = IORESOURCE_MEM;
428
429         rc = papr_scm_nvdimm_init(p);
430         if (rc)
431                 goto err2;
432
433         platform_set_drvdata(pdev, p);
434
435         return 0;
436
437 err2:   drc_pmem_unbind(p);
438 err:    kfree(p);
439         return rc;
440 }
441
442 static int papr_scm_remove(struct platform_device *pdev)
443 {
444         struct papr_scm_priv *p = platform_get_drvdata(pdev);
445
446         nvdimm_bus_unregister(p->bus);
447         drc_pmem_unbind(p);
448         kfree(p);
449
450         return 0;
451 }
452
453 static const struct of_device_id papr_scm_match[] = {
454         { .compatible = "ibm,pmemory" },
455         { },
456 };
457
458 static struct platform_driver papr_scm_driver = {
459         .probe = papr_scm_probe,
460         .remove = papr_scm_remove,
461         .driver = {
462                 .name = "papr_scm",
463                 .owner = THIS_MODULE,
464                 .of_match_table = papr_scm_match,
465         },
466 };
467
468 module_platform_driver(papr_scm_driver);
469 MODULE_DEVICE_TABLE(of, papr_scm_match);
470 MODULE_LICENSE("GPL");
471 MODULE_AUTHOR("IBM Corporation");