2 * IOMMU sysfs class support
4 * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
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.
12 #include <linux/device.h>
13 #include <linux/iommu.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
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.
21 static struct attribute *devices_attr[] = {
25 static const struct attribute_group devices_attr_group = {
27 .attrs = devices_attr,
30 static const struct attribute_group *dev_groups[] = {
35 static void release_device(struct device *dev)
40 static struct class iommu_class = {
42 .dev_release = release_device,
43 .dev_groups = dev_groups,
46 static int __init iommu_dev_init(void)
48 return class_register(&iommu_class);
50 postcore_initcall(iommu_dev_init);
53 * Init the struct device for the IOMMU. IOMMU specific attributes can
54 * be provided as an attribute group, allowing a unique namespace per
57 int iommu_device_sysfs_add(struct iommu_device *iommu,
58 struct device *parent,
59 const struct attribute_group **groups,
65 iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
69 device_initialize(iommu->dev);
71 iommu->dev->class = &iommu_class;
72 iommu->dev->parent = parent;
73 iommu->dev->groups = groups;
76 ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
81 ret = device_add(iommu->dev);
85 dev_set_drvdata(iommu->dev, iommu);
90 put_device(iommu->dev);
94 void iommu_device_sysfs_remove(struct iommu_device *iommu)
96 dev_set_drvdata(iommu->dev, NULL);
97 device_unregister(iommu->dev);
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)
110 if (!iommu || IS_ERR(iommu))
113 ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
114 &link->kobj, dev_name(link));
118 ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
120 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
126 void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
128 if (!iommu || IS_ERR(iommu))
131 sysfs_remove_link(&link->kobj, "iommu");
132 sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));