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