1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2016 Intel Corporation.
7 * Intel Virtio Over PCIe (VOP) Bus driver.
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/dma-mapping.h>
16 static ssize_t device_show(struct device *d,
17 struct device_attribute *attr, char *buf)
19 struct vop_device *dev = dev_to_vop(d);
21 return sprintf(buf, "0x%04x\n", dev->id.device);
23 static DEVICE_ATTR_RO(device);
25 static ssize_t vendor_show(struct device *d,
26 struct device_attribute *attr, char *buf)
28 struct vop_device *dev = dev_to_vop(d);
30 return sprintf(buf, "0x%04x\n", dev->id.vendor);
32 static DEVICE_ATTR_RO(vendor);
34 static ssize_t modalias_show(struct device *d,
35 struct device_attribute *attr, char *buf)
37 struct vop_device *dev = dev_to_vop(d);
39 return sprintf(buf, "vop:d%08Xv%08X\n",
40 dev->id.device, dev->id.vendor);
42 static DEVICE_ATTR_RO(modalias);
44 static struct attribute *vop_dev_attrs[] = {
45 &dev_attr_device.attr,
46 &dev_attr_vendor.attr,
47 &dev_attr_modalias.attr,
50 ATTRIBUTE_GROUPS(vop_dev);
52 static inline int vop_id_match(const struct vop_device *dev,
53 const struct vop_device_id *id)
55 if (id->device != dev->id.device && id->device != VOP_DEV_ANY_ID)
58 return id->vendor == VOP_DEV_ANY_ID || id->vendor == dev->id.vendor;
62 * This looks through all the IDs a driver claims to support. If any of them
63 * match, we return 1 and the kernel will call vop_dev_probe().
65 static int vop_dev_match(struct device *dv, struct device_driver *dr)
68 struct vop_device *dev = dev_to_vop(dv);
69 const struct vop_device_id *ids;
71 ids = drv_to_vop(dr)->id_table;
72 for (i = 0; ids[i].device; i++)
73 if (vop_id_match(dev, &ids[i]))
78 static int vop_uevent(struct device *dv, struct kobj_uevent_env *env)
80 struct vop_device *dev = dev_to_vop(dv);
82 return add_uevent_var(env, "MODALIAS=vop:d%08Xv%08X",
83 dev->id.device, dev->id.vendor);
86 static int vop_dev_probe(struct device *d)
88 struct vop_device *dev = dev_to_vop(d);
89 struct vop_driver *drv = drv_to_vop(dev->dev.driver);
91 return drv->probe(dev);
94 static int vop_dev_remove(struct device *d)
96 struct vop_device *dev = dev_to_vop(d);
97 struct vop_driver *drv = drv_to_vop(dev->dev.driver);
103 static struct bus_type vop_bus = {
105 .match = vop_dev_match,
106 .dev_groups = vop_dev_groups,
107 .uevent = vop_uevent,
108 .probe = vop_dev_probe,
109 .remove = vop_dev_remove,
112 int vop_register_driver(struct vop_driver *driver)
114 driver->driver.bus = &vop_bus;
115 return driver_register(&driver->driver);
117 EXPORT_SYMBOL_GPL(vop_register_driver);
119 void vop_unregister_driver(struct vop_driver *driver)
121 driver_unregister(&driver->driver);
123 EXPORT_SYMBOL_GPL(vop_unregister_driver);
125 static void vop_release_dev(struct device *d)
127 struct vop_device *dev = dev_to_vop(d);
133 vop_register_device(struct device *pdev, int id,
134 const struct dma_map_ops *dma_ops,
135 struct vop_hw_ops *hw_ops, u8 dnode, struct mic_mw *aper,
136 struct dma_chan *chan)
139 struct vop_device *vdev;
141 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
143 return ERR_PTR(-ENOMEM);
145 vdev->dev.parent = pdev;
146 vdev->id.device = id;
147 vdev->id.vendor = VOP_DEV_ANY_ID;
148 vdev->dev.dma_ops = dma_ops;
149 vdev->dev.dma_mask = &vdev->dev.coherent_dma_mask;
150 dma_set_mask(&vdev->dev, DMA_BIT_MASK(64));
151 vdev->dev.release = vop_release_dev;
152 vdev->hw_ops = hw_ops;
153 vdev->dev.bus = &vop_bus;
157 vdev->index = dnode - 1;
158 dev_set_name(&vdev->dev, "vop-dev%u", vdev->index);
160 * device_register() causes the bus infrastructure to look for a
163 ret = device_register(&vdev->dev);
168 put_device(&vdev->dev);
171 EXPORT_SYMBOL_GPL(vop_register_device);
173 void vop_unregister_device(struct vop_device *dev)
175 device_unregister(&dev->dev);
177 EXPORT_SYMBOL_GPL(vop_unregister_device);
179 static int __init vop_init(void)
181 return bus_register(&vop_bus);
184 static void __exit vop_exit(void)
186 bus_unregister(&vop_bus);
189 core_initcall(vop_init);
190 module_exit(vop_exit);
192 MODULE_AUTHOR("Intel Corporation");
193 MODULE_DESCRIPTION("Intel(R) VOP Bus driver");
194 MODULE_LICENSE("GPL v2");