VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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/HvTypes.h>
30 #include <asm/iSeries/HvCallXm.h>
31 #include <asm/iSeries/HvLpConfig.h>
32
33 #define DBGENTER() pr_debug("%s entered\n", __FUNCTION__)
34
35 extern struct subsystem devices_subsys; /* needed for vio_find_name() */
36
37 static const struct vio_device_id *vio_match_device(
38                 const struct vio_device_id *, const struct vio_dev *);
39
40 #ifdef CONFIG_PPC_PSERIES
41 static struct iommu_table *vio_build_iommu_table(struct vio_dev *);
42 static int vio_num_address_cells;
43 #endif
44 static struct vio_dev *vio_bus_device; /* fake "parent" device */
45
46 #ifdef CONFIG_PPC_ISERIES
47 static struct vio_dev *__init vio_register_device_iseries(char *type,
48                 uint32_t unit_num);
49
50 static struct iommu_table veth_iommu_table;
51 static struct iommu_table vio_iommu_table;
52
53 static struct vio_dev _vio_dev  = {
54         .iommu_table = &vio_iommu_table,
55         .dev.bus = &vio_bus_type
56 };
57 struct device *iSeries_vio_dev = &_vio_dev.dev;
58 EXPORT_SYMBOL(iSeries_vio_dev);
59
60 #define device_is_compatible(a, b)      1
61
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         while (ids->type) {
147                 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
148                         device_is_compatible(dev->dev.platform_data, ids->compat))
149                         return ids;
150                 ids++;
151         }
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 #ifdef CONFIG_PPC_PSERIES
198 static void probe_bus_pseries(void)
199 {
200         struct device_node *node_vroot, *of_node;
201
202         node_vroot = find_devices("vdevice");
203         if ((node_vroot == NULL) || (node_vroot->child == NULL))
204                 /* this machine doesn't do virtual IO, and that's ok */
205                 return;
206
207         vio_num_address_cells = prom_n_addr_cells(node_vroot->child);
208
209         /*
210          * Create struct vio_devices for each virtual device in the device tree.
211          * Drivers will associate with them later.
212          */
213         for (of_node = node_vroot->child; of_node != NULL;
214                         of_node = of_node->sibling) {
215                 printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
216                 vio_register_device_node(of_node);
217         }
218 }
219 #endif
220
221 #ifdef CONFIG_PPC_ISERIES
222 static void probe_bus_iseries(void)
223 {
224         HvLpIndexMap vlan_map = HvLpConfig_getVirtualLanIndexMap();
225         struct vio_dev *viodev;
226         int i;
227
228         vlan_map = HvLpConfig_getVirtualLanIndexMap();
229         for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
230                 if ((vlan_map & (0x8000 >> i)) == 0)
231                         continue;
232                 viodev = vio_register_device_iseries("vlan", i);
233                 /* veth is special and has it own iommu_table */
234                 viodev->iommu_table = &veth_iommu_table;
235         }
236         for (i = 0; i < HVMAXARCHITECTEDVIRTUALDISKS; i++)
237                 vio_register_device_iseries("viodasd", i);
238         for (i = 0; i < HVMAXARCHITECTEDVIRTUALCDROMS; i++)
239                 vio_register_device_iseries("viocd", i);
240         for (i = 0; i < HVMAXARCHITECTEDVIRTUALTAPES; i++)
241                 vio_register_device_iseries("viotape", i);
242 }
243 #endif
244
245 /**
246  * vio_bus_init: - Initialize the virtual IO bus
247  */
248 static int __init vio_bus_init(void)
249 {
250         int err;
251
252         err = bus_register(&vio_bus_type);
253         if (err) {
254                 printk(KERN_ERR "failed to register VIO bus\n");
255                 return err;
256         }
257
258         /* the fake parent of all vio devices, just to give us a nice directory */
259         vio_bus_device = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
260         if (!vio_bus_device) {
261                 return 1;
262         }
263         memset(vio_bus_device, 0, sizeof(struct vio_dev));
264         strcpy(vio_bus_device->dev.bus_id, "vio");
265
266         err = device_register(&vio_bus_device->dev);
267         if (err) {
268                 printk(KERN_WARNING "%s: device_register returned %i\n", __FUNCTION__,
269                         err);
270                 kfree(vio_bus_device);
271                 return err;
272         }
273
274 #ifdef CONFIG_PPC_PSERIES
275         probe_bus_pseries();
276 #endif
277 #ifdef CONFIG_PPC_ISERIES
278         probe_bus_iseries();
279 #endif
280
281         return 0;
282 }
283
284 __initcall(vio_bus_init);
285
286 /* vio_dev refcount hit 0 */
287 static void __devinit vio_dev_release(struct device *dev)
288 {
289         DBGENTER();
290
291 #ifdef CONFIG_PPC_PSERIES
292         /* XXX free TCE table */
293         of_node_put(dev->platform_data);
294 #endif
295         kfree(to_vio_dev(dev));
296 }
297
298 #ifdef CONFIG_PPC_PSERIES
299 static ssize_t viodev_show_devspec(struct device *dev, char *buf)
300 {
301         struct device_node *of_node = dev->platform_data;
302
303         return sprintf(buf, "%s\n", of_node->full_name);
304 }
305 DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL);
306 #endif
307
308 static ssize_t viodev_show_name(struct device *dev, char *buf)
309 {
310         return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
311 }
312 DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
313
314 static struct vio_dev * __devinit vio_register_device_common(
315                 struct vio_dev *viodev, char *name, char *type,
316                 uint32_t unit_address, struct iommu_table *iommu_table)
317 {
318         DBGENTER();
319
320         viodev->name = name;
321         viodev->type = type;
322         viodev->unit_address = unit_address;
323         viodev->iommu_table = iommu_table;
324         /* init generic 'struct device' fields: */
325         viodev->dev.parent = &vio_bus_device->dev;
326         viodev->dev.bus = &vio_bus_type;
327         viodev->dev.release = vio_dev_release;
328
329         /* register with generic device framework */
330         if (device_register(&viodev->dev)) {
331                 printk(KERN_ERR "%s: failed to register device %s\n",
332                                 __FUNCTION__, viodev->dev.bus_id);
333                 return NULL;
334         }
335         device_create_file(&viodev->dev, &dev_attr_name);
336
337         return viodev;
338 }
339
340 #ifdef CONFIG_PPC_PSERIES
341 /**
342  * vio_register_device_node: - Register a new vio device.
343  * @of_node:    The OF node for this device.
344  *
345  * Creates and initializes a vio_dev structure from the data in
346  * of_node (dev.platform_data) and adds it to the list of virtual devices.
347  * Returns a pointer to the created vio_dev or NULL if node has
348  * NULL device_type or compatible fields.
349  */
350 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
351 {
352         struct vio_dev *viodev;
353         unsigned int *unit_address;
354         unsigned int *irq_p;
355
356         DBGENTER();
357
358         /* we need the 'device_type' property, in order to match with drivers */
359         if ((NULL == of_node->type)) {
360                 printk(KERN_WARNING
361                         "%s: node %s missing 'device_type'\n", __FUNCTION__,
362                         of_node->name ? of_node->name : "<unknown>");
363                 return NULL;
364         }
365
366         unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
367         if (!unit_address) {
368                 printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
369                         of_node->name ? of_node->name : "<unknown>");
370                 return NULL;
371         }
372
373         /* allocate a vio_dev for this node */
374         viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
375         if (!viodev) {
376                 return NULL;
377         }
378         memset(viodev, 0, sizeof(struct vio_dev));
379
380         viodev->dev.platform_data = of_node_get(of_node);
381
382         viodev->irq = NO_IRQ;
383         irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
384         if (irq_p) {
385                 int virq = virt_irq_create_mapping(*irq_p);
386                 if (virq == NO_IRQ) {
387                         printk(KERN_ERR "Unable to allocate interrupt "
388                                "number for %s\n", of_node->full_name);
389                 } else
390                         viodev->irq = irq_offset_up(virq);
391         }
392
393         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
394
395         /* register with generic device framework */
396         if (vio_register_device_common(viodev, of_node->name, of_node->type,
397                                 *unit_address, vio_build_iommu_table(viodev))
398                         == NULL) {
399                 /* XXX free TCE table */
400                 kfree(viodev);
401                 return NULL;
402         }
403         device_create_file(&viodev->dev, &dev_attr_devspec);
404
405         return viodev;
406 }
407 EXPORT_SYMBOL(vio_register_device_node);
408 #endif
409
410 #ifdef CONFIG_PPC_ISERIES
411 /**
412  * vio_register_device: - Register a new vio device.
413  * @voidev:     The device to register.
414  */
415 static struct vio_dev *__init vio_register_device_iseries(char *type,
416                 uint32_t unit_num)
417 {
418         struct vio_dev *viodev;
419
420         DBGENTER();
421
422         /* allocate a vio_dev for this node */
423         viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
424         if (!viodev)
425                 return NULL;
426         memset(viodev, 0, sizeof(struct vio_dev));
427
428         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%s%d", type, unit_num);
429
430         return vio_register_device_common(viodev, viodev->dev.bus_id, type,
431                         unit_num, &vio_iommu_table);
432 }
433 #endif
434
435 void __devinit vio_unregister_device(struct vio_dev *viodev)
436 {
437         DBGENTER();
438 #ifdef CONFIG_PPC_PSERIES
439         device_remove_file(&viodev->dev, &dev_attr_devspec);
440 #endif
441         device_remove_file(&viodev->dev, &dev_attr_name);
442         device_unregister(&viodev->dev);
443 }
444 EXPORT_SYMBOL(vio_unregister_device);
445
446 #ifdef CONFIG_PPC_PSERIES
447 /**
448  * vio_get_attribute: - get attribute for virtual device
449  * @vdev:       The vio device to get property.
450  * @which:      The property/attribute to be extracted.
451  * @length:     Pointer to length of returned data size (unused if NULL).
452  *
453  * Calls prom.c's get_property() to return the value of the
454  * attribute specified by the preprocessor constant @which
455 */
456 const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
457 {
458         return get_property(vdev->dev.platform_data, (char*)which, length);
459 }
460 EXPORT_SYMBOL(vio_get_attribute);
461
462 /* vio_find_name() - internal because only vio.c knows how we formatted the
463  * kobject name
464  * XXX once vio_bus_type.devices is actually used as a kset in
465  * drivers/base/bus.c, this function should be removed in favor of
466  * "device_find(kobj_name, &vio_bus_type)"
467  */
468 static struct vio_dev *vio_find_name(const char *kobj_name)
469 {
470         struct kobject *found;
471
472         found = kset_find_obj(&devices_subsys.kset, kobj_name);
473         if (!found)
474                 return NULL;
475
476         return to_vio_dev(container_of(found, struct device, kobj));
477 }
478
479 /**
480  * vio_find_node - find an already-registered vio_dev
481  * @vnode: device_node of the virtual device we're looking for
482  */
483 struct vio_dev *vio_find_node(struct device_node *vnode)
484 {
485         uint32_t *unit_address;
486         char kobj_name[BUS_ID_SIZE];
487
488         /* construct the kobject name from the device node */
489         unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
490         if (!unit_address)
491                 return NULL;
492         snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
493
494         return vio_find_name(kobj_name);
495 }
496 EXPORT_SYMBOL(vio_find_node);
497
498 /**
499  * vio_build_iommu_table: - gets the dma information from OF and builds the TCE tree.
500  * @dev: the virtual device.
501  *
502  * Returns a pointer to the built tce tree, or NULL if it can't
503  * find property.
504 */
505 static struct iommu_table * vio_build_iommu_table(struct vio_dev *dev)
506 {
507         unsigned int *dma_window;
508         struct iommu_table *newTceTable;
509         unsigned long offset;
510         unsigned long size;
511         int dma_window_property_size;
512
513         dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size);
514         if(!dma_window) {
515                 return NULL;
516         }
517
518         newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
519
520         /* RPA docs say that #address-cells is always 1 for virtual
521                 devices, but some older boxes' OF returns 2.  This should
522                 be removed by GA, unless there is legacy OFs that still
523                 have 2 for #address-cells */
524         size = ((dma_window[1+vio_num_address_cells] >> PAGE_SHIFT) << 3)
525                 >> PAGE_SHIFT;
526
527         /* This is just an ugly kludge. Remove as soon as the OF for all
528         machines actually follow the spec and encodes the offset field
529         as phys-encode (that is, #address-cells wide)*/
530         if (dma_window_property_size == 12) {
531                 size = ((dma_window[1] >> PAGE_SHIFT) << 3) >> PAGE_SHIFT;
532         } else if (dma_window_property_size == 20) {
533                 size = ((dma_window[4] >> PAGE_SHIFT) << 3) >> PAGE_SHIFT;
534         } else {
535                 printk(KERN_WARNING "vio_build_iommu_table: Invalid size of ibm,my-dma-window=%i, using 0x80 for size\n", dma_window_property_size);
536                 size = 0x80;
537         }
538
539         /*  There should be some code to extract the phys-encoded offset
540                 using prom_n_addr_cells(). However, according to a comment
541                 on earlier versions, it's always zero, so we don't bother */
542         offset = dma_window[1] >>  PAGE_SHIFT;
543
544         /* TCE table size - measured in units of pages of tce table */
545         newTceTable->it_size            = size;
546         /* offset for VIO should always be 0 */
547         newTceTable->it_offset          = offset;
548         newTceTable->it_busno           = 0;
549         newTceTable->it_index           = (unsigned long)dma_window[0];
550         newTceTable->it_type            = TCE_VB;
551         newTceTable->it_entrysize       = sizeof(union tce_entry);
552
553         return iommu_init_table(newTceTable);
554 }
555
556 int vio_enable_interrupts(struct vio_dev *dev)
557 {
558         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
559         if (rc != H_Success) {
560                 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
561         }
562         return rc;
563 }
564 EXPORT_SYMBOL(vio_enable_interrupts);
565
566 int vio_disable_interrupts(struct vio_dev *dev)
567 {
568         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
569         if (rc != H_Success) {
570                 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
571         }
572         return rc;
573 }
574 EXPORT_SYMBOL(vio_disable_interrupts);
575 #endif
576
577 dma_addr_t vio_map_single(struct vio_dev *dev, void *vaddr,
578                           size_t size, enum dma_data_direction direction)
579 {
580         return iommu_map_single(dev->iommu_table, vaddr, size, direction);
581 }
582 EXPORT_SYMBOL(vio_map_single);
583
584 void vio_unmap_single(struct vio_dev *dev, dma_addr_t dma_handle,
585                       size_t size, enum dma_data_direction direction)
586 {
587         iommu_unmap_single(dev->iommu_table, dma_handle, size, direction);
588 }
589 EXPORT_SYMBOL(vio_unmap_single);
590
591 int vio_map_sg(struct vio_dev *vdev, struct scatterlist *sglist, int nelems,
592                enum dma_data_direction direction)
593 {
594         return iommu_map_sg(&vdev->dev, vdev->iommu_table, sglist,
595                         nelems, direction);
596 }
597 EXPORT_SYMBOL(vio_map_sg);
598
599 void vio_unmap_sg(struct vio_dev *vdev, struct scatterlist *sglist, int nelems,
600                   enum dma_data_direction direction)
601 {
602         iommu_unmap_sg(vdev->iommu_table, sglist, nelems, direction);
603 }
604 EXPORT_SYMBOL(vio_unmap_sg);
605
606 void *vio_alloc_consistent(struct vio_dev *dev, size_t size,
607                            dma_addr_t *dma_handle)
608 {
609         return iommu_alloc_consistent(dev->iommu_table, size, dma_handle);
610 }
611 EXPORT_SYMBOL(vio_alloc_consistent);
612
613 void vio_free_consistent(struct vio_dev *dev, size_t size,
614                          void *vaddr, dma_addr_t dma_handle)
615 {
616         iommu_free_consistent(dev->iommu_table, size, vaddr, dma_handle);
617 }
618 EXPORT_SYMBOL(vio_free_consistent);
619
620 static int vio_bus_match(struct device *dev, struct device_driver *drv)
621 {
622         const struct vio_dev *vio_dev = to_vio_dev(dev);
623         struct vio_driver *vio_drv = to_vio_driver(drv);
624         const struct vio_device_id *ids = vio_drv->id_table;
625         const struct vio_device_id *found_id;
626
627         DBGENTER();
628
629         if (!ids)
630                 return 0;
631
632         found_id = vio_match_device(ids, vio_dev);
633         if (found_id)
634                 return 1;
635
636         return 0;
637 }
638
639 struct bus_type vio_bus_type = {
640         .name = "vio",
641         .match = vio_bus_match,
642 };
643
644 EXPORT_SYMBOL(vio_bus_type);