^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * IOMMU sysfs class support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Alex Williamson <alex.williamson@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/iommu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * We provide a common class "devices" group which initially has no attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * As devices are added to the IOMMU, we'll add links to the group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static struct attribute *devices_attr[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static const struct attribute_group devices_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .name = "devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .attrs = devices_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static const struct attribute_group *dev_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) &devices_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct class iommu_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .name = "iommu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .dev_release = release_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .dev_groups = dev_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int __init iommu_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return class_register(&iommu_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) postcore_initcall(iommu_dev_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Init the struct device for the IOMMU. IOMMU specific attributes can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * be provided as an attribute group, allowing a unique namespace per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * IOMMU type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int iommu_device_sysfs_add(struct iommu_device *iommu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const struct attribute_group **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) va_list vargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!iommu->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) device_initialize(iommu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) iommu->dev->class = &iommu_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) iommu->dev->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) iommu->dev->groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) va_start(vargs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) va_end(vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ret = device_add(iommu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dev_set_drvdata(iommu->dev, iommu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) put_device(iommu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void iommu_device_sysfs_remove(struct iommu_device *iommu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev_set_drvdata(iommu->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) device_unregister(iommu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) iommu->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) EXPORT_SYMBOL_GPL(iommu_device_sysfs_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * IOMMU drivers can indicate a device is managed by a given IOMMU using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * this interface. A link to the device will be created in the "devices"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * directory of the IOMMU device in sysfs and an "iommu" link will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * created under the linked device, pointing back at the IOMMU device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int iommu_device_link(struct iommu_device *iommu, struct device *link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!iommu || IS_ERR(iommu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) &link->kobj, dev_name(link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) dev_name(link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) EXPORT_SYMBOL_GPL(iommu_device_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!iommu || IS_ERR(iommu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) sysfs_remove_link(&link->kobj, "iommu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) EXPORT_SYMBOL_GPL(iommu_device_unlink);