1 /*
2 * Intel MIC Platform Software Stack (MPSS)
3 *
4 * Copyright(c) 2014 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 MIC Bus driver.
19 *
20 * This implementation is very similar to the the virtio bus driver
21 * implementation @ drivers/virtio/virtio.c
22 */
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/idr.h>
26 #include <linux/mic_bus.h>
27
device_show(struct device * d,struct device_attribute * attr,char * buf)28 static ssize_t device_show(struct device *d,
29 struct device_attribute *attr, char *buf)
30 {
31 struct mbus_device *dev = dev_to_mbus(d);
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 mbus_device *dev = dev_to_mbus(d);
40 return sprintf(buf, "0x%04x\n", dev->id.vendor);
41 }
42 static DEVICE_ATTR_RO(vendor);
43
modalias_show(struct device * d,struct device_attribute * attr,char * buf)44 static ssize_t modalias_show(struct device *d,
45 struct device_attribute *attr, char *buf)
46 {
47 struct mbus_device *dev = dev_to_mbus(d);
48 return sprintf(buf, "mbus:d%08Xv%08X\n",
49 dev->id.device, dev->id.vendor);
50 }
51 static DEVICE_ATTR_RO(modalias);
52
53 static struct attribute *mbus_dev_attrs[] = {
54 &dev_attr_device.attr,
55 &dev_attr_vendor.attr,
56 &dev_attr_modalias.attr,
57 NULL,
58 };
59 ATTRIBUTE_GROUPS(mbus_dev);
60
mbus_id_match(const struct mbus_device * dev,const struct mbus_device_id * id)61 static inline int mbus_id_match(const struct mbus_device *dev,
62 const struct mbus_device_id *id)
63 {
64 if (id->device != dev->id.device && id->device != MBUS_DEV_ANY_ID)
65 return 0;
66
67 return id->vendor == MBUS_DEV_ANY_ID || id->vendor == dev->id.vendor;
68 }
69
70 /*
71 * This looks through all the IDs a driver claims to support. If any of them
72 * match, we return 1 and the kernel will call mbus_dev_probe().
73 */
mbus_dev_match(struct device * dv,struct device_driver * dr)74 static int mbus_dev_match(struct device *dv, struct device_driver *dr)
75 {
76 unsigned int i;
77 struct mbus_device *dev = dev_to_mbus(dv);
78 const struct mbus_device_id *ids;
79
80 ids = drv_to_mbus(dr)->id_table;
81 for (i = 0; ids[i].device; i++)
82 if (mbus_id_match(dev, &ids[i]))
83 return 1;
84 return 0;
85 }
86
mbus_uevent(struct device * dv,struct kobj_uevent_env * env)87 static int mbus_uevent(struct device *dv, struct kobj_uevent_env *env)
88 {
89 struct mbus_device *dev = dev_to_mbus(dv);
90
91 return add_uevent_var(env, "MODALIAS=mbus:d%08Xv%08X",
92 dev->id.device, dev->id.vendor);
93 }
94
mbus_dev_probe(struct device * d)95 static int mbus_dev_probe(struct device *d)
96 {
97 int err;
98 struct mbus_device *dev = dev_to_mbus(d);
99 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
100
101 err = drv->probe(dev);
102 if (!err)
103 if (drv->scan)
104 drv->scan(dev);
105 return err;
106 }
107
mbus_dev_remove(struct device * d)108 static int mbus_dev_remove(struct device *d)
109 {
110 struct mbus_device *dev = dev_to_mbus(d);
111 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
112
113 drv->remove(dev);
114 return 0;
115 }
116
117 static struct bus_type mic_bus = {
118 .name = "mic_bus",
119 .match = mbus_dev_match,
120 .dev_groups = mbus_dev_groups,
121 .uevent = mbus_uevent,
122 .probe = mbus_dev_probe,
123 .remove = mbus_dev_remove,
124 };
125
mbus_register_driver(struct mbus_driver * driver)126 int mbus_register_driver(struct mbus_driver *driver)
127 {
128 driver->driver.bus = &mic_bus;
129 return driver_register(&driver->driver);
130 }
131 EXPORT_SYMBOL_GPL(mbus_register_driver);
132
mbus_unregister_driver(struct mbus_driver * driver)133 void mbus_unregister_driver(struct mbus_driver *driver)
134 {
135 driver_unregister(&driver->driver);
136 }
137 EXPORT_SYMBOL_GPL(mbus_unregister_driver);
138
mbus_release_dev(struct device * d)139 static void mbus_release_dev(struct device *d)
140 {
141 struct mbus_device *mbdev = dev_to_mbus(d);
142 kfree(mbdev);
143 }
144
145 struct mbus_device *
mbus_register_device(struct device * pdev,int id,const struct dma_map_ops * dma_ops,struct mbus_hw_ops * hw_ops,int index,void __iomem * mmio_va)146 mbus_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
147 struct mbus_hw_ops *hw_ops, int index,
148 void __iomem *mmio_va)
149 {
150 int ret;
151 struct mbus_device *mbdev;
152
153 mbdev = kzalloc(sizeof(*mbdev), GFP_KERNEL);
154 if (!mbdev)
155 return ERR_PTR(-ENOMEM);
156
157 mbdev->mmio_va = mmio_va;
158 mbdev->dev.parent = pdev;
159 mbdev->id.device = id;
160 mbdev->id.vendor = MBUS_DEV_ANY_ID;
161 mbdev->dev.dma_ops = dma_ops;
162 mbdev->dev.dma_mask = &mbdev->dev.coherent_dma_mask;
163 dma_set_mask(&mbdev->dev, DMA_BIT_MASK(64));
164 mbdev->dev.release = mbus_release_dev;
165 mbdev->hw_ops = hw_ops;
166 mbdev->dev.bus = &mic_bus;
167 mbdev->index = index;
168 dev_set_name(&mbdev->dev, "mbus-dev%u", mbdev->index);
169 /*
170 * device_register() causes the bus infrastructure to look for a
171 * matching driver.
172 */
173 ret = device_register(&mbdev->dev);
174 if (ret)
175 goto free_mbdev;
176 return mbdev;
177 free_mbdev:
178 put_device(&mbdev->dev);
179 return ERR_PTR(ret);
180 }
181 EXPORT_SYMBOL_GPL(mbus_register_device);
182
mbus_unregister_device(struct mbus_device * mbdev)183 void mbus_unregister_device(struct mbus_device *mbdev)
184 {
185 device_unregister(&mbdev->dev);
186 }
187 EXPORT_SYMBOL_GPL(mbus_unregister_device);
188
mbus_init(void)189 static int __init mbus_init(void)
190 {
191 return bus_register(&mic_bus);
192 }
193
mbus_exit(void)194 static void __exit mbus_exit(void)
195 {
196 bus_unregister(&mic_bus);
197 }
198
199 core_initcall(mbus_init);
200 module_exit(mbus_exit);
201
202 MODULE_AUTHOR("Intel Corporation");
203 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
204 MODULE_LICENSE("GPL v2");
205