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