1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
3 #include <linux/memremap.h>
4 #include <linux/device.h>
5 #include <linux/mutex.h>
6 #include <linux/list.h>
7 #include <linux/slab.h>
10 #include "dax-private.h"
13 static struct class *dax_class;
15 static DEFINE_MUTEX(dax_bus_lock);
17 #define DAX_NAME_LEN 30
19 struct list_head list;
20 char dev_name[DAX_NAME_LEN];
23 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
26 * We only ever expect to handle device-dax instances, i.e. the
27 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
29 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
32 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
34 return container_of(drv, struct dax_device_driver, drv);
37 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
40 struct dax_id *dax_id;
42 lockdep_assert_held(&dax_bus_lock);
44 list_for_each_entry(dax_id, &dax_drv->ids, list)
45 if (sysfs_streq(dax_id->dev_name, dev_name))
50 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
54 mutex_lock(&dax_bus_lock);
55 match = !!__dax_match_id(dax_drv, dev_name(dev));
56 mutex_unlock(&dax_bus_lock);
66 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
67 size_t count, enum id_action action)
69 struct dax_device_driver *dax_drv = to_dax_drv(drv);
70 unsigned int region_id, id;
71 char devname[DAX_NAME_LEN];
72 struct dax_id *dax_id;
76 fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
79 sprintf(devname, "dax%d.%d", region_id, id);
80 if (!sysfs_streq(buf, devname))
83 mutex_lock(&dax_bus_lock);
84 dax_id = __dax_match_id(dax_drv, buf);
86 if (action == ID_ADD) {
87 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
89 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
90 list_add(&dax_id->list, &dax_drv->ids);
94 /* nothing to remove */;
95 } else if (action == ID_REMOVE) {
96 list_del(&dax_id->list);
99 /* dax_id already added */;
100 mutex_unlock(&dax_bus_lock);
104 if (action == ID_ADD)
105 rc = driver_attach(drv);
111 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
114 return do_id_store(drv, buf, count, ID_ADD);
116 static DRIVER_ATTR_WO(new_id);
118 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
121 return do_id_store(drv, buf, count, ID_REMOVE);
123 static DRIVER_ATTR_WO(remove_id);
125 static struct attribute *dax_drv_attrs[] = {
126 &driver_attr_new_id.attr,
127 &driver_attr_remove_id.attr,
130 ATTRIBUTE_GROUPS(dax_drv);
132 static int dax_bus_match(struct device *dev, struct device_driver *drv);
134 static bool is_static(struct dax_region *dax_region)
136 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
139 static u64 dev_dax_size(struct dev_dax *dev_dax)
144 device_lock_assert(&dev_dax->dev);
146 for (i = 0; i < dev_dax->nr_range; i++)
147 size += range_len(&dev_dax->ranges[i].range);
152 static int dax_bus_probe(struct device *dev)
154 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
155 struct dev_dax *dev_dax = to_dev_dax(dev);
156 struct dax_region *dax_region = dev_dax->region;
159 if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
162 rc = dax_drv->probe(dev_dax);
164 if (rc || is_static(dax_region))
168 * Track new seed creation only after successful probe of the
171 if (dax_region->seed == dev)
172 dax_region->seed = NULL;
177 static int dax_bus_remove(struct device *dev)
179 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
180 struct dev_dax *dev_dax = to_dev_dax(dev);
182 return dax_drv->remove(dev_dax);
185 static struct bus_type dax_bus_type = {
187 .uevent = dax_bus_uevent,
188 .match = dax_bus_match,
189 .probe = dax_bus_probe,
190 .remove = dax_bus_remove,
191 .drv_groups = dax_drv_groups,
194 static int dax_bus_match(struct device *dev, struct device_driver *drv)
196 struct dax_device_driver *dax_drv = to_dax_drv(drv);
199 * All but the 'device-dax' driver, which has 'match_always'
200 * set, requires an exact id match.
202 if (dax_drv->match_always)
205 return dax_match_id(dax_drv, dev);
209 * Rely on the fact that drvdata is set before the attributes are
210 * registered, and that the attributes are unregistered before drvdata
211 * is cleared to assume that drvdata is always valid.
213 static ssize_t id_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct dax_region *dax_region = dev_get_drvdata(dev);
218 return sprintf(buf, "%d\n", dax_region->id);
220 static DEVICE_ATTR_RO(id);
222 static ssize_t region_size_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 struct dax_region *dax_region = dev_get_drvdata(dev);
227 return sprintf(buf, "%llu\n", (unsigned long long)
228 resource_size(&dax_region->res));
230 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
231 region_size_show, NULL);
233 static ssize_t region_align_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct dax_region *dax_region = dev_get_drvdata(dev);
238 return sprintf(buf, "%u\n", dax_region->align);
240 static struct device_attribute dev_attr_region_align =
241 __ATTR(align, 0400, region_align_show, NULL);
243 #define for_each_dax_region_resource(dax_region, res) \
244 for (res = (dax_region)->res.child; res; res = res->sibling)
246 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
248 resource_size_t size = resource_size(&dax_region->res);
249 struct resource *res;
251 device_lock_assert(dax_region->dev);
253 for_each_dax_region_resource(dax_region, res)
254 size -= resource_size(res);
258 static ssize_t available_size_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct dax_region *dax_region = dev_get_drvdata(dev);
262 unsigned long long size;
265 size = dax_region_avail_size(dax_region);
268 return sprintf(buf, "%llu\n", size);
270 static DEVICE_ATTR_RO(available_size);
272 static ssize_t seed_show(struct device *dev,
273 struct device_attribute *attr, char *buf)
275 struct dax_region *dax_region = dev_get_drvdata(dev);
279 if (is_static(dax_region))
283 seed = dax_region->seed;
284 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
289 static DEVICE_ATTR_RO(seed);
291 static ssize_t create_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 struct dax_region *dax_region = dev_get_drvdata(dev);
295 struct device *youngest;
298 if (is_static(dax_region))
302 youngest = dax_region->youngest;
303 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
309 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t len)
312 struct dax_region *dax_region = dev_get_drvdata(dev);
313 unsigned long long avail;
317 if (is_static(dax_region))
320 rc = kstrtoint(buf, 0, &val);
327 avail = dax_region_avail_size(dax_region);
331 struct dev_dax_data data = {
332 .dax_region = dax_region,
336 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
339 rc = PTR_ERR(dev_dax);
342 * In support of crafting multiple new devices
343 * simultaneously multiple seeds can be created,
344 * but only the first one that has not been
345 * successfully bound is tracked as the region
348 if (!dax_region->seed)
349 dax_region->seed = &dev_dax->dev;
350 dax_region->youngest = &dev_dax->dev;
358 static DEVICE_ATTR_RW(create);
360 void kill_dev_dax(struct dev_dax *dev_dax)
362 struct dax_device *dax_dev = dev_dax->dax_dev;
363 struct inode *inode = dax_inode(dax_dev);
366 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
368 EXPORT_SYMBOL_GPL(kill_dev_dax);
370 static void trim_dev_dax_range(struct dev_dax *dev_dax)
372 int i = dev_dax->nr_range - 1;
373 struct range *range = &dev_dax->ranges[i].range;
374 struct dax_region *dax_region = dev_dax->region;
376 device_lock_assert(dax_region->dev);
377 dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
378 (unsigned long long)range->start,
379 (unsigned long long)range->end);
381 __release_region(&dax_region->res, range->start, range_len(range));
382 if (--dev_dax->nr_range == 0) {
383 kfree(dev_dax->ranges);
384 dev_dax->ranges = NULL;
388 static void free_dev_dax_ranges(struct dev_dax *dev_dax)
390 while (dev_dax->nr_range)
391 trim_dev_dax_range(dev_dax);
394 static void unregister_dev_dax(void *dev)
396 struct dev_dax *dev_dax = to_dev_dax(dev);
398 dev_dbg(dev, "%s\n", __func__);
400 kill_dev_dax(dev_dax);
401 free_dev_dax_ranges(dev_dax);
406 /* a return value >= 0 indicates this invocation invalidated the id */
407 static int __free_dev_dax_id(struct dev_dax *dev_dax)
409 struct dax_region *dax_region = dev_dax->region;
410 struct device *dev = &dev_dax->dev;
411 int rc = dev_dax->id;
413 device_lock_assert(dev);
415 if (is_static(dax_region) || dev_dax->id < 0)
417 ida_free(&dax_region->ida, dev_dax->id);
422 static int free_dev_dax_id(struct dev_dax *dev_dax)
424 struct device *dev = &dev_dax->dev;
428 rc = __free_dev_dax_id(dev_dax);
433 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t len)
436 struct dax_region *dax_region = dev_get_drvdata(dev);
437 struct dev_dax *dev_dax;
438 struct device *victim;
442 if (is_static(dax_region))
445 victim = device_find_child_by_name(dax_region->dev, buf);
451 dev_dax = to_dev_dax(victim);
452 if (victim->driver || dev_dax_size(dev_dax))
456 * Invalidate the device so it does not become active
457 * again, but always preserve device-id-0 so that
458 * /sys/bus/dax/ is guaranteed to be populated while any
459 * dax_region is registered.
461 if (dev_dax->id > 0) {
462 do_del = __free_dev_dax_id(dev_dax) >= 0;
464 if (dax_region->seed == victim)
465 dax_region->seed = NULL;
466 if (dax_region->youngest == victim)
467 dax_region->youngest = NULL;
471 device_unlock(victim);
473 /* won the race to invalidate the device, clean it up */
475 devm_release_action(dev, unregister_dev_dax, victim);
481 static DEVICE_ATTR_WO(delete);
483 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
486 struct device *dev = container_of(kobj, struct device, kobj);
487 struct dax_region *dax_region = dev_get_drvdata(dev);
489 if (is_static(dax_region))
490 if (a == &dev_attr_available_size.attr
491 || a == &dev_attr_create.attr
492 || a == &dev_attr_seed.attr
493 || a == &dev_attr_delete.attr)
498 static struct attribute *dax_region_attributes[] = {
499 &dev_attr_available_size.attr,
500 &dev_attr_region_size.attr,
501 &dev_attr_region_align.attr,
502 &dev_attr_create.attr,
504 &dev_attr_delete.attr,
509 static const struct attribute_group dax_region_attribute_group = {
510 .name = "dax_region",
511 .attrs = dax_region_attributes,
512 .is_visible = dax_region_visible,
515 static const struct attribute_group *dax_region_attribute_groups[] = {
516 &dax_region_attribute_group,
520 static void dax_region_free(struct kref *kref)
522 struct dax_region *dax_region;
524 dax_region = container_of(kref, struct dax_region, kref);
528 void dax_region_put(struct dax_region *dax_region)
530 kref_put(&dax_region->kref, dax_region_free);
532 EXPORT_SYMBOL_GPL(dax_region_put);
534 static void dax_region_unregister(void *region)
536 struct dax_region *dax_region = region;
538 sysfs_remove_groups(&dax_region->dev->kobj,
539 dax_region_attribute_groups);
540 dax_region_put(dax_region);
543 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
544 struct range *range, int target_node, unsigned int align,
547 struct dax_region *dax_region;
550 * The DAX core assumes that it can store its private data in
551 * parent->driver_data. This WARN is a reminder / safeguard for
552 * developers of device-dax drivers.
554 if (dev_get_drvdata(parent)) {
555 dev_WARN(parent, "dax core failed to setup private data\n");
559 if (!IS_ALIGNED(range->start, align)
560 || !IS_ALIGNED(range_len(range), align))
563 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
567 dev_set_drvdata(parent, dax_region);
568 kref_init(&dax_region->kref);
569 dax_region->id = region_id;
570 dax_region->align = align;
571 dax_region->dev = parent;
572 dax_region->target_node = target_node;
573 ida_init(&dax_region->ida);
574 dax_region->res = (struct resource) {
575 .start = range->start,
577 .flags = IORESOURCE_MEM | flags,
580 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
585 kref_get(&dax_region->kref);
586 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
590 EXPORT_SYMBOL_GPL(alloc_dax_region);
592 static void dax_mapping_release(struct device *dev)
594 struct dax_mapping *mapping = to_dax_mapping(dev);
595 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
597 ida_free(&dev_dax->ida, mapping->id);
601 static void unregister_dax_mapping(void *data)
603 struct device *dev = data;
604 struct dax_mapping *mapping = to_dax_mapping(dev);
605 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
606 struct dax_region *dax_region = dev_dax->region;
608 dev_dbg(dev, "%s\n", __func__);
610 device_lock_assert(dax_region->dev);
612 dev_dax->ranges[mapping->range_id].mapping = NULL;
613 mapping->range_id = -1;
619 static struct dev_dax_range *get_dax_range(struct device *dev)
621 struct dax_mapping *mapping = to_dax_mapping(dev);
622 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
623 struct dax_region *dax_region = dev_dax->region;
625 device_lock(dax_region->dev);
626 if (mapping->range_id < 0) {
627 device_unlock(dax_region->dev);
631 return &dev_dax->ranges[mapping->range_id];
634 static void put_dax_range(struct dev_dax_range *dax_range)
636 struct dax_mapping *mapping = dax_range->mapping;
637 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
638 struct dax_region *dax_region = dev_dax->region;
640 device_unlock(dax_region->dev);
643 static ssize_t start_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
646 struct dev_dax_range *dax_range;
649 dax_range = get_dax_range(dev);
652 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
653 put_dax_range(dax_range);
657 static DEVICE_ATTR(start, 0400, start_show, NULL);
659 static ssize_t end_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct dev_dax_range *dax_range;
665 dax_range = get_dax_range(dev);
668 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
669 put_dax_range(dax_range);
673 static DEVICE_ATTR(end, 0400, end_show, NULL);
675 static ssize_t pgoff_show(struct device *dev,
676 struct device_attribute *attr, char *buf)
678 struct dev_dax_range *dax_range;
681 dax_range = get_dax_range(dev);
684 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
685 put_dax_range(dax_range);
689 static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
691 static struct attribute *dax_mapping_attributes[] = {
692 &dev_attr_start.attr,
694 &dev_attr_page_offset.attr,
698 static const struct attribute_group dax_mapping_attribute_group = {
699 .attrs = dax_mapping_attributes,
702 static const struct attribute_group *dax_mapping_attribute_groups[] = {
703 &dax_mapping_attribute_group,
707 static struct device_type dax_mapping_type = {
708 .release = dax_mapping_release,
709 .groups = dax_mapping_attribute_groups,
712 static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
714 struct dax_region *dax_region = dev_dax->region;
715 struct dax_mapping *mapping;
719 device_lock_assert(dax_region->dev);
721 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
722 "region disabled\n"))
725 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
728 mapping->range_id = range_id;
729 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
730 if (mapping->id < 0) {
734 dev_dax->ranges[range_id].mapping = mapping;
736 device_initialize(dev);
737 dev->parent = &dev_dax->dev;
738 dev->type = &dax_mapping_type;
739 dev_set_name(dev, "mapping%d", mapping->id);
740 rc = device_add(dev);
746 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
753 static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
754 resource_size_t size)
756 struct dax_region *dax_region = dev_dax->region;
757 struct resource *res = &dax_region->res;
758 struct device *dev = &dev_dax->dev;
759 struct dev_dax_range *ranges;
760 unsigned long pgoff = 0;
761 struct resource *alloc;
764 device_lock_assert(dax_region->dev);
766 /* handle the seed alloc special case */
768 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
769 "0-size allocation must be first\n"))
771 /* nr_range == 0 is elsewhere special cased as 0-size device */
775 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
776 * (dev_dax->nr_range + 1), GFP_KERNEL);
780 alloc = __request_region(res, start, size, dev_name(dev), 0);
783 * If this was an empty set of ranges nothing else
784 * will release @ranges, so do it now.
786 if (!dev_dax->nr_range) {
790 dev_dax->ranges = ranges;
794 for (i = 0; i < dev_dax->nr_range; i++)
795 pgoff += PHYS_PFN(range_len(&ranges[i].range));
796 dev_dax->ranges = ranges;
797 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
800 .start = alloc->start,
805 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
806 &alloc->start, &alloc->end);
808 * A dev_dax instance must be registered before mapping device
809 * children can be added. Defer to devm_create_dev_dax() to add
810 * the initial mapping device.
812 if (!device_is_registered(&dev_dax->dev))
815 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
817 trim_dev_dax_range(dev_dax);
822 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
824 int last_range = dev_dax->nr_range - 1;
825 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
826 struct dax_region *dax_region = dev_dax->region;
827 bool is_shrink = resource_size(res) > size;
828 struct range *range = &dax_range->range;
829 struct device *dev = &dev_dax->dev;
832 device_lock_assert(dax_region->dev);
834 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
837 rc = adjust_resource(res, range->start, size);
841 *range = (struct range) {
842 .start = range->start,
843 .end = range->start + size - 1,
846 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
847 last_range, (unsigned long long) range->start,
848 (unsigned long long) range->end);
853 static ssize_t size_show(struct device *dev,
854 struct device_attribute *attr, char *buf)
856 struct dev_dax *dev_dax = to_dev_dax(dev);
857 unsigned long long size;
860 size = dev_dax_size(dev_dax);
863 return sprintf(buf, "%llu\n", size);
866 static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
869 * The minimum mapping granularity for a device instance is a
870 * single subsection, unless the arch says otherwise.
872 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
875 static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
877 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
878 struct dax_region *dax_region = dev_dax->region;
879 struct device *dev = &dev_dax->dev;
882 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
883 struct range *range = &dev_dax->ranges[i].range;
884 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
885 struct resource *adjust = NULL, *res;
886 resource_size_t shrink;
888 shrink = min_t(u64, to_shrink, range_len(range));
889 if (shrink >= range_len(range)) {
890 devm_release_action(dax_region->dev,
891 unregister_dax_mapping, &mapping->dev);
892 trim_dev_dax_range(dev_dax);
899 for_each_dax_region_resource(dax_region, res)
900 if (strcmp(res->name, dev_name(dev)) == 0
901 && res->start == range->start) {
906 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
907 "failed to find matching resource\n"))
909 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
916 * Only allow adjustments that preserve the relative pgoff of existing
917 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
919 static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
921 struct dev_dax_range *last;
924 if (dev_dax->nr_range == 0)
926 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
928 last = &dev_dax->ranges[dev_dax->nr_range - 1];
929 if (last->range.start != res->start || last->range.end != res->end)
931 for (i = 0; i < dev_dax->nr_range - 1; i++) {
932 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
934 if (dax_range->pgoff > last->pgoff)
941 static ssize_t dev_dax_resize(struct dax_region *dax_region,
942 struct dev_dax *dev_dax, resource_size_t size)
944 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
945 resource_size_t dev_size = dev_dax_size(dev_dax);
946 struct resource *region_res = &dax_region->res;
947 struct device *dev = &dev_dax->dev;
948 struct resource *res, *first;
949 resource_size_t alloc = 0;
954 if (size == dev_size)
956 if (size > dev_size && size - dev_size > avail)
959 return dev_dax_shrink(dev_dax, size);
961 to_alloc = size - dev_size;
962 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
963 "resize of %pa misaligned\n", &to_alloc))
967 * Expand the device into the unused portion of the region. This
968 * may involve adjusting the end of an existing resource, or
969 * allocating a new resource.
972 first = region_res->child;
974 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
977 for (res = first; res; res = res->sibling) {
978 struct resource *next = res->sibling;
980 /* space at the beginning of the region */
981 if (res == first && res->start > dax_region->res.start) {
982 alloc = min(res->start - dax_region->res.start, to_alloc);
983 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
988 /* space between allocations */
989 if (next && next->start > res->end + 1)
990 alloc = min(next->start - (res->end + 1), to_alloc);
992 /* space at the end of the region */
993 if (!alloc && !next && res->end < region_res->end)
994 alloc = min(region_res->end - res->end, to_alloc);
999 if (adjust_ok(dev_dax, res)) {
1000 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1003 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1014 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1015 const char *buf, size_t len)
1018 unsigned long long val;
1019 struct dev_dax *dev_dax = to_dev_dax(dev);
1020 struct dax_region *dax_region = dev_dax->region;
1022 rc = kstrtoull(buf, 0, &val);
1026 if (!alloc_is_aligned(dev_dax, val)) {
1027 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1031 device_lock(dax_region->dev);
1032 if (!dax_region->dev->driver) {
1033 device_unlock(dax_region->dev);
1037 rc = dev_dax_resize(dax_region, dev_dax, val);
1039 device_unlock(dax_region->dev);
1041 return rc == 0 ? len : rc;
1043 static DEVICE_ATTR_RW(size);
1045 static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1047 unsigned long long addr = 0;
1048 char *start, *end, *str;
1049 ssize_t rc = -EINVAL;
1051 str = kstrdup(opt, GFP_KERNEL);
1056 start = strsep(&end, "-");
1060 rc = kstrtoull(start, 16, &addr);
1063 range->start = addr;
1065 rc = kstrtoull(end, 16, &addr);
1075 static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1076 const char *buf, size_t len)
1078 struct dev_dax *dev_dax = to_dev_dax(dev);
1079 struct dax_region *dax_region = dev_dax->region;
1084 rc = range_parse(buf, len, &r);
1089 device_lock(dax_region->dev);
1090 if (!dax_region->dev->driver) {
1091 device_unlock(dax_region->dev);
1096 to_alloc = range_len(&r);
1097 if (alloc_is_aligned(dev_dax, to_alloc))
1098 rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1100 device_unlock(dax_region->dev);
1102 return rc == 0 ? len : rc;
1104 static DEVICE_ATTR_WO(mapping);
1106 static ssize_t align_show(struct device *dev,
1107 struct device_attribute *attr, char *buf)
1109 struct dev_dax *dev_dax = to_dev_dax(dev);
1111 return sprintf(buf, "%d\n", dev_dax->align);
1114 static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1116 resource_size_t dev_size = dev_dax_size(dev_dax);
1117 struct device *dev = &dev_dax->dev;
1120 if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
1121 dev_dbg(dev, "%s: align %u invalid for size %pa\n",
1122 __func__, dev_dax->align, &dev_size);
1126 for (i = 0; i < dev_dax->nr_range; i++) {
1127 size_t len = range_len(&dev_dax->ranges[i].range);
1129 if (!alloc_is_aligned(dev_dax, len)) {
1130 dev_dbg(dev, "%s: align %u invalid for range %d\n",
1131 __func__, dev_dax->align, i);
1139 static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1140 const char *buf, size_t len)
1142 struct dev_dax *dev_dax = to_dev_dax(dev);
1143 struct dax_region *dax_region = dev_dax->region;
1144 unsigned long val, align_save;
1147 rc = kstrtoul(buf, 0, &val);
1151 if (!dax_align_valid(val))
1154 device_lock(dax_region->dev);
1155 if (!dax_region->dev->driver) {
1156 device_unlock(dax_region->dev);
1166 align_save = dev_dax->align;
1167 dev_dax->align = val;
1168 rc = dev_dax_validate_align(dev_dax);
1170 dev_dax->align = align_save;
1173 device_unlock(dax_region->dev);
1174 return rc == 0 ? len : rc;
1176 static DEVICE_ATTR_RW(align);
1178 static int dev_dax_target_node(struct dev_dax *dev_dax)
1180 struct dax_region *dax_region = dev_dax->region;
1182 return dax_region->target_node;
1185 static ssize_t target_node_show(struct device *dev,
1186 struct device_attribute *attr, char *buf)
1188 struct dev_dax *dev_dax = to_dev_dax(dev);
1190 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1192 static DEVICE_ATTR_RO(target_node);
1194 static ssize_t resource_show(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1197 struct dev_dax *dev_dax = to_dev_dax(dev);
1198 struct dax_region *dax_region = dev_dax->region;
1199 unsigned long long start;
1201 if (dev_dax->nr_range < 1)
1202 start = dax_region->res.start;
1204 start = dev_dax->ranges[0].range.start;
1206 return sprintf(buf, "%#llx\n", start);
1208 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1210 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1214 * We only ever expect to handle device-dax instances, i.e. the
1215 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1217 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1219 static DEVICE_ATTR_RO(modalias);
1221 static ssize_t numa_node_show(struct device *dev,
1222 struct device_attribute *attr, char *buf)
1224 return sprintf(buf, "%d\n", dev_to_node(dev));
1226 static DEVICE_ATTR_RO(numa_node);
1228 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1230 struct device *dev = container_of(kobj, struct device, kobj);
1231 struct dev_dax *dev_dax = to_dev_dax(dev);
1232 struct dax_region *dax_region = dev_dax->region;
1234 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1236 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1238 if (a == &dev_attr_mapping.attr && is_static(dax_region))
1240 if ((a == &dev_attr_align.attr ||
1241 a == &dev_attr_size.attr) && is_static(dax_region))
1246 static struct attribute *dev_dax_attributes[] = {
1247 &dev_attr_modalias.attr,
1248 &dev_attr_size.attr,
1249 &dev_attr_mapping.attr,
1250 &dev_attr_target_node.attr,
1251 &dev_attr_align.attr,
1252 &dev_attr_resource.attr,
1253 &dev_attr_numa_node.attr,
1257 static const struct attribute_group dev_dax_attribute_group = {
1258 .attrs = dev_dax_attributes,
1259 .is_visible = dev_dax_visible,
1262 static const struct attribute_group *dax_attribute_groups[] = {
1263 &dev_dax_attribute_group,
1267 static void dev_dax_release(struct device *dev)
1269 struct dev_dax *dev_dax = to_dev_dax(dev);
1270 struct dax_region *dax_region = dev_dax->region;
1271 struct dax_device *dax_dev = dev_dax->dax_dev;
1274 free_dev_dax_id(dev_dax);
1275 dax_region_put(dax_region);
1276 kfree(dev_dax->pgmap);
1280 static const struct device_type dev_dax_type = {
1281 .release = dev_dax_release,
1282 .groups = dax_attribute_groups,
1285 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1287 struct dax_region *dax_region = data->dax_region;
1288 struct device *parent = dax_region->dev;
1289 struct dax_device *dax_dev;
1290 struct dev_dax *dev_dax;
1291 struct inode *inode;
1295 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1297 return ERR_PTR(-ENOMEM);
1299 if (is_static(dax_region)) {
1300 if (dev_WARN_ONCE(parent, data->id < 0,
1301 "dynamic id specified to static region\n")) {
1306 dev_dax->id = data->id;
1308 if (dev_WARN_ONCE(parent, data->id >= 0,
1309 "static id specified to dynamic region\n")) {
1314 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1320 dev_dax->region = dax_region;
1321 dev = &dev_dax->dev;
1322 device_initialize(dev);
1323 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1325 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1330 dev_WARN_ONCE(parent, !is_static(dax_region),
1331 "custom dev_pagemap requires a static dax_region\n");
1333 dev_dax->pgmap = kmemdup(data->pgmap,
1334 sizeof(struct dev_pagemap), GFP_KERNEL);
1335 if (!dev_dax->pgmap) {
1342 * No 'host' or dax_operations since there is no access to this
1343 * device outside of mmap of the resulting character device.
1345 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
1346 if (IS_ERR(dax_dev)) {
1347 rc = PTR_ERR(dax_dev);
1351 /* a device_dax instance is dead while the driver is not attached */
1354 dev_dax->dax_dev = dax_dev;
1355 dev_dax->target_node = dax_region->target_node;
1356 dev_dax->align = dax_region->align;
1357 ida_init(&dev_dax->ida);
1358 kref_get(&dax_region->kref);
1360 inode = dax_inode(dax_dev);
1361 dev->devt = inode->i_rdev;
1362 if (data->subsys == DEV_DAX_BUS)
1363 dev->bus = &dax_bus_type;
1365 dev->class = dax_class;
1366 dev->parent = parent;
1367 dev->type = &dev_dax_type;
1369 rc = device_add(dev);
1371 kill_dev_dax(dev_dax);
1376 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1380 /* register mapping device for the initial allocation range */
1381 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1382 rc = devm_register_dax_mapping(dev_dax, 0);
1390 kfree(dev_dax->pgmap);
1392 free_dev_dax_ranges(dev_dax);
1394 free_dev_dax_id(dev_dax);
1400 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1402 static int match_always_count;
1404 int __dax_driver_register(struct dax_device_driver *dax_drv,
1405 struct module *module, const char *mod_name)
1407 struct device_driver *drv = &dax_drv->drv;
1410 INIT_LIST_HEAD(&dax_drv->ids);
1411 drv->owner = module;
1412 drv->name = mod_name;
1413 drv->mod_name = mod_name;
1414 drv->bus = &dax_bus_type;
1416 /* there can only be one default driver */
1417 mutex_lock(&dax_bus_lock);
1418 match_always_count += dax_drv->match_always;
1419 if (match_always_count > 1) {
1420 match_always_count--;
1424 mutex_unlock(&dax_bus_lock);
1427 return driver_register(drv);
1429 EXPORT_SYMBOL_GPL(__dax_driver_register);
1431 void dax_driver_unregister(struct dax_device_driver *dax_drv)
1433 struct device_driver *drv = &dax_drv->drv;
1434 struct dax_id *dax_id, *_id;
1436 mutex_lock(&dax_bus_lock);
1437 match_always_count -= dax_drv->match_always;
1438 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1439 list_del(&dax_id->list);
1442 mutex_unlock(&dax_bus_lock);
1443 driver_unregister(drv);
1445 EXPORT_SYMBOL_GPL(dax_driver_unregister);
1447 int __init dax_bus_init(void)
1451 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
1452 dax_class = class_create(THIS_MODULE, "dax");
1453 if (IS_ERR(dax_class))
1454 return PTR_ERR(dax_class);
1457 rc = bus_register(&dax_bus_type);
1459 class_destroy(dax_class);
1463 void __exit dax_bus_exit(void)
1465 bus_unregister(&dax_bus_type);
1466 class_destroy(dax_class);