ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / vio.c
1 /*
2  * IBM PowerPC Virtual I/O Infrastructure Support.
3  *
4  *    Copyright (c) 2003 IBM Corp.
5  *     Dave Engebretsen engebret@us.ibm.com
6  *     Santiago Leon santil@us.ibm.com
7  *     Hollis Blanchard <hollisb@us.ibm.com>
8  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  */
14
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/kobject.h>
20 #include <linux/mm.h>
21 #include <linux/dma-mapping.h>
22 #include <asm/rtas.h>
23 #include <asm/iommu.h>
24 #include <asm/dma.h>
25 #include <asm/ppcdebug.h>
26 #include <asm/vio.h>
27 #include <asm/hvcall.h>
28 #include <asm/iSeries/vio.h>
29 #include <asm/iSeries/HvCallXm.h>
30
31 #define DBGENTER() pr_debug("%s entered\n", __FUNCTION__)
32
33 extern struct subsystem devices_subsys; /* needed for vio_find_name() */
34
35 struct iommu_table *vio_build_iommu_table(struct vio_dev *dev);
36
37 #ifdef CONFIG_PPC_PSERIES
38 static int vio_num_address_cells;
39 #endif
40 static struct vio_dev *vio_bus_device; /* fake "parent" device */
41
42 #ifdef CONFIG_PPC_ISERIES
43 static struct iommu_table veth_iommu_table;
44 static struct iommu_table vio_iommu_table;
45
46 static struct vio_dev _veth_dev = {
47         .iommu_table = &veth_iommu_table,
48         .dev.bus = &vio_bus_type
49 };
50 static struct vio_dev _vio_dev  = {
51         .iommu_table = &vio_iommu_table,
52         .dev.bus = &vio_bus_type
53 };
54
55 struct vio_dev *iSeries_veth_dev = &_veth_dev;
56 struct device *iSeries_vio_dev = &_vio_dev.dev;
57
58 EXPORT_SYMBOL(iSeries_veth_dev);
59 EXPORT_SYMBOL(iSeries_vio_dev);
60 #endif
61
62 /* convert from struct device to struct vio_dev and pass to driver.
63  * dev->driver has already been set by generic code because vio_bus_match
64  * succeeded. */
65 static int vio_bus_probe(struct device *dev)
66 {
67         struct vio_dev *viodev = to_vio_dev(dev);
68         struct vio_driver *viodrv = to_vio_driver(dev->driver);
69         const struct vio_device_id *id;
70         int error = -ENODEV;
71
72         DBGENTER();
73
74         if (!viodrv->probe)
75                 return error;
76
77         id = vio_match_device(viodrv->id_table, viodev);
78         if (id) {
79                 error = viodrv->probe(viodev, id);
80         }
81
82         return error;
83 }
84
85 /* convert from struct device to struct vio_dev and pass to driver. */
86 static int vio_bus_remove(struct device *dev)
87 {
88         struct vio_dev *viodev = to_vio_dev(dev);
89         struct vio_driver *viodrv = to_vio_driver(dev->driver);
90
91         DBGENTER();
92
93         if (viodrv->remove) {
94                 return viodrv->remove(viodev);
95         }
96
97         /* driver can't remove */
98         return 1;
99 }
100
101 /**
102  * vio_register_driver: - Register a new vio driver
103  * @drv:        The vio_driver structure to be registered.
104  */
105 int vio_register_driver(struct vio_driver *viodrv)
106 {
107         printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
108                 viodrv->name);
109
110         /* fill in 'struct driver' fields */
111         viodrv->driver.name = viodrv->name;
112         viodrv->driver.bus = &vio_bus_type;
113         viodrv->driver.probe = vio_bus_probe;
114         viodrv->driver.remove = vio_bus_remove;
115
116         return driver_register(&viodrv->driver);
117 }
118 EXPORT_SYMBOL(vio_register_driver);
119
120 /**
121  * vio_unregister_driver - Remove registration of vio driver.
122  * @driver:     The vio_driver struct to be removed form registration
123  */
124 void vio_unregister_driver(struct vio_driver *viodrv)
125 {
126         driver_unregister(&viodrv->driver);
127 }
128 EXPORT_SYMBOL(vio_unregister_driver);
129
130 /**
131  * vio_match_device: - Tell if a VIO device has a matching VIO device id structure.
132  * @ids:        array of VIO device id structures to search in
133  * @dev:        the VIO device structure to match against
134  *
135  * Used by a driver to check whether a VIO device present in the
136  * system is in its list of supported devices. Returns the matching
137  * vio_device_id structure or NULL if there is no match.
138  */
139 const struct vio_device_id * vio_match_device(const struct vio_device_id *ids,
140         const struct vio_dev *dev)
141 {
142         DBGENTER();
143
144 #ifdef CONFIG_PPC_PSERIES
145         while (ids->type) {
146                 if ((strncmp(dev->archdata->type, ids->type, strlen(ids->type)) == 0) &&
147                         device_is_compatible((struct device_node*)dev->archdata, ids->compat))
148                         return ids;
149                 ids++;
150         }
151 #endif
152         return NULL;
153 }
154
155 #ifdef CONFIG_PPC_ISERIES
156 void __init iommu_vio_init(void)
157 {
158         struct iommu_table *t;
159         struct iommu_table_cb cb;
160         unsigned long cbp;
161
162         cb.itc_busno = 255;    /* Bus 255 is the virtual bus */
163         cb.itc_virtbus = 0xff; /* Ask for virtual bus */
164
165         cbp = virt_to_abs(&cb);
166         HvCallXm_getTceTableParms(cbp);
167
168         veth_iommu_table.it_size        = cb.itc_size / 2;
169         veth_iommu_table.it_busno       = cb.itc_busno;
170         veth_iommu_table.it_offset      = cb.itc_offset;
171         veth_iommu_table.it_index       = cb.itc_index;
172         veth_iommu_table.it_type        = TCE_VB;
173         veth_iommu_table.it_entrysize   = sizeof(union tce_entry);
174         veth_iommu_table.it_blocksize   = 1;
175
176         t = iommu_init_table(&veth_iommu_table);
177
178         if (!t)
179                 printk("Virtual Bus VETH TCE table failed.\n");
180
181         vio_iommu_table.it_size         = cb.itc_size - veth_iommu_table.it_size;
182         vio_iommu_table.it_busno        = cb.itc_busno;
183         vio_iommu_table.it_offset       = cb.itc_offset +
184                 veth_iommu_table.it_size * (PAGE_SIZE/sizeof(union tce_entry));
185         vio_iommu_table.it_index        = cb.itc_index;
186         vio_iommu_table.it_type         = TCE_VB;
187         vio_iommu_table.it_entrysize    = sizeof(union tce_entry);
188         vio_iommu_table.it_blocksize    = 1;
189
190         t = iommu_init_table(&vio_iommu_table);
191
192         if (!t)
193                 printk("Virtual Bus VIO TCE table failed.\n");
194 }
195 #endif
196
197 /**
198  * vio_bus_init: - Initialize the virtual IO bus
199  */
200 static int __init vio_bus_init(void)
201 {
202 #ifdef CONFIG_PPC_PSERIES
203         struct device_node *node_vroot, *of_node;
204 #endif
205         int err;
206
207         err = bus_register(&vio_bus_type);
208         if (err) {
209                 printk(KERN_ERR "failed to register VIO bus\n");
210                 return err;
211         }
212
213         /* the fake parent of all vio devices, just to give us a nice directory */
214         vio_bus_device = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
215         if (!vio_bus_device) {
216                 return 1;
217         }
218         memset(vio_bus_device, 0, sizeof(struct vio_dev));
219         strcpy(vio_bus_device->dev.bus_id, "vio");
220
221         err = device_register(&vio_bus_device->dev);
222         if (err) {
223                 printk(KERN_WARNING "%s: device_register returned %i\n", __FUNCTION__,
224                         err);
225                 kfree(vio_bus_device);
226                 return err;
227         }
228
229 #ifdef CONFIG_PPC_PSERIES
230         node_vroot = find_devices("vdevice");
231         if ((node_vroot == NULL) || (node_vroot->child == NULL)) {
232                 /* this machine doesn't do virtual IO, and that's ok */
233                 return 0;
234         }
235
236         vio_num_address_cells = prom_n_addr_cells(node_vroot->child);
237
238         /*
239          * Create struct vio_devices for each virtual device in the device tree.
240          * Drivers will associate with them later.
241          */
242         for (of_node = node_vroot->child;
243                         of_node != NULL;
244                         of_node = of_node->sibling) {
245                 printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
246
247                 vio_register_device(of_node);
248         }
249 #endif
250
251         return 0;
252 }
253
254 __initcall(vio_bus_init);
255
256
257 #ifdef CONFIG_PPC_PSERIES
258 /* vio_dev refcount hit 0 */
259 static void __devinit vio_dev_release(struct device *dev)
260 {
261         struct vio_dev *viodev = to_vio_dev(dev);
262
263         DBGENTER();
264
265         /* XXX free TCE table */
266         of_node_put(viodev->archdata);
267         kfree(viodev);
268 }
269
270 static ssize_t viodev_show_name(struct device *dev, char *buf)
271 {
272         struct vio_dev *viodev = to_vio_dev(dev);
273         struct device_node *of_node = viodev->archdata;
274
275         return sprintf(buf, "%s\n", of_node->name);
276 }
277 DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
278
279 /**
280  * vio_register_device: - Register a new vio device.
281  * @of_node:    The OF node for this device.
282  *
283  * Creates and initializes a vio_dev structure from the data in
284  * of_node (archdata) and adds it to the list of virtual devices.
285  * Returns a pointer to the created vio_dev or NULL if node has
286  * NULL device_type or compatible fields.
287  */
288 struct vio_dev * __devinit vio_register_device(struct device_node *of_node)
289 {
290         struct vio_dev *viodev;
291         unsigned int *unit_address;
292         unsigned int *irq_p;
293
294         DBGENTER();
295
296         /* we need the 'device_type' property, in order to match with drivers */
297         if ((NULL == of_node->type)) {
298                 printk(KERN_WARNING
299                         "%s: node %s missing 'device_type'\n", __FUNCTION__,
300                         of_node->name ? of_node->name : "<unknown>");
301                 return NULL;
302         }
303
304         unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
305         if (!unit_address) {
306                 printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
307                         of_node->name ? of_node->name : "<unknown>");
308                 return NULL;
309         }
310
311         /* allocate a vio_dev for this node */
312         viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
313         if (!viodev) {
314                 return NULL;
315         }
316         memset(viodev, 0, sizeof(struct vio_dev));
317
318         viodev->archdata = (void *)of_node_get(of_node);
319         viodev->unit_address = *unit_address;
320         viodev->iommu_table = vio_build_iommu_table(viodev);
321
322         viodev->irq = NO_IRQ;
323         irq_p = (unsigned int *)get_property(of_node, "interrupts", 0);
324         if (irq_p) {
325                 int virq = virt_irq_create_mapping(*irq_p);
326                 if (virq == NO_IRQ) {
327                         printk(KERN_ERR "Unable to allocate interrupt "
328                                "number for %s\n", of_node->full_name);
329                 } else
330                         viodev->irq = irq_offset_up(virq);
331         }
332
333         /* init generic 'struct device' fields: */
334         viodev->dev.parent = &vio_bus_device->dev;
335         viodev->dev.bus = &vio_bus_type;
336         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", viodev->unit_address);
337         viodev->dev.release = vio_dev_release;
338
339         /* register with generic device framework */
340         if (device_register(&viodev->dev)) {
341                 printk(KERN_ERR "%s: failed to register device %s\n", __FUNCTION__,
342                         viodev->dev.bus_id);
343                 /* XXX free TCE table */
344                 kfree(viodev);
345                 return NULL;
346         }
347         device_create_file(&viodev->dev, &dev_attr_name);
348
349         return viodev;
350 }
351 EXPORT_SYMBOL(vio_register_device);
352
353 void __devinit vio_unregister_device(struct vio_dev *viodev)
354 {
355         DBGENTER();
356         device_unregister(&viodev->dev);
357 }
358 EXPORT_SYMBOL(vio_unregister_device);
359
360 /**
361  * vio_get_attribute: - get attribute for virtual device
362  * @vdev:       The vio device to get property.
363  * @which:      The property/attribute to be extracted.
364  * @length:     Pointer to length of returned data size (unused if NULL).
365  *
366  * Calls prom.c's get_property() to return the value of the
367  * attribute specified by the preprocessor constant @which
368 */
369 const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
370 {
371         return get_property((struct device_node *)vdev->archdata, (char*)which, length);
372 }
373 EXPORT_SYMBOL(vio_get_attribute);
374
375 /* vio_find_name() - internal because only vio.c knows how we formatted the
376  * kobject name
377  * XXX once vio_bus_type.devices is actually used as a kset in
378  * drivers/base/bus.c, this function should be removed in favor of
379  * "device_find(kobj_name, &vio_bus_type)"
380  */
381 static struct vio_dev *vio_find_name(const char *kobj_name)
382 {
383         struct kobject *found;
384
385         found = kset_find_obj(&devices_subsys.kset, kobj_name);
386         if (!found)
387                 return NULL;
388
389         return to_vio_dev(container_of(found, struct device, kobj));
390 }
391
392 /**
393  * vio_find_node - find an already-registered vio_dev
394  * @vnode: device_node of the virtual device we're looking for
395  */
396 struct vio_dev *vio_find_node(struct device_node *vnode)
397 {
398         uint32_t *unit_address;
399         char kobj_name[BUS_ID_SIZE];
400
401         /* construct the kobject name from the device node */
402         unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
403         if (!unit_address)
404                 return NULL;
405         snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
406
407         return vio_find_name(kobj_name);
408 }
409 EXPORT_SYMBOL(vio_find_node);
410
411 /**
412  * vio_build_iommu_table: - gets the dma information from OF and builds the TCE tree.
413  * @dev: the virtual device.
414  *
415  * Returns a pointer to the built tce tree, or NULL if it can't
416  * find property.
417 */
418 struct iommu_table * vio_build_iommu_table(struct vio_dev *dev)
419 {
420         unsigned int *dma_window;
421         struct iommu_table *newTceTable;
422         unsigned long offset;
423         unsigned long size;
424         int dma_window_property_size;
425
426         dma_window = (unsigned int *) get_property((struct device_node *)dev->archdata, "ibm,my-dma-window", &dma_window_property_size);
427         if(!dma_window) {
428                 return NULL;
429         }
430
431         newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
432
433         /* RPA docs say that #address-cells is always 1 for virtual
434                 devices, but some older boxes' OF returns 2.  This should
435                 be removed by GA, unless there is legacy OFs that still
436                 have 2 for #address-cells */
437         size = ((dma_window[1+vio_num_address_cells] >> PAGE_SHIFT) << 3)
438                 >> PAGE_SHIFT;
439
440         /* This is just an ugly kludge. Remove as soon as the OF for all
441         machines actually follow the spec and encodes the offset field
442         as phys-encode (that is, #address-cells wide)*/
443         if (dma_window_property_size == 12) {
444                 size = ((dma_window[1] >> PAGE_SHIFT) << 3) >> PAGE_SHIFT;
445         } else if (dma_window_property_size == 20) {
446                 size = ((dma_window[4] >> PAGE_SHIFT) << 3) >> PAGE_SHIFT;
447         } else {
448                 printk(KERN_WARNING "vio_build_iommu_table: Invalid size of ibm,my-dma-window=%i, using 0x80 for size\n", dma_window_property_size);
449                 size = 0x80;
450         }
451
452         /*  There should be some code to extract the phys-encoded offset
453                 using prom_n_addr_cells(). However, according to a comment
454                 on earlier versions, it's always zero, so we don't bother */
455         offset = dma_window[1] >>  PAGE_SHIFT;
456
457         /* TCE table size - measured in units of pages of tce table */
458         newTceTable->it_size            = size;
459         /* offset for VIO should always be 0 */
460         newTceTable->it_offset          = offset;
461         newTceTable->it_busno           = 0;
462         newTceTable->it_index           = (unsigned long)dma_window[0];
463         newTceTable->it_type            = TCE_VB;
464         newTceTable->it_entrysize       = sizeof(union tce_entry);
465
466         return iommu_init_table(newTceTable);
467 }
468
469 int vio_enable_interrupts(struct vio_dev *dev)
470 {
471         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
472         if (rc != H_Success) {
473                 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
474         }
475         return rc;
476 }
477 EXPORT_SYMBOL(vio_enable_interrupts);
478
479 int vio_disable_interrupts(struct vio_dev *dev)
480 {
481         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
482         if (rc != H_Success) {
483                 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
484         }
485         return rc;
486 }
487 EXPORT_SYMBOL(vio_disable_interrupts);
488 #endif
489
490 dma_addr_t vio_map_single(struct vio_dev *dev, void *vaddr,
491                           size_t size, enum dma_data_direction direction)
492 {
493         return iommu_map_single(dev->iommu_table, vaddr, size, direction);
494 }
495 EXPORT_SYMBOL(vio_map_single);
496
497 void vio_unmap_single(struct vio_dev *dev, dma_addr_t dma_handle,
498                       size_t size, enum dma_data_direction direction)
499 {
500         iommu_unmap_single(dev->iommu_table, dma_handle, size, direction);
501 }
502 EXPORT_SYMBOL(vio_unmap_single);
503
504 int vio_map_sg(struct vio_dev *vdev, struct scatterlist *sglist, int nelems,
505                enum dma_data_direction direction)
506 {
507         return iommu_map_sg(&vdev->dev, vdev->iommu_table, sglist,
508                         nelems, direction);
509 }
510 EXPORT_SYMBOL(vio_map_sg);
511
512 void vio_unmap_sg(struct vio_dev *vdev, struct scatterlist *sglist, int nelems,
513                   enum dma_data_direction direction)
514 {
515         iommu_unmap_sg(vdev->iommu_table, sglist, nelems, direction);
516 }
517 EXPORT_SYMBOL(vio_unmap_sg);
518
519 void *vio_alloc_consistent(struct vio_dev *dev, size_t size,
520                            dma_addr_t *dma_handle)
521 {
522         return iommu_alloc_consistent(dev->iommu_table, size, dma_handle);
523 }
524 EXPORT_SYMBOL(vio_alloc_consistent);
525
526 void vio_free_consistent(struct vio_dev *dev, size_t size,
527                          void *vaddr, dma_addr_t dma_handle)
528 {
529         iommu_free_consistent(dev->iommu_table, size, vaddr, dma_handle);
530 }
531 EXPORT_SYMBOL(vio_free_consistent);
532
533 static int vio_bus_match(struct device *dev, struct device_driver *drv)
534 {
535         const struct vio_dev *vio_dev = to_vio_dev(dev);
536         struct vio_driver *vio_drv = to_vio_driver(drv);
537         const struct vio_device_id *ids = vio_drv->id_table;
538         const struct vio_device_id *found_id;
539
540         DBGENTER();
541
542         if (!ids)
543                 return 0;
544
545         found_id = vio_match_device(ids, vio_dev);
546         if (found_id)
547                 return 1;
548
549         return 0;
550 }
551
552 struct bus_type vio_bus_type = {
553         .name = "vio",
554         .match = vio_bus_match,
555 };
556
557 EXPORT_SYMBOL(vio_bus_type);