xref: /openbmc/linux/drivers/nvdimm/region_devs.c (revision 22d55f02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #include <linux/scatterlist.h>
6 #include <linux/highmem.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/hash.h>
10 #include <linux/sort.h>
11 #include <linux/io.h>
12 #include <linux/nd.h>
13 #include "nd-core.h"
14 #include "nd.h"
15 
16 /*
17  * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
18  * irrelevant.
19  */
20 #include <linux/io-64-nonatomic-hi-lo.h>
21 
22 static DEFINE_IDA(region_ida);
23 static DEFINE_PER_CPU(int, flush_idx);
24 
25 static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm,
26 		struct nd_region_data *ndrd)
27 {
28 	int i, j;
29 
30 	dev_dbg(dev, "%s: map %d flush address%s\n", nvdimm_name(nvdimm),
31 			nvdimm->num_flush, nvdimm->num_flush == 1 ? "" : "es");
32 	for (i = 0; i < (1 << ndrd->hints_shift); i++) {
33 		struct resource *res = &nvdimm->flush_wpq[i];
34 		unsigned long pfn = PHYS_PFN(res->start);
35 		void __iomem *flush_page;
36 
37 		/* check if flush hints share a page */
38 		for (j = 0; j < i; j++) {
39 			struct resource *res_j = &nvdimm->flush_wpq[j];
40 			unsigned long pfn_j = PHYS_PFN(res_j->start);
41 
42 			if (pfn == pfn_j)
43 				break;
44 		}
45 
46 		if (j < i)
47 			flush_page = (void __iomem *) ((unsigned long)
48 					ndrd_get_flush_wpq(ndrd, dimm, j)
49 					& PAGE_MASK);
50 		else
51 			flush_page = devm_nvdimm_ioremap(dev,
52 					PFN_PHYS(pfn), PAGE_SIZE);
53 		if (!flush_page)
54 			return -ENXIO;
55 		ndrd_set_flush_wpq(ndrd, dimm, i, flush_page
56 				+ (res->start & ~PAGE_MASK));
57 	}
58 
59 	return 0;
60 }
61 
62 int nd_region_activate(struct nd_region *nd_region)
63 {
64 	int i, j, num_flush = 0;
65 	struct nd_region_data *ndrd;
66 	struct device *dev = &nd_region->dev;
67 	size_t flush_data_size = sizeof(void *);
68 
69 	nvdimm_bus_lock(&nd_region->dev);
70 	for (i = 0; i < nd_region->ndr_mappings; i++) {
71 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
72 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
73 
74 		if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
75 			nvdimm_bus_unlock(&nd_region->dev);
76 			return -EBUSY;
77 		}
78 
79 		/* at least one null hint slot per-dimm for the "no-hint" case */
80 		flush_data_size += sizeof(void *);
81 		num_flush = min_not_zero(num_flush, nvdimm->num_flush);
82 		if (!nvdimm->num_flush)
83 			continue;
84 		flush_data_size += nvdimm->num_flush * sizeof(void *);
85 	}
86 	nvdimm_bus_unlock(&nd_region->dev);
87 
88 	ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
89 	if (!ndrd)
90 		return -ENOMEM;
91 	dev_set_drvdata(dev, ndrd);
92 
93 	if (!num_flush)
94 		return 0;
95 
96 	ndrd->hints_shift = ilog2(num_flush);
97 	for (i = 0; i < nd_region->ndr_mappings; i++) {
98 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
99 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
100 		int rc = nvdimm_map_flush(&nd_region->dev, nvdimm, i, ndrd);
101 
102 		if (rc)
103 			return rc;
104 	}
105 
106 	/*
107 	 * Clear out entries that are duplicates. This should prevent the
108 	 * extra flushings.
109 	 */
110 	for (i = 0; i < nd_region->ndr_mappings - 1; i++) {
111 		/* ignore if NULL already */
112 		if (!ndrd_get_flush_wpq(ndrd, i, 0))
113 			continue;
114 
115 		for (j = i + 1; j < nd_region->ndr_mappings; j++)
116 			if (ndrd_get_flush_wpq(ndrd, i, 0) ==
117 			    ndrd_get_flush_wpq(ndrd, j, 0))
118 				ndrd_set_flush_wpq(ndrd, j, 0, NULL);
119 	}
120 
121 	return 0;
122 }
123 
124 static void nd_region_release(struct device *dev)
125 {
126 	struct nd_region *nd_region = to_nd_region(dev);
127 	u16 i;
128 
129 	for (i = 0; i < nd_region->ndr_mappings; i++) {
130 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
131 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
132 
133 		put_device(&nvdimm->dev);
134 	}
135 	free_percpu(nd_region->lane);
136 	ida_simple_remove(&region_ida, nd_region->id);
137 	if (is_nd_blk(dev))
138 		kfree(to_nd_blk_region(dev));
139 	else
140 		kfree(nd_region);
141 }
142 
143 static struct device_type nd_blk_device_type = {
144 	.name = "nd_blk",
145 	.release = nd_region_release,
146 };
147 
148 static struct device_type nd_pmem_device_type = {
149 	.name = "nd_pmem",
150 	.release = nd_region_release,
151 };
152 
153 static struct device_type nd_volatile_device_type = {
154 	.name = "nd_volatile",
155 	.release = nd_region_release,
156 };
157 
158 bool is_nd_pmem(struct device *dev)
159 {
160 	return dev ? dev->type == &nd_pmem_device_type : false;
161 }
162 
163 bool is_nd_blk(struct device *dev)
164 {
165 	return dev ? dev->type == &nd_blk_device_type : false;
166 }
167 
168 bool is_nd_volatile(struct device *dev)
169 {
170 	return dev ? dev->type == &nd_volatile_device_type : false;
171 }
172 
173 struct nd_region *to_nd_region(struct device *dev)
174 {
175 	struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
176 
177 	WARN_ON(dev->type->release != nd_region_release);
178 	return nd_region;
179 }
180 EXPORT_SYMBOL_GPL(to_nd_region);
181 
182 struct device *nd_region_dev(struct nd_region *nd_region)
183 {
184 	if (!nd_region)
185 		return NULL;
186 	return &nd_region->dev;
187 }
188 EXPORT_SYMBOL_GPL(nd_region_dev);
189 
190 struct nd_blk_region *to_nd_blk_region(struct device *dev)
191 {
192 	struct nd_region *nd_region = to_nd_region(dev);
193 
194 	WARN_ON(!is_nd_blk(dev));
195 	return container_of(nd_region, struct nd_blk_region, nd_region);
196 }
197 EXPORT_SYMBOL_GPL(to_nd_blk_region);
198 
199 void *nd_region_provider_data(struct nd_region *nd_region)
200 {
201 	return nd_region->provider_data;
202 }
203 EXPORT_SYMBOL_GPL(nd_region_provider_data);
204 
205 void *nd_blk_region_provider_data(struct nd_blk_region *ndbr)
206 {
207 	return ndbr->blk_provider_data;
208 }
209 EXPORT_SYMBOL_GPL(nd_blk_region_provider_data);
210 
211 void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data)
212 {
213 	ndbr->blk_provider_data = data;
214 }
215 EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data);
216 
217 /**
218  * nd_region_to_nstype() - region to an integer namespace type
219  * @nd_region: region-device to interrogate
220  *
221  * This is the 'nstype' attribute of a region as well, an input to the
222  * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
223  * namespace devices with namespace drivers.
224  */
225 int nd_region_to_nstype(struct nd_region *nd_region)
226 {
227 	if (is_memory(&nd_region->dev)) {
228 		u16 i, alias;
229 
230 		for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
231 			struct nd_mapping *nd_mapping = &nd_region->mapping[i];
232 			struct nvdimm *nvdimm = nd_mapping->nvdimm;
233 
234 			if (test_bit(NDD_ALIASING, &nvdimm->flags))
235 				alias++;
236 		}
237 		if (alias)
238 			return ND_DEVICE_NAMESPACE_PMEM;
239 		else
240 			return ND_DEVICE_NAMESPACE_IO;
241 	} else if (is_nd_blk(&nd_region->dev)) {
242 		return ND_DEVICE_NAMESPACE_BLK;
243 	}
244 
245 	return 0;
246 }
247 EXPORT_SYMBOL(nd_region_to_nstype);
248 
249 static ssize_t size_show(struct device *dev,
250 		struct device_attribute *attr, char *buf)
251 {
252 	struct nd_region *nd_region = to_nd_region(dev);
253 	unsigned long long size = 0;
254 
255 	if (is_memory(dev)) {
256 		size = nd_region->ndr_size;
257 	} else if (nd_region->ndr_mappings == 1) {
258 		struct nd_mapping *nd_mapping = &nd_region->mapping[0];
259 
260 		size = nd_mapping->size;
261 	}
262 
263 	return sprintf(buf, "%llu\n", size);
264 }
265 static DEVICE_ATTR_RO(size);
266 
267 static ssize_t deep_flush_show(struct device *dev,
268 		struct device_attribute *attr, char *buf)
269 {
270 	struct nd_region *nd_region = to_nd_region(dev);
271 
272 	/*
273 	 * NOTE: in the nvdimm_has_flush() error case this attribute is
274 	 * not visible.
275 	 */
276 	return sprintf(buf, "%d\n", nvdimm_has_flush(nd_region));
277 }
278 
279 static ssize_t deep_flush_store(struct device *dev, struct device_attribute *attr,
280 		const char *buf, size_t len)
281 {
282 	bool flush;
283 	int rc = strtobool(buf, &flush);
284 	struct nd_region *nd_region = to_nd_region(dev);
285 
286 	if (rc)
287 		return rc;
288 	if (!flush)
289 		return -EINVAL;
290 	nvdimm_flush(nd_region);
291 
292 	return len;
293 }
294 static DEVICE_ATTR_RW(deep_flush);
295 
296 static ssize_t mappings_show(struct device *dev,
297 		struct device_attribute *attr, char *buf)
298 {
299 	struct nd_region *nd_region = to_nd_region(dev);
300 
301 	return sprintf(buf, "%d\n", nd_region->ndr_mappings);
302 }
303 static DEVICE_ATTR_RO(mappings);
304 
305 static ssize_t nstype_show(struct device *dev,
306 		struct device_attribute *attr, char *buf)
307 {
308 	struct nd_region *nd_region = to_nd_region(dev);
309 
310 	return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
311 }
312 static DEVICE_ATTR_RO(nstype);
313 
314 static ssize_t set_cookie_show(struct device *dev,
315 		struct device_attribute *attr, char *buf)
316 {
317 	struct nd_region *nd_region = to_nd_region(dev);
318 	struct nd_interleave_set *nd_set = nd_region->nd_set;
319 	ssize_t rc = 0;
320 
321 	if (is_memory(dev) && nd_set)
322 		/* pass, should be precluded by region_visible */;
323 	else
324 		return -ENXIO;
325 
326 	/*
327 	 * The cookie to show depends on which specification of the
328 	 * labels we are using. If there are not labels then default to
329 	 * the v1.1 namespace label cookie definition. To read all this
330 	 * data we need to wait for probing to settle.
331 	 */
332 	device_lock(dev);
333 	nvdimm_bus_lock(dev);
334 	wait_nvdimm_bus_probe_idle(dev);
335 	if (nd_region->ndr_mappings) {
336 		struct nd_mapping *nd_mapping = &nd_region->mapping[0];
337 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
338 
339 		if (ndd) {
340 			struct nd_namespace_index *nsindex;
341 
342 			nsindex = to_namespace_index(ndd, ndd->ns_current);
343 			rc = sprintf(buf, "%#llx\n",
344 					nd_region_interleave_set_cookie(nd_region,
345 						nsindex));
346 		}
347 	}
348 	nvdimm_bus_unlock(dev);
349 	device_unlock(dev);
350 
351 	if (rc)
352 		return rc;
353 	return sprintf(buf, "%#llx\n", nd_set->cookie1);
354 }
355 static DEVICE_ATTR_RO(set_cookie);
356 
357 resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
358 {
359 	resource_size_t blk_max_overlap = 0, available, overlap;
360 	int i;
361 
362 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
363 
364  retry:
365 	available = 0;
366 	overlap = blk_max_overlap;
367 	for (i = 0; i < nd_region->ndr_mappings; i++) {
368 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
369 		struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
370 
371 		/* if a dimm is disabled the available capacity is zero */
372 		if (!ndd)
373 			return 0;
374 
375 		if (is_memory(&nd_region->dev)) {
376 			available += nd_pmem_available_dpa(nd_region,
377 					nd_mapping, &overlap);
378 			if (overlap > blk_max_overlap) {
379 				blk_max_overlap = overlap;
380 				goto retry;
381 			}
382 		} else if (is_nd_blk(&nd_region->dev))
383 			available += nd_blk_available_dpa(nd_region);
384 	}
385 
386 	return available;
387 }
388 
389 resource_size_t nd_region_allocatable_dpa(struct nd_region *nd_region)
390 {
391 	resource_size_t available = 0;
392 	int i;
393 
394 	if (is_memory(&nd_region->dev))
395 		available = PHYS_ADDR_MAX;
396 
397 	WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
398 	for (i = 0; i < nd_region->ndr_mappings; i++) {
399 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
400 
401 		if (is_memory(&nd_region->dev))
402 			available = min(available,
403 					nd_pmem_max_contiguous_dpa(nd_region,
404 								   nd_mapping));
405 		else if (is_nd_blk(&nd_region->dev))
406 			available += nd_blk_available_dpa(nd_region);
407 	}
408 	if (is_memory(&nd_region->dev))
409 		return available * nd_region->ndr_mappings;
410 	return available;
411 }
412 
413 static ssize_t available_size_show(struct device *dev,
414 		struct device_attribute *attr, char *buf)
415 {
416 	struct nd_region *nd_region = to_nd_region(dev);
417 	unsigned long long available = 0;
418 
419 	/*
420 	 * Flush in-flight updates and grab a snapshot of the available
421 	 * size.  Of course, this value is potentially invalidated the
422 	 * memory nvdimm_bus_lock() is dropped, but that's userspace's
423 	 * problem to not race itself.
424 	 */
425 	nvdimm_bus_lock(dev);
426 	wait_nvdimm_bus_probe_idle(dev);
427 	available = nd_region_available_dpa(nd_region);
428 	nvdimm_bus_unlock(dev);
429 
430 	return sprintf(buf, "%llu\n", available);
431 }
432 static DEVICE_ATTR_RO(available_size);
433 
434 static ssize_t max_available_extent_show(struct device *dev,
435 		struct device_attribute *attr, char *buf)
436 {
437 	struct nd_region *nd_region = to_nd_region(dev);
438 	unsigned long long available = 0;
439 
440 	nvdimm_bus_lock(dev);
441 	wait_nvdimm_bus_probe_idle(dev);
442 	available = nd_region_allocatable_dpa(nd_region);
443 	nvdimm_bus_unlock(dev);
444 
445 	return sprintf(buf, "%llu\n", available);
446 }
447 static DEVICE_ATTR_RO(max_available_extent);
448 
449 static ssize_t init_namespaces_show(struct device *dev,
450 		struct device_attribute *attr, char *buf)
451 {
452 	struct nd_region_data *ndrd = dev_get_drvdata(dev);
453 	ssize_t rc;
454 
455 	nvdimm_bus_lock(dev);
456 	if (ndrd)
457 		rc = sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count);
458 	else
459 		rc = -ENXIO;
460 	nvdimm_bus_unlock(dev);
461 
462 	return rc;
463 }
464 static DEVICE_ATTR_RO(init_namespaces);
465 
466 static ssize_t namespace_seed_show(struct device *dev,
467 		struct device_attribute *attr, char *buf)
468 {
469 	struct nd_region *nd_region = to_nd_region(dev);
470 	ssize_t rc;
471 
472 	nvdimm_bus_lock(dev);
473 	if (nd_region->ns_seed)
474 		rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
475 	else
476 		rc = sprintf(buf, "\n");
477 	nvdimm_bus_unlock(dev);
478 	return rc;
479 }
480 static DEVICE_ATTR_RO(namespace_seed);
481 
482 static ssize_t btt_seed_show(struct device *dev,
483 		struct device_attribute *attr, char *buf)
484 {
485 	struct nd_region *nd_region = to_nd_region(dev);
486 	ssize_t rc;
487 
488 	nvdimm_bus_lock(dev);
489 	if (nd_region->btt_seed)
490 		rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
491 	else
492 		rc = sprintf(buf, "\n");
493 	nvdimm_bus_unlock(dev);
494 
495 	return rc;
496 }
497 static DEVICE_ATTR_RO(btt_seed);
498 
499 static ssize_t pfn_seed_show(struct device *dev,
500 		struct device_attribute *attr, char *buf)
501 {
502 	struct nd_region *nd_region = to_nd_region(dev);
503 	ssize_t rc;
504 
505 	nvdimm_bus_lock(dev);
506 	if (nd_region->pfn_seed)
507 		rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
508 	else
509 		rc = sprintf(buf, "\n");
510 	nvdimm_bus_unlock(dev);
511 
512 	return rc;
513 }
514 static DEVICE_ATTR_RO(pfn_seed);
515 
516 static ssize_t dax_seed_show(struct device *dev,
517 		struct device_attribute *attr, char *buf)
518 {
519 	struct nd_region *nd_region = to_nd_region(dev);
520 	ssize_t rc;
521 
522 	nvdimm_bus_lock(dev);
523 	if (nd_region->dax_seed)
524 		rc = sprintf(buf, "%s\n", dev_name(nd_region->dax_seed));
525 	else
526 		rc = sprintf(buf, "\n");
527 	nvdimm_bus_unlock(dev);
528 
529 	return rc;
530 }
531 static DEVICE_ATTR_RO(dax_seed);
532 
533 static ssize_t read_only_show(struct device *dev,
534 		struct device_attribute *attr, char *buf)
535 {
536 	struct nd_region *nd_region = to_nd_region(dev);
537 
538 	return sprintf(buf, "%d\n", nd_region->ro);
539 }
540 
541 static ssize_t read_only_store(struct device *dev,
542 		struct device_attribute *attr, const char *buf, size_t len)
543 {
544 	bool ro;
545 	int rc = strtobool(buf, &ro);
546 	struct nd_region *nd_region = to_nd_region(dev);
547 
548 	if (rc)
549 		return rc;
550 
551 	nd_region->ro = ro;
552 	return len;
553 }
554 static DEVICE_ATTR_RW(read_only);
555 
556 static ssize_t region_badblocks_show(struct device *dev,
557 		struct device_attribute *attr, char *buf)
558 {
559 	struct nd_region *nd_region = to_nd_region(dev);
560 	ssize_t rc;
561 
562 	device_lock(dev);
563 	if (dev->driver)
564 		rc = badblocks_show(&nd_region->bb, buf, 0);
565 	else
566 		rc = -ENXIO;
567 	device_unlock(dev);
568 
569 	return rc;
570 }
571 static DEVICE_ATTR(badblocks, 0444, region_badblocks_show, NULL);
572 
573 static ssize_t resource_show(struct device *dev,
574 		struct device_attribute *attr, char *buf)
575 {
576 	struct nd_region *nd_region = to_nd_region(dev);
577 
578 	return sprintf(buf, "%#llx\n", nd_region->ndr_start);
579 }
580 static DEVICE_ATTR_RO(resource);
581 
582 static ssize_t persistence_domain_show(struct device *dev,
583 		struct device_attribute *attr, char *buf)
584 {
585 	struct nd_region *nd_region = to_nd_region(dev);
586 
587 	if (test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags))
588 		return sprintf(buf, "cpu_cache\n");
589 	else if (test_bit(ND_REGION_PERSIST_MEMCTRL, &nd_region->flags))
590 		return sprintf(buf, "memory_controller\n");
591 	else
592 		return sprintf(buf, "\n");
593 }
594 static DEVICE_ATTR_RO(persistence_domain);
595 
596 static struct attribute *nd_region_attributes[] = {
597 	&dev_attr_size.attr,
598 	&dev_attr_nstype.attr,
599 	&dev_attr_mappings.attr,
600 	&dev_attr_btt_seed.attr,
601 	&dev_attr_pfn_seed.attr,
602 	&dev_attr_dax_seed.attr,
603 	&dev_attr_deep_flush.attr,
604 	&dev_attr_read_only.attr,
605 	&dev_attr_set_cookie.attr,
606 	&dev_attr_available_size.attr,
607 	&dev_attr_max_available_extent.attr,
608 	&dev_attr_namespace_seed.attr,
609 	&dev_attr_init_namespaces.attr,
610 	&dev_attr_badblocks.attr,
611 	&dev_attr_resource.attr,
612 	&dev_attr_persistence_domain.attr,
613 	NULL,
614 };
615 
616 static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
617 {
618 	struct device *dev = container_of(kobj, typeof(*dev), kobj);
619 	struct nd_region *nd_region = to_nd_region(dev);
620 	struct nd_interleave_set *nd_set = nd_region->nd_set;
621 	int type = nd_region_to_nstype(nd_region);
622 
623 	if (!is_memory(dev) && a == &dev_attr_pfn_seed.attr)
624 		return 0;
625 
626 	if (!is_memory(dev) && a == &dev_attr_dax_seed.attr)
627 		return 0;
628 
629 	if (!is_nd_pmem(dev) && a == &dev_attr_badblocks.attr)
630 		return 0;
631 
632 	if (a == &dev_attr_resource.attr) {
633 		if (is_nd_pmem(dev))
634 			return 0400;
635 		else
636 			return 0;
637 	}
638 
639 	if (a == &dev_attr_deep_flush.attr) {
640 		int has_flush = nvdimm_has_flush(nd_region);
641 
642 		if (has_flush == 1)
643 			return a->mode;
644 		else if (has_flush == 0)
645 			return 0444;
646 		else
647 			return 0;
648 	}
649 
650 	if (a == &dev_attr_persistence_domain.attr) {
651 		if ((nd_region->flags & (BIT(ND_REGION_PERSIST_CACHE)
652 					| BIT(ND_REGION_PERSIST_MEMCTRL))) == 0)
653 			return 0;
654 		return a->mode;
655 	}
656 
657 	if (a != &dev_attr_set_cookie.attr
658 			&& a != &dev_attr_available_size.attr)
659 		return a->mode;
660 
661 	if ((type == ND_DEVICE_NAMESPACE_PMEM
662 				|| type == ND_DEVICE_NAMESPACE_BLK)
663 			&& a == &dev_attr_available_size.attr)
664 		return a->mode;
665 	else if (is_memory(dev) && nd_set)
666 		return a->mode;
667 
668 	return 0;
669 }
670 
671 struct attribute_group nd_region_attribute_group = {
672 	.attrs = nd_region_attributes,
673 	.is_visible = region_visible,
674 };
675 EXPORT_SYMBOL_GPL(nd_region_attribute_group);
676 
677 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
678 		struct nd_namespace_index *nsindex)
679 {
680 	struct nd_interleave_set *nd_set = nd_region->nd_set;
681 
682 	if (!nd_set)
683 		return 0;
684 
685 	if (nsindex && __le16_to_cpu(nsindex->major) == 1
686 			&& __le16_to_cpu(nsindex->minor) == 1)
687 		return nd_set->cookie1;
688 	return nd_set->cookie2;
689 }
690 
691 u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region)
692 {
693 	struct nd_interleave_set *nd_set = nd_region->nd_set;
694 
695 	if (nd_set)
696 		return nd_set->altcookie;
697 	return 0;
698 }
699 
700 void nd_mapping_free_labels(struct nd_mapping *nd_mapping)
701 {
702 	struct nd_label_ent *label_ent, *e;
703 
704 	lockdep_assert_held(&nd_mapping->lock);
705 	list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
706 		list_del(&label_ent->list);
707 		kfree(label_ent);
708 	}
709 }
710 
711 /*
712  * Upon successful probe/remove, take/release a reference on the
713  * associated interleave set (if present), and plant new btt + namespace
714  * seeds.  Also, on the removal of a BLK region, notify the provider to
715  * disable the region.
716  */
717 static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
718 		struct device *dev, bool probe)
719 {
720 	struct nd_region *nd_region;
721 
722 	if (!probe && is_nd_region(dev)) {
723 		int i;
724 
725 		nd_region = to_nd_region(dev);
726 		for (i = 0; i < nd_region->ndr_mappings; i++) {
727 			struct nd_mapping *nd_mapping = &nd_region->mapping[i];
728 			struct nvdimm_drvdata *ndd = nd_mapping->ndd;
729 			struct nvdimm *nvdimm = nd_mapping->nvdimm;
730 
731 			mutex_lock(&nd_mapping->lock);
732 			nd_mapping_free_labels(nd_mapping);
733 			mutex_unlock(&nd_mapping->lock);
734 
735 			put_ndd(ndd);
736 			nd_mapping->ndd = NULL;
737 			if (ndd)
738 				atomic_dec(&nvdimm->busy);
739 		}
740 	}
741 	if (dev->parent && is_nd_region(dev->parent) && probe) {
742 		nd_region = to_nd_region(dev->parent);
743 		nvdimm_bus_lock(dev);
744 		if (nd_region->ns_seed == dev)
745 			nd_region_create_ns_seed(nd_region);
746 		nvdimm_bus_unlock(dev);
747 	}
748 	if (is_nd_btt(dev) && probe) {
749 		struct nd_btt *nd_btt = to_nd_btt(dev);
750 
751 		nd_region = to_nd_region(dev->parent);
752 		nvdimm_bus_lock(dev);
753 		if (nd_region->btt_seed == dev)
754 			nd_region_create_btt_seed(nd_region);
755 		if (nd_region->ns_seed == &nd_btt->ndns->dev)
756 			nd_region_create_ns_seed(nd_region);
757 		nvdimm_bus_unlock(dev);
758 	}
759 	if (is_nd_pfn(dev) && probe) {
760 		struct nd_pfn *nd_pfn = to_nd_pfn(dev);
761 
762 		nd_region = to_nd_region(dev->parent);
763 		nvdimm_bus_lock(dev);
764 		if (nd_region->pfn_seed == dev)
765 			nd_region_create_pfn_seed(nd_region);
766 		if (nd_region->ns_seed == &nd_pfn->ndns->dev)
767 			nd_region_create_ns_seed(nd_region);
768 		nvdimm_bus_unlock(dev);
769 	}
770 	if (is_nd_dax(dev) && probe) {
771 		struct nd_dax *nd_dax = to_nd_dax(dev);
772 
773 		nd_region = to_nd_region(dev->parent);
774 		nvdimm_bus_lock(dev);
775 		if (nd_region->dax_seed == dev)
776 			nd_region_create_dax_seed(nd_region);
777 		if (nd_region->ns_seed == &nd_dax->nd_pfn.ndns->dev)
778 			nd_region_create_ns_seed(nd_region);
779 		nvdimm_bus_unlock(dev);
780 	}
781 }
782 
783 void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
784 {
785 	nd_region_notify_driver_action(nvdimm_bus, dev, true);
786 }
787 
788 void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
789 {
790 	nd_region_notify_driver_action(nvdimm_bus, dev, false);
791 }
792 
793 static ssize_t mappingN(struct device *dev, char *buf, int n)
794 {
795 	struct nd_region *nd_region = to_nd_region(dev);
796 	struct nd_mapping *nd_mapping;
797 	struct nvdimm *nvdimm;
798 
799 	if (n >= nd_region->ndr_mappings)
800 		return -ENXIO;
801 	nd_mapping = &nd_region->mapping[n];
802 	nvdimm = nd_mapping->nvdimm;
803 
804 	return sprintf(buf, "%s,%llu,%llu,%d\n", dev_name(&nvdimm->dev),
805 			nd_mapping->start, nd_mapping->size,
806 			nd_mapping->position);
807 }
808 
809 #define REGION_MAPPING(idx) \
810 static ssize_t mapping##idx##_show(struct device *dev,		\
811 		struct device_attribute *attr, char *buf)	\
812 {								\
813 	return mappingN(dev, buf, idx);				\
814 }								\
815 static DEVICE_ATTR_RO(mapping##idx)
816 
817 /*
818  * 32 should be enough for a while, even in the presence of socket
819  * interleave a 32-way interleave set is a degenerate case.
820  */
821 REGION_MAPPING(0);
822 REGION_MAPPING(1);
823 REGION_MAPPING(2);
824 REGION_MAPPING(3);
825 REGION_MAPPING(4);
826 REGION_MAPPING(5);
827 REGION_MAPPING(6);
828 REGION_MAPPING(7);
829 REGION_MAPPING(8);
830 REGION_MAPPING(9);
831 REGION_MAPPING(10);
832 REGION_MAPPING(11);
833 REGION_MAPPING(12);
834 REGION_MAPPING(13);
835 REGION_MAPPING(14);
836 REGION_MAPPING(15);
837 REGION_MAPPING(16);
838 REGION_MAPPING(17);
839 REGION_MAPPING(18);
840 REGION_MAPPING(19);
841 REGION_MAPPING(20);
842 REGION_MAPPING(21);
843 REGION_MAPPING(22);
844 REGION_MAPPING(23);
845 REGION_MAPPING(24);
846 REGION_MAPPING(25);
847 REGION_MAPPING(26);
848 REGION_MAPPING(27);
849 REGION_MAPPING(28);
850 REGION_MAPPING(29);
851 REGION_MAPPING(30);
852 REGION_MAPPING(31);
853 
854 static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
855 {
856 	struct device *dev = container_of(kobj, struct device, kobj);
857 	struct nd_region *nd_region = to_nd_region(dev);
858 
859 	if (n < nd_region->ndr_mappings)
860 		return a->mode;
861 	return 0;
862 }
863 
864 static struct attribute *mapping_attributes[] = {
865 	&dev_attr_mapping0.attr,
866 	&dev_attr_mapping1.attr,
867 	&dev_attr_mapping2.attr,
868 	&dev_attr_mapping3.attr,
869 	&dev_attr_mapping4.attr,
870 	&dev_attr_mapping5.attr,
871 	&dev_attr_mapping6.attr,
872 	&dev_attr_mapping7.attr,
873 	&dev_attr_mapping8.attr,
874 	&dev_attr_mapping9.attr,
875 	&dev_attr_mapping10.attr,
876 	&dev_attr_mapping11.attr,
877 	&dev_attr_mapping12.attr,
878 	&dev_attr_mapping13.attr,
879 	&dev_attr_mapping14.attr,
880 	&dev_attr_mapping15.attr,
881 	&dev_attr_mapping16.attr,
882 	&dev_attr_mapping17.attr,
883 	&dev_attr_mapping18.attr,
884 	&dev_attr_mapping19.attr,
885 	&dev_attr_mapping20.attr,
886 	&dev_attr_mapping21.attr,
887 	&dev_attr_mapping22.attr,
888 	&dev_attr_mapping23.attr,
889 	&dev_attr_mapping24.attr,
890 	&dev_attr_mapping25.attr,
891 	&dev_attr_mapping26.attr,
892 	&dev_attr_mapping27.attr,
893 	&dev_attr_mapping28.attr,
894 	&dev_attr_mapping29.attr,
895 	&dev_attr_mapping30.attr,
896 	&dev_attr_mapping31.attr,
897 	NULL,
898 };
899 
900 struct attribute_group nd_mapping_attribute_group = {
901 	.is_visible = mapping_visible,
902 	.attrs = mapping_attributes,
903 };
904 EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
905 
906 int nd_blk_region_init(struct nd_region *nd_region)
907 {
908 	struct device *dev = &nd_region->dev;
909 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
910 
911 	if (!is_nd_blk(dev))
912 		return 0;
913 
914 	if (nd_region->ndr_mappings < 1) {
915 		dev_dbg(dev, "invalid BLK region\n");
916 		return -ENXIO;
917 	}
918 
919 	return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
920 }
921 
922 /**
923  * nd_region_acquire_lane - allocate and lock a lane
924  * @nd_region: region id and number of lanes possible
925  *
926  * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
927  * We optimize for the common case where there are 256 lanes, one
928  * per-cpu.  For larger systems we need to lock to share lanes.  For now
929  * this implementation assumes the cost of maintaining an allocator for
930  * free lanes is on the order of the lock hold time, so it implements a
931  * static lane = cpu % num_lanes mapping.
932  *
933  * In the case of a BTT instance on top of a BLK namespace a lane may be
934  * acquired recursively.  We lock on the first instance.
935  *
936  * In the case of a BTT instance on top of PMEM, we only acquire a lane
937  * for the BTT metadata updates.
938  */
939 unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
940 {
941 	unsigned int cpu, lane;
942 
943 	cpu = get_cpu();
944 	if (nd_region->num_lanes < nr_cpu_ids) {
945 		struct nd_percpu_lane *ndl_lock, *ndl_count;
946 
947 		lane = cpu % nd_region->num_lanes;
948 		ndl_count = per_cpu_ptr(nd_region->lane, cpu);
949 		ndl_lock = per_cpu_ptr(nd_region->lane, lane);
950 		if (ndl_count->count++ == 0)
951 			spin_lock(&ndl_lock->lock);
952 	} else
953 		lane = cpu;
954 
955 	return lane;
956 }
957 EXPORT_SYMBOL(nd_region_acquire_lane);
958 
959 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
960 {
961 	if (nd_region->num_lanes < nr_cpu_ids) {
962 		unsigned int cpu = get_cpu();
963 		struct nd_percpu_lane *ndl_lock, *ndl_count;
964 
965 		ndl_count = per_cpu_ptr(nd_region->lane, cpu);
966 		ndl_lock = per_cpu_ptr(nd_region->lane, lane);
967 		if (--ndl_count->count == 0)
968 			spin_unlock(&ndl_lock->lock);
969 		put_cpu();
970 	}
971 	put_cpu();
972 }
973 EXPORT_SYMBOL(nd_region_release_lane);
974 
975 static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
976 		struct nd_region_desc *ndr_desc, struct device_type *dev_type,
977 		const char *caller)
978 {
979 	struct nd_region *nd_region;
980 	struct device *dev;
981 	void *region_buf;
982 	unsigned int i;
983 	int ro = 0;
984 
985 	for (i = 0; i < ndr_desc->num_mappings; i++) {
986 		struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
987 		struct nvdimm *nvdimm = mapping->nvdimm;
988 
989 		if ((mapping->start | mapping->size) % SZ_4K) {
990 			dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
991 					caller, dev_name(&nvdimm->dev), i);
992 
993 			return NULL;
994 		}
995 
996 		if (test_bit(NDD_UNARMED, &nvdimm->flags))
997 			ro = 1;
998 
999 		if (test_bit(NDD_NOBLK, &nvdimm->flags)
1000 				&& dev_type == &nd_blk_device_type) {
1001 			dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not BLK capable\n",
1002 					caller, dev_name(&nvdimm->dev), i);
1003 			return NULL;
1004 		}
1005 	}
1006 
1007 	if (dev_type == &nd_blk_device_type) {
1008 		struct nd_blk_region_desc *ndbr_desc;
1009 		struct nd_blk_region *ndbr;
1010 
1011 		ndbr_desc = to_blk_region_desc(ndr_desc);
1012 		ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
1013 				* ndr_desc->num_mappings,
1014 				GFP_KERNEL);
1015 		if (ndbr) {
1016 			nd_region = &ndbr->nd_region;
1017 			ndbr->enable = ndbr_desc->enable;
1018 			ndbr->do_io = ndbr_desc->do_io;
1019 		}
1020 		region_buf = ndbr;
1021 	} else {
1022 		nd_region = kzalloc(sizeof(struct nd_region)
1023 				+ sizeof(struct nd_mapping)
1024 				* ndr_desc->num_mappings,
1025 				GFP_KERNEL);
1026 		region_buf = nd_region;
1027 	}
1028 
1029 	if (!region_buf)
1030 		return NULL;
1031 	nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
1032 	if (nd_region->id < 0)
1033 		goto err_id;
1034 
1035 	nd_region->lane = alloc_percpu(struct nd_percpu_lane);
1036 	if (!nd_region->lane)
1037 		goto err_percpu;
1038 
1039         for (i = 0; i < nr_cpu_ids; i++) {
1040 		struct nd_percpu_lane *ndl;
1041 
1042 		ndl = per_cpu_ptr(nd_region->lane, i);
1043 		spin_lock_init(&ndl->lock);
1044 		ndl->count = 0;
1045 	}
1046 
1047 	for (i = 0; i < ndr_desc->num_mappings; i++) {
1048 		struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1049 		struct nvdimm *nvdimm = mapping->nvdimm;
1050 
1051 		nd_region->mapping[i].nvdimm = nvdimm;
1052 		nd_region->mapping[i].start = mapping->start;
1053 		nd_region->mapping[i].size = mapping->size;
1054 		nd_region->mapping[i].position = mapping->position;
1055 		INIT_LIST_HEAD(&nd_region->mapping[i].labels);
1056 		mutex_init(&nd_region->mapping[i].lock);
1057 
1058 		get_device(&nvdimm->dev);
1059 	}
1060 	nd_region->ndr_mappings = ndr_desc->num_mappings;
1061 	nd_region->provider_data = ndr_desc->provider_data;
1062 	nd_region->nd_set = ndr_desc->nd_set;
1063 	nd_region->num_lanes = ndr_desc->num_lanes;
1064 	nd_region->flags = ndr_desc->flags;
1065 	nd_region->ro = ro;
1066 	nd_region->numa_node = ndr_desc->numa_node;
1067 	nd_region->target_node = ndr_desc->target_node;
1068 	ida_init(&nd_region->ns_ida);
1069 	ida_init(&nd_region->btt_ida);
1070 	ida_init(&nd_region->pfn_ida);
1071 	ida_init(&nd_region->dax_ida);
1072 	dev = &nd_region->dev;
1073 	dev_set_name(dev, "region%d", nd_region->id);
1074 	dev->parent = &nvdimm_bus->dev;
1075 	dev->type = dev_type;
1076 	dev->groups = ndr_desc->attr_groups;
1077 	dev->of_node = ndr_desc->of_node;
1078 	nd_region->ndr_size = resource_size(ndr_desc->res);
1079 	nd_region->ndr_start = ndr_desc->res->start;
1080 	nd_device_register(dev);
1081 
1082 	return nd_region;
1083 
1084  err_percpu:
1085 	ida_simple_remove(&region_ida, nd_region->id);
1086  err_id:
1087 	kfree(region_buf);
1088 	return NULL;
1089 }
1090 
1091 struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
1092 		struct nd_region_desc *ndr_desc)
1093 {
1094 	ndr_desc->num_lanes = ND_MAX_LANES;
1095 	return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
1096 			__func__);
1097 }
1098 EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
1099 
1100 struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
1101 		struct nd_region_desc *ndr_desc)
1102 {
1103 	if (ndr_desc->num_mappings > 1)
1104 		return NULL;
1105 	ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
1106 	return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
1107 			__func__);
1108 }
1109 EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
1110 
1111 struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
1112 		struct nd_region_desc *ndr_desc)
1113 {
1114 	ndr_desc->num_lanes = ND_MAX_LANES;
1115 	return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
1116 			__func__);
1117 }
1118 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
1119 
1120 /**
1121  * nvdimm_flush - flush any posted write queues between the cpu and pmem media
1122  * @nd_region: blk or interleaved pmem region
1123  */
1124 void nvdimm_flush(struct nd_region *nd_region)
1125 {
1126 	struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
1127 	int i, idx;
1128 
1129 	/*
1130 	 * Try to encourage some diversity in flush hint addresses
1131 	 * across cpus assuming a limited number of flush hints.
1132 	 */
1133 	idx = this_cpu_read(flush_idx);
1134 	idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8));
1135 
1136 	/*
1137 	 * The first wmb() is needed to 'sfence' all previous writes
1138 	 * such that they are architecturally visible for the platform
1139 	 * buffer flush.  Note that we've already arranged for pmem
1140 	 * writes to avoid the cache via memcpy_flushcache().  The final
1141 	 * wmb() ensures ordering for the NVDIMM flush write.
1142 	 */
1143 	wmb();
1144 	for (i = 0; i < nd_region->ndr_mappings; i++)
1145 		if (ndrd_get_flush_wpq(ndrd, i, 0))
1146 			writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
1147 	wmb();
1148 }
1149 EXPORT_SYMBOL_GPL(nvdimm_flush);
1150 
1151 /**
1152  * nvdimm_has_flush - determine write flushing requirements
1153  * @nd_region: blk or interleaved pmem region
1154  *
1155  * Returns 1 if writes require flushing
1156  * Returns 0 if writes do not require flushing
1157  * Returns -ENXIO if flushing capability can not be determined
1158  */
1159 int nvdimm_has_flush(struct nd_region *nd_region)
1160 {
1161 	int i;
1162 
1163 	/* no nvdimm or pmem api == flushing capability unknown */
1164 	if (nd_region->ndr_mappings == 0
1165 			|| !IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API))
1166 		return -ENXIO;
1167 
1168 	for (i = 0; i < nd_region->ndr_mappings; i++) {
1169 		struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1170 		struct nvdimm *nvdimm = nd_mapping->nvdimm;
1171 
1172 		/* flush hints present / available */
1173 		if (nvdimm->num_flush)
1174 			return 1;
1175 	}
1176 
1177 	/*
1178 	 * The platform defines dimm devices without hints, assume
1179 	 * platform persistence mechanism like ADR
1180 	 */
1181 	return 0;
1182 }
1183 EXPORT_SYMBOL_GPL(nvdimm_has_flush);
1184 
1185 int nvdimm_has_cache(struct nd_region *nd_region)
1186 {
1187 	return is_nd_pmem(&nd_region->dev) &&
1188 		!test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags);
1189 }
1190 EXPORT_SYMBOL_GPL(nvdimm_has_cache);
1191 
1192 struct conflict_context {
1193 	struct nd_region *nd_region;
1194 	resource_size_t start, size;
1195 };
1196 
1197 static int region_conflict(struct device *dev, void *data)
1198 {
1199 	struct nd_region *nd_region;
1200 	struct conflict_context *ctx = data;
1201 	resource_size_t res_end, region_end, region_start;
1202 
1203 	if (!is_memory(dev))
1204 		return 0;
1205 
1206 	nd_region = to_nd_region(dev);
1207 	if (nd_region == ctx->nd_region)
1208 		return 0;
1209 
1210 	res_end = ctx->start + ctx->size;
1211 	region_start = nd_region->ndr_start;
1212 	region_end = region_start + nd_region->ndr_size;
1213 	if (ctx->start >= region_start && ctx->start < region_end)
1214 		return -EBUSY;
1215 	if (res_end > region_start && res_end <= region_end)
1216 		return -EBUSY;
1217 	return 0;
1218 }
1219 
1220 int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
1221 		resource_size_t size)
1222 {
1223 	struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
1224 	struct conflict_context ctx = {
1225 		.nd_region = nd_region,
1226 		.start = start,
1227 		.size = size,
1228 	};
1229 
1230 	return device_for_each_child(&nvdimm_bus->dev, &ctx, region_conflict);
1231 }
1232 
1233 void __exit nd_region_devs_exit(void)
1234 {
1235 	ida_destroy(&region_ida);
1236 }
1237