hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: GPL-2.0-only
/*
 * MDEV driver
 *
 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
 *     Author: Neo Jia <cjia@nvidia.com>
 *             Kirti Wankhede <kwankhede@nvidia.com>
 */
 
#include <linux/device.h>
#include <linux/iommu.h>
#include <linux/mdev.h>
 
#include "mdev_private.h"
 
static int mdev_attach_iommu(struct mdev_device *mdev)
{
   int ret;
   struct iommu_group *group;
 
   group = iommu_group_alloc();
   if (IS_ERR(group))
       return PTR_ERR(group);
 
   ret = iommu_group_add_device(group, &mdev->dev);
   if (!ret)
       dev_info(&mdev->dev, "MDEV: group_id = %d\n",
            iommu_group_id(group));
 
   iommu_group_put(group);
   return ret;
}
 
static void mdev_detach_iommu(struct mdev_device *mdev)
{
   iommu_group_remove_device(&mdev->dev);
   dev_info(&mdev->dev, "MDEV: detaching iommu\n");
}
 
static int mdev_probe(struct device *dev)
{
   struct mdev_driver *drv = to_mdev_driver(dev->driver);
   struct mdev_device *mdev = to_mdev_device(dev);
   int ret;
 
   ret = mdev_attach_iommu(mdev);
   if (ret)
       return ret;
 
   if (drv && drv->probe) {
       ret = drv->probe(dev);
       if (ret)
           mdev_detach_iommu(mdev);
   }
 
   return ret;
}
 
static int mdev_remove(struct device *dev)
{
   struct mdev_driver *drv = to_mdev_driver(dev->driver);
   struct mdev_device *mdev = to_mdev_device(dev);
 
   if (drv && drv->remove)
       drv->remove(dev);
 
   mdev_detach_iommu(mdev);
 
   return 0;
}
 
struct bus_type mdev_bus_type = {
   .name        = "mdev",
   .probe        = mdev_probe,
   .remove        = mdev_remove,
};
EXPORT_SYMBOL_GPL(mdev_bus_type);
 
/**
 * mdev_register_driver - register a new MDEV driver
 * @drv: the driver to register
 * @owner: module owner of driver to be registered
 *
 * Returns a negative value on error, otherwise 0.
 **/
int mdev_register_driver(struct mdev_driver *drv, struct module *owner)
{
   /* initialize common driver fields */
   drv->driver.name = drv->name;
   drv->driver.bus = &mdev_bus_type;
   drv->driver.owner = owner;
 
   /* register with core */
   return driver_register(&drv->driver);
}
EXPORT_SYMBOL(mdev_register_driver);
 
/*
 * mdev_unregister_driver - unregister MDEV driver
 * @drv: the driver to unregister
 */
void mdev_unregister_driver(struct mdev_driver *drv)
{
   driver_unregister(&drv->driver);
}
EXPORT_SYMBOL(mdev_unregister_driver);
 
int mdev_bus_register(void)
{
   return bus_register(&mdev_bus_type);
}
 
void mdev_bus_unregister(void)
{
   bus_unregister(&mdev_bus_type);
}