8806ac8226275ffa8f9fe1707bdbf89d2345e6bc
[muen/linux.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
15 #include <asm/plpar_wrappers.h>
16
17 #define BIND_ANY_ADDR (~0ul)
18
19 #define PAPR_SCM_DIMM_CMD_MASK \
20         ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
21          (1ul << ND_CMD_GET_CONFIG_DATA) | \
22          (1ul << ND_CMD_SET_CONFIG_DATA))
23
24 struct papr_scm_priv {
25         struct platform_device *pdev;
26         struct device_node *dn;
27         uint32_t drc_index;
28         uint64_t blocks;
29         uint64_t block_size;
30         int metadata_size;
31
32         uint64_t bound_addr;
33
34         struct nvdimm_bus_descriptor bus_desc;
35         struct nvdimm_bus *bus;
36         struct nvdimm *nvdimm;
37         struct resource res;
38         struct nd_region *region;
39         struct nd_interleave_set nd_set;
40 };
41
42 static int drc_pmem_bind(struct papr_scm_priv *p)
43 {
44         unsigned long ret[PLPAR_HCALL_BUFSIZE];
45         uint64_t rc, token;
46
47         /*
48          * When the hypervisor cannot map all the requested memory in a single
49          * hcall it returns H_BUSY and we call again with the token until
50          * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
51          * leave the system in an undefined state, so we wait.
52          */
53         token = 0;
54
55         do {
56                 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
57                                 p->blocks, BIND_ANY_ADDR, token);
58                 token = ret[0];
59                 cond_resched();
60         } while (rc == H_BUSY);
61
62         if (rc) {
63                 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
64                 return -ENXIO;
65         }
66
67         p->bound_addr = ret[1];
68
69         dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
70
71         return 0;
72 }
73
74 static int drc_pmem_unbind(struct papr_scm_priv *p)
75 {
76         unsigned long ret[PLPAR_HCALL_BUFSIZE];
77         uint64_t rc, token;
78
79         token = 0;
80
81         /* NB: unbind has the same retry requirements mentioned above */
82         do {
83                 rc = plpar_hcall(H_SCM_UNBIND_MEM, ret, p->drc_index,
84                                 p->bound_addr, p->blocks, token);
85                 token = ret[0];
86                 cond_resched();
87         } while (rc == H_BUSY);
88
89         if (rc)
90                 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
91
92         return !!rc;
93 }
94
95 static int papr_scm_meta_get(struct papr_scm_priv *p,
96                         struct nd_cmd_get_config_data_hdr *hdr)
97 {
98         unsigned long data[PLPAR_HCALL_BUFSIZE];
99         int64_t ret;
100
101         if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
102                 return -EINVAL;
103
104         ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
105                         hdr->in_offset, 1);
106
107         if (ret == H_PARAMETER) /* bad DRC index */
108                 return -ENODEV;
109         if (ret)
110                 return -EINVAL; /* other invalid parameter */
111
112         hdr->out_buf[0] = data[0] & 0xff;
113
114         return 0;
115 }
116
117 static int papr_scm_meta_set(struct papr_scm_priv *p,
118                         struct nd_cmd_set_config_hdr *hdr)
119 {
120         int64_t ret;
121
122         if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
123                 return -EINVAL;
124
125         ret = plpar_hcall_norets(H_SCM_WRITE_METADATA,
126                         p->drc_index, hdr->in_offset, hdr->in_buf[0], 1);
127
128         if (ret == H_PARAMETER) /* bad DRC index */
129                 return -ENODEV;
130         if (ret)
131                 return -EINVAL; /* other invalid parameter */
132
133         return 0;
134 }
135
136 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
137                 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
138 {
139         struct nd_cmd_get_config_size *get_size_hdr;
140         struct papr_scm_priv *p;
141
142         /* Only dimm-specific calls are supported atm */
143         if (!nvdimm)
144                 return -EINVAL;
145
146         p = nvdimm_provider_data(nvdimm);
147
148         switch (cmd) {
149         case ND_CMD_GET_CONFIG_SIZE:
150                 get_size_hdr = buf;
151
152                 get_size_hdr->status = 0;
153                 get_size_hdr->max_xfer = 1;
154                 get_size_hdr->config_size = p->metadata_size;
155                 *cmd_rc = 0;
156                 break;
157
158         case ND_CMD_GET_CONFIG_DATA:
159                 *cmd_rc = papr_scm_meta_get(p, buf);
160                 break;
161
162         case ND_CMD_SET_CONFIG_DATA:
163                 *cmd_rc = papr_scm_meta_set(p, buf);
164                 break;
165
166         default:
167                 return -EINVAL;
168         }
169
170         dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
171
172         return 0;
173 }
174
175 static const struct attribute_group *region_attr_groups[] = {
176         &nd_region_attribute_group,
177         &nd_device_attribute_group,
178         &nd_mapping_attribute_group,
179         &nd_numa_attribute_group,
180         NULL,
181 };
182
183 static const struct attribute_group *bus_attr_groups[] = {
184         &nvdimm_bus_attribute_group,
185         NULL,
186 };
187
188 static const struct attribute_group *papr_scm_dimm_groups[] = {
189         &nvdimm_attribute_group,
190         &nd_device_attribute_group,
191         NULL,
192 };
193
194 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
195 {
196         struct device *dev = &p->pdev->dev;
197         struct nd_mapping_desc mapping;
198         struct nd_region_desc ndr_desc;
199         unsigned long dimm_flags;
200
201         p->bus_desc.ndctl = papr_scm_ndctl;
202         p->bus_desc.module = THIS_MODULE;
203         p->bus_desc.of_node = p->pdev->dev.of_node;
204         p->bus_desc.attr_groups = bus_attr_groups;
205         p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
206
207         if (!p->bus_desc.provider_name)
208                 return -ENOMEM;
209
210         p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
211         if (!p->bus) {
212                 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
213                 return -ENXIO;
214         }
215
216         dimm_flags = 0;
217         set_bit(NDD_ALIASING, &dimm_flags);
218
219         p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
220                                 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
221         if (!p->nvdimm) {
222                 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
223                 goto err;
224         }
225
226         if (nvdimm_bus_check_dimm_count(p->bus, 1))
227                 goto err;
228
229         /* now add the region */
230
231         memset(&mapping, 0, sizeof(mapping));
232         mapping.nvdimm = p->nvdimm;
233         mapping.start = 0;
234         mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
235
236         memset(&ndr_desc, 0, sizeof(ndr_desc));
237         ndr_desc.attr_groups = region_attr_groups;
238         ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
239         ndr_desc.target_node = ndr_desc.numa_node;
240         ndr_desc.res = &p->res;
241         ndr_desc.of_node = p->dn;
242         ndr_desc.provider_data = p;
243         ndr_desc.mapping = &mapping;
244         ndr_desc.num_mappings = 1;
245         ndr_desc.nd_set = &p->nd_set;
246         set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
247
248         p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
249         if (!p->region) {
250                 dev_err(dev, "Error registering region %pR from %pOF\n",
251                                 ndr_desc.res, p->dn);
252                 goto err;
253         }
254
255         return 0;
256
257 err:    nvdimm_bus_unregister(p->bus);
258         kfree(p->bus_desc.provider_name);
259         return -ENXIO;
260 }
261
262 static int papr_scm_probe(struct platform_device *pdev)
263 {
264         struct device_node *dn = pdev->dev.of_node;
265         u32 drc_index, metadata_size;
266         u64 blocks, block_size;
267         struct papr_scm_priv *p;
268         const char *uuid_str;
269         u64 uuid[2];
270         int rc;
271
272         /* check we have all the required DT properties */
273         if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
274                 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
275                 return -ENODEV;
276         }
277
278         if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
279                 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
280                 return -ENODEV;
281         }
282
283         if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
284                 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
285                 return -ENODEV;
286         }
287
288         if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
289                 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
290                 return -ENODEV;
291         }
292
293         p = kzalloc(sizeof(*p), GFP_KERNEL);
294         if (!p)
295                 return -ENOMEM;
296
297         /* optional DT properties */
298         of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
299
300         p->dn = dn;
301         p->drc_index = drc_index;
302         p->block_size = block_size;
303         p->blocks = blocks;
304
305         /* We just need to ensure that set cookies are unique across */
306         uuid_parse(uuid_str, (uuid_t *) uuid);
307         p->nd_set.cookie1 = uuid[0];
308         p->nd_set.cookie2 = uuid[1];
309
310         /* might be zero */
311         p->metadata_size = metadata_size;
312         p->pdev = pdev;
313
314         /* request the hypervisor to bind this region to somewhere in memory */
315         rc = drc_pmem_bind(p);
316         if (rc)
317                 goto err;
318
319         /* setup the resource for the newly bound range */
320         p->res.start = p->bound_addr;
321         p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
322         p->res.name  = pdev->name;
323         p->res.flags = IORESOURCE_MEM;
324
325         rc = papr_scm_nvdimm_init(p);
326         if (rc)
327                 goto err2;
328
329         platform_set_drvdata(pdev, p);
330
331         return 0;
332
333 err2:   drc_pmem_unbind(p);
334 err:    kfree(p);
335         return rc;
336 }
337
338 static int papr_scm_remove(struct platform_device *pdev)
339 {
340         struct papr_scm_priv *p = platform_get_drvdata(pdev);
341
342         nvdimm_bus_unregister(p->bus);
343         drc_pmem_unbind(p);
344         kfree(p);
345
346         return 0;
347 }
348
349 static const struct of_device_id papr_scm_match[] = {
350         { .compatible = "ibm,pmemory" },
351         { },
352 };
353
354 static struct platform_driver papr_scm_driver = {
355         .probe = papr_scm_probe,
356         .remove = papr_scm_remove,
357         .driver = {
358                 .name = "papr_scm",
359                 .owner = THIS_MODULE,
360                 .of_match_table = papr_scm_match,
361         },
362 };
363
364 module_platform_driver(papr_scm_driver);
365 MODULE_DEVICE_TABLE(of, papr_scm_match);
366 MODULE_LICENSE("GPL");
367 MODULE_AUTHOR("IBM Corporation");