2 * Copyright(c) 2013-2016 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/memremap.h>
14 #include <linux/blkdev.h>
15 #include <linux/device.h>
16 #include <linux/genhd.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
25 static void nd_pfn_release(struct device *dev)
27 struct nd_region *nd_region = to_nd_region(dev->parent);
28 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
30 dev_dbg(dev, "%s\n", __func__);
31 nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
32 ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
37 static struct device_type nd_pfn_device_type = {
39 .release = nd_pfn_release,
42 bool is_nd_pfn(struct device *dev)
44 return dev ? dev->type == &nd_pfn_device_type : false;
46 EXPORT_SYMBOL(is_nd_pfn);
48 struct nd_pfn *to_nd_pfn(struct device *dev)
50 struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
52 WARN_ON(!is_nd_pfn(dev));
55 EXPORT_SYMBOL(to_nd_pfn);
57 static ssize_t mode_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
60 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
62 switch (nd_pfn->mode) {
64 return sprintf(buf, "ram\n");
66 return sprintf(buf, "pmem\n");
68 return sprintf(buf, "none\n");
72 static ssize_t mode_store(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t len)
75 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
85 if (strncmp(buf, "pmem\n", n) == 0
86 || strncmp(buf, "pmem", n) == 0) {
87 nd_pfn->mode = PFN_MODE_PMEM;
88 } else if (strncmp(buf, "ram\n", n) == 0
89 || strncmp(buf, "ram", n) == 0)
90 nd_pfn->mode = PFN_MODE_RAM;
91 else if (strncmp(buf, "none\n", n) == 0
92 || strncmp(buf, "none", n) == 0)
93 nd_pfn->mode = PFN_MODE_NONE;
97 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
98 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
99 nvdimm_bus_unlock(dev);
102 return rc ? rc : len;
104 static DEVICE_ATTR_RW(mode);
106 static ssize_t align_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
109 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
111 return sprintf(buf, "%ld\n", nd_pfn->align);
114 static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
119 rc = kstrtoul(buf, 0, &val);
123 if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
126 if (nd_pfn->dev.driver)
134 static ssize_t align_store(struct device *dev,
135 struct device_attribute *attr, const char *buf, size_t len)
137 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
141 nvdimm_bus_lock(dev);
142 rc = __align_store(nd_pfn, buf);
143 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
144 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
145 nvdimm_bus_unlock(dev);
148 return rc ? rc : len;
150 static DEVICE_ATTR_RW(align);
152 static ssize_t uuid_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
158 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
159 return sprintf(buf, "\n");
162 static ssize_t uuid_store(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t len)
165 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
169 rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
170 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
171 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
174 return rc ? rc : len;
176 static DEVICE_ATTR_RW(uuid);
178 static ssize_t namespace_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
184 nvdimm_bus_lock(dev);
185 rc = sprintf(buf, "%s\n", nd_pfn->ndns
186 ? dev_name(&nd_pfn->ndns->dev) : "");
187 nvdimm_bus_unlock(dev);
191 static ssize_t namespace_store(struct device *dev,
192 struct device_attribute *attr, const char *buf, size_t len)
194 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
198 nvdimm_bus_lock(dev);
199 rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
200 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
201 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
202 nvdimm_bus_unlock(dev);
207 static DEVICE_ATTR_RW(namespace);
209 static ssize_t resource_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
217 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
218 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
219 struct nd_namespace_common *ndns = nd_pfn->ndns;
220 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
221 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
223 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
224 + start_pad + offset);
226 /* no address to convey if the pfn instance is disabled */
233 static DEVICE_ATTR_RO(resource);
235 static ssize_t size_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
243 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
244 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
245 struct nd_namespace_common *ndns = nd_pfn->ndns;
246 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
247 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
248 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
250 rc = sprintf(buf, "%llu\n", (unsigned long long)
251 resource_size(&nsio->res) - start_pad
252 - end_trunc - offset);
254 /* no size to convey if the pfn instance is disabled */
261 static DEVICE_ATTR_RO(size);
263 static struct attribute *nd_pfn_attributes[] = {
265 &dev_attr_namespace.attr,
267 &dev_attr_align.attr,
268 &dev_attr_resource.attr,
273 static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n)
275 if (a == &dev_attr_resource.attr)
280 struct attribute_group nd_pfn_attribute_group = {
281 .attrs = nd_pfn_attributes,
282 .is_visible = pfn_visible,
285 static const struct attribute_group *nd_pfn_attribute_groups[] = {
286 &nd_pfn_attribute_group,
287 &nd_device_attribute_group,
288 &nd_numa_attribute_group,
292 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
293 struct nd_namespace_common *ndns)
295 struct device *dev = &nd_pfn->dev;
300 nd_pfn->mode = PFN_MODE_NONE;
301 nd_pfn->align = HPAGE_SIZE;
303 device_initialize(&nd_pfn->dev);
304 if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
305 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
306 __func__, dev_name(ndns->claim));
313 static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
315 struct nd_pfn *nd_pfn;
318 nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
322 nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
323 if (nd_pfn->id < 0) {
329 dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
330 dev->groups = nd_pfn_attribute_groups;
331 dev->type = &nd_pfn_device_type;
332 dev->parent = &nd_region->dev;
337 struct device *nd_pfn_create(struct nd_region *nd_region)
339 struct nd_pfn *nd_pfn;
342 if (!is_nd_pmem(&nd_region->dev))
345 nd_pfn = nd_pfn_alloc(nd_region);
346 dev = nd_pfn_devinit(nd_pfn, NULL);
348 __nd_device_register(dev);
353 * nd_pfn_validate - read and validate info-block
354 * @nd_pfn: fsdax namespace runtime state / properties
355 * @sig: 'devdax' or 'fsdax' signature
357 * Upon return the info-block buffer contents (->pfn_sb) are
358 * indeterminate when validation fails, and a coherent info-block
361 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
363 u64 checksum, offset;
364 enum nd_pfn_mode mode;
365 struct nd_namespace_io *nsio;
366 unsigned long align, start_pad;
367 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
368 struct nd_namespace_common *ndns = nd_pfn->ndns;
369 const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
371 if (!pfn_sb || !ndns)
374 if (!is_nd_pmem(nd_pfn->dev.parent))
377 if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
380 if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
383 checksum = le64_to_cpu(pfn_sb->checksum);
384 pfn_sb->checksum = 0;
385 if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
387 pfn_sb->checksum = cpu_to_le64(checksum);
389 if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
392 if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
393 pfn_sb->start_pad = 0;
394 pfn_sb->end_trunc = 0;
397 if (__le16_to_cpu(pfn_sb->version_minor) < 2)
400 switch (le32_to_cpu(pfn_sb->mode)) {
408 align = le32_to_cpu(pfn_sb->align);
409 offset = le64_to_cpu(pfn_sb->dataoff);
410 start_pad = le32_to_cpu(pfn_sb->start_pad);
412 align = 1UL << ilog2(offset);
413 mode = le32_to_cpu(pfn_sb->mode);
417 * When probing a namepace via nd_pfn_probe() the uuid
418 * is NULL (see: nd_pfn_devinit()) we init settings from
421 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
424 nd_pfn->align = align;
428 * When probing a pfn / dax instance we validate the
429 * live settings against the pfn_sb
431 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
435 * If the uuid validates, but other settings mismatch
436 * return EINVAL because userspace has managed to change
437 * the configuration without specifying new
440 if (nd_pfn->align != align || nd_pfn->mode != mode) {
441 dev_err(&nd_pfn->dev,
442 "init failed, settings mismatch\n");
443 dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
444 nd_pfn->align, align, nd_pfn->mode,
450 if (align > nvdimm_namespace_capacity(ndns)) {
451 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
452 align, nvdimm_namespace_capacity(ndns));
457 * These warnings are verbose because they can only trigger in
458 * the case where the physical address alignment of the
459 * namespace has changed since the pfn superblock was
462 nsio = to_nd_namespace_io(&ndns->dev);
463 if (offset >= resource_size(&nsio->res)) {
464 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
465 dev_name(&ndns->dev));
469 if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align))
470 || !IS_ALIGNED(offset, PAGE_SIZE)) {
471 dev_err(&nd_pfn->dev,
472 "bad offset: %#llx dax disabled align: %#lx\n",
479 EXPORT_SYMBOL(nd_pfn_validate);
481 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
484 struct nd_pfn *nd_pfn;
485 struct device *pfn_dev;
486 struct nd_pfn_sb *pfn_sb;
487 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
492 nvdimm_bus_lock(&ndns->dev);
493 nd_pfn = nd_pfn_alloc(nd_region);
494 pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
495 nvdimm_bus_unlock(&ndns->dev);
498 pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
499 nd_pfn = to_nd_pfn(pfn_dev);
500 nd_pfn->pfn_sb = pfn_sb;
501 rc = nd_pfn_validate(nd_pfn, PFN_SIG);
502 dev_dbg(dev, "%s: pfn: %s\n", __func__,
503 rc == 0 ? dev_name(pfn_dev) : "<none>");
505 __nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
508 __nd_device_register(pfn_dev);
512 EXPORT_SYMBOL(nd_pfn_probe);
515 * We hotplug memory at section granularity, pad the reserved area from
516 * the previous section base to the namespace base address.
518 static unsigned long init_altmap_base(resource_size_t base)
520 unsigned long base_pfn = PHYS_PFN(base);
522 return PFN_SECTION_ALIGN_DOWN(base_pfn);
525 static unsigned long init_altmap_reserve(resource_size_t base)
527 unsigned long reserve = PFN_UP(SZ_8K);
528 unsigned long base_pfn = PHYS_PFN(base);
530 reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
534 static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
535 struct resource *res, struct vmem_altmap *altmap)
537 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
538 u64 offset = le64_to_cpu(pfn_sb->dataoff);
539 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
540 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
541 struct nd_namespace_common *ndns = nd_pfn->ndns;
542 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
543 resource_size_t base = nsio->res.start + start_pad;
544 struct vmem_altmap __altmap = {
545 .base_pfn = init_altmap_base(base),
546 .reserve = init_altmap_reserve(base),
549 memcpy(res, &nsio->res, sizeof(*res));
550 res->start += start_pad;
551 res->end -= end_trunc;
553 if (nd_pfn->mode == PFN_MODE_RAM) {
555 return ERR_PTR(-EINVAL);
556 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
558 } else if (nd_pfn->mode == PFN_MODE_PMEM) {
559 nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
560 - offset) / PAGE_SIZE);
561 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
562 dev_info(&nd_pfn->dev,
563 "number of pfns truncated from %lld to %ld\n",
564 le64_to_cpu(nd_pfn->pfn_sb->npfns),
566 memcpy(altmap, &__altmap, sizeof(*altmap));
567 altmap->free = PHYS_PFN(offset - SZ_8K);
570 return ERR_PTR(-ENXIO);
575 static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys)
577 return min_t(u64, PHYS_SECTION_ALIGN_DOWN(phys),
578 ALIGN_DOWN(phys, nd_pfn->align));
581 static int nd_pfn_init(struct nd_pfn *nd_pfn)
583 u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
584 struct nd_namespace_common *ndns = nd_pfn->ndns;
585 u32 start_pad = 0, end_trunc = 0;
586 resource_size_t start, size;
587 struct nd_namespace_io *nsio;
588 struct nd_region *nd_region;
589 struct nd_pfn_sb *pfn_sb;
596 pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
600 nd_pfn->pfn_sb = pfn_sb;
601 if (is_nd_dax(&nd_pfn->dev))
606 rc = nd_pfn_validate(nd_pfn, sig);
610 /* no info block, do init */;
611 memset(pfn_sb, 0, sizeof(*pfn_sb));
613 nd_region = to_nd_region(nd_pfn->dev.parent);
615 dev_info(&nd_pfn->dev,
616 "%s is read-only, unable to init metadata\n",
617 dev_name(&nd_region->dev));
621 memset(pfn_sb, 0, sizeof(*pfn_sb));
624 * Check if pmem collides with 'System RAM' when section aligned and
625 * trim it accordingly
627 nsio = to_nd_namespace_io(&ndns->dev);
628 start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
629 size = resource_size(&nsio->res);
630 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
631 IORES_DESC_NONE) == REGION_MIXED) {
632 start = nsio->res.start;
633 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
636 start = nsio->res.start;
637 size = PHYS_SECTION_ALIGN_UP(start + size) - start;
638 if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
639 IORES_DESC_NONE) == REGION_MIXED
640 || !IS_ALIGNED(start + resource_size(&nsio->res),
642 size = resource_size(&nsio->res);
643 end_trunc = start + size - phys_pmem_align_down(nd_pfn,
647 if (start_pad + end_trunc)
648 dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n",
649 dev_name(&ndns->dev), start_pad + end_trunc);
652 * Note, we use 64 here for the standard size of struct page,
653 * debugging options may cause it to be larger in which case the
654 * implementation will limit the pfns advertised through
655 * ->direct_access() to those that are included in the memmap.
658 size = resource_size(&nsio->res);
659 npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
661 if (nd_pfn->mode == PFN_MODE_PMEM) {
663 * vmemmap_populate_hugepages() allocates the memmap array in
666 offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve,
667 max(nd_pfn->align, HPAGE_SIZE)) - start;
668 } else if (nd_pfn->mode == PFN_MODE_RAM)
669 offset = ALIGN(start + SZ_8K + dax_label_reserve,
670 nd_pfn->align) - start;
674 if (offset + start_pad + end_trunc >= size) {
675 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
676 dev_name(&ndns->dev));
680 npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
681 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
682 pfn_sb->dataoff = cpu_to_le64(offset);
683 pfn_sb->npfns = cpu_to_le64(npfns);
684 memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
685 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
686 memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
687 pfn_sb->version_major = cpu_to_le16(1);
688 pfn_sb->version_minor = cpu_to_le16(3);
689 pfn_sb->start_pad = cpu_to_le32(start_pad);
690 pfn_sb->end_trunc = cpu_to_le32(end_trunc);
691 pfn_sb->align = cpu_to_le32(nd_pfn->align);
692 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
693 pfn_sb->checksum = cpu_to_le64(checksum);
695 return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
699 * Determine the effective resource range and vmem_altmap from an nd_pfn
702 struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
703 struct resource *res, struct vmem_altmap *altmap)
707 if (!nd_pfn->uuid || !nd_pfn->ndns)
708 return ERR_PTR(-ENODEV);
710 rc = nd_pfn_init(nd_pfn);
714 /* we need a valid pfn_sb before we can init a vmem_altmap */
715 return __nvdimm_setup_pfn(nd_pfn, res, altmap);
717 EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);