blob: 5aee26e1bbd6dba393d8d33fe56c52cf9014fcfb [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;
Arnd Bergmann9de82ca2021-03-22 12:44:58 +010093 }
Dan Williams664525b2019-01-24 13:12:04 -080094 } else if (action == ID_REMOVE) {
Dan Williamsd2007812018-11-07 15:31:23 -080095 list_del(&dax_id->list);
96 kfree(dax_id);
Arnd Bergmann9de82ca2021-03-22 12:44:58 +010097 }
Dan Williamsd2007812018-11-07 15:31:23 -080098 mutex_unlock(&dax_bus_lock);
Dan Williams664525b2019-01-24 13:12:04 -080099
100 if (rc < 0)
101 return rc;
102 if (action == ID_ADD)
103 rc = driver_attach(drv);
104 if (rc)
105 return rc;
106 return count;
Dan Williamsd2007812018-11-07 15:31:23 -0800107}
108
109static ssize_t new_id_store(struct device_driver *drv, const char *buf,
110 size_t count)
111{
Dan Williams664525b2019-01-24 13:12:04 -0800112 return do_id_store(drv, buf, count, ID_ADD);
Dan Williamsd2007812018-11-07 15:31:23 -0800113}
114static DRIVER_ATTR_WO(new_id);
115
116static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
117 size_t count)
118{
Dan Williams664525b2019-01-24 13:12:04 -0800119 return do_id_store(drv, buf, count, ID_REMOVE);
Dan Williamsd2007812018-11-07 15:31:23 -0800120}
121static DRIVER_ATTR_WO(remove_id);
122
123static struct attribute *dax_drv_attrs[] = {
124 &driver_attr_new_id.attr,
125 &driver_attr_remove_id.attr,
126 NULL,
127};
128ATTRIBUTE_GROUPS(dax_drv);
129
Dan Williams9567da02017-07-12 17:58:21 -0700130static int dax_bus_match(struct device *dev, struct device_driver *drv);
131
Dan Williamsc2f30112020-10-13 16:50:03 -0700132static bool is_static(struct dax_region *dax_region)
133{
134 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
135}
136
Dan Williams60e93dc2020-10-13 16:50:39 -0700137static u64 dev_dax_size(struct dev_dax *dev_dax)
138{
139 u64 size = 0;
140 int i;
141
142 device_lock_assert(&dev_dax->dev);
143
144 for (i = 0; i < dev_dax->nr_range; i++)
145 size += range_len(&dev_dax->ranges[i].range);
146
147 return size;
148}
149
Dan Williamsf11cf812020-10-13 16:50:08 -0700150static int dax_bus_probe(struct device *dev)
151{
152 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
153 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams0f3da142020-10-13 16:50:13 -0700154 struct dax_region *dax_region = dev_dax->region;
Dan Williams0f3da142020-10-13 16:50:13 -0700155 int rc;
Dan Williamsf11cf812020-10-13 16:50:08 -0700156
Dan Williams60e93dc2020-10-13 16:50:39 -0700157 if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
Dan Williams0f3da142020-10-13 16:50:13 -0700158 return -ENXIO;
159
160 rc = dax_drv->probe(dev_dax);
161
162 if (rc || is_static(dax_region))
163 return rc;
164
165 /*
166 * Track new seed creation only after successful probe of the
167 * previous seed.
168 */
169 if (dax_region->seed == dev)
170 dax_region->seed = NULL;
171
172 return 0;
Dan Williamsf11cf812020-10-13 16:50:08 -0700173}
174
175static int dax_bus_remove(struct device *dev)
176{
177 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
178 struct dev_dax *dev_dax = to_dev_dax(dev);
179
Uwe Kleine-König80299682021-02-05 23:28:39 +0100180 if (dax_drv->remove)
Uwe Kleine-König0d519e02021-02-05 23:28:42 +0100181 dax_drv->remove(dev_dax);
Uwe Kleine-König80299682021-02-05 23:28:39 +0100182
Uwe Kleine-König0d519e02021-02-05 23:28:42 +0100183 return 0;
Dan Williamsf11cf812020-10-13 16:50:08 -0700184}
185
Dan Williams9567da02017-07-12 17:58:21 -0700186static struct bus_type dax_bus_type = {
187 .name = "dax",
188 .uevent = dax_bus_uevent,
189 .match = dax_bus_match,
Dan Williamsf11cf812020-10-13 16:50:08 -0700190 .probe = dax_bus_probe,
191 .remove = dax_bus_remove,
Dan Williamsd2007812018-11-07 15:31:23 -0800192 .drv_groups = dax_drv_groups,
Dan Williams9567da02017-07-12 17:58:21 -0700193};
194
195static int dax_bus_match(struct device *dev, struct device_driver *drv)
196{
Dan Williamsd2007812018-11-07 15:31:23 -0800197 struct dax_device_driver *dax_drv = to_dax_drv(drv);
198
Dan Williams9567da02017-07-12 17:58:21 -0700199 /*
Dan Williamsd2007812018-11-07 15:31:23 -0800200 * All but the 'device-dax' driver, which has 'match_always'
201 * set, requires an exact id match.
Dan Williams9567da02017-07-12 17:58:21 -0700202 */
Dan Williamsd2007812018-11-07 15:31:23 -0800203 if (dax_drv->match_always)
204 return 1;
205
206 return dax_match_id(dax_drv, dev);
Dan Williams9567da02017-07-12 17:58:21 -0700207}
208
Dan Williams51cf7842017-07-12 17:58:21 -0700209/*
210 * Rely on the fact that drvdata is set before the attributes are
211 * registered, and that the attributes are unregistered before drvdata
212 * is cleared to assume that drvdata is always valid.
213 */
214static ssize_t id_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
216{
217 struct dax_region *dax_region = dev_get_drvdata(dev);
218
219 return sprintf(buf, "%d\n", dax_region->id);
220}
221static DEVICE_ATTR_RO(id);
222
223static ssize_t region_size_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct dax_region *dax_region = dev_get_drvdata(dev);
227
228 return sprintf(buf, "%llu\n", (unsigned long long)
229 resource_size(&dax_region->res));
230}
231static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
232 region_size_show, NULL);
233
Dan Williams6d821202020-10-13 16:50:55 -0700234static ssize_t region_align_show(struct device *dev,
Dan Williams51cf7842017-07-12 17:58:21 -0700235 struct device_attribute *attr, char *buf)
236{
237 struct dax_region *dax_region = dev_get_drvdata(dev);
238
239 return sprintf(buf, "%u\n", dax_region->align);
240}
Dan Williams6d821202020-10-13 16:50:55 -0700241static struct device_attribute dev_attr_region_align =
242 __ATTR(align, 0400, region_align_show, NULL);
Dan Williams51cf7842017-07-12 17:58:21 -0700243
Dan Williamsc2f30112020-10-13 16:50:03 -0700244#define for_each_dax_region_resource(dax_region, res) \
245 for (res = (dax_region)->res.child; res; res = res->sibling)
246
247static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
248{
249 resource_size_t size = resource_size(&dax_region->res);
250 struct resource *res;
251
252 device_lock_assert(dax_region->dev);
253
254 for_each_dax_region_resource(dax_region, res)
255 size -= resource_size(res);
256 return size;
257}
258
259static ssize_t available_size_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261{
262 struct dax_region *dax_region = dev_get_drvdata(dev);
263 unsigned long long size;
264
265 device_lock(dev);
266 size = dax_region_avail_size(dax_region);
267 device_unlock(dev);
268
269 return sprintf(buf, "%llu\n", size);
270}
271static DEVICE_ATTR_RO(available_size);
272
Dan Williams0f3da142020-10-13 16:50:13 -0700273static ssize_t seed_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
275{
276 struct dax_region *dax_region = dev_get_drvdata(dev);
277 struct device *seed;
278 ssize_t rc;
279
280 if (is_static(dax_region))
281 return -EINVAL;
282
283 device_lock(dev);
284 seed = dax_region->seed;
285 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
286 device_unlock(dev);
287
288 return rc;
289}
290static DEVICE_ATTR_RO(seed);
291
292static ssize_t create_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 struct dax_region *dax_region = dev_get_drvdata(dev);
296 struct device *youngest;
297 ssize_t rc;
298
299 if (is_static(dax_region))
300 return -EINVAL;
301
302 device_lock(dev);
303 youngest = dax_region->youngest;
304 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
305 device_unlock(dev);
306
307 return rc;
308}
309
310static ssize_t create_store(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t len)
312{
313 struct dax_region *dax_region = dev_get_drvdata(dev);
314 unsigned long long avail;
315 ssize_t rc;
316 int val;
317
318 if (is_static(dax_region))
319 return -EINVAL;
320
321 rc = kstrtoint(buf, 0, &val);
322 if (rc)
323 return rc;
324 if (val != 1)
325 return -EINVAL;
326
327 device_lock(dev);
328 avail = dax_region_avail_size(dax_region);
329 if (avail == 0)
330 rc = -ENOSPC;
331 else {
332 struct dev_dax_data data = {
333 .dax_region = dax_region,
334 .size = 0,
335 .id = -1,
336 };
337 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
338
339 if (IS_ERR(dev_dax))
340 rc = PTR_ERR(dev_dax);
341 else {
342 /*
343 * In support of crafting multiple new devices
344 * simultaneously multiple seeds can be created,
345 * but only the first one that has not been
346 * successfully bound is tracked as the region
347 * seed.
348 */
349 if (!dax_region->seed)
350 dax_region->seed = &dev_dax->dev;
351 dax_region->youngest = &dev_dax->dev;
352 rc = len;
353 }
354 }
355 device_unlock(dev);
356
357 return rc;
358}
359static DEVICE_ATTR_RW(create);
360
361void kill_dev_dax(struct dev_dax *dev_dax)
362{
363 struct dax_device *dax_dev = dev_dax->dax_dev;
364 struct inode *inode = dax_inode(dax_dev);
365
366 kill_dax(dax_dev);
367 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
368}
369EXPORT_SYMBOL_GPL(kill_dev_dax);
370
Dan Williams6268d7d2020-12-18 18:41:41 -0800371static void trim_dev_dax_range(struct dev_dax *dev_dax)
Dan Williams0f3da142020-10-13 16:50:13 -0700372{
Dan Williams6268d7d2020-12-18 18:41:41 -0800373 int i = dev_dax->nr_range - 1;
374 struct range *range = &dev_dax->ranges[i].range;
Dan Williams0f3da142020-10-13 16:50:13 -0700375 struct dax_region *dax_region = dev_dax->region;
Dan Williams0f3da142020-10-13 16:50:13 -0700376
377 device_lock_assert(dax_region->dev);
Dan Williams6268d7d2020-12-18 18:41:41 -0800378 dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
379 (unsigned long long)range->start,
380 (unsigned long long)range->end);
Dan Williams60e93dc2020-10-13 16:50:39 -0700381
Dan Williams6268d7d2020-12-18 18:41:41 -0800382 __release_region(&dax_region->res, range->start, range_len(range));
383 if (--dev_dax->nr_range == 0) {
384 kfree(dev_dax->ranges);
385 dev_dax->ranges = NULL;
Dan Williams60e93dc2020-10-13 16:50:39 -0700386 }
Dan Williams6268d7d2020-12-18 18:41:41 -0800387}
388
389static void free_dev_dax_ranges(struct dev_dax *dev_dax)
390{
391 while (dev_dax->nr_range)
392 trim_dev_dax_range(dev_dax);
Dan Williams0f3da142020-10-13 16:50:13 -0700393}
394
395static void unregister_dev_dax(void *dev)
396{
397 struct dev_dax *dev_dax = to_dev_dax(dev);
398
399 dev_dbg(dev, "%s\n", __func__);
400
401 kill_dev_dax(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700402 free_dev_dax_ranges(dev_dax);
Dan Williams0f3da142020-10-13 16:50:13 -0700403 device_del(dev);
404 put_device(dev);
405}
406
407/* a return value >= 0 indicates this invocation invalidated the id */
408static int __free_dev_dax_id(struct dev_dax *dev_dax)
409{
410 struct dax_region *dax_region = dev_dax->region;
411 struct device *dev = &dev_dax->dev;
412 int rc = dev_dax->id;
413
414 device_lock_assert(dev);
415
416 if (is_static(dax_region) || dev_dax->id < 0)
417 return -1;
418 ida_free(&dax_region->ida, dev_dax->id);
419 dev_dax->id = -1;
420 return rc;
421}
422
423static int free_dev_dax_id(struct dev_dax *dev_dax)
424{
425 struct device *dev = &dev_dax->dev;
426 int rc;
427
428 device_lock(dev);
429 rc = __free_dev_dax_id(dev_dax);
430 device_unlock(dev);
431 return rc;
432}
433
434static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t len)
436{
437 struct dax_region *dax_region = dev_get_drvdata(dev);
438 struct dev_dax *dev_dax;
439 struct device *victim;
440 bool do_del = false;
441 int rc;
442
443 if (is_static(dax_region))
444 return -EINVAL;
445
446 victim = device_find_child_by_name(dax_region->dev, buf);
447 if (!victim)
448 return -ENXIO;
449
450 device_lock(dev);
451 device_lock(victim);
452 dev_dax = to_dev_dax(victim);
Dan Williams60e93dc2020-10-13 16:50:39 -0700453 if (victim->driver || dev_dax_size(dev_dax))
Dan Williams0f3da142020-10-13 16:50:13 -0700454 rc = -EBUSY;
455 else {
456 /*
457 * Invalidate the device so it does not become active
458 * again, but always preserve device-id-0 so that
459 * /sys/bus/dax/ is guaranteed to be populated while any
460 * dax_region is registered.
461 */
462 if (dev_dax->id > 0) {
463 do_del = __free_dev_dax_id(dev_dax) >= 0;
464 rc = len;
465 if (dax_region->seed == victim)
466 dax_region->seed = NULL;
467 if (dax_region->youngest == victim)
468 dax_region->youngest = NULL;
469 } else
470 rc = -EBUSY;
471 }
472 device_unlock(victim);
473
474 /* won the race to invalidate the device, clean it up */
475 if (do_del)
476 devm_release_action(dev, unregister_dev_dax, victim);
477 device_unlock(dev);
478 put_device(victim);
479
480 return rc;
481}
482static DEVICE_ATTR_WO(delete);
483
Dan Williamsc2f30112020-10-13 16:50:03 -0700484static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
485 int n)
486{
487 struct device *dev = container_of(kobj, struct device, kobj);
488 struct dax_region *dax_region = dev_get_drvdata(dev);
489
Dan Williams0f3da142020-10-13 16:50:13 -0700490 if (is_static(dax_region))
491 if (a == &dev_attr_available_size.attr
492 || a == &dev_attr_create.attr
493 || a == &dev_attr_seed.attr
494 || a == &dev_attr_delete.attr)
495 return 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700496 return a->mode;
497}
498
Dan Williams51cf7842017-07-12 17:58:21 -0700499static struct attribute *dax_region_attributes[] = {
Dan Williamsc2f30112020-10-13 16:50:03 -0700500 &dev_attr_available_size.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700501 &dev_attr_region_size.attr,
Dan Williams6d821202020-10-13 16:50:55 -0700502 &dev_attr_region_align.attr,
Dan Williams0f3da142020-10-13 16:50:13 -0700503 &dev_attr_create.attr,
504 &dev_attr_seed.attr,
505 &dev_attr_delete.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700506 &dev_attr_id.attr,
507 NULL,
508};
509
510static const struct attribute_group dax_region_attribute_group = {
511 .name = "dax_region",
512 .attrs = dax_region_attributes,
Dan Williamsc2f30112020-10-13 16:50:03 -0700513 .is_visible = dax_region_visible,
Dan Williams51cf7842017-07-12 17:58:21 -0700514};
515
516static const struct attribute_group *dax_region_attribute_groups[] = {
517 &dax_region_attribute_group,
518 NULL,
519};
520
521static void dax_region_free(struct kref *kref)
522{
523 struct dax_region *dax_region;
524
525 dax_region = container_of(kref, struct dax_region, kref);
526 kfree(dax_region);
527}
528
529void dax_region_put(struct dax_region *dax_region)
530{
531 kref_put(&dax_region->kref, dax_region_free);
532}
533EXPORT_SYMBOL_GPL(dax_region_put);
534
535static void dax_region_unregister(void *region)
536{
537 struct dax_region *dax_region = region;
538
539 sysfs_remove_groups(&dax_region->dev->kobj,
540 dax_region_attribute_groups);
541 dax_region_put(dax_region);
542}
543
544struct dax_region *alloc_dax_region(struct device *parent, int region_id,
Dan Williamsa4574f62020-10-13 16:50:29 -0700545 struct range *range, int target_node, unsigned int align,
Dan Williamsc2f30112020-10-13 16:50:03 -0700546 unsigned long flags)
Dan Williams51cf7842017-07-12 17:58:21 -0700547{
548 struct dax_region *dax_region;
549
550 /*
551 * The DAX core assumes that it can store its private data in
552 * parent->driver_data. This WARN is a reminder / safeguard for
553 * developers of device-dax drivers.
554 */
555 if (dev_get_drvdata(parent)) {
556 dev_WARN(parent, "dax core failed to setup private data\n");
557 return NULL;
558 }
559
Dan Williamsa4574f62020-10-13 16:50:29 -0700560 if (!IS_ALIGNED(range->start, align)
561 || !IS_ALIGNED(range_len(range), align))
Dan Williams51cf7842017-07-12 17:58:21 -0700562 return NULL;
563
564 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
565 if (!dax_region)
566 return NULL;
567
568 dev_set_drvdata(parent, dax_region);
Dan Williams51cf7842017-07-12 17:58:21 -0700569 kref_init(&dax_region->kref);
570 dax_region->id = region_id;
571 dax_region->align = align;
572 dax_region->dev = parent;
Dan Williams8fc5c732018-11-09 12:43:07 -0800573 dax_region->target_node = target_node;
Dan Williams0f3da142020-10-13 16:50:13 -0700574 ida_init(&dax_region->ida);
Dan Williamsc2f30112020-10-13 16:50:03 -0700575 dax_region->res = (struct resource) {
Dan Williamsa4574f62020-10-13 16:50:29 -0700576 .start = range->start,
577 .end = range->end,
Dan Williamsc2f30112020-10-13 16:50:03 -0700578 .flags = IORESOURCE_MEM | flags,
579 };
580
Dan Williams51cf7842017-07-12 17:58:21 -0700581 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
582 kfree(dax_region);
583 return NULL;
584 }
585
586 kref_get(&dax_region->kref);
587 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
588 return NULL;
589 return dax_region;
590}
591EXPORT_SYMBOL_GPL(alloc_dax_region);
592
Dan Williams0b07ce82020-10-13 16:50:45 -0700593static void dax_mapping_release(struct device *dev)
594{
595 struct dax_mapping *mapping = to_dax_mapping(dev);
596 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
597
598 ida_free(&dev_dax->ida, mapping->id);
599 kfree(mapping);
600}
601
602static void unregister_dax_mapping(void *data)
603{
604 struct device *dev = data;
605 struct dax_mapping *mapping = to_dax_mapping(dev);
606 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
607 struct dax_region *dax_region = dev_dax->region;
608
609 dev_dbg(dev, "%s\n", __func__);
610
611 device_lock_assert(dax_region->dev);
612
613 dev_dax->ranges[mapping->range_id].mapping = NULL;
614 mapping->range_id = -1;
615
616 device_del(dev);
617 put_device(dev);
618}
619
620static struct dev_dax_range *get_dax_range(struct device *dev)
621{
622 struct dax_mapping *mapping = to_dax_mapping(dev);
623 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
624 struct dax_region *dax_region = dev_dax->region;
625
626 device_lock(dax_region->dev);
627 if (mapping->range_id < 0) {
628 device_unlock(dax_region->dev);
629 return NULL;
630 }
631
632 return &dev_dax->ranges[mapping->range_id];
633}
634
635static void put_dax_range(struct dev_dax_range *dax_range)
636{
637 struct dax_mapping *mapping = dax_range->mapping;
638 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
639 struct dax_region *dax_region = dev_dax->region;
640
641 device_unlock(dax_region->dev);
642}
643
644static ssize_t start_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
646{
647 struct dev_dax_range *dax_range;
648 ssize_t rc;
649
650 dax_range = get_dax_range(dev);
651 if (!dax_range)
652 return -ENXIO;
653 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
654 put_dax_range(dax_range);
655
656 return rc;
657}
658static DEVICE_ATTR(start, 0400, start_show, NULL);
659
660static ssize_t end_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
662{
663 struct dev_dax_range *dax_range;
664 ssize_t rc;
665
666 dax_range = get_dax_range(dev);
667 if (!dax_range)
668 return -ENXIO;
669 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
670 put_dax_range(dax_range);
671
672 return rc;
673}
674static DEVICE_ATTR(end, 0400, end_show, NULL);
675
676static ssize_t pgoff_show(struct device *dev,
677 struct device_attribute *attr, char *buf)
678{
679 struct dev_dax_range *dax_range;
680 ssize_t rc;
681
682 dax_range = get_dax_range(dev);
683 if (!dax_range)
684 return -ENXIO;
685 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
686 put_dax_range(dax_range);
687
688 return rc;
689}
690static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
691
692static struct attribute *dax_mapping_attributes[] = {
693 &dev_attr_start.attr,
694 &dev_attr_end.attr,
695 &dev_attr_page_offset.attr,
696 NULL,
697};
698
699static const struct attribute_group dax_mapping_attribute_group = {
700 .attrs = dax_mapping_attributes,
701};
702
703static const struct attribute_group *dax_mapping_attribute_groups[] = {
704 &dax_mapping_attribute_group,
705 NULL,
706};
707
708static struct device_type dax_mapping_type = {
709 .release = dax_mapping_release,
710 .groups = dax_mapping_attribute_groups,
711};
712
713static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
714{
715 struct dax_region *dax_region = dev_dax->region;
716 struct dax_mapping *mapping;
717 struct device *dev;
718 int rc;
719
720 device_lock_assert(dax_region->dev);
721
722 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
723 "region disabled\n"))
724 return -ENXIO;
725
726 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
727 if (!mapping)
728 return -ENOMEM;
729 mapping->range_id = range_id;
730 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
731 if (mapping->id < 0) {
732 kfree(mapping);
733 return -ENOMEM;
734 }
735 dev_dax->ranges[range_id].mapping = mapping;
736 dev = &mapping->dev;
737 device_initialize(dev);
738 dev->parent = &dev_dax->dev;
739 dev->type = &dax_mapping_type;
740 dev_set_name(dev, "mapping%d", mapping->id);
741 rc = device_add(dev);
742 if (rc) {
743 put_device(dev);
744 return rc;
745 }
746
747 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
748 dev);
749 if (rc)
750 return rc;
751 return 0;
752}
753
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700754static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
755 resource_size_t size)
Dan Williamsc2f30112020-10-13 16:50:03 -0700756{
757 struct dax_region *dax_region = dev_dax->region;
758 struct resource *res = &dax_region->res;
759 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700760 struct dev_dax_range *ranges;
761 unsigned long pgoff = 0;
Dan Williamsc2f30112020-10-13 16:50:03 -0700762 struct resource *alloc;
Dan Williams0b07ce82020-10-13 16:50:45 -0700763 int i, rc;
Dan Williamsc2f30112020-10-13 16:50:03 -0700764
765 device_lock_assert(dax_region->dev);
766
Dan Williams0f3da142020-10-13 16:50:13 -0700767 /* handle the seed alloc special case */
768 if (!size) {
Dan Williams60e93dc2020-10-13 16:50:39 -0700769 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
770 "0-size allocation must be first\n"))
771 return -EBUSY;
772 /* nr_range == 0 is elsewhere special cased as 0-size device */
Dan Williams0f3da142020-10-13 16:50:13 -0700773 return 0;
774 }
775
Zhen Leiff8da372020-12-19 16:18:40 +0800776 alloc = __request_region(res, start, size, dev_name(dev), 0);
777 if (!alloc)
Dan Williamsc2f30112020-10-13 16:50:03 -0700778 return -ENOMEM;
779
Zhen Leiff8da372020-12-19 16:18:40 +0800780 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
781 * (dev_dax->nr_range + 1), GFP_KERNEL);
782 if (!ranges) {
783 __release_region(res, alloc->start, resource_size(alloc));
Dan Williams60e93dc2020-10-13 16:50:39 -0700784 return -ENOMEM;
785 }
786
787 for (i = 0; i < dev_dax->nr_range; i++)
788 pgoff += PHYS_PFN(range_len(&ranges[i].range));
789 dev_dax->ranges = ranges;
790 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
791 .pgoff = pgoff,
792 .range = {
793 .start = alloc->start,
794 .end = alloc->end,
795 },
Dan Williamsc2f30112020-10-13 16:50:03 -0700796 };
797
Dan Williams60e93dc2020-10-13 16:50:39 -0700798 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
799 &alloc->start, &alloc->end);
Dan Williams0b07ce82020-10-13 16:50:45 -0700800 /*
801 * A dev_dax instance must be registered before mapping device
802 * children can be added. Defer to devm_create_dev_dax() to add
803 * the initial mapping device.
804 */
805 if (!device_is_registered(&dev_dax->dev))
806 return 0;
807
808 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
Dan Williams6268d7d2020-12-18 18:41:41 -0800809 if (rc)
810 trim_dev_dax_range(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700811
Dan Williams6268d7d2020-12-18 18:41:41 -0800812 return rc;
Dan Williamsc2f30112020-10-13 16:50:03 -0700813}
814
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700815static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
816{
Dan Williams60e93dc2020-10-13 16:50:39 -0700817 int last_range = dev_dax->nr_range - 1;
818 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700819 struct dax_region *dax_region = dev_dax->region;
Dan Williams60e93dc2020-10-13 16:50:39 -0700820 bool is_shrink = resource_size(res) > size;
821 struct range *range = &dax_range->range;
822 struct device *dev = &dev_dax->dev;
823 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700824
825 device_lock_assert(dax_region->dev);
826
Dan Williams60e93dc2020-10-13 16:50:39 -0700827 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
828 return -EINVAL;
829
830 rc = adjust_resource(res, range->start, size);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700831 if (rc)
832 return rc;
833
Dan Williams60e93dc2020-10-13 16:50:39 -0700834 *range = (struct range) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700835 .start = range->start,
836 .end = range->start + size - 1,
837 };
838
Dan Williams60e93dc2020-10-13 16:50:39 -0700839 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
840 last_range, (unsigned long long) range->start,
841 (unsigned long long) range->end);
842
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700843 return 0;
844}
845
Dan Williams51cf7842017-07-12 17:58:21 -0700846static ssize_t size_show(struct device *dev,
847 struct device_attribute *attr, char *buf)
848{
849 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -0700850 unsigned long long size;
851
852 device_lock(dev);
853 size = dev_dax_size(dev_dax);
854 device_unlock(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700855
856 return sprintf(buf, "%llu\n", size);
857}
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700858
Dan Williams6d821202020-10-13 16:50:55 -0700859static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700860{
861 /*
862 * The minimum mapping granularity for a device instance is a
863 * single subsection, unless the arch says otherwise.
864 */
Dan Williams6d821202020-10-13 16:50:55 -0700865 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700866}
867
868static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
869{
Dan Williams60e93dc2020-10-13 16:50:39 -0700870 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700871 struct dax_region *dax_region = dev_dax->region;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700872 struct device *dev = &dev_dax->dev;
Dan Williams60e93dc2020-10-13 16:50:39 -0700873 int i;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700874
Dan Williams60e93dc2020-10-13 16:50:39 -0700875 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
876 struct range *range = &dev_dax->ranges[i].range;
Dan Williams0b07ce82020-10-13 16:50:45 -0700877 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
Dan Williams60e93dc2020-10-13 16:50:39 -0700878 struct resource *adjust = NULL, *res;
879 resource_size_t shrink;
880
881 shrink = min_t(u64, to_shrink, range_len(range));
882 if (shrink >= range_len(range)) {
Dan Williams0b07ce82020-10-13 16:50:45 -0700883 devm_release_action(dax_region->dev,
884 unregister_dax_mapping, &mapping->dev);
Dan Williams6268d7d2020-12-18 18:41:41 -0800885 trim_dev_dax_range(dev_dax);
Dan Williams60e93dc2020-10-13 16:50:39 -0700886 to_shrink -= shrink;
887 if (!to_shrink)
888 break;
889 continue;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700890 }
891
Dan Williams60e93dc2020-10-13 16:50:39 -0700892 for_each_dax_region_resource(dax_region, res)
893 if (strcmp(res->name, dev_name(dev)) == 0
894 && res->start == range->start) {
895 adjust = res;
896 break;
897 }
898
899 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
900 "failed to find matching resource\n"))
901 return -ENXIO;
902 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
903 - shrink);
904 }
905 return 0;
906}
907
908/*
909 * Only allow adjustments that preserve the relative pgoff of existing
910 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
911 */
912static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
913{
914 struct dev_dax_range *last;
915 int i;
916
917 if (dev_dax->nr_range == 0)
918 return false;
919 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
920 return false;
921 last = &dev_dax->ranges[dev_dax->nr_range - 1];
922 if (last->range.start != res->start || last->range.end != res->end)
923 return false;
924 for (i = 0; i < dev_dax->nr_range - 1; i++) {
925 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
926
927 if (dax_range->pgoff > last->pgoff)
928 return false;
929 }
930
931 return true;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700932}
933
934static ssize_t dev_dax_resize(struct dax_region *dax_region,
935 struct dev_dax *dev_dax, resource_size_t size)
936{
937 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
Dan Williams60e93dc2020-10-13 16:50:39 -0700938 resource_size_t dev_size = dev_dax_size(dev_dax);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700939 struct resource *region_res = &dax_region->res;
940 struct device *dev = &dev_dax->dev;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700941 struct resource *res, *first;
Dan Williams60e93dc2020-10-13 16:50:39 -0700942 resource_size_t alloc = 0;
943 int rc;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700944
945 if (dev->driver)
946 return -EBUSY;
947 if (size == dev_size)
948 return 0;
949 if (size > dev_size && size - dev_size > avail)
950 return -ENOSPC;
951 if (size < dev_size)
952 return dev_dax_shrink(dev_dax, size);
953
954 to_alloc = size - dev_size;
Dan Williams6d821202020-10-13 16:50:55 -0700955 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700956 "resize of %pa misaligned\n", &to_alloc))
957 return -ENXIO;
958
959 /*
960 * Expand the device into the unused portion of the region. This
961 * may involve adjusting the end of an existing resource, or
962 * allocating a new resource.
963 */
Dan Williams60e93dc2020-10-13 16:50:39 -0700964retry:
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700965 first = region_res->child;
966 if (!first)
967 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
Dan Williams60e93dc2020-10-13 16:50:39 -0700968
969 rc = -ENOSPC;
970 for (res = first; res; res = res->sibling) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700971 struct resource *next = res->sibling;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700972
973 /* space at the beginning of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700974 if (res == first && res->start > dax_region->res.start) {
975 alloc = min(res->start - dax_region->res.start, to_alloc);
976 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
977 break;
978 }
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700979
Dan Williams60e93dc2020-10-13 16:50:39 -0700980 alloc = 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700981 /* space between allocations */
982 if (next && next->start > res->end + 1)
Dan Williams60e93dc2020-10-13 16:50:39 -0700983 alloc = min(next->start - (res->end + 1), to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700984
985 /* space at the end of the region */
Dan Williams60e93dc2020-10-13 16:50:39 -0700986 if (!alloc && !next && res->end < region_res->end)
987 alloc = min(region_res->end - res->end, to_alloc);
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700988
Dan Williams60e93dc2020-10-13 16:50:39 -0700989 if (!alloc)
990 continue;
991
992 if (adjust_ok(dev_dax, res)) {
993 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
994 break;
995 }
996 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
997 break;
Dan Williamsfcffb6a2020-10-13 16:50:24 -0700998 }
Dan Williams60e93dc2020-10-13 16:50:39 -0700999 if (rc)
1000 return rc;
1001 to_alloc -= alloc;
1002 if (to_alloc)
1003 goto retry;
1004 return 0;
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001005}
1006
1007static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1008 const char *buf, size_t len)
1009{
1010 ssize_t rc;
1011 unsigned long long val;
1012 struct dev_dax *dev_dax = to_dev_dax(dev);
1013 struct dax_region *dax_region = dev_dax->region;
1014
1015 rc = kstrtoull(buf, 0, &val);
1016 if (rc)
1017 return rc;
1018
Dan Williams6d821202020-10-13 16:50:55 -07001019 if (!alloc_is_aligned(dev_dax, val)) {
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001020 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1021 return -EINVAL;
1022 }
1023
1024 device_lock(dax_region->dev);
1025 if (!dax_region->dev->driver) {
1026 device_unlock(dax_region->dev);
1027 return -ENXIO;
1028 }
1029 device_lock(dev);
1030 rc = dev_dax_resize(dax_region, dev_dax, val);
1031 device_unlock(dev);
1032 device_unlock(dax_region->dev);
1033
1034 return rc == 0 ? len : rc;
1035}
1036static DEVICE_ATTR_RW(size);
Dan Williams51cf7842017-07-12 17:58:21 -07001037
Joao Martins8490e2e2020-10-13 16:51:06 -07001038static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1039{
1040 unsigned long long addr = 0;
1041 char *start, *end, *str;
Shiyang Ruan7323fb22021-01-26 10:13:31 +08001042 ssize_t rc = -EINVAL;
Joao Martins8490e2e2020-10-13 16:51:06 -07001043
1044 str = kstrdup(opt, GFP_KERNEL);
1045 if (!str)
1046 return rc;
1047
1048 end = str;
1049 start = strsep(&end, "-");
1050 if (!start || !end)
1051 goto err;
1052
1053 rc = kstrtoull(start, 16, &addr);
1054 if (rc)
1055 goto err;
1056 range->start = addr;
1057
1058 rc = kstrtoull(end, 16, &addr);
1059 if (rc)
1060 goto err;
1061 range->end = addr;
1062
1063err:
1064 kfree(str);
1065 return rc;
1066}
1067
1068static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1069 const char *buf, size_t len)
1070{
1071 struct dev_dax *dev_dax = to_dev_dax(dev);
1072 struct dax_region *dax_region = dev_dax->region;
1073 size_t to_alloc;
1074 struct range r;
1075 ssize_t rc;
1076
1077 rc = range_parse(buf, len, &r);
1078 if (rc)
1079 return rc;
1080
1081 rc = -ENXIO;
1082 device_lock(dax_region->dev);
1083 if (!dax_region->dev->driver) {
1084 device_unlock(dax_region->dev);
1085 return rc;
1086 }
1087 device_lock(dev);
1088
1089 to_alloc = range_len(&r);
1090 if (alloc_is_aligned(dev_dax, to_alloc))
1091 rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1092 device_unlock(dev);
1093 device_unlock(dax_region->dev);
1094
1095 return rc == 0 ? len : rc;
1096}
1097static DEVICE_ATTR_WO(mapping);
1098
Dan Williams6d821202020-10-13 16:50:55 -07001099static ssize_t align_show(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1101{
1102 struct dev_dax *dev_dax = to_dev_dax(dev);
1103
1104 return sprintf(buf, "%d\n", dev_dax->align);
1105}
1106
1107static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1108{
Dan Williams6d821202020-10-13 16:50:55 -07001109 struct device *dev = &dev_dax->dev;
1110 int i;
1111
Dan Williams6d821202020-10-13 16:50:55 -07001112 for (i = 0; i < dev_dax->nr_range; i++) {
1113 size_t len = range_len(&dev_dax->ranges[i].range);
1114
1115 if (!alloc_is_aligned(dev_dax, len)) {
1116 dev_dbg(dev, "%s: align %u invalid for range %d\n",
1117 __func__, dev_dax->align, i);
1118 return -EINVAL;
1119 }
1120 }
1121
1122 return 0;
1123}
1124
1125static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1126 const char *buf, size_t len)
1127{
1128 struct dev_dax *dev_dax = to_dev_dax(dev);
1129 struct dax_region *dax_region = dev_dax->region;
1130 unsigned long val, align_save;
1131 ssize_t rc;
1132
1133 rc = kstrtoul(buf, 0, &val);
1134 if (rc)
1135 return -ENXIO;
1136
1137 if (!dax_align_valid(val))
1138 return -EINVAL;
1139
1140 device_lock(dax_region->dev);
1141 if (!dax_region->dev->driver) {
1142 device_unlock(dax_region->dev);
1143 return -ENXIO;
1144 }
1145
1146 device_lock(dev);
1147 if (dev->driver) {
1148 rc = -EBUSY;
1149 goto out_unlock;
1150 }
1151
1152 align_save = dev_dax->align;
1153 dev_dax->align = val;
1154 rc = dev_dax_validate_align(dev_dax);
1155 if (rc)
1156 dev_dax->align = align_save;
1157out_unlock:
1158 device_unlock(dev);
1159 device_unlock(dax_region->dev);
1160 return rc == 0 ? len : rc;
1161}
1162static DEVICE_ATTR_RW(align);
1163
Dan Williams21c75762019-02-20 11:39:36 -08001164static int dev_dax_target_node(struct dev_dax *dev_dax)
1165{
1166 struct dax_region *dax_region = dev_dax->region;
1167
1168 return dax_region->target_node;
1169}
1170
1171static ssize_t target_node_show(struct device *dev,
1172 struct device_attribute *attr, char *buf)
1173{
1174 struct dev_dax *dev_dax = to_dev_dax(dev);
1175
1176 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1177}
1178static DEVICE_ATTR_RO(target_node);
1179
Vishal Verma40cdc602019-06-20 18:40:38 -06001180static ssize_t resource_show(struct device *dev,
1181 struct device_attribute *attr, char *buf)
1182{
1183 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams60e93dc2020-10-13 16:50:39 -07001184 struct dax_region *dax_region = dev_dax->region;
1185 unsigned long long start;
Vishal Verma40cdc602019-06-20 18:40:38 -06001186
Dan Williams60e93dc2020-10-13 16:50:39 -07001187 if (dev_dax->nr_range < 1)
1188 start = dax_region->res.start;
1189 else
1190 start = dev_dax->ranges[0].range.start;
1191
1192 return sprintf(buf, "%#llx\n", start);
Vishal Verma40cdc602019-06-20 18:40:38 -06001193}
Dan Williams153dd282019-11-12 17:12:38 -08001194static DEVICE_ATTR(resource, 0400, resource_show, NULL);
Vishal Verma40cdc602019-06-20 18:40:38 -06001195
Vishal Vermac347bd72019-02-22 16:58:54 -07001196static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1197 char *buf)
1198{
1199 /*
1200 * We only ever expect to handle device-dax instances, i.e. the
1201 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1202 */
1203 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1204}
1205static DEVICE_ATTR_RO(modalias);
1206
Dan Williamscb4dd722019-11-12 17:13:31 -08001207static ssize_t numa_node_show(struct device *dev,
1208 struct device_attribute *attr, char *buf)
1209{
1210 return sprintf(buf, "%d\n", dev_to_node(dev));
1211}
1212static DEVICE_ATTR_RO(numa_node);
1213
Dan Williams21c75762019-02-20 11:39:36 -08001214static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1215{
1216 struct device *dev = container_of(kobj, struct device, kobj);
1217 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001218 struct dax_region *dax_region = dev_dax->region;
Dan Williams21c75762019-02-20 11:39:36 -08001219
1220 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1221 return 0;
Dan Williamscb4dd722019-11-12 17:13:31 -08001222 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1223 return 0;
Joao Martins8490e2e2020-10-13 16:51:06 -07001224 if (a == &dev_attr_mapping.attr && is_static(dax_region))
1225 return 0;
Dan Williams6d821202020-10-13 16:50:55 -07001226 if ((a == &dev_attr_align.attr ||
1227 a == &dev_attr_size.attr) && is_static(dax_region))
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001228 return 0444;
Dan Williams21c75762019-02-20 11:39:36 -08001229 return a->mode;
1230}
1231
Dan Williams51cf7842017-07-12 17:58:21 -07001232static struct attribute *dev_dax_attributes[] = {
Vishal Vermac347bd72019-02-22 16:58:54 -07001233 &dev_attr_modalias.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001234 &dev_attr_size.attr,
Joao Martins8490e2e2020-10-13 16:51:06 -07001235 &dev_attr_mapping.attr,
Dan Williams21c75762019-02-20 11:39:36 -08001236 &dev_attr_target_node.attr,
Dan Williams6d821202020-10-13 16:50:55 -07001237 &dev_attr_align.attr,
Vishal Verma40cdc602019-06-20 18:40:38 -06001238 &dev_attr_resource.attr,
Dan Williamscb4dd722019-11-12 17:13:31 -08001239 &dev_attr_numa_node.attr,
Dan Williams51cf7842017-07-12 17:58:21 -07001240 NULL,
1241};
1242
1243static const struct attribute_group dev_dax_attribute_group = {
1244 .attrs = dev_dax_attributes,
Dan Williams21c75762019-02-20 11:39:36 -08001245 .is_visible = dev_dax_visible,
Dan Williams51cf7842017-07-12 17:58:21 -07001246};
1247
Dan Williams9567da02017-07-12 17:58:21 -07001248static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -07001249 &dev_dax_attribute_group,
1250 NULL,
1251};
Dan Williams51cf7842017-07-12 17:58:21 -07001252
Dan Williams9567da02017-07-12 17:58:21 -07001253static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -07001254{
1255 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -07001256 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -07001257 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -07001258
Dan Williams9567da02017-07-12 17:58:21 -07001259 put_dax(dax_dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001260 free_dev_dax_id(dev_dax);
1261 dax_region_put(dax_region);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001262 kfree(dev_dax->pgmap);
Dan Williams9567da02017-07-12 17:58:21 -07001263 kfree(dev_dax);
1264}
1265
Dan Williams770619a2019-11-12 17:12:23 -08001266static const struct device_type dev_dax_type = {
1267 .release = dev_dax_release,
1268 .groups = dax_attribute_groups,
1269};
1270
Dan Williams174ebec2020-10-13 16:49:38 -07001271struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
Dan Williams9567da02017-07-12 17:58:21 -07001272{
Dan Williams174ebec2020-10-13 16:49:38 -07001273 struct dax_region *dax_region = data->dax_region;
Dan Williams9567da02017-07-12 17:58:21 -07001274 struct device *parent = dax_region->dev;
1275 struct dax_device *dax_dev;
1276 struct dev_dax *dev_dax;
1277 struct inode *inode;
1278 struct device *dev;
Dan Williamsc2f30112020-10-13 16:50:03 -07001279 int rc;
Dan Williams9567da02017-07-12 17:58:21 -07001280
Dan Williams9567da02017-07-12 17:58:21 -07001281 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1282 if (!dev_dax)
1283 return ERR_PTR(-ENOMEM);
1284
Dan Williams0f3da142020-10-13 16:50:13 -07001285 if (is_static(dax_region)) {
1286 if (dev_WARN_ONCE(parent, data->id < 0,
1287 "dynamic id specified to static region\n")) {
1288 rc = -EINVAL;
1289 goto err_id;
1290 }
1291
1292 dev_dax->id = data->id;
1293 } else {
1294 if (dev_WARN_ONCE(parent, data->id >= 0,
1295 "static id specified to dynamic region\n")) {
1296 rc = -EINVAL;
1297 goto err_id;
1298 }
1299
1300 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1301 if (rc < 0)
1302 goto err_id;
1303 dev_dax->id = rc;
1304 }
1305
Dan Williamsc2f30112020-10-13 16:50:03 -07001306 dev_dax->region = dax_region;
1307 dev = &dev_dax->dev;
1308 device_initialize(dev);
Dan Williams0f3da142020-10-13 16:50:13 -07001309 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
Dan Williamsc2f30112020-10-13 16:50:03 -07001310
Dan Williamsfcffb6a2020-10-13 16:50:24 -07001311 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
Dan Williamsc2f30112020-10-13 16:50:03 -07001312 if (rc)
1313 goto err_range;
1314
Dan Williamsf5516ec2020-10-13 16:49:43 -07001315 if (data->pgmap) {
Dan Williamsc2f30112020-10-13 16:50:03 -07001316 dev_WARN_ONCE(parent, !is_static(dax_region),
1317 "custom dev_pagemap requires a static dax_region\n");
1318
Dan Williamsf5516ec2020-10-13 16:49:43 -07001319 dev_dax->pgmap = kmemdup(data->pgmap,
1320 sizeof(struct dev_pagemap), GFP_KERNEL);
Dan Williamsc2f30112020-10-13 16:50:03 -07001321 if (!dev_dax->pgmap) {
1322 rc = -ENOMEM;
Dan Williamsf5516ec2020-10-13 16:49:43 -07001323 goto err_pgmap;
Dan Williamsc2f30112020-10-13 16:50:03 -07001324 }
Dan Williamsf5516ec2020-10-13 16:49:43 -07001325 }
Dan Williams89ec9f22018-10-29 15:52:42 -07001326
Dan Williams9567da02017-07-12 17:58:21 -07001327 /*
1328 * No 'host' or dax_operations since there is no access to this
1329 * device outside of mmap of the resulting character device.
1330 */
Pankaj Guptafefc1d92019-07-05 19:33:24 +05301331 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001332 if (IS_ERR(dax_dev)) {
1333 rc = PTR_ERR(dax_dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -07001334 goto err_alloc_dax;
Vivek Goyal4e4ced92020-04-01 12:11:25 -04001335 }
Dan Williams9567da02017-07-12 17:58:21 -07001336
1337 /* a device_dax instance is dead while the driver is not attached */
1338 kill_dax(dax_dev);
1339
Dan Williams9567da02017-07-12 17:58:21 -07001340 dev_dax->dax_dev = dax_dev;
Dan Williams8fc5c732018-11-09 12:43:07 -08001341 dev_dax->target_node = dax_region->target_node;
Joao Martins33cf94d2020-10-13 16:50:50 -07001342 dev_dax->align = dax_region->align;
Dan Williams0b07ce82020-10-13 16:50:45 -07001343 ida_init(&dev_dax->ida);
Dan Williams9567da02017-07-12 17:58:21 -07001344 kref_get(&dax_region->kref);
1345
1346 inode = dax_inode(dax_dev);
1347 dev->devt = inode->i_rdev;
Dan Williams174ebec2020-10-13 16:49:38 -07001348 if (data->subsys == DEV_DAX_BUS)
Dan Williams730926c2017-07-16 13:51:53 -07001349 dev->bus = &dax_bus_type;
1350 else
1351 dev->class = dax_class;
Dan Williams9567da02017-07-12 17:58:21 -07001352 dev->parent = parent;
Dan Williams770619a2019-11-12 17:12:23 -08001353 dev->type = &dev_dax_type;
Dan Williams9567da02017-07-12 17:58:21 -07001354
1355 rc = device_add(dev);
1356 if (rc) {
1357 kill_dev_dax(dev_dax);
1358 put_device(dev);
1359 return ERR_PTR(rc);
1360 }
1361
1362 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1363 if (rc)
1364 return ERR_PTR(rc);
1365
Dan Williams0b07ce82020-10-13 16:50:45 -07001366 /* register mapping device for the initial allocation range */
1367 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1368 rc = devm_register_dax_mapping(dev_dax, 0);
1369 if (rc)
1370 return ERR_PTR(rc);
1371 }
1372
Dan Williams9567da02017-07-12 17:58:21 -07001373 return dev_dax;
Dan Williamsc2f30112020-10-13 16:50:03 -07001374
Dan Williamsf5516ec2020-10-13 16:49:43 -07001375err_alloc_dax:
1376 kfree(dev_dax->pgmap);
1377err_pgmap:
Dan Williams60e93dc2020-10-13 16:50:39 -07001378 free_dev_dax_ranges(dev_dax);
Dan Williamsc2f30112020-10-13 16:50:03 -07001379err_range:
Dan Williams0f3da142020-10-13 16:50:13 -07001380 free_dev_dax_id(dev_dax);
1381err_id:
Dan Williams9567da02017-07-12 17:58:21 -07001382 kfree(dev_dax);
1383
1384 return ERR_PTR(rc);
1385}
Dan Williams174ebec2020-10-13 16:49:38 -07001386EXPORT_SYMBOL_GPL(devm_create_dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -07001387
Dan Williamsd2007812018-11-07 15:31:23 -08001388static int match_always_count;
1389
1390int __dax_driver_register(struct dax_device_driver *dax_drv,
Dan Williams9567da02017-07-12 17:58:21 -07001391 struct module *module, const char *mod_name)
1392{
Dan Williamsd2007812018-11-07 15:31:23 -08001393 struct device_driver *drv = &dax_drv->drv;
1394 int rc = 0;
1395
Uwe Kleine-König5b8e64f2021-02-05 23:28:38 +01001396 /*
1397 * dax_bus_probe() calls dax_drv->probe() unconditionally.
1398 * So better be safe than sorry and ensure it is provided.
1399 */
1400 if (!dax_drv->probe)
1401 return -EINVAL;
1402
Dan Williamsd2007812018-11-07 15:31:23 -08001403 INIT_LIST_HEAD(&dax_drv->ids);
Dan Williams9567da02017-07-12 17:58:21 -07001404 drv->owner = module;
1405 drv->name = mod_name;
1406 drv->mod_name = mod_name;
1407 drv->bus = &dax_bus_type;
Dan Williamsd2007812018-11-07 15:31:23 -08001408
1409 /* there can only be one default driver */
1410 mutex_lock(&dax_bus_lock);
1411 match_always_count += dax_drv->match_always;
1412 if (match_always_count > 1) {
1413 match_always_count--;
1414 WARN_ON(1);
1415 rc = -EINVAL;
1416 }
1417 mutex_unlock(&dax_bus_lock);
1418 if (rc)
1419 return rc;
Uwe Kleine-Könige307bf12021-02-05 23:28:40 +01001420
1421 rc = driver_register(drv);
1422 if (rc && dax_drv->match_always) {
1423 mutex_lock(&dax_bus_lock);
1424 match_always_count -= dax_drv->match_always;
1425 mutex_unlock(&dax_bus_lock);
1426 }
1427
1428 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001429}
1430EXPORT_SYMBOL_GPL(__dax_driver_register);
1431
Dan Williamsd2007812018-11-07 15:31:23 -08001432void dax_driver_unregister(struct dax_device_driver *dax_drv)
1433{
Dan Williams730926c2017-07-16 13:51:53 -07001434 struct device_driver *drv = &dax_drv->drv;
Dan Williamsd2007812018-11-07 15:31:23 -08001435 struct dax_id *dax_id, *_id;
1436
1437 mutex_lock(&dax_bus_lock);
1438 match_always_count -= dax_drv->match_always;
1439 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1440 list_del(&dax_id->list);
1441 kfree(dax_id);
1442 }
1443 mutex_unlock(&dax_bus_lock);
Dan Williams730926c2017-07-16 13:51:53 -07001444 driver_unregister(drv);
Dan Williamsd2007812018-11-07 15:31:23 -08001445}
1446EXPORT_SYMBOL_GPL(dax_driver_unregister);
1447
Dan Williams9567da02017-07-12 17:58:21 -07001448int __init dax_bus_init(void)
1449{
Dan Williams730926c2017-07-16 13:51:53 -07001450 int rc;
1451
1452 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
1453 dax_class = class_create(THIS_MODULE, "dax");
1454 if (IS_ERR(dax_class))
1455 return PTR_ERR(dax_class);
1456 }
1457
1458 rc = bus_register(&dax_bus_type);
1459 if (rc)
1460 class_destroy(dax_class);
1461 return rc;
Dan Williams9567da02017-07-12 17:58:21 -07001462}
1463
1464void __exit dax_bus_exit(void)
1465{
1466 bus_unregister(&dax_bus_type);
Dan Williams730926c2017-07-16 13:51:53 -07001467 class_destroy(dax_class);
Dan Williams9567da02017-07-12 17:58:21 -07001468}