blob: d6939234a009fdbe852ff974e6d6b11816c9c998 [file] [log] [blame]
Alex Williamsonc61959e2014-06-12 16:12:24 -06001/*
2 * IOMMU sysfs class support
3 *
4 * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/device.h>
13#include <linux/iommu.h>
14#include <linux/module.h>
15
16/*
17 * We provide a common class "devices" group which initially has no attributes.
18 * As devices are added to the IOMMU, we'll add links to the group.
19 */
20static struct attribute *devices_attr[] = {
21 NULL,
22};
23
24static const struct attribute_group iommu_devices_attr_group = {
25 .name = "devices",
26 .attrs = devices_attr,
27};
28
29static const struct attribute_group *iommu_dev_groups[] = {
30 &iommu_devices_attr_group,
31 NULL,
32};
33
34static void iommu_release_device(struct device *dev)
35{
36 kfree(dev);
37}
38
39static struct class iommu_class = {
40 .name = "iommu",
41 .dev_release = iommu_release_device,
42 .dev_groups = iommu_dev_groups,
43};
44
45static int __init iommu_dev_init(void)
46{
47 return class_register(&iommu_class);
48}
49postcore_initcall(iommu_dev_init);
50
51/*
52 * Create an IOMMU device and return a pointer to it. IOMMU specific
53 * attributes can be provided as an attribute group, allowing a unique
54 * namespace per IOMMU type.
55 */
56struct device *iommu_device_create(struct device *parent, void *drvdata,
57 const struct attribute_group **groups,
58 const char *fmt, ...)
59{
60 struct device *dev;
61 va_list vargs;
62 int ret;
63
64 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
65 if (!dev)
66 return ERR_PTR(-ENOMEM);
67
68 device_initialize(dev);
69
70 dev->class = &iommu_class;
71 dev->parent = parent;
72 dev->groups = groups;
73 dev_set_drvdata(dev, drvdata);
74
75 va_start(vargs, fmt);
76 ret = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
77 va_end(vargs);
78 if (ret)
79 goto error;
80
81 ret = device_add(dev);
82 if (ret)
83 goto error;
84
85 return dev;
86
87error:
88 put_device(dev);
89 return ERR_PTR(ret);
90}
91
92void iommu_device_destroy(struct device *dev)
93{
94 if (!dev || IS_ERR(dev))
95 return;
96
97 device_unregister(dev);
98}
99
100/*
101 * IOMMU drivers can indicate a device is managed by a given IOMMU using
102 * this interface. A link to the device will be created in the "devices"
103 * directory of the IOMMU device in sysfs and an "iommu" link will be
104 * created under the linked device, pointing back at the IOMMU device.
105 */
106int iommu_device_link(struct device *dev, struct device *link)
107{
108 int ret;
109
110 if (!dev || IS_ERR(dev))
111 return -ENODEV;
112
113 ret = sysfs_add_link_to_group(&dev->kobj, "devices",
114 &link->kobj, dev_name(link));
115 if (ret)
116 return ret;
117
118 ret = sysfs_create_link_nowarn(&link->kobj, &dev->kobj, "iommu");
119 if (ret)
120 sysfs_remove_link_from_group(&dev->kobj, "devices",
121 dev_name(link));
122
123 return ret;
124}
125
126void iommu_device_unlink(struct device *dev, struct device *link)
127{
128 if (!dev || IS_ERR(dev))
129 return;
130
131 sysfs_remove_link(&link->kobj, "iommu");
132 sysfs_remove_link_from_group(&dev->kobj, "devices", dev_name(link));
133}