1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
10 #include <linux/device.h>
11 #include <linux/iommu.h>
12 #include <linux/mdev.h>
14 #include "mdev_private.h"
16 static int mdev_attach_iommu(struct mdev_device *mdev)
19 struct iommu_group *group;
21 group = iommu_group_alloc();
23 return PTR_ERR(group);
25 ret = iommu_group_add_device(group, &mdev->dev);
27 dev_info(&mdev->dev, "MDEV: group_id = %d\n",
28 iommu_group_id(group));
30 iommu_group_put(group);
34 static void mdev_detach_iommu(struct mdev_device *mdev)
36 iommu_group_remove_device(&mdev->dev);
37 dev_info(&mdev->dev, "MDEV: detaching iommu\n");
40 static int mdev_probe(struct device *dev)
42 struct mdev_driver *drv =
43 container_of(dev->driver, struct mdev_driver, driver);
44 struct mdev_device *mdev = to_mdev_device(dev);
47 ret = mdev_attach_iommu(mdev);
52 ret = drv->probe(mdev);
54 mdev_detach_iommu(mdev);
60 static int mdev_remove(struct device *dev)
62 struct mdev_driver *drv =
63 container_of(dev->driver, struct mdev_driver, driver);
64 struct mdev_device *mdev = to_mdev_device(dev);
69 mdev_detach_iommu(mdev);
74 static int mdev_match(struct device *dev, struct device_driver *drv)
77 * No drivers automatically match. Drivers are only bound by explicit
78 * device_driver_attach()
83 struct bus_type mdev_bus_type = {
86 .remove = mdev_remove,
89 EXPORT_SYMBOL_GPL(mdev_bus_type);
92 * mdev_register_driver - register a new MDEV driver
93 * @drv: the driver to register
95 * Returns a negative value on error, otherwise 0.
97 int mdev_register_driver(struct mdev_driver *drv)
99 /* initialize common driver fields */
100 drv->driver.bus = &mdev_bus_type;
102 /* register with core */
103 return driver_register(&drv->driver);
105 EXPORT_SYMBOL(mdev_register_driver);
108 * mdev_unregister_driver - unregister MDEV driver
109 * @drv: the driver to unregister
111 void mdev_unregister_driver(struct mdev_driver *drv)
113 driver_unregister(&drv->driver);
115 EXPORT_SYMBOL(mdev_unregister_driver);
117 int mdev_bus_register(void)
119 return bus_register(&mdev_bus_type);
122 void mdev_bus_unregister(void)
124 bus_unregister(&mdev_bus_type);