blob: 0ac4a9c0fd18eeb15f059990a948eea8f73b3fce [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 Williams60e93dc2020-10-13 16:50:39 -0700370static void free_dev_dax_ranges(struct dev_dax *dev_dax)
Dan Williams0f3da142020-10-13 16:50:13 -0700371{
372 struct dax_region *dax_region = dev_dax->region;
Dan Williams60e93dc2020-10-13 16:50:39 -0700373 int i;
Dan Williams0f3da142020-10-13 16:50:13 -0700374
375 device_lock_assert(dax_region->dev);
Dan Williams60e93dc2020-10-13 16:50:39 -0700376 for (i = 0; i < dev_dax->nr_range; i++) {
377 struct range *range = &dev_dax->ranges[i].range;
378
Dan Williams0f3da142020-10-13 16:50:13 -0700379 __release_region(&dax_region->res, range->start,
380 range_len(range));
Dan Williams60e93dc2020-10-13 16:50:39 -0700381 }
382 dev_dax->nr_range = 0;
Dan Williams0f3da142020-10-13 16:50:13 -0700383}
384
385static void unregister_dev_dax(void *dev)
386{
387 struct dev_dax *dev_dax = to_dev_dax(dev);
388
389 dev_dbg(dev, "%s\n", __func__);
390
391 kill_dev_dax(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700392 free_dev_dax_ranges(dev_dax);
Dan Williams0f3da142020-10-13 16:50:13 -0700393 device_del(dev);
394 put_device(dev);
395}
396
397/* a return value >= 0 indicates this invocation invalidated the id */
398static int __free_dev_dax_id(struct dev_dax *dev_dax)
399{
400 struct dax_region *dax_region = dev_dax->region;
401 struct device *dev = &dev_dax->dev;
402 int rc = dev_dax->id;
403
404 device_lock_assert(dev);
405
406 if (is_static(dax_region) || dev_dax->id < 0)
407 return -1;
408 ida_free(&dax_region->ida, dev_dax->id);
409 dev_dax->id = -1;
410 return rc;
411}
412
413static int free_dev_dax_id(struct dev_dax *dev_dax)
414{
415 struct device *dev = &dev_dax->dev;
416 int rc;
417
418 device_lock(dev);
419 rc = __free_dev_dax_id(dev_dax);
420 device_unlock(dev);
421 return rc;
422}
423
424static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t len)
426{
427 struct dax_region *dax_region = dev_get_drvdata(dev);
428 struct dev_dax *dev_dax;
429 struct device *victim;
430 bool do_del = false;
431 int rc;
432
433 if (is_static(dax_region))
434 return -EINVAL;
435
436 victim = device_find_child_by_name(dax_region->dev, buf);
437 if (!victim)
438 return -ENXIO;
439
440 device_lock(dev);
441 device_lock(victim);
442 dev_dax = to_dev_dax(victim);
Dan Williams60e93dc2020-10-13 16:50:39 -0700443 if (victim->driver || dev_dax_size(dev_dax))
Dan Williams0f3da142020-10-13 16:50:13 -0700444 rc = -EBUSY;
445 else {
446 /*
447 * Invalidate the device so it does not become active
448 * again, but always preserve device-id-0 so that
449 * /sys/bus/dax/ is guaranteed to be populated while any
450 * dax_region is registered.
451 */
452 if (dev_dax->id > 0) {
453 do_del = __free_dev_dax_id(dev_dax) >= 0;
454 rc = len;
455 if (dax_region->seed == victim)
456 dax_region->seed = NULL;
457 if (dax_region->youngest == victim)
458 dax_region->youngest = NULL;
459 } else
460 rc = -EBUSY;
461 }
462 device_unlock(victim);
463
464 /* won the race to invalidate the device, clean it up */
465 if (do_del)
466 devm_release_action(dev, unregister_dev_dax, victim);
467 device_unlock(dev);
468 put_device(victim);
469
470 return rc;
471}
472static DEVICE_ATTR_WO(delete);
473
Dan Williamsc2f30112020-10-13 16:50:03 -0700474static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
475 int n)
476{
477 struct device *dev = container_of(kobj, struct device, kobj);
478 struct dax_region *dax_region = dev_get_drvdata(dev);
479
Dan Williams0f3da142020-10-13 16:50:13 -0700480 if (is_static(dax_region))
481 if (a == &dev_attr_available_size.attr
482 || a == &dev_attr_create.attr
483 || a == &dev_attr_seed.attr
484 || a == &dev_attr_delete.attr)
485 return 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700486 return a->mode;
487}
488
Dan Williams51cf7842017-07-12 17:58:21 -0700489static struct attribute *dax_region_attributes[] = {
Dan Williamsc2f30112020-10-13 16:50:03 -0700490 &dev_attr_available_size.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700491 &dev_attr_region_size.attr,
Dan Williams6d821202020-10-13 16:50:55 -0700492 &dev_attr_region_align.attr,
Dan Williams0f3da142020-10-13 16:50:13 -0700493 &dev_attr_create.attr,
494 &dev_attr_seed.attr,
495 &dev_attr_delete.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700496 &dev_attr_id.attr,
497 NULL,
498};
499
500static const struct attribute_group dax_region_attribute_group = {
501 .name = "dax_region",
502 .attrs = dax_region_attributes,
Dan Williamsc2f30112020-10-13 16:50:03 -0700503 .is_visible = dax_region_visible,
Dan Williams51cf7842017-07-12 17:58:21 -0700504};
505
506static const struct attribute_group *dax_region_attribute_groups[] = {
507 &dax_region_attribute_group,
508 NULL,
509};
510
511static void dax_region_free(struct kref *kref)
512{
513 struct dax_region *dax_region;
514
515 dax_region = container_of(kref, struct dax_region, kref);
516 kfree(dax_region);
517}
518
519void dax_region_put(struct dax_region *dax_region)
520{
521 kref_put(&dax_region->kref, dax_region_free);
522}
523EXPORT_SYMBOL_GPL(dax_region_put);
524
525static void dax_region_unregister(void *region)
526{
527 struct dax_region *dax_region = region;
528
529 sysfs_remove_groups(&dax_region->dev->kobj,
530 dax_region_attribute_groups);
531 dax_region_put(dax_region);
532}
533
534struct dax_region *alloc_dax_region(struct device *parent, int region_id,
Dan Williamsa4574f62020-10-13 16:50:29 -0700535 struct range *range, int target_node, unsigned int align,
Dan Williamsc2f30112020-10-13 16:50:03 -0700536 unsigned long flags)
Dan Williams51cf7842017-07-12 17:58:21 -0700537{
538 struct dax_region *dax_region;
539
540 /*
541 * The DAX core assumes that it can store its private data in
542 * parent->driver_data. This WARN is a reminder / safeguard for
543 * developers of device-dax drivers.
544 */
545 if (dev_get_drvdata(parent)) {
546 dev_WARN(parent, "dax core failed to setup private data\n");
547 return NULL;
548 }
549
Dan Williamsa4574f62020-10-13 16:50:29 -0700550 if (!IS_ALIGNED(range->start, align)
551 || !IS_ALIGNED(range_len(range), align))
Dan Williams51cf7842017-07-12 17:58:21 -0700552 return NULL;
553
554 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
555 if (!dax_region)
556 return NULL;
557
558 dev_set_drvdata(parent, dax_region);
Dan Williams51cf7842017-07-12 17:58:21 -0700559 kref_init(&dax_region->kref);
560 dax_region->id = region_id;
561 dax_region->align = align;
562 dax_region->dev = parent;
Dan Williams8fc5c732018-11-09 12:43:07 -0800563 dax_region->target_node = target_node;
Dan Williams0f3da142020-10-13 16:50:13 -0700564 ida_init(&dax_region->ida);
Dan Williamsc2f30112020-10-13 16:50:03 -0700565 dax_region->res = (struct resource) {
Dan Williamsa4574f62020-10-13 16:50:29 -0700566 .start = range->start,
567 .end = range->end,
Dan Williamsc2f30112020-10-13 16:50:03 -0700568 .flags = IORESOURCE_MEM | flags,
569 };
570
Dan Williams51cf7842017-07-12 17:58:21 -0700571 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
572 kfree(dax_region);
573 return NULL;
574 }
575
576 kref_get(&dax_region->kref);
577 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
578 return NULL;
579 return dax_region;
580}
581EXPORT_SYMBOL_GPL(alloc_dax_region);
582
Dan Williams0b07ce82020-10-13 16:50:45 -0700583static void dax_mapping_release(struct device *dev)
584{
585 struct dax_mapping *mapping = to_dax_mapping(dev);
586 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
587
588 ida_free(&dev_dax->ida, mapping->id);
589 kfree(mapping);
590}
591
592static void unregister_dax_mapping(void *data)
593{
594 struct device *dev = data;
595 struct dax_mapping *mapping = to_dax_mapping(dev);
596 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
597 struct dax_region *dax_region = dev_dax->region;
598
599 dev_dbg(dev, "%s\n", __func__);
600
601 device_lock_assert(dax_region->dev);
602
603 dev_dax->ranges[mapping->range_id].mapping = NULL;
604 mapping->range_id = -1;
605
606 device_del(dev);
607 put_device(dev);
608}
609
610static struct dev_dax_range *get_dax_range(struct device *dev)
611{
612 struct dax_mapping *mapping = to_dax_mapping(dev);
613 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
614 struct dax_region *dax_region = dev_dax->region;
615
616 device_lock(dax_region->dev);
617 if (mapping->range_id < 0) {
618 device_unlock(dax_region->dev);
619 return NULL;
620 }
621
622 return &dev_dax->ranges[mapping->range_id];
623}
624
625static void put_dax_range(struct dev_dax_range *dax_range)
626{
627 struct dax_mapping *mapping = dax_range->mapping;
628 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
629 struct dax_region *dax_region = dev_dax->region;
630
631 device_unlock(dax_region->dev);
632}
633
634static ssize_t start_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
636{
637 struct dev_dax_range *dax_range;
638 ssize_t rc;
639
640 dax_range = get_dax_range(dev);
641 if (!dax_range)
642 return -ENXIO;
643 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
644 put_dax_range(dax_range);
645
646 return rc;
647}
648static DEVICE_ATTR(start, 0400, start_show, NULL);
649
650static ssize_t end_show(struct device *dev,
651 struct device_attribute *attr, char *buf)
652{
653 struct dev_dax_range *dax_range;
654 ssize_t rc;
655
656 dax_range = get_dax_range(dev);
657 if (!dax_range)
658 return -ENXIO;
659 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
660 put_dax_range(dax_range);
661
662 return rc;
663}
664static DEVICE_ATTR(end, 0400, end_show, NULL);
665
666static ssize_t pgoff_show(struct device *dev,
667 struct device_attribute *attr, char *buf)
668{
669 struct dev_dax_range *dax_range;
670 ssize_t rc;
671
672 dax_range = get_dax_range(dev);
673 if (!dax_range)
674 return -ENXIO;
675 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
676 put_dax_range(dax_range);
677
678 return rc;
679}
680static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
681
682static struct attribute *dax_mapping_attributes[] = {
683 &dev_attr_start.attr,
684 &dev_attr_end.attr,
685 &dev_attr_page_offset.attr,
686 NULL,
687};
688
689static const struct attribute_group dax_mapping_attribute_group = {
690 .attrs = dax_mapping_attributes,
691};
692
693static const struct attribute_group *dax_mapping_attribute_groups[] = {
694 &dax_mapping_attribute_group,
695 NULL,
696};
697
698static struct device_type dax_mapping_type = {
699 .release = dax_mapping_release,
700 .groups = dax_mapping_attribute_groups,
701};
702
703static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
704{
705 struct dax_region *dax_region = dev_dax->region;
706 struct dax_mapping *mapping;
707 struct device *dev;
708 int rc;
709
710 device_lock_assert(dax_region->dev);
711
712 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
713 "region disabled\n"))
714 return -ENXIO;
715
716 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
717 if (!mapping)
718 return -ENOMEM;
719 mapping->range_id = range_id;
720 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
721 if (mapping->id < 0) {
722 kfree(mapping);
723 return -ENOMEM;
724 }
725 dev_dax->ranges[range_id].mapping = mapping;
726 dev = &mapping->dev;
727 device_initialize(dev);
728 dev->parent = &dev_dax->dev;
729 dev->type = &dax_mapping_type;
730 dev_set_name(dev, "mapping%d", mapping->id);
731 rc = device_add(dev);
732 if (rc) {
733 put_device(dev);
734 return rc;
735 }
736
737 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
738 dev);
739 if (rc)
740 return rc;
741 return 0;
742}
743
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700744static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
745 resource_size_t size)
Dan Williamsc2f30112020-10-13 16:50:03 -0700746{
747 struct dax_region *dax_region = dev_dax->region;
748 struct resource *res = &dax_region->res;
749 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700750 struct dev_dax_range *ranges;
751 unsigned long pgoff = 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700752 struct resource *alloc;
Dan Williams0b07ce82020-10-13 16:50:45 -0700753 int i, rc;
Dan Williamsc2f30112020-10-13 16:50:03 -0700754
755 device_lock_assert(dax_region->dev);
756
Dan Williams0f3da142020-10-13 16:50:13 -0700757 /* handle the seed alloc special case */
758 if (!size) {
Dan Williams60e93dc2020-10-13 16:50:39 -0700759 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
760 "0-size allocation must be first\n"))
761 return -EBUSY;
762 /* nr_range == 0 is elsewhere special cased as 0-size device */
Dan Williams0f3da142020-10-13 16:50:13 -0700763 return 0;
764 }
765
Dan Williams60e93dc2020-10-13 16:50:39 -0700766 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
767 * (dev_dax->nr_range + 1), GFP_KERNEL);
768 if (!ranges)
Dan Williamsc2f30112020-10-13 16:50:03 -0700769 return -ENOMEM;
770
Dan Williams60e93dc2020-10-13 16:50:39 -0700771 alloc = __request_region(res, start, size, dev_name(dev), 0);
772 if (!alloc) {
773 /*
774 * If this was an empty set of ranges nothing else
775 * will release @ranges, so do it now.
776 */
777 if (!dev_dax->nr_range) {
778 kfree(ranges);
779 ranges = NULL;
780 }
781 dev_dax->ranges = ranges;
782 return -ENOMEM;
783 }
784
785 for (i = 0; i < dev_dax->nr_range; i++)
786 pgoff += PHYS_PFN(range_len(&ranges[i].range));
787 dev_dax->ranges = ranges;
788 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
789 .pgoff = pgoff,
790 .range = {
791 .start = alloc->start,
792 .end = alloc->end,
793 },
Dan Williamsc2f30112020-10-13 16:50:03 -0700794 };
795
Dan Williams60e93dc2020-10-13 16:50:39 -0700796 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
797 &alloc->start, &alloc->end);
Dan Williams0b07ce82020-10-13 16:50:45 -0700798 /*
799 * A dev_dax instance must be registered before mapping device
800 * children can be added. Defer to devm_create_dev_dax() to add
801 * the initial mapping device.
802 */
803 if (!device_is_registered(&dev_dax->dev))
804 return 0;
805
806 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
807 if (rc) {
808 dev_dbg(dev, "delete range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
809 &alloc->start, &alloc->end);
810 dev_dax->nr_range--;
811 __release_region(res, alloc->start, resource_size(alloc));
812 return rc;
813 }
Dan Williams60e93dc2020-10-13 16:50:39 -0700814
Dan Williamsc2f30112020-10-13 16:50:03 -0700815 return 0;
816}
817
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700818static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
819{
Dan Williams60e93dc2020-10-13 16:50:39 -0700820 int last_range = dev_dax->nr_range - 1;
821 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700822 struct dax_region *dax_region = dev_dax->region;
Dan Williams60e93dc2020-10-13 16:50:39 -0700823 bool is_shrink = resource_size(res) > size;
824 struct range *range = &dax_range->range;
825 struct device *dev = &dev_dax->dev;
826 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700827
828 device_lock_assert(dax_region->dev);
829
Dan Williams60e93dc2020-10-13 16:50:39 -0700830 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
831 return -EINVAL;
832
833 rc = adjust_resource(res, range->start, size);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700834 if (rc)
835 return rc;
836
Dan Williams60e93dc2020-10-13 16:50:39 -0700837 *range = (struct range) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700838 .start = range->start,
839 .end = range->start + size - 1,
840 };
841
Dan Williams60e93dc2020-10-13 16:50:39 -0700842 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
843 last_range, (unsigned long long) range->start,
844 (unsigned long long) range->end);
845
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700846 return 0;
847}
848
Dan Williams51cf7842017-07-12 17:58:21 -0700849static ssize_t size_show(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
852 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -0700853 unsigned long long size;
854
855 device_lock(dev);
856 size = dev_dax_size(dev_dax);
857 device_unlock(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700858
859 return sprintf(buf, "%llu\n", size);
860}
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700861
Dan Williams6d821202020-10-13 16:50:55 -0700862static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700863{
864 /*
865 * The minimum mapping granularity for a device instance is a
866 * single subsection, unless the arch says otherwise.
867 */
Dan Williams6d821202020-10-13 16:50:55 -0700868 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700869}
870
871static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
872{
Dan Williams60e93dc2020-10-13 16:50:39 -0700873 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700874 struct dax_region *dax_region = dev_dax->region;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700875 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700876 int i;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700877
Dan Williams60e93dc2020-10-13 16:50:39 -0700878 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
879 struct range *range = &dev_dax->ranges[i].range;
Dan Williams0b07ce82020-10-13 16:50:45 -0700880 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
Dan Williams60e93dc2020-10-13 16:50:39 -0700881 struct resource *adjust = NULL, *res;
882 resource_size_t shrink;
883
884 shrink = min_t(u64, to_shrink, range_len(range));
885 if (shrink >= range_len(range)) {
Dan Williams0b07ce82020-10-13 16:50:45 -0700886 devm_release_action(dax_region->dev,
887 unregister_dax_mapping, &mapping->dev);
Dan Williams60e93dc2020-10-13 16:50:39 -0700888 __release_region(&dax_region->res, range->start,
889 range_len(range));
890 dev_dax->nr_range--;
891 dev_dbg(dev, "delete range[%d]: %#llx:%#llx\n", i,
892 (unsigned long long) range->start,
893 (unsigned long long) range->end);
894 to_shrink -= shrink;
895 if (!to_shrink)
896 break;
897 continue;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700898 }
899
Dan Williams60e93dc2020-10-13 16:50:39 -0700900 for_each_dax_region_resource(dax_region, res)
901 if (strcmp(res->name, dev_name(dev)) == 0
902 && res->start == range->start) {
903 adjust = res;
904 break;
905 }
906
907 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
908 "failed to find matching resource\n"))
909 return -ENXIO;
910 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
911 - shrink);
912 }
913 return 0;
914}
915
916/*
917 * Only allow adjustments that preserve the relative pgoff of existing
918 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
919 */
920static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
921{
922 struct dev_dax_range *last;
923 int i;
924
925 if (dev_dax->nr_range == 0)
926 return false;
927 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
928 return false;
929 last = &dev_dax->ranges[dev_dax->nr_range - 1];
930 if (last->range.start != res->start || last->range.end != res->end)
931 return false;
932 for (i = 0; i < dev_dax->nr_range - 1; i++) {
933 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
934
935 if (dax_range->pgoff > last->pgoff)
936 return false;
937 }
938
939 return true;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700940}
941
942static ssize_t dev_dax_resize(struct dax_region *dax_region,
943 struct dev_dax *dev_dax, resource_size_t size)
944{
945 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
Dan Williams60e93dc2020-10-13 16:50:39 -0700946 resource_size_t dev_size = dev_dax_size(dev_dax);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700947 struct resource *region_res = &dax_region->res;
948 struct device *dev = &dev_dax->dev;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700949 struct resource *res, *first;
Dan Williams60e93dc2020-10-13 16:50:39 -0700950 resource_size_t alloc = 0;
951 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700952
953 if (dev->driver)
954 return -EBUSY;
955 if (size == dev_size)
956 return 0;
957 if (size > dev_size && size - dev_size > avail)
958 return -ENOSPC;
959 if (size < dev_size)
960 return dev_dax_shrink(dev_dax, size);
961
962 to_alloc = size - dev_size;
Dan Williams6d821202020-10-13 16:50:55 -0700963 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700964 "resize of %pa misaligned\n", &to_alloc))
965 return -ENXIO;
966
967 /*
968 * Expand the device into the unused portion of the region. This
969 * may involve adjusting the end of an existing resource, or
970 * allocating a new resource.
971 */
Dan Williams60e93dc2020-10-13 16:50:39 -0700972retry:
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700973 first = region_res->child;
974 if (!first)
975 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
Dan Williams60e93dc2020-10-13 16:50:39 -0700976
977 rc = -ENOSPC;
978 for (res = first; res; res = res->sibling) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700979 struct resource *next = res->sibling;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700980
981 /* space at the beginning of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700982 if (res == first && res->start > dax_region->res.start) {
983 alloc = min(res->start - dax_region->res.start, to_alloc);
984 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
985 break;
986 }
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700987
Dan Williams60e93dc2020-10-13 16:50:39 -0700988 alloc = 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700989 /* space between allocations */
990 if (next && next->start > res->end + 1)
Dan Williams60e93dc2020-10-13 16:50:39 -0700991 alloc = min(next->start - (res->end + 1), to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700992
993 /* space at the end of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700994 if (!alloc && !next && res->end < region_res->end)
995 alloc = min(region_res->end - res->end, to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700996
Dan Williams60e93dc2020-10-13 16:50:39 -0700997 if (!alloc)
998 continue;
999
1000 if (adjust_ok(dev_dax, res)) {
1001 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1002 break;
1003 }
1004 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1005 break;
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001006 }
Dan Williams60e93dc2020-10-13 16:50:39 -07001007 if (rc)
1008 return rc;
1009 to_alloc -= alloc;
1010 if (to_alloc)
1011 goto retry;
1012 return 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001013}
1014
1015static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1016 const char *buf, size_t len)
1017{
1018 ssize_t rc;
1019 unsigned long long val;
1020 struct dev_dax *dev_dax = to_dev_dax(dev);
1021 struct dax_region *dax_region = dev_dax->region;
1022
1023 rc = kstrtoull(buf, 0, &val);
1024 if (rc)
1025 return rc;
1026
Dan Williams6d821202020-10-13 16:50:55 -07001027 if (!alloc_is_aligned(dev_dax, val)) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001028 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1029 return -EINVAL;
1030 }
1031
1032 device_lock(dax_region->dev);
1033 if (!dax_region->dev->driver) {
1034 device_unlock(dax_region->dev);
1035 return -ENXIO;
1036 }
1037 device_lock(dev);
1038 rc = dev_dax_resize(dax_region, dev_dax, val);
1039 device_unlock(dev);
1040 device_unlock(dax_region->dev);
1041
1042 return rc == 0 ? len : rc;
1043}
1044static DEVICE_ATTR_RW(size);
Dan Williams51cf7842017-07-12 17:58:21 -07001045
Dan Williams6d821202020-10-13 16:50:55 -07001046static ssize_t align_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048{
1049 struct dev_dax *dev_dax = to_dev_dax(dev);
1050
1051 return sprintf(buf, "%d\n", dev_dax->align);
1052}
1053
1054static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1055{
1056 resource_size_t dev_size = dev_dax_size(dev_dax);
1057 struct device *dev = &dev_dax->dev;
1058 int i;
1059
1060 if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
1061 dev_dbg(dev, "%s: align %u invalid for size %pa\n",
1062 __func__, dev_dax->align, &dev_size);
1063 return -EINVAL;
1064 }
1065
1066 for (i = 0; i < dev_dax->nr_range; i++) {
1067 size_t len = range_len(&dev_dax->ranges[i].range);
1068
1069 if (!alloc_is_aligned(dev_dax, len)) {
1070 dev_dbg(dev, "%s: align %u invalid for range %d\n",
1071 __func__, dev_dax->align, i);
1072 return -EINVAL;
1073 }
1074 }
1075
1076 return 0;
1077}
1078
1079static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1080 const char *buf, size_t len)
1081{
1082 struct dev_dax *dev_dax = to_dev_dax(dev);
1083 struct dax_region *dax_region = dev_dax->region;
1084 unsigned long val, align_save;
1085 ssize_t rc;
1086
1087 rc = kstrtoul(buf, 0, &val);
1088 if (rc)
1089 return -ENXIO;
1090
1091 if (!dax_align_valid(val))
1092 return -EINVAL;
1093
1094 device_lock(dax_region->dev);
1095 if (!dax_region->dev->driver) {
1096 device_unlock(dax_region->dev);
1097 return -ENXIO;
1098 }
1099
1100 device_lock(dev);
1101 if (dev->driver) {
1102 rc = -EBUSY;
1103 goto out_unlock;
1104 }
1105
1106 align_save = dev_dax->align;
1107 dev_dax->align = val;
1108 rc = dev_dax_validate_align(dev_dax);
1109 if (rc)
1110 dev_dax->align = align_save;
1111out_unlock:
1112 device_unlock(dev);
1113 device_unlock(dax_region->dev);
1114 return rc == 0 ? len : rc;
1115}
1116static DEVICE_ATTR_RW(align);
1117
Dan Williams21c75762019-02-20 11:39:36 -08001118static int dev_dax_target_node(struct dev_dax *dev_dax)
1119{
1120 struct dax_region *dax_region = dev_dax->region;
1121
1122 return dax_region->target_node;
1123}
1124
1125static ssize_t target_node_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127{
1128 struct dev_dax *dev_dax = to_dev_dax(dev);
1129
1130 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1131}
1132static DEVICE_ATTR_RO(target_node);
1133
Vishal Verma40cdc602019-06-20 18:40:38 -06001134static ssize_t resource_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136{
1137 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -07001138 struct dax_region *dax_region = dev_dax->region;
1139 unsigned long long start;
Vishal Verma40cdc602019-06-20 18:40:38 -06001140
Dan Williams60e93dc2020-10-13 16:50:39 -07001141 if (dev_dax->nr_range < 1)
1142 start = dax_region->res.start;
1143 else
1144 start = dev_dax->ranges[0].range.start;
1145
1146 return sprintf(buf, "%#llx\n", start);
Vishal Verma40cdc602019-06-20 18:40:38 -06001147}
Dan Williams153dd282019-11-12 17:12:38 -08001148static DEVICE_ATTR(resource, 0400, resource_show, NULL);
Vishal Verma40cdc602019-06-20 18:40:38 -06001149
Vishal Vermac347bd72019-02-22 16:58:54 -07001150static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1151 char *buf)
1152{
1153 /*
1154 * We only ever expect to handle device-dax instances, i.e. the
1155 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1156 */
1157 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1158}
1159static DEVICE_ATTR_RO(modalias);
1160
Dan Williamscb4dd722019-11-12 17:13:31 -08001161static ssize_t numa_node_show(struct device *dev,
1162 struct device_attribute *attr, char *buf)
1163{
1164 return sprintf(buf, "%d\n", dev_to_node(dev));
1165}
1166static DEVICE_ATTR_RO(numa_node);
1167
Dan Williams21c75762019-02-20 11:39:36 -08001168static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1169{
1170 struct device *dev = container_of(kobj, struct device, kobj);
1171 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001172 struct dax_region *dax_region = dev_dax->region;
Dan Williams21c75762019-02-20 11:39:36 -08001173
1174 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1175 return 0;
Dan Williamscb4dd722019-11-12 17:13:31 -08001176 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1177 return 0;
Dan Williams6d821202020-10-13 16:50:55 -07001178 if ((a == &dev_attr_align.attr ||
1179 a == &dev_attr_size.attr) && is_static(dax_region))
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001180 return 0444;
Dan Williams21c75762019-02-20 11:39:36 -08001181 return a->mode;
1182}
1183
Dan Williams51cf7842017-07-12 17:58:21 -07001184static struct attribute *dev_dax_attributes[] = {
Vishal Vermac347bd72019-02-22 16:58:54 -07001185 &dev_attr_modalias.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001186 &dev_attr_size.attr,
Dan Williams21c75762019-02-20 11:39:36 -08001187 &dev_attr_target_node.attr,
Dan Williams6d821202020-10-13 16:50:55 -07001188 &dev_attr_align.attr,
Vishal Verma40cdc602019-06-20 18:40:38 -06001189 &dev_attr_resource.attr,
Dan Williamscb4dd722019-11-12 17:13:31 -08001190 &dev_attr_numa_node.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001191 NULL,
1192};
1193
1194static const struct attribute_group dev_dax_attribute_group = {
1195 .attrs = dev_dax_attributes,
Dan Williams21c75762019-02-20 11:39:36 -08001196 .is_visible = dev_dax_visible,
Dan Williams51cf7842017-07-12 17:58:21 -07001197};
1198
Dan Williams9567da02017-07-12 17:58:21 -07001199static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -07001200 &dev_dax_attribute_group,
1201 NULL,
1202};
Dan Williams51cf7842017-07-12 17:58:21 -07001203
Dan Williams9567da02017-07-12 17:58:21 -07001204static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -07001205{
1206 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -07001207 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -07001208 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -07001209
Dan Williams9567da02017-07-12 17:58:21 -07001210 put_dax(dax_dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001211 free_dev_dax_id(dev_dax);
1212 dax_region_put(dax_region);
Dan Williams60e93dc2020-10-13 16:50:39 -07001213 kfree(dev_dax->ranges);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001214 kfree(dev_dax->pgmap);
Dan Williams9567da02017-07-12 17:58:21 -07001215 kfree(dev_dax);
1216}
1217
Dan Williams770619a2019-11-12 17:12:23 -08001218static const struct device_type dev_dax_type = {
1219 .release = dev_dax_release,
1220 .groups = dax_attribute_groups,
1221};
1222
Dan Williams174ebec2020-10-13 16:49:38 -07001223struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
Dan Williams9567da02017-07-12 17:58:21 -07001224{
Dan Williams174ebec2020-10-13 16:49:38 -07001225 struct dax_region *dax_region = data->dax_region;
Dan Williams9567da02017-07-12 17:58:21 -07001226 struct device *parent = dax_region->dev;
1227 struct dax_device *dax_dev;
1228 struct dev_dax *dev_dax;
1229 struct inode *inode;
1230 struct device *dev;
Dan Williamsc2f30112020-10-13 16:50:03 -07001231 int rc;
Dan Williams9567da02017-07-12 17:58:21 -07001232
Dan Williams9567da02017-07-12 17:58:21 -07001233 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1234 if (!dev_dax)
1235 return ERR_PTR(-ENOMEM);
1236
Dan Williams0f3da142020-10-13 16:50:13 -07001237 if (is_static(dax_region)) {
1238 if (dev_WARN_ONCE(parent, data->id < 0,
1239 "dynamic id specified to static region\n")) {
1240 rc = -EINVAL;
1241 goto err_id;
1242 }
1243
1244 dev_dax->id = data->id;
1245 } else {
1246 if (dev_WARN_ONCE(parent, data->id >= 0,
1247 "static id specified to dynamic region\n")) {
1248 rc = -EINVAL;
1249 goto err_id;
1250 }
1251
1252 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1253 if (rc < 0)
1254 goto err_id;
1255 dev_dax->id = rc;
1256 }
1257
Dan Williamsc2f30112020-10-13 16:50:03 -07001258 dev_dax->region = dax_region;
1259 dev = &dev_dax->dev;
1260 device_initialize(dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001261 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
Dan Williamsc2f30112020-10-13 16:50:03 -07001262
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001263 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
Dan Williamsc2f30112020-10-13 16:50:03 -07001264 if (rc)
1265 goto err_range;
1266
Dan Williamsf5516ec2020-10-13 16:49:43 -07001267 if (data->pgmap) {
Dan Williamsc2f30112020-10-13 16:50:03 -07001268 dev_WARN_ONCE(parent, !is_static(dax_region),
1269 "custom dev_pagemap requires a static dax_region\n");
1270
Dan Williamsf5516ec2020-10-13 16:49:43 -07001271 dev_dax->pgmap = kmemdup(data->pgmap,
1272 sizeof(struct dev_pagemap), GFP_KERNEL);
Dan Williamsc2f30112020-10-13 16:50:03 -07001273 if (!dev_dax->pgmap) {
1274 rc = -ENOMEM;
Dan Williamsf5516ec2020-10-13 16:49:43 -07001275 goto err_pgmap;
Dan Williamsc2f30112020-10-13 16:50:03 -07001276 }
Dan Williamsf5516ec2020-10-13 16:49:43 -07001277 }
Dan Williams89ec9f22018-10-29 15:52:42 -07001278
Dan Williams9567da02017-07-12 17:58:21 -07001279 /*
1280 * No 'host' or dax_operations since there is no access to this
1281 * device outside of mmap of the resulting character device.
1282 */
Pankaj Guptafefc1d92019-07-05 19:33:24 +05301283 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001284 if (IS_ERR(dax_dev)) {
1285 rc = PTR_ERR(dax_dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001286 goto err_alloc_dax;
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001287 }
Dan Williams9567da02017-07-12 17:58:21 -07001288
1289 /* a device_dax instance is dead while the driver is not attached */
1290 kill_dax(dax_dev);
1291
Dan Williams9567da02017-07-12 17:58:21 -07001292 dev_dax->dax_dev = dax_dev;
Dan Williams8fc5c732018-11-09 12:43:07 -08001293 dev_dax->target_node = dax_region->target_node;
Joao Martins33cf94d2020-10-13 16:50:50 -07001294 dev_dax->align = dax_region->align;
Dan Williams0b07ce82020-10-13 16:50:45 -07001295 ida_init(&dev_dax->ida);
Dan Williams9567da02017-07-12 17:58:21 -07001296 kref_get(&dax_region->kref);
1297
1298 inode = dax_inode(dax_dev);
1299 dev->devt = inode->i_rdev;
Dan Williams174ebec2020-10-13 16:49:38 -07001300 if (data->subsys == DEV_DAX_BUS)
Dan Williams730926c2017-07-16 13:51:53 -07001301 dev->bus = &dax_bus_type;
1302 else
1303 dev->class = dax_class;
Dan Williams9567da02017-07-12 17:58:21 -07001304 dev->parent = parent;
Dan Williams770619a2019-11-12 17:12:23 -08001305 dev->type = &dev_dax_type;
Dan Williams9567da02017-07-12 17:58:21 -07001306
1307 rc = device_add(dev);
1308 if (rc) {
1309 kill_dev_dax(dev_dax);
1310 put_device(dev);
1311 return ERR_PTR(rc);
1312 }
1313
1314 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1315 if (rc)
1316 return ERR_PTR(rc);
1317
Dan Williams0b07ce82020-10-13 16:50:45 -07001318 /* register mapping device for the initial allocation range */
1319 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1320 rc = devm_register_dax_mapping(dev_dax, 0);
1321 if (rc)
1322 return ERR_PTR(rc);
1323 }
1324
Dan Williams9567da02017-07-12 17:58:21 -07001325 return dev_dax;
Dan Williamsc2f30112020-10-13 16:50:03 -07001326
Dan Williamsf5516ec2020-10-13 16:49:43 -07001327err_alloc_dax:
1328 kfree(dev_dax->pgmap);
1329err_pgmap:
Dan Williams60e93dc2020-10-13 16:50:39 -07001330 free_dev_dax_ranges(dev_dax);
Dan Williamsc2f30112020-10-13 16:50:03 -07001331err_range:
Dan Williams0f3da142020-10-13 16:50:13 -07001332 free_dev_dax_id(dev_dax);
1333err_id:
Dan Williams9567da02017-07-12 17:58:21 -07001334 kfree(dev_dax);
1335
1336 return ERR_PTR(rc);
1337}
Dan Williams174ebec2020-10-13 16:49:38 -07001338EXPORT_SYMBOL_GPL(devm_create_dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -07001339
Dan Williamsd2007812018-11-07 15:31:23 -08001340static int match_always_count;
1341
1342int __dax_driver_register(struct dax_device_driver *dax_drv,
Dan Williams9567da02017-07-12 17:58:21 -07001343 struct module *module, const char *mod_name)
1344{
Dan Williamsd2007812018-11-07 15:31:23 -08001345 struct device_driver *drv = &dax_drv->drv;
1346 int rc = 0;
1347
1348 INIT_LIST_HEAD(&dax_drv->ids);
Dan Williams9567da02017-07-12 17:58:21 -07001349 drv->owner = module;
1350 drv->name = mod_name;
1351 drv->mod_name = mod_name;
1352 drv->bus = &dax_bus_type;
Dan Williamsd2007812018-11-07 15:31:23 -08001353
1354 /* there can only be one default driver */
1355 mutex_lock(&dax_bus_lock);
1356 match_always_count += dax_drv->match_always;
1357 if (match_always_count > 1) {
1358 match_always_count--;
1359 WARN_ON(1);
1360 rc = -EINVAL;
1361 }
1362 mutex_unlock(&dax_bus_lock);
1363 if (rc)
1364 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001365 return driver_register(drv);
1366}
1367EXPORT_SYMBOL_GPL(__dax_driver_register);
1368
Dan Williamsd2007812018-11-07 15:31:23 -08001369void dax_driver_unregister(struct dax_device_driver *dax_drv)
1370{
Dan Williams730926c2017-07-16 13:51:53 -07001371 struct device_driver *drv = &dax_drv->drv;
Dan Williamsd2007812018-11-07 15:31:23 -08001372 struct dax_id *dax_id, *_id;
1373
1374 mutex_lock(&dax_bus_lock);
1375 match_always_count -= dax_drv->match_always;
1376 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1377 list_del(&dax_id->list);
1378 kfree(dax_id);
1379 }
1380 mutex_unlock(&dax_bus_lock);
Dan Williams730926c2017-07-16 13:51:53 -07001381 driver_unregister(drv);
Dan Williamsd2007812018-11-07 15:31:23 -08001382}
1383EXPORT_SYMBOL_GPL(dax_driver_unregister);
1384
Dan Williams9567da02017-07-12 17:58:21 -07001385int __init dax_bus_init(void)
1386{
Dan Williams730926c2017-07-16 13:51:53 -07001387 int rc;
1388
1389 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
1390 dax_class = class_create(THIS_MODULE, "dax");
1391 if (IS_ERR(dax_class))
1392 return PTR_ERR(dax_class);
1393 }
1394
1395 rc = bus_register(&dax_bus_type);
1396 if (rc)
1397 class_destroy(dax_class);
1398 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001399}
1400
1401void __exit dax_bus_exit(void)
1402{
1403 bus_unregister(&dax_bus_type);
Dan Williams730926c2017-07-16 13:51:53 -07001404 class_destroy(dax_class);
Dan Williams9567da02017-07-12 17:58:21 -07001405}