Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 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> |
Joerg Roedel | ffd78f0 | 2014-07-07 12:01:21 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 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 | /* |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 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. |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 56 | */ |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 57 | int iommu_device_sysfs_add(struct iommu_device *iommu, |
| 58 | struct device *parent, |
| 59 | const struct attribute_group **groups, |
| 60 | const char *fmt, ...) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 61 | { |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 62 | va_list vargs; |
| 63 | int ret; |
| 64 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 65 | device_initialize(&iommu->dev); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 66 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 67 | iommu->dev.class = &iommu_class; |
| 68 | iommu->dev.parent = parent; |
| 69 | iommu->dev.groups = groups; |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 70 | |
| 71 | va_start(vargs, fmt); |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 72 | ret = kobject_set_name_vargs(&iommu->dev.kobj, fmt, vargs); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 73 | va_end(vargs); |
| 74 | if (ret) |
| 75 | goto error; |
| 76 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 77 | ret = device_add(&iommu->dev); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 78 | if (ret) |
| 79 | goto error; |
| 80 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 81 | return 0; |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 82 | |
| 83 | error: |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 84 | put_device(&iommu->dev); |
| 85 | return ret; |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 88 | void iommu_device_sysfs_remove(struct iommu_device *iommu) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 89 | { |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 90 | device_unregister(&iommu->dev); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 91 | } |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 92 | /* |
| 93 | * IOMMU drivers can indicate a device is managed by a given IOMMU using |
| 94 | * this interface. A link to the device will be created in the "devices" |
| 95 | * directory of the IOMMU device in sysfs and an "iommu" link will be |
| 96 | * created under the linked device, pointing back at the IOMMU device. |
| 97 | */ |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 98 | int iommu_device_link(struct iommu_device *iommu, struct device *link) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 99 | { |
| 100 | int ret; |
| 101 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 102 | if (!iommu || IS_ERR(iommu)) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 103 | return -ENODEV; |
| 104 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 105 | ret = sysfs_add_link_to_group(&iommu->dev.kobj, "devices", |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 106 | &link->kobj, dev_name(link)); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 110 | ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev.kobj, "iommu"); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 111 | if (ret) |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 112 | sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 113 | dev_name(link)); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 118 | void iommu_device_unlink(struct iommu_device *iommu, struct device *link) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 119 | { |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 120 | if (!iommu || IS_ERR(iommu)) |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 121 | return; |
| 122 | |
| 123 | sysfs_remove_link(&link->kobj, "iommu"); |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 124 | sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", dev_name(link)); |
Alex Williamson | c61959e | 2014-06-12 16:12:24 -0600 | [diff] [blame] | 125 | } |