]> git.karo-electronics.de Git - linux-beck.git/blob - arch/powerpc/kernel/vio.c
19529297a2dca7c827b64dddda6f94912b460c17
[linux-beck.git] / 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/init.h>
17 #include <linux/console.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/dma-mapping.h>
21 #include <asm/iommu.h>
22 #include <asm/dma.h>
23 #include <asm/vio.h>
24 #include <asm/prom.h>
25 #include <asm/firmware.h>
26
27 struct vio_dev vio_bus_device  = { /* fake "parent" device */
28         .name = vio_bus_device.dev.bus_id,
29         .type = "",
30         .dev.bus_id = "vio",
31         .dev.bus = &vio_bus_type,
32 };
33
34 static struct vio_bus_ops vio_bus_ops;
35
36 /**
37  * vio_match_device: - Tell if a VIO device has a matching
38  *                      VIO device id structure.
39  * @ids:        array of VIO device id structures to search in
40  * @dev:        the VIO device structure to match against
41  *
42  * Used by a driver to check whether a VIO device present in the
43  * system is in its list of supported devices. Returns the matching
44  * vio_device_id structure or NULL if there is no match.
45  */
46 static const struct vio_device_id *vio_match_device(
47                 const struct vio_device_id *ids, const struct vio_dev *dev)
48 {
49         while (ids->type[0] != '\0') {
50                 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
51                     device_is_compatible(dev->dev.platform_data, ids->compat))
52                         return ids;
53                 ids++;
54         }
55         return NULL;
56 }
57
58 /*
59  * Convert from struct device to struct vio_dev and pass to driver.
60  * dev->driver has already been set by generic code because vio_bus_match
61  * succeeded.
62  */
63 static int vio_bus_probe(struct device *dev)
64 {
65         struct vio_dev *viodev = to_vio_dev(dev);
66         struct vio_driver *viodrv = to_vio_driver(dev->driver);
67         const struct vio_device_id *id;
68         int error = -ENODEV;
69
70         if (!viodrv->probe)
71                 return error;
72
73         id = vio_match_device(viodrv->id_table, viodev);
74         if (id)
75                 error = viodrv->probe(viodev, id);
76
77         return error;
78 }
79
80 /* convert from struct device to struct vio_dev and pass to driver. */
81 static int vio_bus_remove(struct device *dev)
82 {
83         struct vio_dev *viodev = to_vio_dev(dev);
84         struct vio_driver *viodrv = to_vio_driver(dev->driver);
85
86         if (viodrv->remove)
87                 return viodrv->remove(viodev);
88
89         /* driver can't remove */
90         return 1;
91 }
92
93 /* convert from struct device to struct vio_dev and pass to driver. */
94 static void vio_bus_shutdown(struct device *dev)
95 {
96         struct vio_dev *viodev = to_vio_dev(dev);
97         struct vio_driver *viodrv = to_vio_driver(dev->driver);
98
99         if (dev->driver && viodrv->shutdown)
100                 viodrv->shutdown(viodev);
101 }
102
103 /**
104  * vio_register_driver: - Register a new vio driver
105  * @drv:        The vio_driver structure to be registered.
106  */
107 int vio_register_driver(struct vio_driver *viodrv)
108 {
109         printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
110                 viodrv->driver.name);
111
112         /* fill in 'struct driver' fields */
113         viodrv->driver.bus = &vio_bus_type;
114
115         return driver_register(&viodrv->driver);
116 }
117 EXPORT_SYMBOL(vio_register_driver);
118
119 /**
120  * vio_unregister_driver - Remove registration of vio driver.
121  * @driver:     The vio_driver struct to be removed form registration
122  */
123 void vio_unregister_driver(struct vio_driver *viodrv)
124 {
125         driver_unregister(&viodrv->driver);
126 }
127 EXPORT_SYMBOL(vio_unregister_driver);
128
129 /**
130  * vio_register_device_node: - Register a new vio device.
131  * @of_node:    The OF node for this device.
132  *
133  * Creates and initializes a vio_dev structure from the data in
134  * of_node (dev.platform_data) and adds it to the list of virtual devices.
135  * Returns a pointer to the created vio_dev or NULL if node has
136  * NULL device_type or compatible fields.
137  */
138 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
139 {
140         struct vio_dev *viodev;
141         unsigned int *unit_address;
142         unsigned int *irq_p;
143
144         /* we need the 'device_type' property, in order to match with drivers */
145         if (of_node->type == NULL) {
146                 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
147                                 __FUNCTION__,
148                                 of_node->name ? of_node->name : "<unknown>");
149                 return NULL;
150         }
151
152         unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
153         if (unit_address == NULL) {
154                 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
155                                 __FUNCTION__,
156                                 of_node->name ? of_node->name : "<unknown>");
157                 return NULL;
158         }
159
160         /* allocate a vio_dev for this node */
161         viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
162         if (viodev == NULL)
163                 return NULL;
164
165         viodev->dev.platform_data = of_node_get(of_node);
166
167         viodev->irq = NO_IRQ;
168         irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
169         if (irq_p) {
170                 int virq = virt_irq_create_mapping(*irq_p);
171                 if (virq == NO_IRQ) {
172                         printk(KERN_ERR "Unable to allocate interrupt "
173                                "number for %s\n", of_node->full_name);
174                 } else
175                         viodev->irq = irq_offset_up(virq);
176         }
177
178         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
179         viodev->name = of_node->name;
180         viodev->type = of_node->type;
181         viodev->unit_address = *unit_address;
182         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
183                 unit_address = (unsigned int *)get_property(of_node,
184                                 "linux,unit_address", NULL);
185                 if (unit_address != NULL)
186                         viodev->unit_address = *unit_address;
187         }
188         viodev->iommu_table = vio_bus_ops.build_iommu_table(viodev);
189
190         /* register with generic device framework */
191         if (vio_register_device(viodev) == NULL) {
192                 /* XXX free TCE table */
193                 kfree(viodev);
194                 return NULL;
195         }
196
197         return viodev;
198 }
199 EXPORT_SYMBOL(vio_register_device_node);
200
201 /**
202  * vio_bus_init: - Initialize the virtual IO bus
203  */
204 int __init vio_bus_init(struct vio_bus_ops *ops)
205 {
206         int err;
207         struct device_node *node_vroot;
208
209         vio_bus_ops = *ops;
210
211         err = bus_register(&vio_bus_type);
212         if (err) {
213                 printk(KERN_ERR "failed to register VIO bus\n");
214                 return err;
215         }
216
217         /*
218          * The fake parent of all vio devices, just to give us
219          * a nice directory
220          */
221         err = device_register(&vio_bus_device.dev);
222         if (err) {
223                 printk(KERN_WARNING "%s: device_register returned %i\n",
224                                 __FUNCTION__, err);
225                 return err;
226         }
227
228         node_vroot = find_devices("vdevice");
229         if (node_vroot) {
230                 struct device_node *of_node;
231
232                 /*
233                  * Create struct vio_devices for each virtual device in
234                  * the device tree. Drivers will associate with them later.
235                  */
236                 for (of_node = node_vroot->child; of_node != NULL;
237                                 of_node = of_node->sibling) {
238                         printk(KERN_DEBUG "%s: processing %p\n",
239                                         __FUNCTION__, of_node);
240                         vio_register_device_node(of_node);
241                 }
242         }
243
244         return 0;
245 }
246
247 /* vio_dev refcount hit 0 */
248 static void __devinit vio_dev_release(struct device *dev)
249 {
250         if (dev->platform_data) {
251                 /* XXX free TCE table */
252                 of_node_put(dev->platform_data);
253         }
254         kfree(to_vio_dev(dev));
255 }
256
257 static ssize_t name_show(struct device *dev,
258                 struct device_attribute *attr, char *buf)
259 {
260         return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
261 }
262
263 static ssize_t devspec_show(struct device *dev,
264                 struct device_attribute *attr, char *buf)
265 {
266         struct device_node *of_node = dev->platform_data;
267
268         return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
269 }
270
271 static struct device_attribute vio_dev_attrs[] = {
272         __ATTR_RO(name),
273         __ATTR_RO(devspec),
274         __ATTR_NULL
275 };
276
277 struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
278 {
279         /* init generic 'struct device' fields: */
280         viodev->dev.parent = &vio_bus_device.dev;
281         viodev->dev.bus = &vio_bus_type;
282         viodev->dev.release = vio_dev_release;
283
284         /* register with generic device framework */
285         if (device_register(&viodev->dev)) {
286                 printk(KERN_ERR "%s: failed to register device %s\n",
287                                 __FUNCTION__, viodev->dev.bus_id);
288                 return NULL;
289         }
290
291         return viodev;
292 }
293
294 void __devinit vio_unregister_device(struct vio_dev *viodev)
295 {
296         device_unregister(&viodev->dev);
297 }
298 EXPORT_SYMBOL(vio_unregister_device);
299
300 static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
301                           size_t size, enum dma_data_direction direction)
302 {
303         return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
304                         ~0ul, direction);
305 }
306
307 static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
308                       size_t size, enum dma_data_direction direction)
309 {
310         iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
311                         direction);
312 }
313
314 static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
315                 int nelems, enum dma_data_direction direction)
316 {
317         return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
318                         nelems, ~0ul, direction);
319 }
320
321 static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
322                 int nelems, enum dma_data_direction direction)
323 {
324         iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
325 }
326
327 static void *vio_alloc_coherent(struct device *dev, size_t size,
328                            dma_addr_t *dma_handle, gfp_t flag)
329 {
330         return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
331                         dma_handle, ~0ul, flag);
332 }
333
334 static void vio_free_coherent(struct device *dev, size_t size,
335                          void *vaddr, dma_addr_t dma_handle)
336 {
337         iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
338                         dma_handle);
339 }
340
341 static int vio_dma_supported(struct device *dev, u64 mask)
342 {
343         return 1;
344 }
345
346 struct dma_mapping_ops vio_dma_ops = {
347         .alloc_coherent = vio_alloc_coherent,
348         .free_coherent = vio_free_coherent,
349         .map_single = vio_map_single,
350         .unmap_single = vio_unmap_single,
351         .map_sg = vio_map_sg,
352         .unmap_sg = vio_unmap_sg,
353         .dma_supported = vio_dma_supported,
354 };
355
356 static int vio_bus_match(struct device *dev, struct device_driver *drv)
357 {
358         const struct vio_dev *vio_dev = to_vio_dev(dev);
359         struct vio_driver *vio_drv = to_vio_driver(drv);
360         const struct vio_device_id *ids = vio_drv->id_table;
361
362         return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
363 }
364
365 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
366                         char *buffer, int buffer_size)
367 {
368         const struct vio_dev *vio_dev = to_vio_dev(dev);
369         struct device_node *dn = dev->platform_data;
370         char *cp;
371         int length;
372
373         if (!num_envp)
374                 return -ENOMEM;
375
376         if (!dn)
377                 return -ENODEV;
378         cp = (char *)get_property(dn, "compatible", &length);
379         if (!cp)
380                 return -ENODEV;
381
382         envp[0] = buffer;
383         length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
384                                 vio_dev->type, cp);
385         if ((buffer_size - length) <= 0)
386                 return -ENOMEM;
387         envp[1] = NULL;
388         return 0;
389 }
390
391 struct bus_type vio_bus_type = {
392         .name = "vio",
393         .dev_attrs = vio_dev_attrs,
394         .uevent = vio_hotplug,
395         .match = vio_bus_match,
396         .probe = vio_bus_probe,
397         .remove = vio_bus_remove,
398         .shutdown = vio_bus_shutdown,
399 };
400
401 /**
402  * vio_get_attribute: - get attribute for virtual device
403  * @vdev:       The vio device to get property.
404  * @which:      The property/attribute to be extracted.
405  * @length:     Pointer to length of returned data size (unused if NULL).
406  *
407  * Calls prom.c's get_property() to return the value of the
408  * attribute specified by @which
409 */
410 const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
411 {
412         return get_property(vdev->dev.platform_data, which, length);
413 }
414 EXPORT_SYMBOL(vio_get_attribute);