Linux Audio

Check our new training course

Linux kernel drivers training

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