2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/scatterlist.h>
14 #include <linux/highmem.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/hash.h>
18 #include <linux/pmem.h>
19 #include <linux/sort.h>
26 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
29 #include <linux/io-64-nonatomic-hi-lo.h>
31 static DEFINE_IDA(region_ida
);
32 static DEFINE_PER_CPU(int, flush_idx
);
34 static int nvdimm_map_flush(struct device
*dev
, struct nvdimm
*nvdimm
, int dimm
,
35 struct nd_region_data
*ndrd
)
39 dev_dbg(dev
, "%s: map %d flush address%s\n", nvdimm_name(nvdimm
),
40 nvdimm
->num_flush
, nvdimm
->num_flush
== 1 ? "" : "es");
41 for (i
= 0; i
< nvdimm
->num_flush
; i
++) {
42 struct resource
*res
= &nvdimm
->flush_wpq
[i
];
43 unsigned long pfn
= PHYS_PFN(res
->start
);
44 void __iomem
*flush_page
;
46 /* check if flush hints share a page */
47 for (j
= 0; j
< i
; j
++) {
48 struct resource
*res_j
= &nvdimm
->flush_wpq
[j
];
49 unsigned long pfn_j
= PHYS_PFN(res_j
->start
);
56 flush_page
= (void __iomem
*) ((unsigned long)
57 ndrd
->flush_wpq
[dimm
][j
] & PAGE_MASK
);
59 flush_page
= devm_nvdimm_ioremap(dev
,
60 PHYS_PFN(pfn
), PAGE_SIZE
);
63 ndrd
->flush_wpq
[dimm
][i
] = flush_page
64 + (res
->start
& ~PAGE_MASK
);
70 int nd_region_activate(struct nd_region
*nd_region
)
73 struct nd_region_data
*ndrd
;
74 struct device
*dev
= &nd_region
->dev
;
75 size_t flush_data_size
= sizeof(void *);
77 nvdimm_bus_lock(&nd_region
->dev
);
78 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
79 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
80 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
82 /* at least one null hint slot per-dimm for the "no-hint" case */
83 flush_data_size
+= sizeof(void *);
84 num_flush
= min_not_zero(num_flush
, nvdimm
->num_flush
);
85 if (!nvdimm
->num_flush
)
87 flush_data_size
+= nvdimm
->num_flush
* sizeof(void *);
89 nvdimm_bus_unlock(&nd_region
->dev
);
91 ndrd
= devm_kzalloc(dev
, sizeof(*ndrd
) + flush_data_size
, GFP_KERNEL
);
94 dev_set_drvdata(dev
, ndrd
);
96 ndrd
->flush_mask
= (1 << ilog2(num_flush
)) - 1;
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
);
109 static void nd_region_release(struct device
*dev
)
111 struct nd_region
*nd_region
= to_nd_region(dev
);
114 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
115 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
116 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
118 put_device(&nvdimm
->dev
);
120 free_percpu(nd_region
->lane
);
121 ida_simple_remove(®ion_ida
, nd_region
->id
);
123 kfree(to_nd_blk_region(dev
));
128 static struct device_type nd_blk_device_type
= {
130 .release
= nd_region_release
,
133 static struct device_type nd_pmem_device_type
= {
135 .release
= nd_region_release
,
138 static struct device_type nd_volatile_device_type
= {
139 .name
= "nd_volatile",
140 .release
= nd_region_release
,
143 bool is_nd_pmem(struct device
*dev
)
145 return dev
? dev
->type
== &nd_pmem_device_type
: false;
148 bool is_nd_blk(struct device
*dev
)
150 return dev
? dev
->type
== &nd_blk_device_type
: false;
153 struct nd_region
*to_nd_region(struct device
*dev
)
155 struct nd_region
*nd_region
= container_of(dev
, struct nd_region
, dev
);
157 WARN_ON(dev
->type
->release
!= nd_region_release
);
160 EXPORT_SYMBOL_GPL(to_nd_region
);
162 struct nd_blk_region
*to_nd_blk_region(struct device
*dev
)
164 struct nd_region
*nd_region
= to_nd_region(dev
);
166 WARN_ON(!is_nd_blk(dev
));
167 return container_of(nd_region
, struct nd_blk_region
, nd_region
);
169 EXPORT_SYMBOL_GPL(to_nd_blk_region
);
171 void *nd_region_provider_data(struct nd_region
*nd_region
)
173 return nd_region
->provider_data
;
175 EXPORT_SYMBOL_GPL(nd_region_provider_data
);
177 void *nd_blk_region_provider_data(struct nd_blk_region
*ndbr
)
179 return ndbr
->blk_provider_data
;
181 EXPORT_SYMBOL_GPL(nd_blk_region_provider_data
);
183 void nd_blk_region_set_provider_data(struct nd_blk_region
*ndbr
, void *data
)
185 ndbr
->blk_provider_data
= data
;
187 EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data
);
190 * nd_region_to_nstype() - region to an integer namespace type
191 * @nd_region: region-device to interrogate
193 * This is the 'nstype' attribute of a region as well, an input to the
194 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
195 * namespace devices with namespace drivers.
197 int nd_region_to_nstype(struct nd_region
*nd_region
)
199 if (is_nd_pmem(&nd_region
->dev
)) {
202 for (i
= 0, alias
= 0; i
< nd_region
->ndr_mappings
; i
++) {
203 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
204 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
206 if (nvdimm
->flags
& NDD_ALIASING
)
210 return ND_DEVICE_NAMESPACE_PMEM
;
212 return ND_DEVICE_NAMESPACE_IO
;
213 } else if (is_nd_blk(&nd_region
->dev
)) {
214 return ND_DEVICE_NAMESPACE_BLK
;
219 EXPORT_SYMBOL(nd_region_to_nstype
);
221 static ssize_t
size_show(struct device
*dev
,
222 struct device_attribute
*attr
, char *buf
)
224 struct nd_region
*nd_region
= to_nd_region(dev
);
225 unsigned long long size
= 0;
227 if (is_nd_pmem(dev
)) {
228 size
= nd_region
->ndr_size
;
229 } else if (nd_region
->ndr_mappings
== 1) {
230 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
232 size
= nd_mapping
->size
;
235 return sprintf(buf
, "%llu\n", size
);
237 static DEVICE_ATTR_RO(size
);
239 static ssize_t
mappings_show(struct device
*dev
,
240 struct device_attribute
*attr
, char *buf
)
242 struct nd_region
*nd_region
= to_nd_region(dev
);
244 return sprintf(buf
, "%d\n", nd_region
->ndr_mappings
);
246 static DEVICE_ATTR_RO(mappings
);
248 static ssize_t
nstype_show(struct device
*dev
,
249 struct device_attribute
*attr
, char *buf
)
251 struct nd_region
*nd_region
= to_nd_region(dev
);
253 return sprintf(buf
, "%d\n", nd_region_to_nstype(nd_region
));
255 static DEVICE_ATTR_RO(nstype
);
257 static ssize_t
set_cookie_show(struct device
*dev
,
258 struct device_attribute
*attr
, char *buf
)
260 struct nd_region
*nd_region
= to_nd_region(dev
);
261 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
263 if (is_nd_pmem(dev
) && nd_set
)
264 /* pass, should be precluded by region_visible */;
268 return sprintf(buf
, "%#llx\n", nd_set
->cookie
);
270 static DEVICE_ATTR_RO(set_cookie
);
272 resource_size_t
nd_region_available_dpa(struct nd_region
*nd_region
)
274 resource_size_t blk_max_overlap
= 0, available
, overlap
;
277 WARN_ON(!is_nvdimm_bus_locked(&nd_region
->dev
));
281 overlap
= blk_max_overlap
;
282 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
283 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
284 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
286 /* if a dimm is disabled the available capacity is zero */
290 if (is_nd_pmem(&nd_region
->dev
)) {
291 available
+= nd_pmem_available_dpa(nd_region
,
292 nd_mapping
, &overlap
);
293 if (overlap
> blk_max_overlap
) {
294 blk_max_overlap
= overlap
;
297 } else if (is_nd_blk(&nd_region
->dev
)) {
298 available
+= nd_blk_available_dpa(nd_mapping
);
305 static ssize_t
available_size_show(struct device
*dev
,
306 struct device_attribute
*attr
, char *buf
)
308 struct nd_region
*nd_region
= to_nd_region(dev
);
309 unsigned long long available
= 0;
312 * Flush in-flight updates and grab a snapshot of the available
313 * size. Of course, this value is potentially invalidated the
314 * memory nvdimm_bus_lock() is dropped, but that's userspace's
315 * problem to not race itself.
317 nvdimm_bus_lock(dev
);
318 wait_nvdimm_bus_probe_idle(dev
);
319 available
= nd_region_available_dpa(nd_region
);
320 nvdimm_bus_unlock(dev
);
322 return sprintf(buf
, "%llu\n", available
);
324 static DEVICE_ATTR_RO(available_size
);
326 static ssize_t
init_namespaces_show(struct device
*dev
,
327 struct device_attribute
*attr
, char *buf
)
329 struct nd_region_data
*ndrd
= dev_get_drvdata(dev
);
332 nvdimm_bus_lock(dev
);
334 rc
= sprintf(buf
, "%d/%d\n", ndrd
->ns_active
, ndrd
->ns_count
);
337 nvdimm_bus_unlock(dev
);
341 static DEVICE_ATTR_RO(init_namespaces
);
343 static ssize_t
namespace_seed_show(struct device
*dev
,
344 struct device_attribute
*attr
, char *buf
)
346 struct nd_region
*nd_region
= to_nd_region(dev
);
349 nvdimm_bus_lock(dev
);
350 if (nd_region
->ns_seed
)
351 rc
= sprintf(buf
, "%s\n", dev_name(nd_region
->ns_seed
));
353 rc
= sprintf(buf
, "\n");
354 nvdimm_bus_unlock(dev
);
357 static DEVICE_ATTR_RO(namespace_seed
);
359 static ssize_t
btt_seed_show(struct device
*dev
,
360 struct device_attribute
*attr
, char *buf
)
362 struct nd_region
*nd_region
= to_nd_region(dev
);
365 nvdimm_bus_lock(dev
);
366 if (nd_region
->btt_seed
)
367 rc
= sprintf(buf
, "%s\n", dev_name(nd_region
->btt_seed
));
369 rc
= sprintf(buf
, "\n");
370 nvdimm_bus_unlock(dev
);
374 static DEVICE_ATTR_RO(btt_seed
);
376 static ssize_t
pfn_seed_show(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
379 struct nd_region
*nd_region
= to_nd_region(dev
);
382 nvdimm_bus_lock(dev
);
383 if (nd_region
->pfn_seed
)
384 rc
= sprintf(buf
, "%s\n", dev_name(nd_region
->pfn_seed
));
386 rc
= sprintf(buf
, "\n");
387 nvdimm_bus_unlock(dev
);
391 static DEVICE_ATTR_RO(pfn_seed
);
393 static ssize_t
dax_seed_show(struct device
*dev
,
394 struct device_attribute
*attr
, char *buf
)
396 struct nd_region
*nd_region
= to_nd_region(dev
);
399 nvdimm_bus_lock(dev
);
400 if (nd_region
->dax_seed
)
401 rc
= sprintf(buf
, "%s\n", dev_name(nd_region
->dax_seed
));
403 rc
= sprintf(buf
, "\n");
404 nvdimm_bus_unlock(dev
);
408 static DEVICE_ATTR_RO(dax_seed
);
410 static ssize_t
read_only_show(struct device
*dev
,
411 struct device_attribute
*attr
, char *buf
)
413 struct nd_region
*nd_region
= to_nd_region(dev
);
415 return sprintf(buf
, "%d\n", nd_region
->ro
);
418 static ssize_t
read_only_store(struct device
*dev
,
419 struct device_attribute
*attr
, const char *buf
, size_t len
)
422 int rc
= strtobool(buf
, &ro
);
423 struct nd_region
*nd_region
= to_nd_region(dev
);
431 static DEVICE_ATTR_RW(read_only
);
433 static struct attribute
*nd_region_attributes
[] = {
435 &dev_attr_nstype
.attr
,
436 &dev_attr_mappings
.attr
,
437 &dev_attr_btt_seed
.attr
,
438 &dev_attr_pfn_seed
.attr
,
439 &dev_attr_dax_seed
.attr
,
440 &dev_attr_read_only
.attr
,
441 &dev_attr_set_cookie
.attr
,
442 &dev_attr_available_size
.attr
,
443 &dev_attr_namespace_seed
.attr
,
444 &dev_attr_init_namespaces
.attr
,
448 static umode_t
region_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
450 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
451 struct nd_region
*nd_region
= to_nd_region(dev
);
452 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
453 int type
= nd_region_to_nstype(nd_region
);
455 if (!is_nd_pmem(dev
) && a
== &dev_attr_pfn_seed
.attr
)
458 if (!is_nd_pmem(dev
) && a
== &dev_attr_dax_seed
.attr
)
461 if (a
!= &dev_attr_set_cookie
.attr
462 && a
!= &dev_attr_available_size
.attr
)
465 if ((type
== ND_DEVICE_NAMESPACE_PMEM
466 || type
== ND_DEVICE_NAMESPACE_BLK
)
467 && a
== &dev_attr_available_size
.attr
)
469 else if (is_nd_pmem(dev
) && nd_set
)
475 struct attribute_group nd_region_attribute_group
= {
476 .attrs
= nd_region_attributes
,
477 .is_visible
= region_visible
,
479 EXPORT_SYMBOL_GPL(nd_region_attribute_group
);
481 u64
nd_region_interleave_set_cookie(struct nd_region
*nd_region
)
483 struct nd_interleave_set
*nd_set
= nd_region
->nd_set
;
486 return nd_set
->cookie
;
491 * Upon successful probe/remove, take/release a reference on the
492 * associated interleave set (if present), and plant new btt + namespace
493 * seeds. Also, on the removal of a BLK region, notify the provider to
494 * disable the region.
496 static void nd_region_notify_driver_action(struct nvdimm_bus
*nvdimm_bus
,
497 struct device
*dev
, bool probe
)
499 struct nd_region
*nd_region
;
501 if (!probe
&& (is_nd_pmem(dev
) || is_nd_blk(dev
))) {
504 nd_region
= to_nd_region(dev
);
505 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
506 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[i
];
507 struct nvdimm_drvdata
*ndd
= nd_mapping
->ndd
;
508 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
510 kfree(nd_mapping
->labels
);
511 nd_mapping
->labels
= NULL
;
513 nd_mapping
->ndd
= NULL
;
515 atomic_dec(&nvdimm
->busy
);
521 if (dev
->parent
&& is_nd_blk(dev
->parent
) && probe
) {
522 nd_region
= to_nd_region(dev
->parent
);
523 nvdimm_bus_lock(dev
);
524 if (nd_region
->ns_seed
== dev
)
525 nd_region_create_blk_seed(nd_region
);
526 nvdimm_bus_unlock(dev
);
528 if (is_nd_btt(dev
) && probe
) {
529 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
531 nd_region
= to_nd_region(dev
->parent
);
532 nvdimm_bus_lock(dev
);
533 if (nd_region
->btt_seed
== dev
)
534 nd_region_create_btt_seed(nd_region
);
535 if (nd_region
->ns_seed
== &nd_btt
->ndns
->dev
&&
536 is_nd_blk(dev
->parent
))
537 nd_region_create_blk_seed(nd_region
);
538 nvdimm_bus_unlock(dev
);
540 if (is_nd_pfn(dev
) && probe
) {
541 nd_region
= to_nd_region(dev
->parent
);
542 nvdimm_bus_lock(dev
);
543 if (nd_region
->pfn_seed
== dev
)
544 nd_region_create_pfn_seed(nd_region
);
545 nvdimm_bus_unlock(dev
);
547 if (is_nd_dax(dev
) && probe
) {
548 nd_region
= to_nd_region(dev
->parent
);
549 nvdimm_bus_lock(dev
);
550 if (nd_region
->dax_seed
== dev
)
551 nd_region_create_dax_seed(nd_region
);
552 nvdimm_bus_unlock(dev
);
556 void nd_region_probe_success(struct nvdimm_bus
*nvdimm_bus
, struct device
*dev
)
558 nd_region_notify_driver_action(nvdimm_bus
, dev
, true);
561 void nd_region_disable(struct nvdimm_bus
*nvdimm_bus
, struct device
*dev
)
563 nd_region_notify_driver_action(nvdimm_bus
, dev
, false);
566 static ssize_t
mappingN(struct device
*dev
, char *buf
, int n
)
568 struct nd_region
*nd_region
= to_nd_region(dev
);
569 struct nd_mapping
*nd_mapping
;
570 struct nvdimm
*nvdimm
;
572 if (n
>= nd_region
->ndr_mappings
)
574 nd_mapping
= &nd_region
->mapping
[n
];
575 nvdimm
= nd_mapping
->nvdimm
;
577 return sprintf(buf
, "%s,%llu,%llu\n", dev_name(&nvdimm
->dev
),
578 nd_mapping
->start
, nd_mapping
->size
);
581 #define REGION_MAPPING(idx) \
582 static ssize_t mapping##idx##_show(struct device *dev, \
583 struct device_attribute *attr, char *buf) \
585 return mappingN(dev, buf, idx); \
587 static DEVICE_ATTR_RO(mapping##idx)
590 * 32 should be enough for a while, even in the presence of socket
591 * interleave a 32-way interleave set is a degenerate case.
626 static umode_t
mapping_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
628 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
629 struct nd_region
*nd_region
= to_nd_region(dev
);
631 if (n
< nd_region
->ndr_mappings
)
636 static struct attribute
*mapping_attributes
[] = {
637 &dev_attr_mapping0
.attr
,
638 &dev_attr_mapping1
.attr
,
639 &dev_attr_mapping2
.attr
,
640 &dev_attr_mapping3
.attr
,
641 &dev_attr_mapping4
.attr
,
642 &dev_attr_mapping5
.attr
,
643 &dev_attr_mapping6
.attr
,
644 &dev_attr_mapping7
.attr
,
645 &dev_attr_mapping8
.attr
,
646 &dev_attr_mapping9
.attr
,
647 &dev_attr_mapping10
.attr
,
648 &dev_attr_mapping11
.attr
,
649 &dev_attr_mapping12
.attr
,
650 &dev_attr_mapping13
.attr
,
651 &dev_attr_mapping14
.attr
,
652 &dev_attr_mapping15
.attr
,
653 &dev_attr_mapping16
.attr
,
654 &dev_attr_mapping17
.attr
,
655 &dev_attr_mapping18
.attr
,
656 &dev_attr_mapping19
.attr
,
657 &dev_attr_mapping20
.attr
,
658 &dev_attr_mapping21
.attr
,
659 &dev_attr_mapping22
.attr
,
660 &dev_attr_mapping23
.attr
,
661 &dev_attr_mapping24
.attr
,
662 &dev_attr_mapping25
.attr
,
663 &dev_attr_mapping26
.attr
,
664 &dev_attr_mapping27
.attr
,
665 &dev_attr_mapping28
.attr
,
666 &dev_attr_mapping29
.attr
,
667 &dev_attr_mapping30
.attr
,
668 &dev_attr_mapping31
.attr
,
672 struct attribute_group nd_mapping_attribute_group
= {
673 .is_visible
= mapping_visible
,
674 .attrs
= mapping_attributes
,
676 EXPORT_SYMBOL_GPL(nd_mapping_attribute_group
);
678 int nd_blk_region_init(struct nd_region
*nd_region
)
680 struct device
*dev
= &nd_region
->dev
;
681 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
686 if (nd_region
->ndr_mappings
< 1) {
687 dev_err(dev
, "invalid BLK region\n");
691 return to_nd_blk_region(dev
)->enable(nvdimm_bus
, dev
);
695 * nd_region_acquire_lane - allocate and lock a lane
696 * @nd_region: region id and number of lanes possible
698 * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
699 * We optimize for the common case where there are 256 lanes, one
700 * per-cpu. For larger systems we need to lock to share lanes. For now
701 * this implementation assumes the cost of maintaining an allocator for
702 * free lanes is on the order of the lock hold time, so it implements a
703 * static lane = cpu % num_lanes mapping.
705 * In the case of a BTT instance on top of a BLK namespace a lane may be
706 * acquired recursively. We lock on the first instance.
708 * In the case of a BTT instance on top of PMEM, we only acquire a lane
709 * for the BTT metadata updates.
711 unsigned int nd_region_acquire_lane(struct nd_region
*nd_region
)
713 unsigned int cpu
, lane
;
716 if (nd_region
->num_lanes
< nr_cpu_ids
) {
717 struct nd_percpu_lane
*ndl_lock
, *ndl_count
;
719 lane
= cpu
% nd_region
->num_lanes
;
720 ndl_count
= per_cpu_ptr(nd_region
->lane
, cpu
);
721 ndl_lock
= per_cpu_ptr(nd_region
->lane
, lane
);
722 if (ndl_count
->count
++ == 0)
723 spin_lock(&ndl_lock
->lock
);
729 EXPORT_SYMBOL(nd_region_acquire_lane
);
731 void nd_region_release_lane(struct nd_region
*nd_region
, unsigned int lane
)
733 if (nd_region
->num_lanes
< nr_cpu_ids
) {
734 unsigned int cpu
= get_cpu();
735 struct nd_percpu_lane
*ndl_lock
, *ndl_count
;
737 ndl_count
= per_cpu_ptr(nd_region
->lane
, cpu
);
738 ndl_lock
= per_cpu_ptr(nd_region
->lane
, lane
);
739 if (--ndl_count
->count
== 0)
740 spin_unlock(&ndl_lock
->lock
);
745 EXPORT_SYMBOL(nd_region_release_lane
);
747 static struct nd_region
*nd_region_create(struct nvdimm_bus
*nvdimm_bus
,
748 struct nd_region_desc
*ndr_desc
, struct device_type
*dev_type
,
751 struct nd_region
*nd_region
;
757 for (i
= 0; i
< ndr_desc
->num_mappings
; i
++) {
758 struct nd_mapping
*nd_mapping
= &ndr_desc
->nd_mapping
[i
];
759 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
761 if ((nd_mapping
->start
| nd_mapping
->size
) % SZ_4K
) {
762 dev_err(&nvdimm_bus
->dev
, "%s: %s mapping%d is not 4K aligned\n",
763 caller
, dev_name(&nvdimm
->dev
), i
);
768 if (nvdimm
->flags
& NDD_UNARMED
)
772 if (dev_type
== &nd_blk_device_type
) {
773 struct nd_blk_region_desc
*ndbr_desc
;
774 struct nd_blk_region
*ndbr
;
776 ndbr_desc
= to_blk_region_desc(ndr_desc
);
777 ndbr
= kzalloc(sizeof(*ndbr
) + sizeof(struct nd_mapping
)
778 * ndr_desc
->num_mappings
,
781 nd_region
= &ndbr
->nd_region
;
782 ndbr
->enable
= ndbr_desc
->enable
;
783 ndbr
->do_io
= ndbr_desc
->do_io
;
787 nd_region
= kzalloc(sizeof(struct nd_region
)
788 + sizeof(struct nd_mapping
)
789 * ndr_desc
->num_mappings
,
791 region_buf
= nd_region
;
796 nd_region
->id
= ida_simple_get(®ion_ida
, 0, 0, GFP_KERNEL
);
797 if (nd_region
->id
< 0)
800 nd_region
->lane
= alloc_percpu(struct nd_percpu_lane
);
801 if (!nd_region
->lane
)
804 for (i
= 0; i
< nr_cpu_ids
; i
++) {
805 struct nd_percpu_lane
*ndl
;
807 ndl
= per_cpu_ptr(nd_region
->lane
, i
);
808 spin_lock_init(&ndl
->lock
);
812 memcpy(nd_region
->mapping
, ndr_desc
->nd_mapping
,
813 sizeof(struct nd_mapping
) * ndr_desc
->num_mappings
);
814 for (i
= 0; i
< ndr_desc
->num_mappings
; i
++) {
815 struct nd_mapping
*nd_mapping
= &ndr_desc
->nd_mapping
[i
];
816 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
818 get_device(&nvdimm
->dev
);
820 nd_region
->ndr_mappings
= ndr_desc
->num_mappings
;
821 nd_region
->provider_data
= ndr_desc
->provider_data
;
822 nd_region
->nd_set
= ndr_desc
->nd_set
;
823 nd_region
->num_lanes
= ndr_desc
->num_lanes
;
824 nd_region
->flags
= ndr_desc
->flags
;
826 nd_region
->numa_node
= ndr_desc
->numa_node
;
827 ida_init(&nd_region
->ns_ida
);
828 ida_init(&nd_region
->btt_ida
);
829 ida_init(&nd_region
->pfn_ida
);
830 ida_init(&nd_region
->dax_ida
);
831 dev
= &nd_region
->dev
;
832 dev_set_name(dev
, "region%d", nd_region
->id
);
833 dev
->parent
= &nvdimm_bus
->dev
;
834 dev
->type
= dev_type
;
835 dev
->groups
= ndr_desc
->attr_groups
;
836 nd_region
->ndr_size
= resource_size(ndr_desc
->res
);
837 nd_region
->ndr_start
= ndr_desc
->res
->start
;
838 nd_device_register(dev
);
843 ida_simple_remove(®ion_ida
, nd_region
->id
);
849 struct nd_region
*nvdimm_pmem_region_create(struct nvdimm_bus
*nvdimm_bus
,
850 struct nd_region_desc
*ndr_desc
)
852 ndr_desc
->num_lanes
= ND_MAX_LANES
;
853 return nd_region_create(nvdimm_bus
, ndr_desc
, &nd_pmem_device_type
,
856 EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create
);
858 struct nd_region
*nvdimm_blk_region_create(struct nvdimm_bus
*nvdimm_bus
,
859 struct nd_region_desc
*ndr_desc
)
861 if (ndr_desc
->num_mappings
> 1)
863 ndr_desc
->num_lanes
= min(ndr_desc
->num_lanes
, ND_MAX_LANES
);
864 return nd_region_create(nvdimm_bus
, ndr_desc
, &nd_blk_device_type
,
867 EXPORT_SYMBOL_GPL(nvdimm_blk_region_create
);
869 struct nd_region
*nvdimm_volatile_region_create(struct nvdimm_bus
*nvdimm_bus
,
870 struct nd_region_desc
*ndr_desc
)
872 ndr_desc
->num_lanes
= ND_MAX_LANES
;
873 return nd_region_create(nvdimm_bus
, ndr_desc
, &nd_volatile_device_type
,
876 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create
);
879 * nvdimm_flush - flush any posted write queues between the cpu and pmem media
880 * @nd_region: blk or interleaved pmem region
882 void nvdimm_flush(struct nd_region
*nd_region
)
884 struct nd_region_data
*ndrd
= dev_get_drvdata(&nd_region
->dev
);
888 * Try to encourage some diversity in flush hint addresses
889 * across cpus assuming a limited number of flush hints.
891 idx
= this_cpu_read(flush_idx
);
892 idx
= this_cpu_add_return(flush_idx
, hash_32(current
->pid
+ idx
, 8));
895 * The first wmb() is needed to 'sfence' all previous writes
896 * such that they are architecturally visible for the platform
897 * buffer flush. Note that we've already arranged for pmem
898 * writes to avoid the cache via arch_memcpy_to_pmem(). The
899 * final wmb() ensures ordering for the NVDIMM flush write.
902 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++)
903 if (ndrd
->flush_wpq
[i
][0])
904 writeq(1, ndrd
->flush_wpq
[i
][idx
& ndrd
->flush_mask
]);
907 EXPORT_SYMBOL_GPL(nvdimm_flush
);
910 * nvdimm_has_flush - determine write flushing requirements
911 * @nd_region: blk or interleaved pmem region
913 * Returns 1 if writes require flushing
914 * Returns 0 if writes do not require flushing
915 * Returns -ENXIO if flushing capability can not be determined
917 int nvdimm_has_flush(struct nd_region
*nd_region
)
919 struct nd_region_data
*ndrd
= dev_get_drvdata(&nd_region
->dev
);
922 /* no nvdimm == flushing capability unknown */
923 if (nd_region
->ndr_mappings
== 0)
926 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++)
927 /* flush hints present, flushing required */
928 if (ndrd
->flush_wpq
[i
][0])
932 * The platform defines dimm devices without hints, assume
933 * platform persistence mechanism like ADR
937 EXPORT_SYMBOL_GPL(nvdimm_has_flush
);
939 void __exit
nd_region_devs_exit(void)
941 ida_destroy(®ion_ida
);