1 /*
2 * Intel MIC Platform Software Stack (MPSS)
3 *
4 * Copyright(c) 2016 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
17 *
18 * Intel Virtio Over PCIe (VOP) Bus driver.
19 */
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/idr.h>
23 #include <linux/dma-mapping.h>
24
25 #include "vop_bus.h"
26
device_show(struct device * d,struct device_attribute * attr,char * buf)27 static ssize_t device_show(struct device *d,
28 struct device_attribute *attr, char *buf)
29 {
30 struct vop_device *dev = dev_to_vop(d);
31
32 return sprintf(buf, "0x%04x\n", dev->id.device);
33 }
34 static DEVICE_ATTR_RO(device);
35
vendor_show(struct device * d,struct device_attribute * attr,char * buf)36 static ssize_t vendor_show(struct device *d,
37 struct device_attribute *attr, char *buf)
38 {
39 struct vop_device *dev = dev_to_vop(d);
40
41 return sprintf(buf, "0x%04x\n", dev->id.vendor);
42 }
43 static DEVICE_ATTR_RO(vendor);
44
modalias_show(struct device * d,struct device_attribute * attr,char * buf)45 static ssize_t modalias_show(struct device *d,
46 struct device_attribute *attr, char *buf)
47 {
48 struct vop_device *dev = dev_to_vop(d);
49
50 return sprintf(buf, "vop:d%08Xv%08X\n",
51 dev->id.device, dev->id.vendor);
52 }
53 static DEVICE_ATTR_RO(modalias);
54
55 static struct attribute *vop_dev_attrs[] = {
56 &dev_attr_device.attr,
57 &dev_attr_vendor.attr,
58 &dev_attr_modalias.attr,
59 NULL,
60 };
61 ATTRIBUTE_GROUPS(vop_dev);
62
vop_id_match(const struct vop_device * dev,const struct vop_device_id * id)63 static inline int vop_id_match(const struct vop_device *dev,
64 const struct vop_device_id *id)
65 {
66 if (id->device != dev->id.device && id->device != VOP_DEV_ANY_ID)
67 return 0;
68
69 return id->vendor == VOP_DEV_ANY_ID || id->vendor == dev->id.vendor;
70 }
71
72 /*
73 * This looks through all the IDs a driver claims to support. If any of them
74 * match, we return 1 and the kernel will call vop_dev_probe().
75 */
vop_dev_match(struct device * dv,struct device_driver * dr)76 static int vop_dev_match(struct device *dv, struct device_driver *dr)
77 {
78 unsigned int i;
79 struct vop_device *dev = dev_to_vop(dv);
80 const struct vop_device_id *ids;
81
82 ids = drv_to_vop(dr)->id_table;
83 for (i = 0; ids[i].device; i++)
84 if (vop_id_match(dev, &ids[i]))
85 return 1;
86 return 0;
87 }
88
vop_uevent(struct device * dv,struct kobj_uevent_env * env)89 static int vop_uevent(struct device *dv, struct kobj_uevent_env *env)
90 {
91 struct vop_device *dev = dev_to_vop(dv);
92
93 return add_uevent_var(env, "MODALIAS=vop:d%08Xv%08X",
94 dev->id.device, dev->id.vendor);
95 }
96
vop_dev_probe(struct device * d)97 static int vop_dev_probe(struct device *d)
98 {
99 struct vop_device *dev = dev_to_vop(d);
100 struct vop_driver *drv = drv_to_vop(dev->dev.driver);
101
102 return drv->probe(dev);
103 }
104
vop_dev_remove(struct device * d)105 static int vop_dev_remove(struct device *d)
106 {
107 struct vop_device *dev = dev_to_vop(d);
108 struct vop_driver *drv = drv_to_vop(dev->dev.driver);
109
110 drv->remove(dev);
111 return 0;
112 }
113
114 static struct bus_type vop_bus = {
115 .name = "vop_bus",
116 .match = vop_dev_match,
117 .dev_groups = vop_dev_groups,
118 .uevent = vop_uevent,
119 .probe = vop_dev_probe,
120 .remove = vop_dev_remove,
121 };
122
vop_register_driver(struct vop_driver * driver)123 int vop_register_driver(struct vop_driver *driver)
124 {
125 driver->driver.bus = &vop_bus;
126 return driver_register(&driver->driver);
127 }
128 EXPORT_SYMBOL_GPL(vop_register_driver);
129
vop_unregister_driver(struct vop_driver * driver)130 void vop_unregister_driver(struct vop_driver *driver)
131 {
132 driver_unregister(&driver->driver);
133 }
134 EXPORT_SYMBOL_GPL(vop_unregister_driver);
135
vop_release_dev(struct device * d)136 static void vop_release_dev(struct device *d)
137 {
138 struct vop_device *dev = dev_to_vop(d);
139
140 kfree(dev);
141 }
142
143 struct vop_device *
vop_register_device(struct device * pdev,int id,const struct dma_map_ops * dma_ops,struct vop_hw_ops * hw_ops,u8 dnode,struct mic_mw * aper,struct dma_chan * chan)144 vop_register_device(struct device *pdev, int id,
145 const struct dma_map_ops *dma_ops,
146 struct vop_hw_ops *hw_ops, u8 dnode, struct mic_mw *aper,
147 struct dma_chan *chan)
148 {
149 int ret;
150 struct vop_device *vdev;
151
152 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
153 if (!vdev)
154 return ERR_PTR(-ENOMEM);
155
156 vdev->dev.parent = pdev;
157 vdev->id.device = id;
158 vdev->id.vendor = VOP_DEV_ANY_ID;
159 vdev->dev.dma_ops = dma_ops;
160 vdev->dev.dma_mask = &vdev->dev.coherent_dma_mask;
161 dma_set_mask(&vdev->dev, DMA_BIT_MASK(64));
162 vdev->dev.release = vop_release_dev;
163 vdev->hw_ops = hw_ops;
164 vdev->dev.bus = &vop_bus;
165 vdev->dnode = dnode;
166 vdev->aper = aper;
167 vdev->dma_ch = chan;
168 vdev->index = dnode - 1;
169 dev_set_name(&vdev->dev, "vop-dev%u", vdev->index);
170 /*
171 * device_register() causes the bus infrastructure to look for a
172 * matching driver.
173 */
174 ret = device_register(&vdev->dev);
175 if (ret)
176 goto free_vdev;
177 return vdev;
178 free_vdev:
179 put_device(&vdev->dev);
180 return ERR_PTR(ret);
181 }
182 EXPORT_SYMBOL_GPL(vop_register_device);
183
vop_unregister_device(struct vop_device * dev)184 void vop_unregister_device(struct vop_device *dev)
185 {
186 device_unregister(&dev->dev);
187 }
188 EXPORT_SYMBOL_GPL(vop_unregister_device);
189
vop_init(void)190 static int __init vop_init(void)
191 {
192 return bus_register(&vop_bus);
193 }
194
vop_exit(void)195 static void __exit vop_exit(void)
196 {
197 bus_unregister(&vop_bus);
198 }
199
200 core_initcall(vop_init);
201 module_exit(vop_exit);
202
203 MODULE_AUTHOR("Intel Corporation");
204 MODULE_DESCRIPTION("Intel(R) VOP Bus driver");
205 MODULE_LICENSE("GPL v2");
206