4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5 * Author: Neo Jia <cjia@nvidia.com>
6 * Kirti Wankhede <kwankhede@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/iommu.h>
15 #include <linux/mdev.h>
17 #include "mdev_private.h"
19 static int mdev_attach_iommu(struct mdev_device *mdev)
22 struct iommu_group *group;
24 group = iommu_group_alloc();
26 return PTR_ERR(group);
28 ret = iommu_group_add_device(group, &mdev->dev);
30 dev_info(&mdev->dev, "MDEV: group_id = %d\n",
31 iommu_group_id(group));
33 iommu_group_put(group);
37 static void mdev_detach_iommu(struct mdev_device *mdev)
39 iommu_group_remove_device(&mdev->dev);
40 dev_info(&mdev->dev, "MDEV: detaching iommu\n");
43 static int mdev_probe(struct device *dev)
45 struct mdev_driver *drv = to_mdev_driver(dev->driver);
46 struct mdev_device *mdev = to_mdev_device(dev);
49 ret = mdev_attach_iommu(mdev);
53 if (drv && drv->probe) {
54 ret = drv->probe(dev);
56 mdev_detach_iommu(mdev);
62 static int mdev_remove(struct device *dev)
64 struct mdev_driver *drv = to_mdev_driver(dev->driver);
65 struct mdev_device *mdev = to_mdev_device(dev);
67 if (drv && drv->remove)
70 mdev_detach_iommu(mdev);
75 struct bus_type mdev_bus_type = {
78 .remove = mdev_remove,
80 EXPORT_SYMBOL_GPL(mdev_bus_type);
83 * mdev_register_driver - register a new MDEV driver
84 * @drv: the driver to register
85 * @owner: module owner of driver to be registered
87 * Returns a negative value on error, otherwise 0.
89 int mdev_register_driver(struct mdev_driver *drv, struct module *owner)
91 /* initialize common driver fields */
92 drv->driver.name = drv->name;
93 drv->driver.bus = &mdev_bus_type;
94 drv->driver.owner = owner;
96 /* register with core */
97 return driver_register(&drv->driver);
99 EXPORT_SYMBOL(mdev_register_driver);
102 * mdev_unregister_driver - unregister MDEV driver
103 * @drv: the driver to unregister
105 void mdev_unregister_driver(struct mdev_driver *drv)
107 driver_unregister(&drv->driver);
109 EXPORT_SYMBOL(mdev_unregister_driver);
111 int mdev_bus_register(void)
113 return bus_register(&mdev_bus_type);
116 void mdev_bus_unregister(void)
118 bus_unregister(&mdev_bus_type);