blob: 99869217fbec7d862af4bba0e0a6facbe34a7168 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alex Williamsonc61959e2014-06-12 16:12:24 -06002/*
3 * IOMMU sysfs class support
4 *
5 * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
Alex Williamsonc61959e2014-06-12 16:12:24 -06007 */
8
9#include <linux/device.h>
10#include <linux/iommu.h>
Paul Gortmakerc1af7b42018-12-01 14:19:09 -050011#include <linux/init.h>
Joerg Roedelffd78f02014-07-07 12:01:21 +020012#include <linux/slab.h>
Alex Williamsonc61959e2014-06-12 16:12:24 -060013
14/*
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.
17 */
18static struct attribute *devices_attr[] = {
19 NULL,
20};
21
Joerg Roedel6954cf92018-12-05 13:57:43 +010022static const struct attribute_group devices_attr_group = {
Alex Williamsonc61959e2014-06-12 16:12:24 -060023 .name = "devices",
24 .attrs = devices_attr,
25};
26
Joerg Roedel6954cf92018-12-05 13:57:43 +010027static const struct attribute_group *dev_groups[] = {
28 &devices_attr_group,
Alex Williamsonc61959e2014-06-12 16:12:24 -060029 NULL,
30};
31
Joerg Roedel6954cf92018-12-05 13:57:43 +010032static void release_device(struct device *dev)
Alex Williamsonc61959e2014-06-12 16:12:24 -060033{
34 kfree(dev);
35}
36
37static struct class iommu_class = {
38 .name = "iommu",
Joerg Roedel6954cf92018-12-05 13:57:43 +010039 .dev_release = release_device,
40 .dev_groups = dev_groups,
Alex Williamsonc61959e2014-06-12 16:12:24 -060041};
42
43static int __init iommu_dev_init(void)
44{
45 return class_register(&iommu_class);
46}
47postcore_initcall(iommu_dev_init);
48
49/*
Joerg Roedel39ab9552017-02-01 16:56:46 +010050 * 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.
Alex Williamsonc61959e2014-06-12 16:12:24 -060053 */
Joerg Roedel39ab9552017-02-01 16:56:46 +010054int iommu_device_sysfs_add(struct iommu_device *iommu,
55 struct device *parent,
56 const struct attribute_group **groups,
57 const char *fmt, ...)
Alex Williamsonc61959e2014-06-12 16:12:24 -060058{
Alex Williamsonc61959e2014-06-12 16:12:24 -060059 va_list vargs;
60 int ret;
61
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020062 iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
63 if (!iommu->dev)
64 return -ENOMEM;
Alex Williamsonc61959e2014-06-12 16:12:24 -060065
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020066 device_initialize(iommu->dev);
67
68 iommu->dev->class = &iommu_class;
69 iommu->dev->parent = parent;
70 iommu->dev->groups = groups;
Alex Williamsonc61959e2014-06-12 16:12:24 -060071
72 va_start(vargs, fmt);
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020073 ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
Alex Williamsonc61959e2014-06-12 16:12:24 -060074 va_end(vargs);
75 if (ret)
76 goto error;
77
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020078 ret = device_add(iommu->dev);
Alex Williamsonc61959e2014-06-12 16:12:24 -060079 if (ret)
80 goto error;
81
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020082 dev_set_drvdata(iommu->dev, iommu);
83
Joerg Roedel39ab9552017-02-01 16:56:46 +010084 return 0;
Alex Williamsonc61959e2014-06-12 16:12:24 -060085
86error:
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020087 put_device(iommu->dev);
Joerg Roedel39ab9552017-02-01 16:56:46 +010088 return ret;
Alex Williamsonc61959e2014-06-12 16:12:24 -060089}
Will Deacona7ba5c32019-12-19 12:03:37 +000090EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
Alex Williamsonc61959e2014-06-12 16:12:24 -060091
Joerg Roedel39ab9552017-02-01 16:56:46 +010092void iommu_device_sysfs_remove(struct iommu_device *iommu)
Alex Williamsonc61959e2014-06-12 16:12:24 -060093{
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020094 dev_set_drvdata(iommu->dev, NULL);
95 device_unregister(iommu->dev);
96 iommu->dev = NULL;
Alex Williamsonc61959e2014-06-12 16:12:24 -060097}
Will Deacona7ba5c32019-12-19 12:03:37 +000098EXPORT_SYMBOL_GPL(iommu_device_sysfs_remove);
99
Alex Williamsonc61959e2014-06-12 16:12:24 -0600100/*
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 */
Joerg Roedele3d10af2017-02-01 17:23:22 +0100106int iommu_device_link(struct iommu_device *iommu, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600107{
108 int ret;
109
Joerg Roedele3d10af2017-02-01 17:23:22 +0100110 if (!iommu || IS_ERR(iommu))
Alex Williamsonc61959e2014-06-12 16:12:24 -0600111 return -ENODEV;
112
Joerg Roedel2926a2aa2017-08-14 17:19:26 +0200113 ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
Alex Williamsonc61959e2014-06-12 16:12:24 -0600114 &link->kobj, dev_name(link));
115 if (ret)
116 return ret;
117
Joerg Roedel2926a2aa2017-08-14 17:19:26 +0200118 ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
Alex Williamsonc61959e2014-06-12 16:12:24 -0600119 if (ret)
Joerg Roedel2926a2aa2017-08-14 17:19:26 +0200120 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
Alex Williamsonc61959e2014-06-12 16:12:24 -0600121 dev_name(link));
122
123 return ret;
124}
Will Deacona7ba5c32019-12-19 12:03:37 +0000125EXPORT_SYMBOL_GPL(iommu_device_link);
Alex Williamsonc61959e2014-06-12 16:12:24 -0600126
Joerg Roedele3d10af2017-02-01 17:23:22 +0100127void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600128{
Joerg Roedele3d10af2017-02-01 17:23:22 +0100129 if (!iommu || IS_ERR(iommu))
Alex Williamsonc61959e2014-06-12 16:12:24 -0600130 return;
131
132 sysfs_remove_link(&link->kobj, "iommu");
Joerg Roedel2926a2aa2017-08-14 17:19:26 +0200133 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
Alex Williamsonc61959e2014-06-12 16:12:24 -0600134}
Will Deacona7ba5c32019-12-19 12:03:37 +0000135EXPORT_SYMBOL_GPL(iommu_device_unlink);