1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/types.h>
17#include <linux/device.h>
18#include <linux/init.h>
19#include <linux/console.h>
20#include <linux/module.h>
21#include <linux/mm.h>
22#include <linux/dma-mapping.h>
23#include <linux/kobject.h>
24
25#include <asm/iommu.h>
26#include <asm/dma.h>
27#include <asm/vio.h>
28#include <asm/prom.h>
29#include <asm/firmware.h>
30#include <asm/tce.h>
31#include <asm/abs_addr.h>
32#include <asm/page.h>
33#include <asm/hvcall.h>
34#include <asm/iseries/vio.h>
35#include <asm/iseries/hv_types.h>
36#include <asm/iseries/hv_lp_config.h>
37#include <asm/iseries/hv_call_xm.h>
38#include <asm/iseries/iommu.h>
39
40extern struct kset devices_subsys;
41
42static struct bus_type vio_bus_type;
43
44static struct vio_dev vio_bus_device = {
45 .name = vio_bus_device.dev.bus_id,
46 .type = "",
47 .dev.bus_id = "vio",
48 .dev.bus = &vio_bus_type,
49};
50
51static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
52{
53 const unsigned char *dma_window;
54 struct iommu_table *tbl;
55 unsigned long offset, size;
56
57 if (firmware_has_feature(FW_FEATURE_ISERIES))
58 return vio_build_iommu_table_iseries(dev);
59
60 dma_window = of_get_property(dev->dev.archdata.of_node,
61 "ibm,my-dma-window", NULL);
62 if (!dma_window)
63 return NULL;
64
65 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
66
67 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
68 &tbl->it_index, &offset, &size);
69
70
71 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
72
73 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
74 tbl->it_busno = 0;
75 tbl->it_type = TCE_VB;
76
77 return iommu_init_table(tbl, -1);
78}
79
80
81
82
83
84
85
86
87
88
89
90static const struct vio_device_id *vio_match_device(
91 const struct vio_device_id *ids, const struct vio_dev *dev)
92{
93 while (ids->type[0] != '\0') {
94 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
95 of_device_is_compatible(dev->dev.archdata.of_node,
96 ids->compat))
97 return ids;
98 ids++;
99 }
100 return NULL;
101}
102
103
104
105
106
107
108static int vio_bus_probe(struct device *dev)
109{
110 struct vio_dev *viodev = to_vio_dev(dev);
111 struct vio_driver *viodrv = to_vio_driver(dev->driver);
112 const struct vio_device_id *id;
113 int error = -ENODEV;
114
115 if (!viodrv->probe)
116 return error;
117
118 id = vio_match_device(viodrv->id_table, viodev);
119 if (id)
120 error = viodrv->probe(viodev, id);
121
122 return error;
123}
124
125
126static int vio_bus_remove(struct device *dev)
127{
128 struct vio_dev *viodev = to_vio_dev(dev);
129 struct vio_driver *viodrv = to_vio_driver(dev->driver);
130
131 if (viodrv->remove)
132 return viodrv->remove(viodev);
133
134
135 return 1;
136}
137
138
139
140
141
142int vio_register_driver(struct vio_driver *viodrv)
143{
144 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
145 viodrv->driver.name);
146
147
148 viodrv->driver.bus = &vio_bus_type;
149
150 return driver_register(&viodrv->driver);
151}
152EXPORT_SYMBOL(vio_register_driver);
153
154
155
156
157
158void vio_unregister_driver(struct vio_driver *viodrv)
159{
160 driver_unregister(&viodrv->driver);
161}
162EXPORT_SYMBOL(vio_unregister_driver);
163
164
165static void __devinit vio_dev_release(struct device *dev)
166{
167
168 of_node_put(dev->archdata.of_node);
169 kfree(to_vio_dev(dev));
170}
171
172
173
174
175
176
177
178
179
180
181struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
182{
183 struct vio_dev *viodev;
184 const unsigned int *unit_address;
185
186
187 if (of_node->type == NULL) {
188 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
189 __FUNCTION__,
190 of_node->name ? of_node->name : "<unknown>");
191 return NULL;
192 }
193
194 unit_address = of_get_property(of_node, "reg", NULL);
195 if (unit_address == NULL) {
196 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
197 __FUNCTION__,
198 of_node->name ? of_node->name : "<unknown>");
199 return NULL;
200 }
201
202
203 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
204 if (viodev == NULL)
205 return NULL;
206
207 viodev->irq = irq_of_parse_and_map(of_node, 0);
208
209 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
210 viodev->name = of_node->name;
211 viodev->type = of_node->type;
212 viodev->unit_address = *unit_address;
213 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
214 unit_address = of_get_property(of_node,
215 "linux,unit_address", NULL);
216 if (unit_address != NULL)
217 viodev->unit_address = *unit_address;
218 }
219 viodev->dev.archdata.of_node = of_node_get(of_node);
220 viodev->dev.archdata.dma_ops = &dma_iommu_ops;
221 viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
222 viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
223
224
225 viodev->dev.parent = &vio_bus_device.dev;
226 viodev->dev.bus = &vio_bus_type;
227 viodev->dev.release = vio_dev_release;
228
229
230 if (device_register(&viodev->dev)) {
231 printk(KERN_ERR "%s: failed to register device %s\n",
232 __FUNCTION__, viodev->dev.bus_id);
233
234 kfree(viodev);
235 return NULL;
236 }
237
238 return viodev;
239}
240EXPORT_SYMBOL(vio_register_device_node);
241
242
243
244
245static int __init vio_bus_init(void)
246{
247 int err;
248 struct device_node *node_vroot;
249
250 err = bus_register(&vio_bus_type);
251 if (err) {
252 printk(KERN_ERR "failed to register VIO bus\n");
253 return err;
254 }
255
256
257
258
259
260 err = device_register(&vio_bus_device.dev);
261 if (err) {
262 printk(KERN_WARNING "%s: device_register returned %i\n",
263 __FUNCTION__, err);
264 return err;
265 }
266
267 node_vroot = of_find_node_by_name(NULL, "vdevice");
268 if (node_vroot) {
269 struct device_node *of_node;
270
271
272
273
274
275 for (of_node = node_vroot->child; of_node != NULL;
276 of_node = of_node->sibling)
277 vio_register_device_node(of_node);
278 of_node_put(node_vroot);
279 }
280
281 return 0;
282}
283__initcall(vio_bus_init);
284
285static ssize_t name_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
289}
290
291static ssize_t devspec_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
294 struct device_node *of_node = dev->archdata.of_node;
295
296 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
297}
298
299static struct device_attribute vio_dev_attrs[] = {
300 __ATTR_RO(name),
301 __ATTR_RO(devspec),
302 __ATTR_NULL
303};
304
305void __devinit vio_unregister_device(struct vio_dev *viodev)
306{
307 device_unregister(&viodev->dev);
308}
309EXPORT_SYMBOL(vio_unregister_device);
310
311static int vio_bus_match(struct device *dev, struct device_driver *drv)
312{
313 const struct vio_dev *vio_dev = to_vio_dev(dev);
314 struct vio_driver *vio_drv = to_vio_driver(drv);
315 const struct vio_device_id *ids = vio_drv->id_table;
316
317 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
318}
319
320static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
321{
322 const struct vio_dev *vio_dev = to_vio_dev(dev);
323 struct device_node *dn;
324 const char *cp;
325
326 dn = dev->archdata.of_node;
327 if (!dn)
328 return -ENODEV;
329 cp = of_get_property(dn, "compatible", NULL);
330 if (!cp)
331 return -ENODEV;
332
333 add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
334 return 0;
335}
336
337static struct bus_type vio_bus_type = {
338 .name = "vio",
339 .dev_attrs = vio_dev_attrs,
340 .uevent = vio_hotplug,
341 .match = vio_bus_match,
342 .probe = vio_bus_probe,
343 .remove = vio_bus_remove,
344};
345
346
347
348
349
350
351
352
353
354
355const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
356{
357 return of_get_property(vdev->dev.archdata.of_node, which, length);
358}
359EXPORT_SYMBOL(vio_get_attribute);
360
361#ifdef CONFIG_PPC_PSERIES
362
363
364
365
366
367
368static struct vio_dev *vio_find_name(const char *kobj_name)
369{
370 struct kobject *found;
371
372 found = kset_find_obj(&devices_subsys, kobj_name);
373 if (!found)
374 return NULL;
375
376 return to_vio_dev(container_of(found, struct device, kobj));
377}
378
379
380
381
382
383struct vio_dev *vio_find_node(struct device_node *vnode)
384{
385 const uint32_t *unit_address;
386 char kobj_name[BUS_ID_SIZE];
387
388
389 unit_address = of_get_property(vnode, "reg", NULL);
390 if (!unit_address)
391 return NULL;
392 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
393
394 return vio_find_name(kobj_name);
395}
396EXPORT_SYMBOL(vio_find_node);
397
398int vio_enable_interrupts(struct vio_dev *dev)
399{
400 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
401 if (rc != H_SUCCESS)
402 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
403 return rc;
404}
405EXPORT_SYMBOL(vio_enable_interrupts);
406
407int vio_disable_interrupts(struct vio_dev *dev)
408{
409 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
410 if (rc != H_SUCCESS)
411 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
412 return rc;
413}
414EXPORT_SYMBOL(vio_disable_interrupts);
415#endif
416