c500531ca23b0658767c674ff1631dffd78fb2e2
[platform/kernel/linux-rpi.git] / drivers / nvdimm / pfn_devs.c
1 /*
2  * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
3  *
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.
7  *
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.
12  */
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>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include "nd-core.h"
22 #include "pfn.h"
23 #include "nd.h"
24
25 static void nd_pfn_release(struct device *dev)
26 {
27         struct nd_region *nd_region = to_nd_region(dev->parent);
28         struct nd_pfn *nd_pfn = to_nd_pfn(dev);
29
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);
33         kfree(nd_pfn->uuid);
34         kfree(nd_pfn);
35 }
36
37 static struct device_type nd_pfn_device_type = {
38         .name = "nd_pfn",
39         .release = nd_pfn_release,
40 };
41
42 bool is_nd_pfn(struct device *dev)
43 {
44         return dev ? dev->type == &nd_pfn_device_type : false;
45 }
46 EXPORT_SYMBOL(is_nd_pfn);
47
48 struct nd_pfn *to_nd_pfn(struct device *dev)
49 {
50         struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
51
52         WARN_ON(!is_nd_pfn(dev));
53         return nd_pfn;
54 }
55 EXPORT_SYMBOL(to_nd_pfn);
56
57 static ssize_t mode_show(struct device *dev,
58                 struct device_attribute *attr, char *buf)
59 {
60         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
61
62         switch (nd_pfn->mode) {
63         case PFN_MODE_RAM:
64                 return sprintf(buf, "ram\n");
65         case PFN_MODE_PMEM:
66                 return sprintf(buf, "pmem\n");
67         default:
68                 return sprintf(buf, "none\n");
69         }
70 }
71
72 static ssize_t mode_store(struct device *dev,
73                 struct device_attribute *attr, const char *buf, size_t len)
74 {
75         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
76         ssize_t rc = 0;
77
78         device_lock(dev);
79         nvdimm_bus_lock(dev);
80         if (dev->driver)
81                 rc = -EBUSY;
82         else {
83                 size_t n = len - 1;
84
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;
94                 else
95                         rc = -EINVAL;
96         }
97         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
98                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
99         nvdimm_bus_unlock(dev);
100         device_unlock(dev);
101
102         return rc ? rc : len;
103 }
104 static DEVICE_ATTR_RW(mode);
105
106 static ssize_t align_show(struct device *dev,
107                 struct device_attribute *attr, char *buf)
108 {
109         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
110
111         return sprintf(buf, "%ld\n", nd_pfn->align);
112 }
113
114 static const unsigned long *nd_pfn_supported_alignments(void)
115 {
116         /*
117          * This needs to be a non-static variable because the *_SIZE
118          * macros aren't always constants.
119          */
120         const unsigned long supported_alignments[] = {
121                 PAGE_SIZE,
122 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
123                 HPAGE_PMD_SIZE,
124 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
125                 HPAGE_PUD_SIZE,
126 #endif
127 #endif
128                 0,
129         };
130         static unsigned long data[ARRAY_SIZE(supported_alignments)];
131
132         memcpy(data, supported_alignments, sizeof(data));
133
134         return data;
135 }
136
137 static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
138 {
139         unsigned long val;
140         int rc;
141
142         rc = kstrtoul(buf, 0, &val);
143         if (rc)
144                 return rc;
145
146         if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
147                 return -EINVAL;
148
149         if (nd_pfn->dev.driver)
150                 return -EBUSY;
151         else
152                 nd_pfn->align = val;
153
154         return 0;
155 }
156
157 static ssize_t align_store(struct device *dev,
158                 struct device_attribute *attr, const char *buf, size_t len)
159 {
160         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
161         ssize_t rc;
162
163         device_lock(dev);
164         nvdimm_bus_lock(dev);
165         rc = __align_store(nd_pfn, buf);
166         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
167                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
168         nvdimm_bus_unlock(dev);
169         device_unlock(dev);
170
171         return rc ? rc : len;
172 }
173 static DEVICE_ATTR_RW(align);
174
175 static ssize_t uuid_show(struct device *dev,
176                 struct device_attribute *attr, char *buf)
177 {
178         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
179
180         if (nd_pfn->uuid)
181                 return sprintf(buf, "%pUb\n", nd_pfn->uuid);
182         return sprintf(buf, "\n");
183 }
184
185 static ssize_t uuid_store(struct device *dev,
186                 struct device_attribute *attr, const char *buf, size_t len)
187 {
188         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
189         ssize_t rc;
190
191         device_lock(dev);
192         rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
193         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
194                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
195         device_unlock(dev);
196
197         return rc ? rc : len;
198 }
199 static DEVICE_ATTR_RW(uuid);
200
201 static ssize_t namespace_show(struct device *dev,
202                 struct device_attribute *attr, char *buf)
203 {
204         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
205         ssize_t rc;
206
207         nvdimm_bus_lock(dev);
208         rc = sprintf(buf, "%s\n", nd_pfn->ndns
209                         ? dev_name(&nd_pfn->ndns->dev) : "");
210         nvdimm_bus_unlock(dev);
211         return rc;
212 }
213
214 static ssize_t namespace_store(struct device *dev,
215                 struct device_attribute *attr, const char *buf, size_t len)
216 {
217         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
218         ssize_t rc;
219
220         device_lock(dev);
221         nvdimm_bus_lock(dev);
222         rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
223         dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
224                         rc, buf, buf[len - 1] == '\n' ? "" : "\n");
225         nvdimm_bus_unlock(dev);
226         device_unlock(dev);
227
228         return rc;
229 }
230 static DEVICE_ATTR_RW(namespace);
231
232 static ssize_t resource_show(struct device *dev,
233                 struct device_attribute *attr, char *buf)
234 {
235         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
236         ssize_t rc;
237
238         device_lock(dev);
239         if (dev->driver) {
240                 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
241                 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
242                 struct nd_namespace_common *ndns = nd_pfn->ndns;
243                 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
244                 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
245
246                 rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
247                                 + start_pad + offset);
248         } else {
249                 /* no address to convey if the pfn instance is disabled */
250                 rc = -ENXIO;
251         }
252         device_unlock(dev);
253
254         return rc;
255 }
256 static DEVICE_ATTR_RO(resource);
257
258 static ssize_t size_show(struct device *dev,
259                 struct device_attribute *attr, char *buf)
260 {
261         struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
262         ssize_t rc;
263
264         device_lock(dev);
265         if (dev->driver) {
266                 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
267                 u64 offset = __le64_to_cpu(pfn_sb->dataoff);
268                 struct nd_namespace_common *ndns = nd_pfn->ndns;
269                 u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
270                 u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
271                 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
272
273                 rc = sprintf(buf, "%llu\n", (unsigned long long)
274                                 resource_size(&nsio->res) - start_pad
275                                 - end_trunc - offset);
276         } else {
277                 /* no size to convey if the pfn instance is disabled */
278                 rc = -ENXIO;
279         }
280         device_unlock(dev);
281
282         return rc;
283 }
284 static DEVICE_ATTR_RO(size);
285
286 static ssize_t supported_alignments_show(struct device *dev,
287                 struct device_attribute *attr, char *buf)
288 {
289         return nd_size_select_show(0, nd_pfn_supported_alignments(), buf);
290 }
291 static DEVICE_ATTR_RO(supported_alignments);
292
293 static struct attribute *nd_pfn_attributes[] = {
294         &dev_attr_mode.attr,
295         &dev_attr_namespace.attr,
296         &dev_attr_uuid.attr,
297         &dev_attr_align.attr,
298         &dev_attr_resource.attr,
299         &dev_attr_size.attr,
300         &dev_attr_supported_alignments.attr,
301         NULL,
302 };
303
304 struct attribute_group nd_pfn_attribute_group = {
305         .attrs = nd_pfn_attributes,
306 };
307
308 static const struct attribute_group *nd_pfn_attribute_groups[] = {
309         &nd_pfn_attribute_group,
310         &nd_device_attribute_group,
311         &nd_numa_attribute_group,
312         NULL,
313 };
314
315 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
316                 struct nd_namespace_common *ndns)
317 {
318         struct device *dev = &nd_pfn->dev;
319
320         if (!nd_pfn)
321                 return NULL;
322
323         nd_pfn->mode = PFN_MODE_NONE;
324         nd_pfn->align = PFN_DEFAULT_ALIGNMENT;
325         dev = &nd_pfn->dev;
326         device_initialize(&nd_pfn->dev);
327         if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
328                 dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
329                                 __func__, dev_name(ndns->claim));
330                 put_device(dev);
331                 return NULL;
332         }
333         return dev;
334 }
335
336 static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
337 {
338         struct nd_pfn *nd_pfn;
339         struct device *dev;
340
341         nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
342         if (!nd_pfn)
343                 return NULL;
344
345         nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
346         if (nd_pfn->id < 0) {
347                 kfree(nd_pfn);
348                 return NULL;
349         }
350
351         dev = &nd_pfn->dev;
352         dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
353         dev->groups = nd_pfn_attribute_groups;
354         dev->type = &nd_pfn_device_type;
355         dev->parent = &nd_region->dev;
356
357         return nd_pfn;
358 }
359
360 struct device *nd_pfn_create(struct nd_region *nd_region)
361 {
362         struct nd_pfn *nd_pfn;
363         struct device *dev;
364
365         if (!is_memory(&nd_region->dev))
366                 return NULL;
367
368         nd_pfn = nd_pfn_alloc(nd_region);
369         dev = nd_pfn_devinit(nd_pfn, NULL);
370
371         __nd_device_register(dev);
372         return dev;
373 }
374
375 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
376 {
377         u64 checksum, offset;
378         unsigned long align;
379         enum nd_pfn_mode mode;
380         struct nd_namespace_io *nsio;
381         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
382         struct nd_namespace_common *ndns = nd_pfn->ndns;
383         const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
384
385         if (!pfn_sb || !ndns)
386                 return -ENODEV;
387
388         if (!is_memory(nd_pfn->dev.parent))
389                 return -ENODEV;
390
391         if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
392                 return -ENXIO;
393
394         if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
395                 return -ENODEV;
396
397         checksum = le64_to_cpu(pfn_sb->checksum);
398         pfn_sb->checksum = 0;
399         if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
400                 return -ENODEV;
401         pfn_sb->checksum = cpu_to_le64(checksum);
402
403         if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
404                 return -ENODEV;
405
406         if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
407                 pfn_sb->start_pad = 0;
408                 pfn_sb->end_trunc = 0;
409         }
410
411         if (__le16_to_cpu(pfn_sb->version_minor) < 2)
412                 pfn_sb->align = 0;
413
414         switch (le32_to_cpu(pfn_sb->mode)) {
415         case PFN_MODE_RAM:
416         case PFN_MODE_PMEM:
417                 break;
418         default:
419                 return -ENXIO;
420         }
421
422         align = le32_to_cpu(pfn_sb->align);
423         offset = le64_to_cpu(pfn_sb->dataoff);
424         if (align == 0)
425                 align = 1UL << ilog2(offset);
426         mode = le32_to_cpu(pfn_sb->mode);
427
428         if (!nd_pfn->uuid) {
429                 /*
430                  * When probing a namepace via nd_pfn_probe() the uuid
431                  * is NULL (see: nd_pfn_devinit()) we init settings from
432                  * pfn_sb
433                  */
434                 nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
435                 if (!nd_pfn->uuid)
436                         return -ENOMEM;
437                 nd_pfn->align = align;
438                 nd_pfn->mode = mode;
439         } else {
440                 /*
441                  * When probing a pfn / dax instance we validate the
442                  * live settings against the pfn_sb
443                  */
444                 if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
445                         return -ENODEV;
446
447                 /*
448                  * If the uuid validates, but other settings mismatch
449                  * return EINVAL because userspace has managed to change
450                  * the configuration without specifying new
451                  * identification.
452                  */
453                 if (nd_pfn->align != align || nd_pfn->mode != mode) {
454                         dev_err(&nd_pfn->dev,
455                                         "init failed, settings mismatch\n");
456                         dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
457                                         nd_pfn->align, align, nd_pfn->mode,
458                                         mode);
459                         return -EINVAL;
460                 }
461         }
462
463         if (align > nvdimm_namespace_capacity(ndns)) {
464                 dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
465                                 align, nvdimm_namespace_capacity(ndns));
466                 return -EINVAL;
467         }
468
469         /*
470          * These warnings are verbose because they can only trigger in
471          * the case where the physical address alignment of the
472          * namespace has changed since the pfn superblock was
473          * established.
474          */
475         nsio = to_nd_namespace_io(&ndns->dev);
476         if (offset >= resource_size(&nsio->res)) {
477                 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
478                                 dev_name(&ndns->dev));
479                 return -EBUSY;
480         }
481
482         if ((align && !IS_ALIGNED(offset, align))
483                         || !IS_ALIGNED(offset, PAGE_SIZE)) {
484                 dev_err(&nd_pfn->dev,
485                                 "bad offset: %#llx dax disabled align: %#lx\n",
486                                 offset, align);
487                 return -ENXIO;
488         }
489
490         return 0;
491 }
492 EXPORT_SYMBOL(nd_pfn_validate);
493
494 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
495 {
496         int rc;
497         struct nd_pfn *nd_pfn;
498         struct device *pfn_dev;
499         struct nd_pfn_sb *pfn_sb;
500         struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
501
502         if (ndns->force_raw)
503                 return -ENODEV;
504
505         switch (ndns->claim_class) {
506         case NVDIMM_CCLASS_NONE:
507         case NVDIMM_CCLASS_PFN:
508                 break;
509         default:
510                 return -ENODEV;
511         }
512
513         nvdimm_bus_lock(&ndns->dev);
514         nd_pfn = nd_pfn_alloc(nd_region);
515         pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
516         nvdimm_bus_unlock(&ndns->dev);
517         if (!pfn_dev)
518                 return -ENOMEM;
519         pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
520         nd_pfn = to_nd_pfn(pfn_dev);
521         nd_pfn->pfn_sb = pfn_sb;
522         rc = nd_pfn_validate(nd_pfn, PFN_SIG);
523         dev_dbg(dev, "%s: pfn: %s\n", __func__,
524                         rc == 0 ? dev_name(pfn_dev) : "<none>");
525         if (rc < 0) {
526                 nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
527                 put_device(pfn_dev);
528         } else
529                 __nd_device_register(pfn_dev);
530
531         return rc;
532 }
533 EXPORT_SYMBOL(nd_pfn_probe);
534
535 /*
536  * We hotplug memory at section granularity, pad the reserved area from
537  * the previous section base to the namespace base address.
538  */
539 static unsigned long init_altmap_base(resource_size_t base)
540 {
541         unsigned long base_pfn = PHYS_PFN(base);
542
543         return PFN_SECTION_ALIGN_DOWN(base_pfn);
544 }
545
546 static unsigned long init_altmap_reserve(resource_size_t base)
547 {
548         unsigned long reserve = PHYS_PFN(SZ_8K);
549         unsigned long base_pfn = PHYS_PFN(base);
550
551         reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
552         return reserve;
553 }
554
555 static struct vmem_altmap *__nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
556                 struct resource *res, struct vmem_altmap *altmap)
557 {
558         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
559         u64 offset = le64_to_cpu(pfn_sb->dataoff);
560         u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
561         u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
562         struct nd_namespace_common *ndns = nd_pfn->ndns;
563         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
564         resource_size_t base = nsio->res.start + start_pad;
565         struct vmem_altmap __altmap = {
566                 .base_pfn = init_altmap_base(base),
567                 .reserve = init_altmap_reserve(base),
568         };
569
570         memcpy(res, &nsio->res, sizeof(*res));
571         res->start += start_pad;
572         res->end -= end_trunc;
573
574         if (nd_pfn->mode == PFN_MODE_RAM) {
575                 if (offset < SZ_8K)
576                         return ERR_PTR(-EINVAL);
577                 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
578                 altmap = NULL;
579         } else if (nd_pfn->mode == PFN_MODE_PMEM) {
580                 nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res)
581                                         - offset) / PAGE_SIZE);
582                 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
583                         dev_info(&nd_pfn->dev,
584                                         "number of pfns truncated from %lld to %ld\n",
585                                         le64_to_cpu(nd_pfn->pfn_sb->npfns),
586                                         nd_pfn->npfns);
587                 memcpy(altmap, &__altmap, sizeof(*altmap));
588                 altmap->free = PHYS_PFN(offset - SZ_8K);
589                 altmap->alloc = 0;
590         } else
591                 return ERR_PTR(-ENXIO);
592
593         return altmap;
594 }
595
596 static int nd_pfn_init(struct nd_pfn *nd_pfn)
597 {
598         u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
599         struct nd_namespace_common *ndns = nd_pfn->ndns;
600         u32 start_pad = 0, end_trunc = 0;
601         resource_size_t start, size;
602         struct nd_namespace_io *nsio;
603         struct nd_region *nd_region;
604         struct nd_pfn_sb *pfn_sb;
605         unsigned long npfns;
606         phys_addr_t offset;
607         const char *sig;
608         u64 checksum;
609         int rc;
610
611         pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
612         if (!pfn_sb)
613                 return -ENOMEM;
614
615         nd_pfn->pfn_sb = pfn_sb;
616         if (is_nd_dax(&nd_pfn->dev))
617                 sig = DAX_SIG;
618         else
619                 sig = PFN_SIG;
620         rc = nd_pfn_validate(nd_pfn, sig);
621         if (rc != -ENODEV)
622                 return rc;
623
624         /* no info block, do init */;
625         nd_region = to_nd_region(nd_pfn->dev.parent);
626         if (nd_region->ro) {
627                 dev_info(&nd_pfn->dev,
628                                 "%s is read-only, unable to init metadata\n",
629                                 dev_name(&nd_region->dev));
630                 return -ENXIO;
631         }
632
633         memset(pfn_sb, 0, sizeof(*pfn_sb));
634
635         /*
636          * Check if pmem collides with 'System RAM' when section aligned and
637          * trim it accordingly
638          */
639         nsio = to_nd_namespace_io(&ndns->dev);
640         start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
641         size = resource_size(&nsio->res);
642         if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
643                                 IORES_DESC_NONE) == REGION_MIXED) {
644                 start = nsio->res.start;
645                 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
646         }
647
648         start = nsio->res.start;
649         size = PHYS_SECTION_ALIGN_UP(start + size) - start;
650         if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
651                                 IORES_DESC_NONE) == REGION_MIXED) {
652                 size = resource_size(&nsio->res);
653                 end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size);
654         }
655
656         if (start_pad + end_trunc)
657                 dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n",
658                                 dev_name(&ndns->dev), start_pad + end_trunc);
659
660         /*
661          * Note, we use 64 here for the standard size of struct page,
662          * debugging options may cause it to be larger in which case the
663          * implementation will limit the pfns advertised through
664          * ->direct_access() to those that are included in the memmap.
665          */
666         start += start_pad;
667         size = resource_size(&nsio->res);
668         npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
669                         / PAGE_SIZE);
670         if (nd_pfn->mode == PFN_MODE_PMEM) {
671                 /*
672                  * The altmap should be padded out to the block size used
673                  * when populating the vmemmap. This *should* be equal to
674                  * PMD_SIZE for most architectures.
675                  */
676                 offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve,
677                                 max(nd_pfn->align, PMD_SIZE)) - start;
678         } else if (nd_pfn->mode == PFN_MODE_RAM)
679                 offset = ALIGN(start + SZ_8K + dax_label_reserve,
680                                 nd_pfn->align) - start;
681         else
682                 return -ENXIO;
683
684         if (offset + start_pad + end_trunc >= size) {
685                 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
686                                 dev_name(&ndns->dev));
687                 return -ENXIO;
688         }
689
690         npfns = (size - offset - start_pad - end_trunc) / SZ_4K;
691         pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
692         pfn_sb->dataoff = cpu_to_le64(offset);
693         pfn_sb->npfns = cpu_to_le64(npfns);
694         memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
695         memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
696         memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
697         pfn_sb->version_major = cpu_to_le16(1);
698         pfn_sb->version_minor = cpu_to_le16(2);
699         pfn_sb->start_pad = cpu_to_le32(start_pad);
700         pfn_sb->end_trunc = cpu_to_le32(end_trunc);
701         pfn_sb->align = cpu_to_le32(nd_pfn->align);
702         checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
703         pfn_sb->checksum = cpu_to_le64(checksum);
704
705         return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
706 }
707
708 /*
709  * Determine the effective resource range and vmem_altmap from an nd_pfn
710  * instance.
711  */
712 struct vmem_altmap *nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
713                 struct resource *res, struct vmem_altmap *altmap)
714 {
715         int rc;
716
717         if (!nd_pfn->uuid || !nd_pfn->ndns)
718                 return ERR_PTR(-ENODEV);
719
720         rc = nd_pfn_init(nd_pfn);
721         if (rc)
722                 return ERR_PTR(rc);
723
724         /* we need a valid pfn_sb before we can init a vmem_altmap */
725         return __nvdimm_setup_pfn(nd_pfn, res, altmap);
726 }
727 EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);