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 		return -ENXIO;
346 	}
347 
348 	dimm_flags = 0;
349 	set_bit(NDD_ALIASING, &dimm_flags);
350 
351 	p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
352 				dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
353 	if (!p->nvdimm) {
354 		dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
355 		goto err;
356 	}
357 
358 	if (nvdimm_bus_check_dimm_count(p->bus, 1))
359 		goto err;
360 
361 	/* now add the region */
362 
363 	memset(&mapping, 0, sizeof(mapping));
364 	mapping.nvdimm = p->nvdimm;
365 	mapping.start = 0;
366 	mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
367 
368 	memset(&ndr_desc, 0, sizeof(ndr_desc));
369 	ndr_desc.attr_groups = region_attr_groups;
370 	target_nid = dev_to_node(&p->pdev->dev);
371 	online_nid = papr_scm_node(target_nid);
372 	ndr_desc.numa_node = online_nid;
373 	ndr_desc.target_node = target_nid;
374 	ndr_desc.res = &p->res;
375 	ndr_desc.of_node = p->dn;
376 	ndr_desc.provider_data = p;
377 	ndr_desc.mapping = &mapping;
378 	ndr_desc.num_mappings = 1;
379 	ndr_desc.nd_set = &p->nd_set;
380 	set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
381 
382 	if (p->is_volatile)
383 		p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
384 	else
385 		p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
386 	if (!p->region) {
387 		dev_err(dev, "Error registering region %pR from %pOF\n",
388 				ndr_desc.res, p->dn);
389 		goto err;
390 	}
391 	if (target_nid != online_nid)
392 		dev_info(dev, "Region registered with target node %d and online node %d",
393 			 target_nid, online_nid);
394 
395 	return 0;
396 
397 err:	nvdimm_bus_unregister(p->bus);
398 	kfree(p->bus_desc.provider_name);
399 	return -ENXIO;
400 }
401 
402 static int papr_scm_probe(struct platform_device *pdev)
403 {
404 	struct device_node *dn = pdev->dev.of_node;
405 	u32 drc_index, metadata_size;
406 	u64 blocks, block_size;
407 	struct papr_scm_priv *p;
408 	const char *uuid_str;
409 	u64 uuid[2];
410 	int rc;
411 
412 	/* check we have all the required DT properties */
413 	if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
414 		dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
415 		return -ENODEV;
416 	}
417 
418 	if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
419 		dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
420 		return -ENODEV;
421 	}
422 
423 	if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
424 		dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
425 		return -ENODEV;
426 	}
427 
428 	if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
429 		dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
430 		return -ENODEV;
431 	}
432 
433 
434 	p = kzalloc(sizeof(*p), GFP_KERNEL);
435 	if (!p)
436 		return -ENOMEM;
437 
438 	/* optional DT properties */
439 	of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
440 
441 	p->dn = dn;
442 	p->drc_index = drc_index;
443 	p->block_size = block_size;
444 	p->blocks = blocks;
445 	p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
446 
447 	/* We just need to ensure that set cookies are unique across */
448 	uuid_parse(uuid_str, (uuid_t *) uuid);
449 	/*
450 	 * cookie1 and cookie2 are not really little endian
451 	 * we store a little endian representation of the
452 	 * uuid str so that we can compare this with the label
453 	 * area cookie irrespective of the endian config with which
454 	 * the kernel is built.
455 	 */
456 	p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
457 	p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
458 
459 	/* might be zero */
460 	p->metadata_size = metadata_size;
461 	p->pdev = pdev;
462 
463 	/* request the hypervisor to bind this region to somewhere in memory */
464 	rc = drc_pmem_bind(p);
465 
466 	/* If phyp says drc memory still bound then force unbound and retry */
467 	if (rc == H_OVERLAP)
468 		rc = drc_pmem_query_n_bind(p);
469 
470 	if (rc != H_SUCCESS) {
471 		dev_err(&p->pdev->dev, "bind err: %d\n", rc);
472 		rc = -ENXIO;
473 		goto err;
474 	}
475 
476 	/* setup the resource for the newly bound range */
477 	p->res.start = p->bound_addr;
478 	p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
479 	p->res.name  = pdev->name;
480 	p->res.flags = IORESOURCE_MEM;
481 
482 	rc = papr_scm_nvdimm_init(p);
483 	if (rc)
484 		goto err2;
485 
486 	platform_set_drvdata(pdev, p);
487 
488 	return 0;
489 
490 err2:	drc_pmem_unbind(p);
491 err:	kfree(p);
492 	return rc;
493 }
494 
495 static int papr_scm_remove(struct platform_device *pdev)
496 {
497 	struct papr_scm_priv *p = platform_get_drvdata(pdev);
498 
499 	nvdimm_bus_unregister(p->bus);
500 	drc_pmem_unbind(p);
501 	kfree(p);
502 
503 	return 0;
504 }
505 
506 static const struct of_device_id papr_scm_match[] = {
507 	{ .compatible = "ibm,pmemory" },
508 	{ },
509 };
510 
511 static struct platform_driver papr_scm_driver = {
512 	.probe = papr_scm_probe,
513 	.remove = papr_scm_remove,
514 	.driver = {
515 		.name = "papr_scm",
516 		.of_match_table = papr_scm_match,
517 	},
518 };
519 
520 module_platform_driver(papr_scm_driver);
521 MODULE_DEVICE_TABLE(of, papr_scm_match);
522 MODULE_LICENSE("GPL");
523 MODULE_AUTHOR("IBM Corporation");
524