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