Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iommu / iommu-sysfs.c
blob99869217fbec7d862af4bba0e0a6facbe34a7168
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * IOMMU sysfs class support
5 * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
7 */
9 #include <linux/device.h>
10 #include <linux/iommu.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
15 * We provide a common class "devices" group which initially has no attributes.
16 * As devices are added to the IOMMU, we'll add links to the group.
18 static struct attribute *devices_attr[] = {
19 NULL,
22 static const struct attribute_group devices_attr_group = {
23 .name = "devices",
24 .attrs = devices_attr,
27 static const struct attribute_group *dev_groups[] = {
28 &devices_attr_group,
29 NULL,
32 static void release_device(struct device *dev)
34 kfree(dev);
37 static struct class iommu_class = {
38 .name = "iommu",
39 .dev_release = release_device,
40 .dev_groups = dev_groups,
43 static int __init iommu_dev_init(void)
45 return class_register(&iommu_class);
47 postcore_initcall(iommu_dev_init);
50 * Init the struct device for the IOMMU. IOMMU specific attributes can
51 * be provided as an attribute group, allowing a unique namespace per
52 * IOMMU type.
54 int iommu_device_sysfs_add(struct iommu_device *iommu,
55 struct device *parent,
56 const struct attribute_group **groups,
57 const char *fmt, ...)
59 va_list vargs;
60 int ret;
62 iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
63 if (!iommu->dev)
64 return -ENOMEM;
66 device_initialize(iommu->dev);
68 iommu->dev->class = &iommu_class;
69 iommu->dev->parent = parent;
70 iommu->dev->groups = groups;
72 va_start(vargs, fmt);
73 ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
74 va_end(vargs);
75 if (ret)
76 goto error;
78 ret = device_add(iommu->dev);
79 if (ret)
80 goto error;
82 dev_set_drvdata(iommu->dev, iommu);
84 return 0;
86 error:
87 put_device(iommu->dev);
88 return ret;
90 EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
92 void iommu_device_sysfs_remove(struct iommu_device *iommu)
94 dev_set_drvdata(iommu->dev, NULL);
95 device_unregister(iommu->dev);
96 iommu->dev = NULL;
98 EXPORT_SYMBOL_GPL(iommu_device_sysfs_remove);
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.
106 int iommu_device_link(struct iommu_device *iommu, struct device *link)
108 int ret;
110 if (!iommu || IS_ERR(iommu))
111 return -ENODEV;
113 ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
114 &link->kobj, dev_name(link));
115 if (ret)
116 return ret;
118 ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
119 if (ret)
120 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
121 dev_name(link));
123 return ret;
125 EXPORT_SYMBOL_GPL(iommu_device_link);
127 void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
129 if (!iommu || IS_ERR(iommu))
130 return;
132 sysfs_remove_link(&link->kobj, "iommu");
133 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
135 EXPORT_SYMBOL_GPL(iommu_device_unlink);