eaf7f6992a2f943764b6ef5234ebcf9db659fac2
[safe/jmp/linux-2.6] / arch / powerpc / kernel / vio.c
1 /*
2  * IBM PowerPC Virtual I/O Infrastructure Support.
3  *
4  *    Copyright (c) 2003-2005 IBM Corp.
5  *     Dave Engebretsen engebret@us.ibm.com
6  *     Santiago Leon santil@us.ibm.com
7  *     Hollis Blanchard <hollisb@us.ibm.com>
8  *     Stephen Rothwell
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
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
40 extern struct kset devices_subsys; /* needed for vio_find_name() */
41
42 static struct bus_type vio_bus_type;
43
44 static struct vio_dev vio_bus_device  = { /* fake "parent" device */
45         .name = vio_bus_device.dev.bus_id,
46         .type = "",
47         .dev.bus_id = "vio",
48         .dev.bus = &vio_bus_type,
49 };
50
51 #ifdef CONFIG_PPC_ISERIES
52 static struct iommu_table veth_iommu_table;
53 struct iommu_table vio_iommu_table;
54
55 static void __init iommu_vio_init(void)
56 {
57         iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);
58         veth_iommu_table.it_size /= 2;
59         vio_iommu_table = veth_iommu_table;
60         vio_iommu_table.it_offset += veth_iommu_table.it_size;
61
62         if (!iommu_init_table(&veth_iommu_table, -1))
63                 printk("Virtual Bus VETH TCE table failed.\n");
64         if (!iommu_init_table(&vio_iommu_table, -1))
65                 printk("Virtual Bus VIO TCE table failed.\n");
66 }
67 #else
68 static void __init iommu_vio_init(void)
69 {
70 }
71 #endif
72
73 static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
74 {
75 #ifdef CONFIG_PPC_ISERIES
76         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
77                 if (strcmp(dev->type, "network") == 0)
78                         return &veth_iommu_table;
79                 return &vio_iommu_table;
80         } else
81 #endif
82         {
83                 const unsigned char *dma_window;
84                 struct iommu_table *tbl;
85                 unsigned long offset, size;
86
87                 dma_window = of_get_property(dev->dev.archdata.of_node,
88                                           "ibm,my-dma-window", NULL);
89                 if (!dma_window)
90                         return NULL;
91
92                 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
93
94                 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
95                                     &tbl->it_index, &offset, &size);
96
97                 /* TCE table size - measured in tce entries */
98                 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
99                 /* offset for VIO should always be 0 */
100                 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
101                 tbl->it_busno = 0;
102                 tbl->it_type = TCE_VB;
103
104                 return iommu_init_table(tbl, -1);
105         }
106 }
107
108 /**
109  * vio_match_device: - Tell if a VIO device has a matching
110  *                      VIO device id structure.
111  * @ids:        array of VIO device id structures to search in
112  * @dev:        the VIO device structure to match against
113  *
114  * Used by a driver to check whether a VIO device present in the
115  * system is in its list of supported devices. Returns the matching
116  * vio_device_id structure or NULL if there is no match.
117  */
118 static const struct vio_device_id *vio_match_device(
119                 const struct vio_device_id *ids, const struct vio_dev *dev)
120 {
121         while (ids->type[0] != '\0') {
122                 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
123                     of_device_is_compatible(dev->dev.archdata.of_node,
124                                          ids->compat))
125                         return ids;
126                 ids++;
127         }
128         return NULL;
129 }
130
131 /*
132  * Convert from struct device to struct vio_dev and pass to driver.
133  * dev->driver has already been set by generic code because vio_bus_match
134  * succeeded.
135  */
136 static int vio_bus_probe(struct device *dev)
137 {
138         struct vio_dev *viodev = to_vio_dev(dev);
139         struct vio_driver *viodrv = to_vio_driver(dev->driver);
140         const struct vio_device_id *id;
141         int error = -ENODEV;
142
143         if (!viodrv->probe)
144                 return error;
145
146         id = vio_match_device(viodrv->id_table, viodev);
147         if (id)
148                 error = viodrv->probe(viodev, id);
149
150         return error;
151 }
152
153 /* convert from struct device to struct vio_dev and pass to driver. */
154 static int vio_bus_remove(struct device *dev)
155 {
156         struct vio_dev *viodev = to_vio_dev(dev);
157         struct vio_driver *viodrv = to_vio_driver(dev->driver);
158
159         if (viodrv->remove)
160                 return viodrv->remove(viodev);
161
162         /* driver can't remove */
163         return 1;
164 }
165
166 /**
167  * vio_register_driver: - Register a new vio driver
168  * @drv:        The vio_driver structure to be registered.
169  */
170 int vio_register_driver(struct vio_driver *viodrv)
171 {
172         printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
173                 viodrv->driver.name);
174
175         /* fill in 'struct driver' fields */
176         viodrv->driver.bus = &vio_bus_type;
177
178         return driver_register(&viodrv->driver);
179 }
180 EXPORT_SYMBOL(vio_register_driver);
181
182 /**
183  * vio_unregister_driver - Remove registration of vio driver.
184  * @driver:     The vio_driver struct to be removed form registration
185  */
186 void vio_unregister_driver(struct vio_driver *viodrv)
187 {
188         driver_unregister(&viodrv->driver);
189 }
190 EXPORT_SYMBOL(vio_unregister_driver);
191
192 /* vio_dev refcount hit 0 */
193 static void __devinit vio_dev_release(struct device *dev)
194 {
195         /* XXX should free TCE table */
196         of_node_put(dev->archdata.of_node);
197         kfree(to_vio_dev(dev));
198 }
199
200 /**
201  * vio_register_device_node: - Register a new vio device.
202  * @of_node:    The OF node for this device.
203  *
204  * Creates and initializes a vio_dev structure from the data in
205  * of_node and adds it to the list of virtual devices.
206  * Returns a pointer to the created vio_dev or NULL if node has
207  * NULL device_type or compatible fields.
208  */
209 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
210 {
211         struct vio_dev *viodev;
212         const unsigned int *unit_address;
213
214         /* we need the 'device_type' property, in order to match with drivers */
215         if (of_node->type == NULL) {
216                 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
217                                 __FUNCTION__,
218                                 of_node->name ? of_node->name : "<unknown>");
219                 return NULL;
220         }
221
222         unit_address = of_get_property(of_node, "reg", NULL);
223         if (unit_address == NULL) {
224                 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
225                                 __FUNCTION__,
226                                 of_node->name ? of_node->name : "<unknown>");
227                 return NULL;
228         }
229
230         /* allocate a vio_dev for this node */
231         viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
232         if (viodev == NULL)
233                 return NULL;
234
235         viodev->irq = irq_of_parse_and_map(of_node, 0);
236
237         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
238         viodev->name = of_node->name;
239         viodev->type = of_node->type;
240         viodev->unit_address = *unit_address;
241         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
242                 unit_address = of_get_property(of_node,
243                                 "linux,unit_address", NULL);
244                 if (unit_address != NULL)
245                         viodev->unit_address = *unit_address;
246         }
247         viodev->dev.archdata.of_node = of_node_get(of_node);
248         viodev->dev.archdata.dma_ops = &dma_iommu_ops;
249         viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
250         viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
251
252         /* init generic 'struct device' fields: */
253         viodev->dev.parent = &vio_bus_device.dev;
254         viodev->dev.bus = &vio_bus_type;
255         viodev->dev.release = vio_dev_release;
256
257         /* register with generic device framework */
258         if (device_register(&viodev->dev)) {
259                 printk(KERN_ERR "%s: failed to register device %s\n",
260                                 __FUNCTION__, viodev->dev.bus_id);
261                 /* XXX free TCE table */
262                 kfree(viodev);
263                 return NULL;
264         }
265
266         return viodev;
267 }
268 EXPORT_SYMBOL(vio_register_device_node);
269
270 /**
271  * vio_bus_init: - Initialize the virtual IO bus
272  */
273 static int __init vio_bus_init(void)
274 {
275         int err;
276         struct device_node *node_vroot;
277
278         if (firmware_has_feature(FW_FEATURE_ISERIES))
279                 iommu_vio_init();
280
281         err = bus_register(&vio_bus_type);
282         if (err) {
283                 printk(KERN_ERR "failed to register VIO bus\n");
284                 return err;
285         }
286
287         /*
288          * The fake parent of all vio devices, just to give us
289          * a nice directory
290          */
291         err = device_register(&vio_bus_device.dev);
292         if (err) {
293                 printk(KERN_WARNING "%s: device_register returned %i\n",
294                                 __FUNCTION__, err);
295                 return err;
296         }
297
298         node_vroot = of_find_node_by_name(NULL, "vdevice");
299         if (node_vroot) {
300                 struct device_node *of_node;
301
302                 /*
303                  * Create struct vio_devices for each virtual device in
304                  * the device tree. Drivers will associate with them later.
305                  */
306                 for (of_node = node_vroot->child; of_node != NULL;
307                                 of_node = of_node->sibling)
308                         vio_register_device_node(of_node);
309                 of_node_put(node_vroot);
310         }
311
312         return 0;
313 }
314 __initcall(vio_bus_init);
315
316 static ssize_t name_show(struct device *dev,
317                 struct device_attribute *attr, char *buf)
318 {
319         return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
320 }
321
322 static ssize_t devspec_show(struct device *dev,
323                 struct device_attribute *attr, char *buf)
324 {
325         struct device_node *of_node = dev->archdata.of_node;
326
327         return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
328 }
329
330 static struct device_attribute vio_dev_attrs[] = {
331         __ATTR_RO(name),
332         __ATTR_RO(devspec),
333         __ATTR_NULL
334 };
335
336 void __devinit vio_unregister_device(struct vio_dev *viodev)
337 {
338         device_unregister(&viodev->dev);
339 }
340 EXPORT_SYMBOL(vio_unregister_device);
341
342 static int vio_bus_match(struct device *dev, struct device_driver *drv)
343 {
344         const struct vio_dev *vio_dev = to_vio_dev(dev);
345         struct vio_driver *vio_drv = to_vio_driver(drv);
346         const struct vio_device_id *ids = vio_drv->id_table;
347
348         return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
349 }
350
351 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
352                         char *buffer, int buffer_size)
353 {
354         const struct vio_dev *vio_dev = to_vio_dev(dev);
355         struct device_node *dn;
356         const char *cp;
357         int length;
358
359         if (!num_envp)
360                 return -ENOMEM;
361
362         dn = dev->archdata.of_node;
363         if (!dn)
364                 return -ENODEV;
365         cp = of_get_property(dn, "compatible", &length);
366         if (!cp)
367                 return -ENODEV;
368
369         envp[0] = buffer;
370         length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
371                                 vio_dev->type, cp);
372         if ((buffer_size - length) <= 0)
373                 return -ENOMEM;
374         envp[1] = NULL;
375         return 0;
376 }
377
378 static struct bus_type vio_bus_type = {
379         .name = "vio",
380         .dev_attrs = vio_dev_attrs,
381         .uevent = vio_hotplug,
382         .match = vio_bus_match,
383         .probe = vio_bus_probe,
384         .remove = vio_bus_remove,
385 };
386
387 /**
388  * vio_get_attribute: - get attribute for virtual device
389  * @vdev:       The vio device to get property.
390  * @which:      The property/attribute to be extracted.
391  * @length:     Pointer to length of returned data size (unused if NULL).
392  *
393  * Calls prom.c's of_get_property() to return the value of the
394  * attribute specified by @which
395 */
396 const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
397 {
398         return of_get_property(vdev->dev.archdata.of_node, which, length);
399 }
400 EXPORT_SYMBOL(vio_get_attribute);
401
402 #ifdef CONFIG_PPC_PSERIES
403 /* vio_find_name() - internal because only vio.c knows how we formatted the
404  * kobject name
405  * XXX once vio_bus_type.devices is actually used as a kset in
406  * drivers/base/bus.c, this function should be removed in favor of
407  * "device_find(kobj_name, &vio_bus_type)"
408  */
409 static struct vio_dev *vio_find_name(const char *kobj_name)
410 {
411         struct kobject *found;
412
413         found = kset_find_obj(&devices_subsys, kobj_name);
414         if (!found)
415                 return NULL;
416
417         return to_vio_dev(container_of(found, struct device, kobj));
418 }
419
420 /**
421  * vio_find_node - find an already-registered vio_dev
422  * @vnode: device_node of the virtual device we're looking for
423  */
424 struct vio_dev *vio_find_node(struct device_node *vnode)
425 {
426         const uint32_t *unit_address;
427         char kobj_name[BUS_ID_SIZE];
428
429         /* construct the kobject name from the device node */
430         unit_address = of_get_property(vnode, "reg", NULL);
431         if (!unit_address)
432                 return NULL;
433         snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
434
435         return vio_find_name(kobj_name);
436 }
437 EXPORT_SYMBOL(vio_find_node);
438
439 int vio_enable_interrupts(struct vio_dev *dev)
440 {
441         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
442         if (rc != H_SUCCESS)
443                 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
444         return rc;
445 }
446 EXPORT_SYMBOL(vio_enable_interrupts);
447
448 int vio_disable_interrupts(struct vio_dev *dev)
449 {
450         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
451         if (rc != H_SUCCESS)
452                 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
453         return rc;
454 }
455 EXPORT_SYMBOL(vio_disable_interrupts);
456 #endif /* CONFIG_PPC_PSERIES */