1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/scatterlist.h>
6 #include <linux/memregion.h>
7 #include <linux/highmem.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/hash.h>
11 #include <linux/sort.h>
18 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
21 #include <linux/io-64-nonatomic-hi-lo.h>
23 static DEFINE_PER_CPU(int, flush_idx);
25 static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm,
26 struct nd_region_data *ndrd)
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;
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);
47 flush_page = (void __iomem *) ((unsigned long)
48 ndrd_get_flush_wpq(ndrd, dimm, j)
51 flush_page = devm_nvdimm_ioremap(dev,
52 PFN_PHYS(pfn), PAGE_SIZE);
55 ndrd_set_flush_wpq(ndrd, dimm, i, flush_page
56 + (res->start & ~PAGE_MASK));
62 int nd_region_activate(struct nd_region *nd_region)
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 *);
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;
74 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
75 nvdimm_bus_unlock(&nd_region->dev);
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)
84 flush_data_size += nvdimm->num_flush * sizeof(void *);
86 nvdimm_bus_unlock(&nd_region->dev);
88 ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
91 dev_set_drvdata(dev, ndrd);
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);
107 * Clear out entries that are duplicates. This should prevent the
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))
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);
124 static void nd_region_release(struct device *dev)
126 struct nd_region *nd_region = to_nd_region(dev);
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;
133 put_device(&nvdimm->dev);
135 free_percpu(nd_region->lane);
136 memregion_free(nd_region->id);
138 kfree(to_nd_blk_region(dev));
143 struct nd_region *to_nd_region(struct device *dev)
145 struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
147 WARN_ON(dev->type->release != nd_region_release);
150 EXPORT_SYMBOL_GPL(to_nd_region);
152 struct device *nd_region_dev(struct nd_region *nd_region)
156 return &nd_region->dev;
158 EXPORT_SYMBOL_GPL(nd_region_dev);
160 struct nd_blk_region *to_nd_blk_region(struct device *dev)
162 struct nd_region *nd_region = to_nd_region(dev);
164 WARN_ON(!is_nd_blk(dev));
165 return container_of(nd_region, struct nd_blk_region, nd_region);
167 EXPORT_SYMBOL_GPL(to_nd_blk_region);
169 void *nd_region_provider_data(struct nd_region *nd_region)
171 return nd_region->provider_data;
173 EXPORT_SYMBOL_GPL(nd_region_provider_data);
175 void *nd_blk_region_provider_data(struct nd_blk_region *ndbr)
177 return ndbr->blk_provider_data;
179 EXPORT_SYMBOL_GPL(nd_blk_region_provider_data);
181 void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data)
183 ndbr->blk_provider_data = data;
185 EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data);
188 * nd_region_to_nstype() - region to an integer namespace type
189 * @nd_region: region-device to interrogate
191 * This is the 'nstype' attribute of a region as well, an input to the
192 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
193 * namespace devices with namespace drivers.
195 int nd_region_to_nstype(struct nd_region *nd_region)
197 if (is_memory(&nd_region->dev)) {
200 for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
201 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
202 struct nvdimm *nvdimm = nd_mapping->nvdimm;
204 if (test_bit(NDD_ALIASING, &nvdimm->flags))
208 return ND_DEVICE_NAMESPACE_PMEM;
210 return ND_DEVICE_NAMESPACE_IO;
211 } else if (is_nd_blk(&nd_region->dev)) {
212 return ND_DEVICE_NAMESPACE_BLK;
217 EXPORT_SYMBOL(nd_region_to_nstype);
219 static ssize_t size_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct nd_region *nd_region = to_nd_region(dev);
223 unsigned long long size = 0;
225 if (is_memory(dev)) {
226 size = nd_region->ndr_size;
227 } else if (nd_region->ndr_mappings == 1) {
228 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
230 size = nd_mapping->size;
233 return sprintf(buf, "%llu\n", size);
235 static DEVICE_ATTR_RO(size);
237 static ssize_t deep_flush_show(struct device *dev,
238 struct device_attribute *attr, char *buf)
240 struct nd_region *nd_region = to_nd_region(dev);
243 * NOTE: in the nvdimm_has_flush() error case this attribute is
246 return sprintf(buf, "%d\n", nvdimm_has_flush(nd_region));
249 static ssize_t deep_flush_store(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t len)
253 int rc = strtobool(buf, &flush);
254 struct nd_region *nd_region = to_nd_region(dev);
260 rc = nvdimm_flush(nd_region, NULL);
266 static DEVICE_ATTR_RW(deep_flush);
268 static ssize_t mappings_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct nd_region *nd_region = to_nd_region(dev);
273 return sprintf(buf, "%d\n", nd_region->ndr_mappings);
275 static DEVICE_ATTR_RO(mappings);
277 static ssize_t nstype_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct nd_region *nd_region = to_nd_region(dev);
282 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
284 static DEVICE_ATTR_RO(nstype);
286 static ssize_t set_cookie_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct nd_region *nd_region = to_nd_region(dev);
290 struct nd_interleave_set *nd_set = nd_region->nd_set;
293 if (is_memory(dev) && nd_set)
294 /* pass, should be precluded by region_visible */;
299 * The cookie to show depends on which specification of the
300 * labels we are using. If there are not labels then default to
301 * the v1.1 namespace label cookie definition. To read all this
302 * data we need to wait for probing to settle.
305 nvdimm_bus_lock(dev);
306 wait_nvdimm_bus_probe_idle(dev);
307 if (nd_region->ndr_mappings) {
308 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
309 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
312 struct nd_namespace_index *nsindex;
314 nsindex = to_namespace_index(ndd, ndd->ns_current);
315 rc = sprintf(buf, "%#llx\n",
316 nd_region_interleave_set_cookie(nd_region,
320 nvdimm_bus_unlock(dev);
321 nd_device_unlock(dev);
325 return sprintf(buf, "%#llx\n", nd_set->cookie1);
327 static DEVICE_ATTR_RO(set_cookie);
329 resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
331 resource_size_t blk_max_overlap = 0, available, overlap;
334 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
338 overlap = blk_max_overlap;
339 for (i = 0; i < nd_region->ndr_mappings; i++) {
340 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
341 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
343 /* if a dimm is disabled the available capacity is zero */
347 if (is_memory(&nd_region->dev)) {
348 available += nd_pmem_available_dpa(nd_region,
349 nd_mapping, &overlap);
350 if (overlap > blk_max_overlap) {
351 blk_max_overlap = overlap;
354 } else if (is_nd_blk(&nd_region->dev))
355 available += nd_blk_available_dpa(nd_region);
361 resource_size_t nd_region_allocatable_dpa(struct nd_region *nd_region)
363 resource_size_t available = 0;
366 if (is_memory(&nd_region->dev))
367 available = PHYS_ADDR_MAX;
369 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
370 for (i = 0; i < nd_region->ndr_mappings; i++) {
371 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
373 if (is_memory(&nd_region->dev))
374 available = min(available,
375 nd_pmem_max_contiguous_dpa(nd_region,
377 else if (is_nd_blk(&nd_region->dev))
378 available += nd_blk_available_dpa(nd_region);
380 if (is_memory(&nd_region->dev))
381 return available * nd_region->ndr_mappings;
385 static ssize_t available_size_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
388 struct nd_region *nd_region = to_nd_region(dev);
389 unsigned long long available = 0;
392 * Flush in-flight updates and grab a snapshot of the available
393 * size. Of course, this value is potentially invalidated the
394 * memory nvdimm_bus_lock() is dropped, but that's userspace's
395 * problem to not race itself.
398 nvdimm_bus_lock(dev);
399 wait_nvdimm_bus_probe_idle(dev);
400 available = nd_region_available_dpa(nd_region);
401 nvdimm_bus_unlock(dev);
402 nd_device_unlock(dev);
404 return sprintf(buf, "%llu\n", available);
406 static DEVICE_ATTR_RO(available_size);
408 static ssize_t max_available_extent_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
411 struct nd_region *nd_region = to_nd_region(dev);
412 unsigned long long available = 0;
415 nvdimm_bus_lock(dev);
416 wait_nvdimm_bus_probe_idle(dev);
417 available = nd_region_allocatable_dpa(nd_region);
418 nvdimm_bus_unlock(dev);
419 nd_device_unlock(dev);
421 return sprintf(buf, "%llu\n", available);
423 static DEVICE_ATTR_RO(max_available_extent);
425 static ssize_t init_namespaces_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct nd_region_data *ndrd = dev_get_drvdata(dev);
431 nvdimm_bus_lock(dev);
433 rc = sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count);
436 nvdimm_bus_unlock(dev);
440 static DEVICE_ATTR_RO(init_namespaces);
442 static ssize_t namespace_seed_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct nd_region *nd_region = to_nd_region(dev);
448 nvdimm_bus_lock(dev);
449 if (nd_region->ns_seed)
450 rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
452 rc = sprintf(buf, "\n");
453 nvdimm_bus_unlock(dev);
456 static DEVICE_ATTR_RO(namespace_seed);
458 static ssize_t btt_seed_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
461 struct nd_region *nd_region = to_nd_region(dev);
464 nvdimm_bus_lock(dev);
465 if (nd_region->btt_seed)
466 rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
468 rc = sprintf(buf, "\n");
469 nvdimm_bus_unlock(dev);
473 static DEVICE_ATTR_RO(btt_seed);
475 static ssize_t pfn_seed_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct nd_region *nd_region = to_nd_region(dev);
481 nvdimm_bus_lock(dev);
482 if (nd_region->pfn_seed)
483 rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
485 rc = sprintf(buf, "\n");
486 nvdimm_bus_unlock(dev);
490 static DEVICE_ATTR_RO(pfn_seed);
492 static ssize_t dax_seed_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct nd_region *nd_region = to_nd_region(dev);
498 nvdimm_bus_lock(dev);
499 if (nd_region->dax_seed)
500 rc = sprintf(buf, "%s\n", dev_name(nd_region->dax_seed));
502 rc = sprintf(buf, "\n");
503 nvdimm_bus_unlock(dev);
507 static DEVICE_ATTR_RO(dax_seed);
509 static ssize_t read_only_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct nd_region *nd_region = to_nd_region(dev);
514 return sprintf(buf, "%d\n", nd_region->ro);
517 static ssize_t read_only_store(struct device *dev,
518 struct device_attribute *attr, const char *buf, size_t len)
521 int rc = strtobool(buf, &ro);
522 struct nd_region *nd_region = to_nd_region(dev);
530 static DEVICE_ATTR_RW(read_only);
532 static ssize_t region_badblocks_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct nd_region *nd_region = to_nd_region(dev);
540 rc = badblocks_show(&nd_region->bb, buf, 0);
543 nd_device_unlock(dev);
547 static DEVICE_ATTR(badblocks, 0444, region_badblocks_show, NULL);
549 static ssize_t resource_show(struct device *dev,
550 struct device_attribute *attr, char *buf)
552 struct nd_region *nd_region = to_nd_region(dev);
554 return sprintf(buf, "%#llx\n", nd_region->ndr_start);
556 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
558 static ssize_t persistence_domain_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
561 struct nd_region *nd_region = to_nd_region(dev);
563 if (test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags))
564 return sprintf(buf, "cpu_cache\n");
565 else if (test_bit(ND_REGION_PERSIST_MEMCTRL, &nd_region->flags))
566 return sprintf(buf, "memory_controller\n");
568 return sprintf(buf, "\n");
570 static DEVICE_ATTR_RO(persistence_domain);
572 static struct attribute *nd_region_attributes[] = {
574 &dev_attr_nstype.attr,
575 &dev_attr_mappings.attr,
576 &dev_attr_btt_seed.attr,
577 &dev_attr_pfn_seed.attr,
578 &dev_attr_dax_seed.attr,
579 &dev_attr_deep_flush.attr,
580 &dev_attr_read_only.attr,
581 &dev_attr_set_cookie.attr,
582 &dev_attr_available_size.attr,
583 &dev_attr_max_available_extent.attr,
584 &dev_attr_namespace_seed.attr,
585 &dev_attr_init_namespaces.attr,
586 &dev_attr_badblocks.attr,
587 &dev_attr_resource.attr,
588 &dev_attr_persistence_domain.attr,
592 static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
594 struct device *dev = container_of(kobj, typeof(*dev), kobj);
595 struct nd_region *nd_region = to_nd_region(dev);
596 struct nd_interleave_set *nd_set = nd_region->nd_set;
597 int type = nd_region_to_nstype(nd_region);
599 if (!is_memory(dev) && a == &dev_attr_pfn_seed.attr)
602 if (!is_memory(dev) && a == &dev_attr_dax_seed.attr)
605 if (!is_memory(dev) && a == &dev_attr_badblocks.attr)
608 if (a == &dev_attr_resource.attr && !is_memory(dev))
611 if (a == &dev_attr_deep_flush.attr) {
612 int has_flush = nvdimm_has_flush(nd_region);
616 else if (has_flush == 0)
622 if (a == &dev_attr_persistence_domain.attr) {
623 if ((nd_region->flags & (BIT(ND_REGION_PERSIST_CACHE)
624 | BIT(ND_REGION_PERSIST_MEMCTRL))) == 0)
629 if (a != &dev_attr_set_cookie.attr
630 && a != &dev_attr_available_size.attr)
633 if ((type == ND_DEVICE_NAMESPACE_PMEM
634 || type == ND_DEVICE_NAMESPACE_BLK)
635 && a == &dev_attr_available_size.attr)
637 else if (is_memory(dev) && nd_set)
643 static ssize_t mappingN(struct device *dev, char *buf, int n)
645 struct nd_region *nd_region = to_nd_region(dev);
646 struct nd_mapping *nd_mapping;
647 struct nvdimm *nvdimm;
649 if (n >= nd_region->ndr_mappings)
651 nd_mapping = &nd_region->mapping[n];
652 nvdimm = nd_mapping->nvdimm;
654 return sprintf(buf, "%s,%llu,%llu,%d\n", dev_name(&nvdimm->dev),
655 nd_mapping->start, nd_mapping->size,
656 nd_mapping->position);
659 #define REGION_MAPPING(idx) \
660 static ssize_t mapping##idx##_show(struct device *dev, \
661 struct device_attribute *attr, char *buf) \
663 return mappingN(dev, buf, idx); \
665 static DEVICE_ATTR_RO(mapping##idx)
668 * 32 should be enough for a while, even in the presence of socket
669 * interleave a 32-way interleave set is a degenerate case.
704 static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
706 struct device *dev = container_of(kobj, struct device, kobj);
707 struct nd_region *nd_region = to_nd_region(dev);
709 if (n < nd_region->ndr_mappings)
714 static struct attribute *mapping_attributes[] = {
715 &dev_attr_mapping0.attr,
716 &dev_attr_mapping1.attr,
717 &dev_attr_mapping2.attr,
718 &dev_attr_mapping3.attr,
719 &dev_attr_mapping4.attr,
720 &dev_attr_mapping5.attr,
721 &dev_attr_mapping6.attr,
722 &dev_attr_mapping7.attr,
723 &dev_attr_mapping8.attr,
724 &dev_attr_mapping9.attr,
725 &dev_attr_mapping10.attr,
726 &dev_attr_mapping11.attr,
727 &dev_attr_mapping12.attr,
728 &dev_attr_mapping13.attr,
729 &dev_attr_mapping14.attr,
730 &dev_attr_mapping15.attr,
731 &dev_attr_mapping16.attr,
732 &dev_attr_mapping17.attr,
733 &dev_attr_mapping18.attr,
734 &dev_attr_mapping19.attr,
735 &dev_attr_mapping20.attr,
736 &dev_attr_mapping21.attr,
737 &dev_attr_mapping22.attr,
738 &dev_attr_mapping23.attr,
739 &dev_attr_mapping24.attr,
740 &dev_attr_mapping25.attr,
741 &dev_attr_mapping26.attr,
742 &dev_attr_mapping27.attr,
743 &dev_attr_mapping28.attr,
744 &dev_attr_mapping29.attr,
745 &dev_attr_mapping30.attr,
746 &dev_attr_mapping31.attr,
750 static const struct attribute_group nd_mapping_attribute_group = {
751 .is_visible = mapping_visible,
752 .attrs = mapping_attributes,
755 static const struct attribute_group nd_region_attribute_group = {
756 .attrs = nd_region_attributes,
757 .is_visible = region_visible,
760 static const struct attribute_group *nd_region_attribute_groups[] = {
761 &nd_device_attribute_group,
762 &nd_region_attribute_group,
763 &nd_numa_attribute_group,
764 &nd_mapping_attribute_group,
768 static const struct device_type nd_blk_device_type = {
770 .release = nd_region_release,
771 .groups = nd_region_attribute_groups,
774 static const struct device_type nd_pmem_device_type = {
776 .release = nd_region_release,
777 .groups = nd_region_attribute_groups,
780 static const struct device_type nd_volatile_device_type = {
781 .name = "nd_volatile",
782 .release = nd_region_release,
783 .groups = nd_region_attribute_groups,
786 bool is_nd_pmem(struct device *dev)
788 return dev ? dev->type == &nd_pmem_device_type : false;
791 bool is_nd_blk(struct device *dev)
793 return dev ? dev->type == &nd_blk_device_type : false;
796 bool is_nd_volatile(struct device *dev)
798 return dev ? dev->type == &nd_volatile_device_type : false;
801 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
802 struct nd_namespace_index *nsindex)
804 struct nd_interleave_set *nd_set = nd_region->nd_set;
809 if (nsindex && __le16_to_cpu(nsindex->major) == 1
810 && __le16_to_cpu(nsindex->minor) == 1)
811 return nd_set->cookie1;
812 return nd_set->cookie2;
815 u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region)
817 struct nd_interleave_set *nd_set = nd_region->nd_set;
820 return nd_set->altcookie;
824 void nd_mapping_free_labels(struct nd_mapping *nd_mapping)
826 struct nd_label_ent *label_ent, *e;
828 lockdep_assert_held(&nd_mapping->lock);
829 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
830 list_del(&label_ent->list);
836 * When a namespace is activated create new seeds for the next
837 * namespace, or namespace-personality to be configured.
839 void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev)
841 nvdimm_bus_lock(dev);
842 if (nd_region->ns_seed == dev) {
843 nd_region_create_ns_seed(nd_region);
844 } else if (is_nd_btt(dev)) {
845 struct nd_btt *nd_btt = to_nd_btt(dev);
847 if (nd_region->btt_seed == dev)
848 nd_region_create_btt_seed(nd_region);
849 if (nd_region->ns_seed == &nd_btt->ndns->dev)
850 nd_region_create_ns_seed(nd_region);
851 } else if (is_nd_pfn(dev)) {
852 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
854 if (nd_region->pfn_seed == dev)
855 nd_region_create_pfn_seed(nd_region);
856 if (nd_region->ns_seed == &nd_pfn->ndns->dev)
857 nd_region_create_ns_seed(nd_region);
858 } else if (is_nd_dax(dev)) {
859 struct nd_dax *nd_dax = to_nd_dax(dev);
861 if (nd_region->dax_seed == dev)
862 nd_region_create_dax_seed(nd_region);
863 if (nd_region->ns_seed == &nd_dax->nd_pfn.ndns->dev)
864 nd_region_create_ns_seed(nd_region);
866 nvdimm_bus_unlock(dev);
869 int nd_blk_region_init(struct nd_region *nd_region)
871 struct device *dev = &nd_region->dev;
872 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
877 if (nd_region->ndr_mappings < 1) {
878 dev_dbg(dev, "invalid BLK region\n");
882 return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
886 * nd_region_acquire_lane - allocate and lock a lane
887 * @nd_region: region id and number of lanes possible
889 * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
890 * We optimize for the common case where there are 256 lanes, one
891 * per-cpu. For larger systems we need to lock to share lanes. For now
892 * this implementation assumes the cost of maintaining an allocator for
893 * free lanes is on the order of the lock hold time, so it implements a
894 * static lane = cpu % num_lanes mapping.
896 * In the case of a BTT instance on top of a BLK namespace a lane may be
897 * acquired recursively. We lock on the first instance.
899 * In the case of a BTT instance on top of PMEM, we only acquire a lane
900 * for the BTT metadata updates.
902 unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
904 unsigned int cpu, lane;
907 if (nd_region->num_lanes < nr_cpu_ids) {
908 struct nd_percpu_lane *ndl_lock, *ndl_count;
910 lane = cpu % nd_region->num_lanes;
911 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
912 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
913 if (ndl_count->count++ == 0)
914 spin_lock(&ndl_lock->lock);
920 EXPORT_SYMBOL(nd_region_acquire_lane);
922 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
924 if (nd_region->num_lanes < nr_cpu_ids) {
925 unsigned int cpu = get_cpu();
926 struct nd_percpu_lane *ndl_lock, *ndl_count;
928 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
929 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
930 if (--ndl_count->count == 0)
931 spin_unlock(&ndl_lock->lock);
936 EXPORT_SYMBOL(nd_region_release_lane);
938 static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
939 struct nd_region_desc *ndr_desc,
940 const struct device_type *dev_type, const char *caller)
942 struct nd_region *nd_region;
948 for (i = 0; i < ndr_desc->num_mappings; i++) {
949 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
950 struct nvdimm *nvdimm = mapping->nvdimm;
952 if ((mapping->start | mapping->size) % PAGE_SIZE) {
953 dev_err(&nvdimm_bus->dev,
954 "%s: %s mapping%d is not %ld aligned\n",
955 caller, dev_name(&nvdimm->dev), i, PAGE_SIZE);
959 if (test_bit(NDD_UNARMED, &nvdimm->flags))
962 if (test_bit(NDD_NOBLK, &nvdimm->flags)
963 && dev_type == &nd_blk_device_type) {
964 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not BLK capable\n",
965 caller, dev_name(&nvdimm->dev), i);
970 if (dev_type == &nd_blk_device_type) {
971 struct nd_blk_region_desc *ndbr_desc;
972 struct nd_blk_region *ndbr;
974 ndbr_desc = to_blk_region_desc(ndr_desc);
975 ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
976 * ndr_desc->num_mappings,
979 nd_region = &ndbr->nd_region;
980 ndbr->enable = ndbr_desc->enable;
981 ndbr->do_io = ndbr_desc->do_io;
985 nd_region = kzalloc(struct_size(nd_region, mapping,
986 ndr_desc->num_mappings),
988 region_buf = nd_region;
993 nd_region->id = memregion_alloc(GFP_KERNEL);
994 if (nd_region->id < 0)
997 nd_region->lane = alloc_percpu(struct nd_percpu_lane);
998 if (!nd_region->lane)
1001 for (i = 0; i < nr_cpu_ids; i++) {
1002 struct nd_percpu_lane *ndl;
1004 ndl = per_cpu_ptr(nd_region->lane, i);
1005 spin_lock_init(&ndl->lock);
1009 for (i = 0; i < ndr_desc->num_mappings; i++) {
1010 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1011 struct nvdimm *nvdimm = mapping->nvdimm;
1013 nd_region->mapping[i].nvdimm = nvdimm;
1014 nd_region->mapping[i].start = mapping->start;
1015 nd_region->mapping[i].size = mapping->size;
1016 nd_region->mapping[i].position = mapping->position;
1017 INIT_LIST_HEAD(&nd_region->mapping[i].labels);
1018 mutex_init(&nd_region->mapping[i].lock);
1020 get_device(&nvdimm->dev);
1022 nd_region->ndr_mappings = ndr_desc->num_mappings;
1023 nd_region->provider_data = ndr_desc->provider_data;
1024 nd_region->nd_set = ndr_desc->nd_set;
1025 nd_region->num_lanes = ndr_desc->num_lanes;
1026 nd_region->flags = ndr_desc->flags;
1028 nd_region->numa_node = ndr_desc->numa_node;
1029 nd_region->target_node = ndr_desc->target_node;
1030 ida_init(&nd_region->ns_ida);
1031 ida_init(&nd_region->btt_ida);
1032 ida_init(&nd_region->pfn_ida);
1033 ida_init(&nd_region->dax_ida);
1034 dev = &nd_region->dev;
1035 dev_set_name(dev, "region%d", nd_region->id);
1036 dev->parent = &nvdimm_bus->dev;
1037 dev->type = dev_type;
1038 dev->groups = ndr_desc->attr_groups;
1039 dev->of_node = ndr_desc->of_node;
1040 nd_region->ndr_size = resource_size(ndr_desc->res);
1041 nd_region->ndr_start = ndr_desc->res->start;
1042 if (ndr_desc->flush)
1043 nd_region->flush = ndr_desc->flush;
1045 nd_region->flush = NULL;
1047 nd_device_register(dev);
1052 memregion_free(nd_region->id);
1058 struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
1059 struct nd_region_desc *ndr_desc)
1061 ndr_desc->num_lanes = ND_MAX_LANES;
1062 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
1065 EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
1067 struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
1068 struct nd_region_desc *ndr_desc)
1070 if (ndr_desc->num_mappings > 1)
1072 ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
1073 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
1076 EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
1078 struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
1079 struct nd_region_desc *ndr_desc)
1081 ndr_desc->num_lanes = ND_MAX_LANES;
1082 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
1085 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
1087 int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
1091 if (!nd_region->flush)
1092 rc = generic_nvdimm_flush(nd_region);
1094 if (nd_region->flush(nd_region, bio))
1101 * nvdimm_flush - flush any posted write queues between the cpu and pmem media
1102 * @nd_region: blk or interleaved pmem region
1104 int generic_nvdimm_flush(struct nd_region *nd_region)
1106 struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
1110 * Try to encourage some diversity in flush hint addresses
1111 * across cpus assuming a limited number of flush hints.
1113 idx = this_cpu_read(flush_idx);
1114 idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8));
1117 * The first wmb() is needed to 'sfence' all previous writes
1118 * such that they are architecturally visible for the platform
1119 * buffer flush. Note that we've already arranged for pmem
1120 * writes to avoid the cache via memcpy_flushcache(). The final
1121 * wmb() ensures ordering for the NVDIMM flush write.
1124 for (i = 0; i < nd_region->ndr_mappings; i++)
1125 if (ndrd_get_flush_wpq(ndrd, i, 0))
1126 writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
1131 EXPORT_SYMBOL_GPL(nvdimm_flush);
1134 * nvdimm_has_flush - determine write flushing requirements
1135 * @nd_region: blk or interleaved pmem region
1137 * Returns 1 if writes require flushing
1138 * Returns 0 if writes do not require flushing
1139 * Returns -ENXIO if flushing capability can not be determined
1141 int nvdimm_has_flush(struct nd_region *nd_region)
1145 /* no nvdimm or pmem api == flushing capability unknown */
1146 if (nd_region->ndr_mappings == 0
1147 || !IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API))
1150 for (i = 0; i < nd_region->ndr_mappings; i++) {
1151 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1152 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1154 /* flush hints present / available */
1155 if (nvdimm->num_flush)
1160 * The platform defines dimm devices without hints, assume
1161 * platform persistence mechanism like ADR
1165 EXPORT_SYMBOL_GPL(nvdimm_has_flush);
1167 int nvdimm_has_cache(struct nd_region *nd_region)
1169 return is_nd_pmem(&nd_region->dev) &&
1170 !test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags);
1172 EXPORT_SYMBOL_GPL(nvdimm_has_cache);
1174 bool is_nvdimm_sync(struct nd_region *nd_region)
1176 if (is_nd_volatile(&nd_region->dev))
1179 return is_nd_pmem(&nd_region->dev) &&
1180 !test_bit(ND_REGION_ASYNC, &nd_region->flags);
1182 EXPORT_SYMBOL_GPL(is_nvdimm_sync);
1184 struct conflict_context {
1185 struct nd_region *nd_region;
1186 resource_size_t start, size;
1189 static int region_conflict(struct device *dev, void *data)
1191 struct nd_region *nd_region;
1192 struct conflict_context *ctx = data;
1193 resource_size_t res_end, region_end, region_start;
1195 if (!is_memory(dev))
1198 nd_region = to_nd_region(dev);
1199 if (nd_region == ctx->nd_region)
1202 res_end = ctx->start + ctx->size;
1203 region_start = nd_region->ndr_start;
1204 region_end = region_start + nd_region->ndr_size;
1205 if (ctx->start >= region_start && ctx->start < region_end)
1207 if (res_end > region_start && res_end <= region_end)
1212 int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
1213 resource_size_t size)
1215 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
1216 struct conflict_context ctx = {
1217 .nd_region = nd_region,
1222 return device_for_each_child(&nvdimm_bus->dev, &ctx, region_conflict);