blob: c298330ba2b7590984ca8cb3397a753acd4e990b [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>
Paul Gortmakerc1af7b42018-12-01 14:19:09 -050014#include <linux/init.h>
Joerg Roedelffd78f02014-07-07 12:01:21 +020015#include <linux/slab.h>
Alex Williamsonc61959e2014-06-12 16:12:24 -060016
17/*
18 * We provide a common class "devices" group which initially has no attributes.
19 * As devices are added to the IOMMU, we'll add links to the group.
20 */
21static struct attribute *devices_attr[] = {
22 NULL,
23};
24
25static const struct attribute_group iommu_devices_attr_group = {
26 .name = "devices",
27 .attrs = devices_attr,
28};
29
30static const struct attribute_group *iommu_dev_groups[] = {
31 &iommu_devices_attr_group,
32 NULL,
33};
34
35static void iommu_release_device(struct device *dev)
36{
37 kfree(dev);
38}
39
40static struct class iommu_class = {
41 .name = "iommu",
42 .dev_release = iommu_release_device,
43 .dev_groups = iommu_dev_groups,
44};
45
46static int __init iommu_dev_init(void)
47{
48 return class_register(&iommu_class);
49}
50postcore_initcall(iommu_dev_init);
51
52/*
Joerg Roedel39ab9552017-02-01 16:56:46 +010053 * Init the struct device for the IOMMU. IOMMU specific attributes can
54 * be provided as an attribute group, allowing a unique namespace per
55 * IOMMU type.
Alex Williamsonc61959e2014-06-12 16:12:24 -060056 */
Joerg Roedel39ab9552017-02-01 16:56:46 +010057int iommu_device_sysfs_add(struct iommu_device *iommu,
58 struct device *parent,
59 const struct attribute_group **groups,
60 const char *fmt, ...)
Alex Williamsonc61959e2014-06-12 16:12:24 -060061{
Alex Williamsonc61959e2014-06-12 16:12:24 -060062 va_list vargs;
63 int ret;
64
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020065 iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
66 if (!iommu->dev)
67 return -ENOMEM;
Alex Williamsonc61959e2014-06-12 16:12:24 -060068
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020069 device_initialize(iommu->dev);
70
71 iommu->dev->class = &iommu_class;
72 iommu->dev->parent = parent;
73 iommu->dev->groups = groups;
Alex Williamsonc61959e2014-06-12 16:12:24 -060074
75 va_start(vargs, fmt);
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020076 ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
Alex Williamsonc61959e2014-06-12 16:12:24 -060077 va_end(vargs);
78 if (ret)
79 goto error;
80
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020081 ret = device_add(iommu->dev);
Alex Williamsonc61959e2014-06-12 16:12:24 -060082 if (ret)
83 goto error;
84
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020085 dev_set_drvdata(iommu->dev, iommu);
86
Joerg Roedel39ab9552017-02-01 16:56:46 +010087 return 0;
Alex Williamsonc61959e2014-06-12 16:12:24 -060088
89error:
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020090 put_device(iommu->dev);
Joerg Roedel39ab9552017-02-01 16:56:46 +010091 return ret;
Alex Williamsonc61959e2014-06-12 16:12:24 -060092}
93
Joerg Roedel39ab9552017-02-01 16:56:46 +010094void iommu_device_sysfs_remove(struct iommu_device *iommu)
Alex Williamsonc61959e2014-06-12 16:12:24 -060095{
Joerg Roedel2926a2aa2017-08-14 17:19:26 +020096 dev_set_drvdata(iommu->dev, NULL);
97 device_unregister(iommu->dev);
98 iommu->dev = NULL;
Alex Williamsonc61959e2014-06-12 16:12:24 -060099}
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}
125
Joerg Roedele3d10af2017-02-01 17:23:22 +0100126void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
Alex Williamsonc61959e2014-06-12 16:12:24 -0600127{
Joerg Roedele3d10af2017-02-01 17:23:22 +0100128 if (!iommu || IS_ERR(iommu))
Alex Williamsonc61959e2014-06-12 16:12:24 -0600129 return;
130
131 sysfs_remove_link(&link->kobj, "iommu");
Joerg Roedel2926a2aa2017-08-14 17:19:26 +0200132 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
Alex Williamsonc61959e2014-06-12 16:12:24 -0600133}