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