Commit | Line | Data |
---|---|---|
c61959ec AW |
1 | /* |
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> | |
ffd78f00 | 15 | #include <linux/slab.h> |
c61959ec AW |
16 | |
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 | */ | |
21 | static struct attribute *devices_attr[] = { | |
22 | NULL, | |
23 | }; | |
24 | ||
25 | static const struct attribute_group iommu_devices_attr_group = { | |
26 | .name = "devices", | |
27 | .attrs = devices_attr, | |
28 | }; | |
29 | ||
30 | static const struct attribute_group *iommu_dev_groups[] = { | |
31 | &iommu_devices_attr_group, | |
32 | NULL, | |
33 | }; | |
34 | ||
35 | static void iommu_release_device(struct device *dev) | |
36 | { | |
37 | kfree(dev); | |
38 | } | |
39 | ||
40 | static struct class iommu_class = { | |
41 | .name = "iommu", | |
42 | .dev_release = iommu_release_device, | |
43 | .dev_groups = iommu_dev_groups, | |
44 | }; | |
45 | ||
46 | static int __init iommu_dev_init(void) | |
47 | { | |
48 | return class_register(&iommu_class); | |
49 | } | |
50 | postcore_initcall(iommu_dev_init); | |
51 | ||
52 | /* | |
39ab9555 JR |
53 | * 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. | |
c61959ec | 56 | */ |
39ab9555 JR |
57 | int iommu_device_sysfs_add(struct iommu_device *iommu, |
58 | struct device *parent, | |
59 | const struct attribute_group **groups, | |
60 | const char *fmt, ...) | |
c61959ec | 61 | { |
c61959ec AW |
62 | va_list vargs; |
63 | int ret; | |
64 | ||
2926a2aa JR |
65 | iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL); |
66 | if (!iommu->dev) | |
67 | return -ENOMEM; | |
c61959ec | 68 | |
2926a2aa JR |
69 | device_initialize(iommu->dev); |
70 | ||
71 | iommu->dev->class = &iommu_class; | |
72 | iommu->dev->parent = parent; | |
73 | iommu->dev->groups = groups; | |
c61959ec AW |
74 | |
75 | va_start(vargs, fmt); | |
2926a2aa | 76 | ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs); |
c61959ec AW |
77 | va_end(vargs); |
78 | if (ret) | |
79 | goto error; | |
80 | ||
2926a2aa | 81 | ret = device_add(iommu->dev); |
c61959ec AW |
82 | if (ret) |
83 | goto error; | |
84 | ||
2926a2aa JR |
85 | dev_set_drvdata(iommu->dev, iommu); |
86 | ||
39ab9555 | 87 | return 0; |
c61959ec AW |
88 | |
89 | error: | |
2926a2aa | 90 | put_device(iommu->dev); |
39ab9555 | 91 | return ret; |
c61959ec AW |
92 | } |
93 | ||
39ab9555 | 94 | void iommu_device_sysfs_remove(struct iommu_device *iommu) |
c61959ec | 95 | { |
2926a2aa JR |
96 | dev_set_drvdata(iommu->dev, NULL); |
97 | device_unregister(iommu->dev); | |
98 | iommu->dev = NULL; | |
c61959ec | 99 | } |
c61959ec AW |
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 | */ | |
e3d10af1 | 106 | int iommu_device_link(struct iommu_device *iommu, struct device *link) |
c61959ec AW |
107 | { |
108 | int ret; | |
109 | ||
e3d10af1 | 110 | if (!iommu || IS_ERR(iommu)) |
c61959ec AW |
111 | return -ENODEV; |
112 | ||
2926a2aa | 113 | ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices", |
c61959ec AW |
114 | &link->kobj, dev_name(link)); |
115 | if (ret) | |
116 | return ret; | |
117 | ||
2926a2aa | 118 | ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu"); |
c61959ec | 119 | if (ret) |
2926a2aa | 120 | sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", |
c61959ec AW |
121 | dev_name(link)); |
122 | ||
123 | return ret; | |
124 | } | |
125 | ||
e3d10af1 | 126 | void iommu_device_unlink(struct iommu_device *iommu, struct device *link) |
c61959ec | 127 | { |
e3d10af1 | 128 | if (!iommu || IS_ERR(iommu)) |
c61959ec AW |
129 | return; |
130 | ||
131 | sysfs_remove_link(&link->kobj, "iommu"); | |
2926a2aa | 132 | sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link)); |
c61959ec | 133 | } |