blob: c1d379bd7af330741b1065603df4ba1d1d15d946 [file] [log] [blame]
Dan Williams51cf7842017-07-12 17:58:21 -07001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
Dan Williams89ec9f22018-10-29 15:52:42 -07003#include <linux/memremap.h>
Dan Williams51cf7842017-07-12 17:58:21 -07004#include <linux/device.h>
Dan Williamsd2007812018-11-07 15:31:23 -08005#include <linux/mutex.h>
6#include <linux/list.h>
Dan Williams51cf7842017-07-12 17:58:21 -07007#include <linux/slab.h>
8#include <linux/dax.h>
Dan Williamsfcffb6a2020-10-13 16:50:24 -07009#include <linux/io.h>
Dan Williams51cf7842017-07-12 17:58:21 -070010#include "dax-private.h"
11#include "bus.h"
12
Dan Williams730926c2017-07-16 13:51:53 -070013static struct class *dax_class;
14
Dan Williamsd2007812018-11-07 15:31:23 -080015static DEFINE_MUTEX(dax_bus_lock);
16
17#define DAX_NAME_LEN 30
18struct dax_id {
19 struct list_head list;
20 char dev_name[DAX_NAME_LEN];
21};
22
Dan Williams9567da02017-07-12 17:58:21 -070023static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
24{
25 /*
26 * We only ever expect to handle device-dax instances, i.e. the
27 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
28 */
29 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
30}
31
Dan Williamsd2007812018-11-07 15:31:23 -080032static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
33{
34 return container_of(drv, struct dax_device_driver, drv);
35}
36
37static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
38 const char *dev_name)
39{
40 struct dax_id *dax_id;
41
42 lockdep_assert_held(&dax_bus_lock);
43
44 list_for_each_entry(dax_id, &dax_drv->ids, list)
45 if (sysfs_streq(dax_id->dev_name, dev_name))
46 return dax_id;
47 return NULL;
48}
49
50static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
51{
52 int match;
53
54 mutex_lock(&dax_bus_lock);
55 match = !!__dax_match_id(dax_drv, dev_name(dev));
56 mutex_unlock(&dax_bus_lock);
57
58 return match;
59}
60
Dan Williams664525b2019-01-24 13:12:04 -080061enum id_action {
62 ID_REMOVE,
63 ID_ADD,
64};
65
Dan Williamsd2007812018-11-07 15:31:23 -080066static ssize_t do_id_store(struct device_driver *drv, const char *buf,
Dan Williams664525b2019-01-24 13:12:04 -080067 size_t count, enum id_action action)
Dan Williamsd2007812018-11-07 15:31:23 -080068{
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;
73 ssize_t rc = count;
74 int fields;
75
76 fields = sscanf(buf, "dax%d.%d", &region_id, &id);
77 if (fields != 2)
78 return -EINVAL;
79 sprintf(devname, "dax%d.%d", region_id, id);
80 if (!sysfs_streq(buf, devname))
81 return -EINVAL;
82
83 mutex_lock(&dax_bus_lock);
84 dax_id = __dax_match_id(dax_drv, buf);
85 if (!dax_id) {
Dan Williams664525b2019-01-24 13:12:04 -080086 if (action == ID_ADD) {
Dan Williamsd2007812018-11-07 15:31:23 -080087 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
88 if (dax_id) {
89 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
90 list_add(&dax_id->list, &dax_drv->ids);
91 } else
92 rc = -ENOMEM;
93 } else
94 /* nothing to remove */;
Dan Williams664525b2019-01-24 13:12:04 -080095 } else if (action == ID_REMOVE) {
Dan Williamsd2007812018-11-07 15:31:23 -080096 list_del(&dax_id->list);
97 kfree(dax_id);
98 } else
99 /* dax_id already added */;
100 mutex_unlock(&dax_bus_lock);
Dan Williams664525b2019-01-24 13:12:04 -0800101
102 if (rc < 0)
103 return rc;
104 if (action == ID_ADD)
105 rc = driver_attach(drv);
106 if (rc)
107 return rc;
108 return count;
Dan Williamsd2007812018-11-07 15:31:23 -0800109}
110
111static ssize_t new_id_store(struct device_driver *drv, const char *buf,
112 size_t count)
113{
Dan Williams664525b2019-01-24 13:12:04 -0800114 return do_id_store(drv, buf, count, ID_ADD);
Dan Williamsd2007812018-11-07 15:31:23 -0800115}
116static DRIVER_ATTR_WO(new_id);
117
118static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
119 size_t count)
120{
Dan Williams664525b2019-01-24 13:12:04 -0800121 return do_id_store(drv, buf, count, ID_REMOVE);
Dan Williamsd2007812018-11-07 15:31:23 -0800122}
123static DRIVER_ATTR_WO(remove_id);
124
125static struct attribute *dax_drv_attrs[] = {
126 &driver_attr_new_id.attr,
127 &driver_attr_remove_id.attr,
128 NULL,
129};
130ATTRIBUTE_GROUPS(dax_drv);
131
Dan Williams9567da02017-07-12 17:58:21 -0700132static int dax_bus_match(struct device *dev, struct device_driver *drv);
133
Dan Williamsc2f30112020-10-13 16:50:03 -0700134static bool is_static(struct dax_region *dax_region)
135{
136 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
137}
138
Dan Williams60e93dc2020-10-13 16:50:39 -0700139static u64 dev_dax_size(struct dev_dax *dev_dax)
140{
141 u64 size = 0;
142 int i;
143
144 device_lock_assert(&dev_dax->dev);
145
146 for (i = 0; i < dev_dax->nr_range; i++)
147 size += range_len(&dev_dax->ranges[i].range);
148
149 return size;
150}
151
Dan Williamsf11cf812020-10-13 16:50:08 -0700152static int dax_bus_probe(struct device *dev)
153{
154 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
155 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams0f3da142020-10-13 16:50:13 -0700156 struct dax_region *dax_region = dev_dax->region;
Dan Williams0f3da142020-10-13 16:50:13 -0700157 int rc;
Dan Williamsf11cf812020-10-13 16:50:08 -0700158
Dan Williams60e93dc2020-10-13 16:50:39 -0700159 if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
Dan Williams0f3da142020-10-13 16:50:13 -0700160 return -ENXIO;
161
162 rc = dax_drv->probe(dev_dax);
163
164 if (rc || is_static(dax_region))
165 return rc;
166
167 /*
168 * Track new seed creation only after successful probe of the
169 * previous seed.
170 */
171 if (dax_region->seed == dev)
172 dax_region->seed = NULL;
173
174 return 0;
Dan Williamsf11cf812020-10-13 16:50:08 -0700175}
176
177static int dax_bus_remove(struct device *dev)
178{
179 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
180 struct dev_dax *dev_dax = to_dev_dax(dev);
181
182 return dax_drv->remove(dev_dax);
183}
184
Dan Williams9567da02017-07-12 17:58:21 -0700185static struct bus_type dax_bus_type = {
186 .name = "dax",
187 .uevent = dax_bus_uevent,
188 .match = dax_bus_match,
Dan Williamsf11cf812020-10-13 16:50:08 -0700189 .probe = dax_bus_probe,
190 .remove = dax_bus_remove,
Dan Williamsd2007812018-11-07 15:31:23 -0800191 .drv_groups = dax_drv_groups,
Dan Williams9567da02017-07-12 17:58:21 -0700192};
193
194static int dax_bus_match(struct device *dev, struct device_driver *drv)
195{
Dan Williamsd2007812018-11-07 15:31:23 -0800196 struct dax_device_driver *dax_drv = to_dax_drv(drv);
197
Dan Williams9567da02017-07-12 17:58:21 -0700198 /*
Dan Williamsd2007812018-11-07 15:31:23 -0800199 * All but the 'device-dax' driver, which has 'match_always'
200 * set, requires an exact id match.
Dan Williams9567da02017-07-12 17:58:21 -0700201 */
Dan Williamsd2007812018-11-07 15:31:23 -0800202 if (dax_drv->match_always)
203 return 1;
204
205 return dax_match_id(dax_drv, dev);
Dan Williams9567da02017-07-12 17:58:21 -0700206}
207
Dan Williams51cf7842017-07-12 17:58:21 -0700208/*
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.
212 */
213static ssize_t id_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct dax_region *dax_region = dev_get_drvdata(dev);
217
218 return sprintf(buf, "%d\n", dax_region->id);
219}
220static DEVICE_ATTR_RO(id);
221
222static ssize_t region_size_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 struct dax_region *dax_region = dev_get_drvdata(dev);
226
227 return sprintf(buf, "%llu\n", (unsigned long long)
228 resource_size(&dax_region->res));
229}
230static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
231 region_size_show, NULL);
232
Dan Williams6d821202020-10-13 16:50:55 -0700233static ssize_t region_align_show(struct device *dev,
Dan Williams51cf7842017-07-12 17:58:21 -0700234 struct device_attribute *attr, char *buf)
235{
236 struct dax_region *dax_region = dev_get_drvdata(dev);
237
238 return sprintf(buf, "%u\n", dax_region->align);
239}
Dan Williams6d821202020-10-13 16:50:55 -0700240static struct device_attribute dev_attr_region_align =
241 __ATTR(align, 0400, region_align_show, NULL);
Dan Williams51cf7842017-07-12 17:58:21 -0700242
Dan Williamsc2f30112020-10-13 16:50:03 -0700243#define for_each_dax_region_resource(dax_region, res) \
244 for (res = (dax_region)->res.child; res; res = res->sibling)
245
246static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
247{
248 resource_size_t size = resource_size(&dax_region->res);
249 struct resource *res;
250
251 device_lock_assert(dax_region->dev);
252
253 for_each_dax_region_resource(dax_region, res)
254 size -= resource_size(res);
255 return size;
256}
257
258static ssize_t available_size_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
260{
261 struct dax_region *dax_region = dev_get_drvdata(dev);
262 unsigned long long size;
263
264 device_lock(dev);
265 size = dax_region_avail_size(dax_region);
266 device_unlock(dev);
267
268 return sprintf(buf, "%llu\n", size);
269}
270static DEVICE_ATTR_RO(available_size);
271
Dan Williams0f3da142020-10-13 16:50:13 -0700272static ssize_t seed_show(struct device *dev,
273 struct device_attribute *attr, char *buf)
274{
275 struct dax_region *dax_region = dev_get_drvdata(dev);
276 struct device *seed;
277 ssize_t rc;
278
279 if (is_static(dax_region))
280 return -EINVAL;
281
282 device_lock(dev);
283 seed = dax_region->seed;
284 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
285 device_unlock(dev);
286
287 return rc;
288}
289static DEVICE_ATTR_RO(seed);
290
291static ssize_t create_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
294 struct dax_region *dax_region = dev_get_drvdata(dev);
295 struct device *youngest;
296 ssize_t rc;
297
298 if (is_static(dax_region))
299 return -EINVAL;
300
301 device_lock(dev);
302 youngest = dax_region->youngest;
303 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
304 device_unlock(dev);
305
306 return rc;
307}
308
309static ssize_t create_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t len)
311{
312 struct dax_region *dax_region = dev_get_drvdata(dev);
313 unsigned long long avail;
314 ssize_t rc;
315 int val;
316
317 if (is_static(dax_region))
318 return -EINVAL;
319
320 rc = kstrtoint(buf, 0, &val);
321 if (rc)
322 return rc;
323 if (val != 1)
324 return -EINVAL;
325
326 device_lock(dev);
327 avail = dax_region_avail_size(dax_region);
328 if (avail == 0)
329 rc = -ENOSPC;
330 else {
331 struct dev_dax_data data = {
332 .dax_region = dax_region,
333 .size = 0,
334 .id = -1,
335 };
336 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
337
338 if (IS_ERR(dev_dax))
339 rc = PTR_ERR(dev_dax);
340 else {
341 /*
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
346 * seed.
347 */
348 if (!dax_region->seed)
349 dax_region->seed = &dev_dax->dev;
350 dax_region->youngest = &dev_dax->dev;
351 rc = len;
352 }
353 }
354 device_unlock(dev);
355
356 return rc;
357}
358static DEVICE_ATTR_RW(create);
359
360void kill_dev_dax(struct dev_dax *dev_dax)
361{
362 struct dax_device *dax_dev = dev_dax->dax_dev;
363 struct inode *inode = dax_inode(dax_dev);
364
365 kill_dax(dax_dev);
366 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
367}
368EXPORT_SYMBOL_GPL(kill_dev_dax);
369
Dan Williams12d377b2020-12-18 18:41:41 -0800370static void trim_dev_dax_range(struct dev_dax *dev_dax)
Dan Williams0f3da142020-10-13 16:50:13 -0700371{
Dan Williams12d377b2020-12-18 18:41:41 -0800372 int i = dev_dax->nr_range - 1;
373 struct range *range = &dev_dax->ranges[i].range;
Dan Williams0f3da142020-10-13 16:50:13 -0700374 struct dax_region *dax_region = dev_dax->region;
Dan Williams0f3da142020-10-13 16:50:13 -0700375
376 device_lock_assert(dax_region->dev);
Dan Williams12d377b2020-12-18 18:41:41 -0800377 dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
378 (unsigned long long)range->start,
379 (unsigned long long)range->end);
Dan Williams60e93dc2020-10-13 16:50:39 -0700380
Dan Williams12d377b2020-12-18 18:41:41 -0800381 __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;
Dan Williams60e93dc2020-10-13 16:50:39 -0700385 }
Dan Williams12d377b2020-12-18 18:41:41 -0800386}
387
388static void free_dev_dax_ranges(struct dev_dax *dev_dax)
389{
390 while (dev_dax->nr_range)
391 trim_dev_dax_range(dev_dax);
Dan Williams0f3da142020-10-13 16:50:13 -0700392}
393
394static void unregister_dev_dax(void *dev)
395{
396 struct dev_dax *dev_dax = to_dev_dax(dev);
397
398 dev_dbg(dev, "%s\n", __func__);
399
400 kill_dev_dax(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700401 free_dev_dax_ranges(dev_dax);
Dan Williams0f3da142020-10-13 16:50:13 -0700402 device_del(dev);
403 put_device(dev);
404}
405
406/* a return value >= 0 indicates this invocation invalidated the id */
407static int __free_dev_dax_id(struct dev_dax *dev_dax)
408{
409 struct dax_region *dax_region = dev_dax->region;
410 struct device *dev = &dev_dax->dev;
411 int rc = dev_dax->id;
412
413 device_lock_assert(dev);
414
415 if (is_static(dax_region) || dev_dax->id < 0)
416 return -1;
417 ida_free(&dax_region->ida, dev_dax->id);
418 dev_dax->id = -1;
419 return rc;
420}
421
422static int free_dev_dax_id(struct dev_dax *dev_dax)
423{
424 struct device *dev = &dev_dax->dev;
425 int rc;
426
427 device_lock(dev);
428 rc = __free_dev_dax_id(dev_dax);
429 device_unlock(dev);
430 return rc;
431}
432
433static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t len)
435{
436 struct dax_region *dax_region = dev_get_drvdata(dev);
437 struct dev_dax *dev_dax;
438 struct device *victim;
439 bool do_del = false;
440 int rc;
441
442 if (is_static(dax_region))
443 return -EINVAL;
444
445 victim = device_find_child_by_name(dax_region->dev, buf);
446 if (!victim)
447 return -ENXIO;
448
449 device_lock(dev);
450 device_lock(victim);
451 dev_dax = to_dev_dax(victim);
Dan Williams60e93dc2020-10-13 16:50:39 -0700452 if (victim->driver || dev_dax_size(dev_dax))
Dan Williams0f3da142020-10-13 16:50:13 -0700453 rc = -EBUSY;
454 else {
455 /*
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.
460 */
461 if (dev_dax->id > 0) {
462 do_del = __free_dev_dax_id(dev_dax) >= 0;
463 rc = len;
464 if (dax_region->seed == victim)
465 dax_region->seed = NULL;
466 if (dax_region->youngest == victim)
467 dax_region->youngest = NULL;
468 } else
469 rc = -EBUSY;
470 }
471 device_unlock(victim);
472
473 /* won the race to invalidate the device, clean it up */
474 if (do_del)
475 devm_release_action(dev, unregister_dev_dax, victim);
476 device_unlock(dev);
477 put_device(victim);
478
479 return rc;
480}
481static DEVICE_ATTR_WO(delete);
482
Dan Williamsc2f30112020-10-13 16:50:03 -0700483static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
484 int n)
485{
486 struct device *dev = container_of(kobj, struct device, kobj);
487 struct dax_region *dax_region = dev_get_drvdata(dev);
488
Dan Williams0f3da142020-10-13 16:50:13 -0700489 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)
494 return 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700495 return a->mode;
496}
497
Dan Williams51cf7842017-07-12 17:58:21 -0700498static struct attribute *dax_region_attributes[] = {
Dan Williamsc2f30112020-10-13 16:50:03 -0700499 &dev_attr_available_size.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700500 &dev_attr_region_size.attr,
Dan Williams6d821202020-10-13 16:50:55 -0700501 &dev_attr_region_align.attr,
Dan Williams0f3da142020-10-13 16:50:13 -0700502 &dev_attr_create.attr,
503 &dev_attr_seed.attr,
504 &dev_attr_delete.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700505 &dev_attr_id.attr,
506 NULL,
507};
508
509static const struct attribute_group dax_region_attribute_group = {
510 .name = "dax_region",
511 .attrs = dax_region_attributes,
Dan Williamsc2f30112020-10-13 16:50:03 -0700512 .is_visible = dax_region_visible,
Dan Williams51cf7842017-07-12 17:58:21 -0700513};
514
515static const struct attribute_group *dax_region_attribute_groups[] = {
516 &dax_region_attribute_group,
517 NULL,
518};
519
520static void dax_region_free(struct kref *kref)
521{
522 struct dax_region *dax_region;
523
524 dax_region = container_of(kref, struct dax_region, kref);
525 kfree(dax_region);
526}
527
528void dax_region_put(struct dax_region *dax_region)
529{
530 kref_put(&dax_region->kref, dax_region_free);
531}
532EXPORT_SYMBOL_GPL(dax_region_put);
533
534static void dax_region_unregister(void *region)
535{
536 struct dax_region *dax_region = region;
537
538 sysfs_remove_groups(&dax_region->dev->kobj,
539 dax_region_attribute_groups);
540 dax_region_put(dax_region);
541}
542
543struct dax_region *alloc_dax_region(struct device *parent, int region_id,
Dan Williamsa4574f62020-10-13 16:50:29 -0700544 struct range *range, int target_node, unsigned int align,
Dan Williamsc2f30112020-10-13 16:50:03 -0700545 unsigned long flags)
Dan Williams51cf7842017-07-12 17:58:21 -0700546{
547 struct dax_region *dax_region;
548
549 /*
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.
553 */
554 if (dev_get_drvdata(parent)) {
555 dev_WARN(parent, "dax core failed to setup private data\n");
556 return NULL;
557 }
558
Dan Williamsa4574f62020-10-13 16:50:29 -0700559 if (!IS_ALIGNED(range->start, align)
560 || !IS_ALIGNED(range_len(range), align))
Dan Williams51cf7842017-07-12 17:58:21 -0700561 return NULL;
562
563 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
564 if (!dax_region)
565 return NULL;
566
567 dev_set_drvdata(parent, dax_region);
Dan Williams51cf7842017-07-12 17:58:21 -0700568 kref_init(&dax_region->kref);
569 dax_region->id = region_id;
570 dax_region->align = align;
571 dax_region->dev = parent;
Dan Williams8fc5c732018-11-09 12:43:07 -0800572 dax_region->target_node = target_node;
Dan Williams0f3da142020-10-13 16:50:13 -0700573 ida_init(&dax_region->ida);
Dan Williamsc2f30112020-10-13 16:50:03 -0700574 dax_region->res = (struct resource) {
Dan Williamsa4574f62020-10-13 16:50:29 -0700575 .start = range->start,
576 .end = range->end,
Dan Williamsc2f30112020-10-13 16:50:03 -0700577 .flags = IORESOURCE_MEM | flags,
578 };
579
Dan Williams51cf7842017-07-12 17:58:21 -0700580 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
581 kfree(dax_region);
582 return NULL;
583 }
584
585 kref_get(&dax_region->kref);
586 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
587 return NULL;
588 return dax_region;
589}
590EXPORT_SYMBOL_GPL(alloc_dax_region);
591
Dan Williams0b07ce82020-10-13 16:50:45 -0700592static void dax_mapping_release(struct device *dev)
593{
594 struct dax_mapping *mapping = to_dax_mapping(dev);
595 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
596
597 ida_free(&dev_dax->ida, mapping->id);
598 kfree(mapping);
599}
600
601static void unregister_dax_mapping(void *data)
602{
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;
607
608 dev_dbg(dev, "%s\n", __func__);
609
610 device_lock_assert(dax_region->dev);
611
612 dev_dax->ranges[mapping->range_id].mapping = NULL;
613 mapping->range_id = -1;
614
615 device_del(dev);
616 put_device(dev);
617}
618
619static struct dev_dax_range *get_dax_range(struct device *dev)
620{
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;
624
625 device_lock(dax_region->dev);
626 if (mapping->range_id < 0) {
627 device_unlock(dax_region->dev);
628 return NULL;
629 }
630
631 return &dev_dax->ranges[mapping->range_id];
632}
633
634static void put_dax_range(struct dev_dax_range *dax_range)
635{
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;
639
640 device_unlock(dax_region->dev);
641}
642
643static ssize_t start_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 struct dev_dax_range *dax_range;
647 ssize_t rc;
648
649 dax_range = get_dax_range(dev);
650 if (!dax_range)
651 return -ENXIO;
652 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
653 put_dax_range(dax_range);
654
655 return rc;
656}
657static DEVICE_ATTR(start, 0400, start_show, NULL);
658
659static ssize_t end_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
661{
662 struct dev_dax_range *dax_range;
663 ssize_t rc;
664
665 dax_range = get_dax_range(dev);
666 if (!dax_range)
667 return -ENXIO;
668 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
669 put_dax_range(dax_range);
670
671 return rc;
672}
673static DEVICE_ATTR(end, 0400, end_show, NULL);
674
675static ssize_t pgoff_show(struct device *dev,
676 struct device_attribute *attr, char *buf)
677{
678 struct dev_dax_range *dax_range;
679 ssize_t rc;
680
681 dax_range = get_dax_range(dev);
682 if (!dax_range)
683 return -ENXIO;
684 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
685 put_dax_range(dax_range);
686
687 return rc;
688}
689static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
690
691static struct attribute *dax_mapping_attributes[] = {
692 &dev_attr_start.attr,
693 &dev_attr_end.attr,
694 &dev_attr_page_offset.attr,
695 NULL,
696};
697
698static const struct attribute_group dax_mapping_attribute_group = {
699 .attrs = dax_mapping_attributes,
700};
701
702static const struct attribute_group *dax_mapping_attribute_groups[] = {
703 &dax_mapping_attribute_group,
704 NULL,
705};
706
707static struct device_type dax_mapping_type = {
708 .release = dax_mapping_release,
709 .groups = dax_mapping_attribute_groups,
710};
711
712static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
713{
714 struct dax_region *dax_region = dev_dax->region;
715 struct dax_mapping *mapping;
716 struct device *dev;
717 int rc;
718
719 device_lock_assert(dax_region->dev);
720
721 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
722 "region disabled\n"))
723 return -ENXIO;
724
725 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
726 if (!mapping)
727 return -ENOMEM;
728 mapping->range_id = range_id;
729 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
730 if (mapping->id < 0) {
731 kfree(mapping);
732 return -ENOMEM;
733 }
734 dev_dax->ranges[range_id].mapping = mapping;
735 dev = &mapping->dev;
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);
741 if (rc) {
742 put_device(dev);
743 return rc;
744 }
745
746 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
747 dev);
748 if (rc)
749 return rc;
750 return 0;
751}
752
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700753static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
754 resource_size_t size)
Dan Williamsc2f30112020-10-13 16:50:03 -0700755{
756 struct dax_region *dax_region = dev_dax->region;
757 struct resource *res = &dax_region->res;
758 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700759 struct dev_dax_range *ranges;
760 unsigned long pgoff = 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700761 struct resource *alloc;
Dan Williams0b07ce82020-10-13 16:50:45 -0700762 int i, rc;
Dan Williamsc2f30112020-10-13 16:50:03 -0700763
764 device_lock_assert(dax_region->dev);
765
Dan Williams0f3da142020-10-13 16:50:13 -0700766 /* handle the seed alloc special case */
767 if (!size) {
Dan Williams60e93dc2020-10-13 16:50:39 -0700768 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
769 "0-size allocation must be first\n"))
770 return -EBUSY;
771 /* nr_range == 0 is elsewhere special cased as 0-size device */
Dan Williams0f3da142020-10-13 16:50:13 -0700772 return 0;
773 }
774
Dan Williams60e93dc2020-10-13 16:50:39 -0700775 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
776 * (dev_dax->nr_range + 1), GFP_KERNEL);
777 if (!ranges)
Dan Williamsc2f30112020-10-13 16:50:03 -0700778 return -ENOMEM;
779
Dan Williams60e93dc2020-10-13 16:50:39 -0700780 alloc = __request_region(res, start, size, dev_name(dev), 0);
781 if (!alloc) {
782 /*
783 * If this was an empty set of ranges nothing else
784 * will release @ranges, so do it now.
785 */
786 if (!dev_dax->nr_range) {
787 kfree(ranges);
788 ranges = NULL;
789 }
790 dev_dax->ranges = ranges;
791 return -ENOMEM;
792 }
793
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) {
798 .pgoff = pgoff,
799 .range = {
800 .start = alloc->start,
801 .end = alloc->end,
802 },
Dan Williamsc2f30112020-10-13 16:50:03 -0700803 };
804
Dan Williams60e93dc2020-10-13 16:50:39 -0700805 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
806 &alloc->start, &alloc->end);
Dan Williams0b07ce82020-10-13 16:50:45 -0700807 /*
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.
811 */
812 if (!device_is_registered(&dev_dax->dev))
813 return 0;
814
815 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
Dan Williams12d377b2020-12-18 18:41:41 -0800816 if (rc)
817 trim_dev_dax_range(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700818
Dan Williams12d377b2020-12-18 18:41:41 -0800819 return rc;
Dan Williamsc2f30112020-10-13 16:50:03 -0700820}
821
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700822static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
823{
Dan Williams60e93dc2020-10-13 16:50:39 -0700824 int last_range = dev_dax->nr_range - 1;
825 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700826 struct dax_region *dax_region = dev_dax->region;
Dan Williams60e93dc2020-10-13 16:50:39 -0700827 bool is_shrink = resource_size(res) > size;
828 struct range *range = &dax_range->range;
829 struct device *dev = &dev_dax->dev;
830 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700831
832 device_lock_assert(dax_region->dev);
833
Dan Williams60e93dc2020-10-13 16:50:39 -0700834 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
835 return -EINVAL;
836
837 rc = adjust_resource(res, range->start, size);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700838 if (rc)
839 return rc;
840
Dan Williams60e93dc2020-10-13 16:50:39 -0700841 *range = (struct range) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700842 .start = range->start,
843 .end = range->start + size - 1,
844 };
845
Dan Williams60e93dc2020-10-13 16:50:39 -0700846 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);
849
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700850 return 0;
851}
852
Dan Williams51cf7842017-07-12 17:58:21 -0700853static ssize_t size_show(struct device *dev,
854 struct device_attribute *attr, char *buf)
855{
856 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -0700857 unsigned long long size;
858
859 device_lock(dev);
860 size = dev_dax_size(dev_dax);
861 device_unlock(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700862
863 return sprintf(buf, "%llu\n", size);
864}
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700865
Dan Williams6d821202020-10-13 16:50:55 -0700866static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700867{
868 /*
869 * The minimum mapping granularity for a device instance is a
870 * single subsection, unless the arch says otherwise.
871 */
Dan Williams6d821202020-10-13 16:50:55 -0700872 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700873}
874
875static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
876{
Dan Williams60e93dc2020-10-13 16:50:39 -0700877 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700878 struct dax_region *dax_region = dev_dax->region;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700879 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700880 int i;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700881
Dan Williams60e93dc2020-10-13 16:50:39 -0700882 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
883 struct range *range = &dev_dax->ranges[i].range;
Dan Williams0b07ce82020-10-13 16:50:45 -0700884 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
Dan Williams60e93dc2020-10-13 16:50:39 -0700885 struct resource *adjust = NULL, *res;
886 resource_size_t shrink;
887
888 shrink = min_t(u64, to_shrink, range_len(range));
889 if (shrink >= range_len(range)) {
Dan Williams0b07ce82020-10-13 16:50:45 -0700890 devm_release_action(dax_region->dev,
891 unregister_dax_mapping, &mapping->dev);
Dan Williams12d377b2020-12-18 18:41:41 -0800892 trim_dev_dax_range(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700893 to_shrink -= shrink;
894 if (!to_shrink)
895 break;
896 continue;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700897 }
898
Dan Williams60e93dc2020-10-13 16:50:39 -0700899 for_each_dax_region_resource(dax_region, res)
900 if (strcmp(res->name, dev_name(dev)) == 0
901 && res->start == range->start) {
902 adjust = res;
903 break;
904 }
905
906 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
907 "failed to find matching resource\n"))
908 return -ENXIO;
909 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
910 - shrink);
911 }
912 return 0;
913}
914
915/*
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.
918 */
919static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
920{
921 struct dev_dax_range *last;
922 int i;
923
924 if (dev_dax->nr_range == 0)
925 return false;
926 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
927 return false;
928 last = &dev_dax->ranges[dev_dax->nr_range - 1];
929 if (last->range.start != res->start || last->range.end != res->end)
930 return false;
931 for (i = 0; i < dev_dax->nr_range - 1; i++) {
932 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
933
934 if (dax_range->pgoff > last->pgoff)
935 return false;
936 }
937
938 return true;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700939}
940
941static ssize_t dev_dax_resize(struct dax_region *dax_region,
942 struct dev_dax *dev_dax, resource_size_t size)
943{
944 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
Dan Williams60e93dc2020-10-13 16:50:39 -0700945 resource_size_t dev_size = dev_dax_size(dev_dax);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700946 struct resource *region_res = &dax_region->res;
947 struct device *dev = &dev_dax->dev;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700948 struct resource *res, *first;
Dan Williams60e93dc2020-10-13 16:50:39 -0700949 resource_size_t alloc = 0;
950 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700951
952 if (dev->driver)
953 return -EBUSY;
954 if (size == dev_size)
955 return 0;
956 if (size > dev_size && size - dev_size > avail)
957 return -ENOSPC;
958 if (size < dev_size)
959 return dev_dax_shrink(dev_dax, size);
960
961 to_alloc = size - dev_size;
Dan Williams6d821202020-10-13 16:50:55 -0700962 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700963 "resize of %pa misaligned\n", &to_alloc))
964 return -ENXIO;
965
966 /*
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.
970 */
Dan Williams60e93dc2020-10-13 16:50:39 -0700971retry:
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700972 first = region_res->child;
973 if (!first)
974 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
Dan Williams60e93dc2020-10-13 16:50:39 -0700975
976 rc = -ENOSPC;
977 for (res = first; res; res = res->sibling) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700978 struct resource *next = res->sibling;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700979
980 /* space at the beginning of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700981 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);
984 break;
985 }
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700986
Dan Williams60e93dc2020-10-13 16:50:39 -0700987 alloc = 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700988 /* space between allocations */
989 if (next && next->start > res->end + 1)
Dan Williams60e93dc2020-10-13 16:50:39 -0700990 alloc = min(next->start - (res->end + 1), to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700991
992 /* space at the end of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700993 if (!alloc && !next && res->end < region_res->end)
994 alloc = min(region_res->end - res->end, to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700995
Dan Williams60e93dc2020-10-13 16:50:39 -0700996 if (!alloc)
997 continue;
998
999 if (adjust_ok(dev_dax, res)) {
1000 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1001 break;
1002 }
1003 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1004 break;
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001005 }
Dan Williams60e93dc2020-10-13 16:50:39 -07001006 if (rc)
1007 return rc;
1008 to_alloc -= alloc;
1009 if (to_alloc)
1010 goto retry;
1011 return 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001012}
1013
1014static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1015 const char *buf, size_t len)
1016{
1017 ssize_t rc;
1018 unsigned long long val;
1019 struct dev_dax *dev_dax = to_dev_dax(dev);
1020 struct dax_region *dax_region = dev_dax->region;
1021
1022 rc = kstrtoull(buf, 0, &val);
1023 if (rc)
1024 return rc;
1025
Dan Williams6d821202020-10-13 16:50:55 -07001026 if (!alloc_is_aligned(dev_dax, val)) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001027 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1028 return -EINVAL;
1029 }
1030
1031 device_lock(dax_region->dev);
1032 if (!dax_region->dev->driver) {
1033 device_unlock(dax_region->dev);
1034 return -ENXIO;
1035 }
1036 device_lock(dev);
1037 rc = dev_dax_resize(dax_region, dev_dax, val);
1038 device_unlock(dev);
1039 device_unlock(dax_region->dev);
1040
1041 return rc == 0 ? len : rc;
1042}
1043static DEVICE_ATTR_RW(size);
Dan Williams51cf7842017-07-12 17:58:21 -07001044
Joao Martins8490e2e2020-10-13 16:51:06 -07001045static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1046{
1047 unsigned long long addr = 0;
1048 char *start, *end, *str;
Shiyang Ruanfc229172021-01-26 10:13:31 +08001049 ssize_t rc = -EINVAL;
Joao Martins8490e2e2020-10-13 16:51:06 -07001050
1051 str = kstrdup(opt, GFP_KERNEL);
1052 if (!str)
1053 return rc;
1054
1055 end = str;
1056 start = strsep(&end, "-");
1057 if (!start || !end)
1058 goto err;
1059
1060 rc = kstrtoull(start, 16, &addr);
1061 if (rc)
1062 goto err;
1063 range->start = addr;
1064
1065 rc = kstrtoull(end, 16, &addr);
1066 if (rc)
1067 goto err;
1068 range->end = addr;
1069
1070err:
1071 kfree(str);
1072 return rc;
1073}
1074
1075static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1076 const char *buf, size_t len)
1077{
1078 struct dev_dax *dev_dax = to_dev_dax(dev);
1079 struct dax_region *dax_region = dev_dax->region;
1080 size_t to_alloc;
1081 struct range r;
1082 ssize_t rc;
1083
1084 rc = range_parse(buf, len, &r);
1085 if (rc)
1086 return rc;
1087
1088 rc = -ENXIO;
1089 device_lock(dax_region->dev);
1090 if (!dax_region->dev->driver) {
1091 device_unlock(dax_region->dev);
1092 return rc;
1093 }
1094 device_lock(dev);
1095
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);
1099 device_unlock(dev);
1100 device_unlock(dax_region->dev);
1101
1102 return rc == 0 ? len : rc;
1103}
1104static DEVICE_ATTR_WO(mapping);
1105
Dan Williams6d821202020-10-13 16:50:55 -07001106static ssize_t align_show(struct device *dev,
1107 struct device_attribute *attr, char *buf)
1108{
1109 struct dev_dax *dev_dax = to_dev_dax(dev);
1110
1111 return sprintf(buf, "%d\n", dev_dax->align);
1112}
1113
1114static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1115{
1116 resource_size_t dev_size = dev_dax_size(dev_dax);
1117 struct device *dev = &dev_dax->dev;
1118 int i;
1119
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);
1123 return -EINVAL;
1124 }
1125
1126 for (i = 0; i < dev_dax->nr_range; i++) {
1127 size_t len = range_len(&dev_dax->ranges[i].range);
1128
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);
1132 return -EINVAL;
1133 }
1134 }
1135
1136 return 0;
1137}
1138
1139static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1140 const char *buf, size_t len)
1141{
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;
1145 ssize_t rc;
1146
1147 rc = kstrtoul(buf, 0, &val);
1148 if (rc)
1149 return -ENXIO;
1150
1151 if (!dax_align_valid(val))
1152 return -EINVAL;
1153
1154 device_lock(dax_region->dev);
1155 if (!dax_region->dev->driver) {
1156 device_unlock(dax_region->dev);
1157 return -ENXIO;
1158 }
1159
1160 device_lock(dev);
1161 if (dev->driver) {
1162 rc = -EBUSY;
1163 goto out_unlock;
1164 }
1165
1166 align_save = dev_dax->align;
1167 dev_dax->align = val;
1168 rc = dev_dax_validate_align(dev_dax);
1169 if (rc)
1170 dev_dax->align = align_save;
1171out_unlock:
1172 device_unlock(dev);
1173 device_unlock(dax_region->dev);
1174 return rc == 0 ? len : rc;
1175}
1176static DEVICE_ATTR_RW(align);
1177
Dan Williams21c75762019-02-20 11:39:36 -08001178static int dev_dax_target_node(struct dev_dax *dev_dax)
1179{
1180 struct dax_region *dax_region = dev_dax->region;
1181
1182 return dax_region->target_node;
1183}
1184
1185static ssize_t target_node_show(struct device *dev,
1186 struct device_attribute *attr, char *buf)
1187{
1188 struct dev_dax *dev_dax = to_dev_dax(dev);
1189
1190 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1191}
1192static DEVICE_ATTR_RO(target_node);
1193
Vishal Verma40cdc602019-06-20 18:40:38 -06001194static ssize_t resource_show(struct device *dev,
1195 struct device_attribute *attr, char *buf)
1196{
1197 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -07001198 struct dax_region *dax_region = dev_dax->region;
1199 unsigned long long start;
Vishal Verma40cdc602019-06-20 18:40:38 -06001200
Dan Williams60e93dc2020-10-13 16:50:39 -07001201 if (dev_dax->nr_range < 1)
1202 start = dax_region->res.start;
1203 else
1204 start = dev_dax->ranges[0].range.start;
1205
1206 return sprintf(buf, "%#llx\n", start);
Vishal Verma40cdc602019-06-20 18:40:38 -06001207}
Dan Williams153dd282019-11-12 17:12:38 -08001208static DEVICE_ATTR(resource, 0400, resource_show, NULL);
Vishal Verma40cdc602019-06-20 18:40:38 -06001209
Vishal Vermac347bd72019-02-22 16:58:54 -07001210static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1211 char *buf)
1212{
1213 /*
1214 * We only ever expect to handle device-dax instances, i.e. the
1215 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1216 */
1217 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1218}
1219static DEVICE_ATTR_RO(modalias);
1220
Dan Williamscb4dd722019-11-12 17:13:31 -08001221static ssize_t numa_node_show(struct device *dev,
1222 struct device_attribute *attr, char *buf)
1223{
1224 return sprintf(buf, "%d\n", dev_to_node(dev));
1225}
1226static DEVICE_ATTR_RO(numa_node);
1227
Dan Williams21c75762019-02-20 11:39:36 -08001228static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1229{
1230 struct device *dev = container_of(kobj, struct device, kobj);
1231 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001232 struct dax_region *dax_region = dev_dax->region;
Dan Williams21c75762019-02-20 11:39:36 -08001233
1234 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1235 return 0;
Dan Williamscb4dd722019-11-12 17:13:31 -08001236 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1237 return 0;
Joao Martins8490e2e2020-10-13 16:51:06 -07001238 if (a == &dev_attr_mapping.attr && is_static(dax_region))
1239 return 0;
Dan Williams6d821202020-10-13 16:50:55 -07001240 if ((a == &dev_attr_align.attr ||
1241 a == &dev_attr_size.attr) && is_static(dax_region))
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001242 return 0444;
Dan Williams21c75762019-02-20 11:39:36 -08001243 return a->mode;
1244}
1245
Dan Williams51cf7842017-07-12 17:58:21 -07001246static struct attribute *dev_dax_attributes[] = {
Vishal Vermac347bd72019-02-22 16:58:54 -07001247 &dev_attr_modalias.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001248 &dev_attr_size.attr,
Joao Martins8490e2e2020-10-13 16:51:06 -07001249 &dev_attr_mapping.attr,
Dan Williams21c75762019-02-20 11:39:36 -08001250 &dev_attr_target_node.attr,
Dan Williams6d821202020-10-13 16:50:55 -07001251 &dev_attr_align.attr,
Vishal Verma40cdc602019-06-20 18:40:38 -06001252 &dev_attr_resource.attr,
Dan Williamscb4dd722019-11-12 17:13:31 -08001253 &dev_attr_numa_node.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001254 NULL,
1255};
1256
1257static const struct attribute_group dev_dax_attribute_group = {
1258 .attrs = dev_dax_attributes,
Dan Williams21c75762019-02-20 11:39:36 -08001259 .is_visible = dev_dax_visible,
Dan Williams51cf7842017-07-12 17:58:21 -07001260};
1261
Dan Williams9567da02017-07-12 17:58:21 -07001262static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -07001263 &dev_dax_attribute_group,
1264 NULL,
1265};
Dan Williams51cf7842017-07-12 17:58:21 -07001266
Dan Williams9567da02017-07-12 17:58:21 -07001267static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -07001268{
1269 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -07001270 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -07001271 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -07001272
Dan Williams9567da02017-07-12 17:58:21 -07001273 put_dax(dax_dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001274 free_dev_dax_id(dev_dax);
1275 dax_region_put(dax_region);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001276 kfree(dev_dax->pgmap);
Dan Williams9567da02017-07-12 17:58:21 -07001277 kfree(dev_dax);
1278}
1279
Dan Williams770619a2019-11-12 17:12:23 -08001280static const struct device_type dev_dax_type = {
1281 .release = dev_dax_release,
1282 .groups = dax_attribute_groups,
1283};
1284
Dan Williams174ebec2020-10-13 16:49:38 -07001285struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
Dan Williams9567da02017-07-12 17:58:21 -07001286{
Dan Williams174ebec2020-10-13 16:49:38 -07001287 struct dax_region *dax_region = data->dax_region;
Dan Williams9567da02017-07-12 17:58:21 -07001288 struct device *parent = dax_region->dev;
1289 struct dax_device *dax_dev;
1290 struct dev_dax *dev_dax;
1291 struct inode *inode;
1292 struct device *dev;
Dan Williamsc2f30112020-10-13 16:50:03 -07001293 int rc;
Dan Williams9567da02017-07-12 17:58:21 -07001294
Dan Williams9567da02017-07-12 17:58:21 -07001295 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1296 if (!dev_dax)
1297 return ERR_PTR(-ENOMEM);
1298
Dan Williams0f3da142020-10-13 16:50:13 -07001299 if (is_static(dax_region)) {
1300 if (dev_WARN_ONCE(parent, data->id < 0,
1301 "dynamic id specified to static region\n")) {
1302 rc = -EINVAL;
1303 goto err_id;
1304 }
1305
1306 dev_dax->id = data->id;
1307 } else {
1308 if (dev_WARN_ONCE(parent, data->id >= 0,
1309 "static id specified to dynamic region\n")) {
1310 rc = -EINVAL;
1311 goto err_id;
1312 }
1313
1314 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1315 if (rc < 0)
1316 goto err_id;
1317 dev_dax->id = rc;
1318 }
1319
Dan Williamsc2f30112020-10-13 16:50:03 -07001320 dev_dax->region = dax_region;
1321 dev = &dev_dax->dev;
1322 device_initialize(dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001323 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
Dan Williamsc2f30112020-10-13 16:50:03 -07001324
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001325 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
Dan Williamsc2f30112020-10-13 16:50:03 -07001326 if (rc)
1327 goto err_range;
1328
Dan Williamsf5516ec2020-10-13 16:49:43 -07001329 if (data->pgmap) {
Dan Williamsc2f30112020-10-13 16:50:03 -07001330 dev_WARN_ONCE(parent, !is_static(dax_region),
1331 "custom dev_pagemap requires a static dax_region\n");
1332
Dan Williamsf5516ec2020-10-13 16:49:43 -07001333 dev_dax->pgmap = kmemdup(data->pgmap,
1334 sizeof(struct dev_pagemap), GFP_KERNEL);
Dan Williamsc2f30112020-10-13 16:50:03 -07001335 if (!dev_dax->pgmap) {
1336 rc = -ENOMEM;
Dan Williamsf5516ec2020-10-13 16:49:43 -07001337 goto err_pgmap;
Dan Williamsc2f30112020-10-13 16:50:03 -07001338 }
Dan Williamsf5516ec2020-10-13 16:49:43 -07001339 }
Dan Williams89ec9f22018-10-29 15:52:42 -07001340
Dan Williams9567da02017-07-12 17:58:21 -07001341 /*
1342 * No 'host' or dax_operations since there is no access to this
1343 * device outside of mmap of the resulting character device.
1344 */
Pankaj Guptafefc1d92019-07-05 19:33:24 +05301345 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001346 if (IS_ERR(dax_dev)) {
1347 rc = PTR_ERR(dax_dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001348 goto err_alloc_dax;
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001349 }
Dan Williams9567da02017-07-12 17:58:21 -07001350
1351 /* a device_dax instance is dead while the driver is not attached */
1352 kill_dax(dax_dev);
1353
Dan Williams9567da02017-07-12 17:58:21 -07001354 dev_dax->dax_dev = dax_dev;
Dan Williams8fc5c732018-11-09 12:43:07 -08001355 dev_dax->target_node = dax_region->target_node;
Joao Martins33cf94d2020-10-13 16:50:50 -07001356 dev_dax->align = dax_region->align;
Dan Williams0b07ce82020-10-13 16:50:45 -07001357 ida_init(&dev_dax->ida);
Dan Williams9567da02017-07-12 17:58:21 -07001358 kref_get(&dax_region->kref);
1359
1360 inode = dax_inode(dax_dev);
1361 dev->devt = inode->i_rdev;
Dan Williams174ebec2020-10-13 16:49:38 -07001362 if (data->subsys == DEV_DAX_BUS)
Dan Williams730926c2017-07-16 13:51:53 -07001363 dev->bus = &dax_bus_type;
1364 else
1365 dev->class = dax_class;
Dan Williams9567da02017-07-12 17:58:21 -07001366 dev->parent = parent;
Dan Williams770619a2019-11-12 17:12:23 -08001367 dev->type = &dev_dax_type;
Dan Williams9567da02017-07-12 17:58:21 -07001368
1369 rc = device_add(dev);
1370 if (rc) {
1371 kill_dev_dax(dev_dax);
1372 put_device(dev);
1373 return ERR_PTR(rc);
1374 }
1375
1376 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1377 if (rc)
1378 return ERR_PTR(rc);
1379
Dan Williams0b07ce82020-10-13 16:50:45 -07001380 /* 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);
1383 if (rc)
1384 return ERR_PTR(rc);
1385 }
1386
Dan Williams9567da02017-07-12 17:58:21 -07001387 return dev_dax;
Dan Williamsc2f30112020-10-13 16:50:03 -07001388
Dan Williamsf5516ec2020-10-13 16:49:43 -07001389err_alloc_dax:
1390 kfree(dev_dax->pgmap);
1391err_pgmap:
Dan Williams60e93dc2020-10-13 16:50:39 -07001392 free_dev_dax_ranges(dev_dax);
Dan Williamsc2f30112020-10-13 16:50:03 -07001393err_range:
Dan Williams0f3da142020-10-13 16:50:13 -07001394 free_dev_dax_id(dev_dax);
1395err_id:
Dan Williams9567da02017-07-12 17:58:21 -07001396 kfree(dev_dax);
1397
1398 return ERR_PTR(rc);
1399}
Dan Williams174ebec2020-10-13 16:49:38 -07001400EXPORT_SYMBOL_GPL(devm_create_dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -07001401
Dan Williamsd2007812018-11-07 15:31:23 -08001402static int match_always_count;
1403
1404int __dax_driver_register(struct dax_device_driver *dax_drv,
Dan Williams9567da02017-07-12 17:58:21 -07001405 struct module *module, const char *mod_name)
1406{
Dan Williamsd2007812018-11-07 15:31:23 -08001407 struct device_driver *drv = &dax_drv->drv;
1408 int rc = 0;
1409
1410 INIT_LIST_HEAD(&dax_drv->ids);
Dan Williams9567da02017-07-12 17:58:21 -07001411 drv->owner = module;
1412 drv->name = mod_name;
1413 drv->mod_name = mod_name;
1414 drv->bus = &dax_bus_type;
Dan Williamsd2007812018-11-07 15:31:23 -08001415
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--;
1421 WARN_ON(1);
1422 rc = -EINVAL;
1423 }
1424 mutex_unlock(&dax_bus_lock);
1425 if (rc)
1426 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001427 return driver_register(drv);
1428}
1429EXPORT_SYMBOL_GPL(__dax_driver_register);
1430
Dan Williamsd2007812018-11-07 15:31:23 -08001431void dax_driver_unregister(struct dax_device_driver *dax_drv)
1432{
Dan Williams730926c2017-07-16 13:51:53 -07001433 struct device_driver *drv = &dax_drv->drv;
Dan Williamsd2007812018-11-07 15:31:23 -08001434 struct dax_id *dax_id, *_id;
1435
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);
1440 kfree(dax_id);
1441 }
1442 mutex_unlock(&dax_bus_lock);
Dan Williams730926c2017-07-16 13:51:53 -07001443 driver_unregister(drv);
Dan Williamsd2007812018-11-07 15:31:23 -08001444}
1445EXPORT_SYMBOL_GPL(dax_driver_unregister);
1446
Dan Williams9567da02017-07-12 17:58:21 -07001447int __init dax_bus_init(void)
1448{
Dan Williams730926c2017-07-16 13:51:53 -07001449 int rc;
1450
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);
1455 }
1456
1457 rc = bus_register(&dax_bus_type);
1458 if (rc)
1459 class_destroy(dax_class);
1460 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001461}
1462
1463void __exit dax_bus_exit(void)
1464{
1465 bus_unregister(&dax_bus_type);
Dan Williams730926c2017-07-16 13:51:53 -07001466 class_destroy(dax_class);
Dan Williams9567da02017-07-12 17:58:21 -07001467}