]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/kernel/vio.c
fd631d4d1602a2cddd3ba215c91aaa76fa3277a6
[linux-2.6-omap-h63xx.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/types.h>
17 #include <linux/device.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/module.h>
21 #include <linux/mm.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/kobject.h>
24
25 #include <asm/iommu.h>
26 #include <asm/dma.h>
27 #include <asm/vio.h>
28 #include <asm/prom.h>
29 #include <asm/firmware.h>
30 #include <asm/tce.h>
31 #include <asm/abs_addr.h>
32 #include <asm/page.h>
33 #include <asm/hvcall.h>
34 #include <asm/iseries/vio.h>
35 #include <asm/iseries/hv_types.h>
36 #include <asm/iseries/hv_lp_config.h>
37 #include <asm/iseries/hv_call_xm.h>
38 #include <asm/iseries/iommu.h>
39
40 extern struct kset devices_subsys; /* needed for vio_find_name() */
41
42 static struct bus_type vio_bus_type;
43
44 static struct vio_dev vio_bus_device  = { /* fake "parent" device */
45         .name = vio_bus_device.dev.bus_id,
46         .type = "",
47         .dev.bus_id = "vio",
48         .dev.bus = &vio_bus_type,
49 };
50
51 #ifdef CONFIG_PPC_ISERIES
52 struct device *iSeries_vio_dev = &vio_bus_device.dev;
53 EXPORT_SYMBOL(iSeries_vio_dev);
54
55 static struct iommu_table veth_iommu_table;
56 static struct iommu_table vio_iommu_table;
57
58 static void __init iommu_vio_init(void)
59 {
60         iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);
61         veth_iommu_table.it_size /= 2;
62         vio_iommu_table = veth_iommu_table;
63         vio_iommu_table.it_offset += veth_iommu_table.it_size;
64
65         if (!iommu_init_table(&veth_iommu_table, -1))
66                 printk("Virtual Bus VETH TCE table failed.\n");
67         if (!iommu_init_table(&vio_iommu_table, -1))
68                 printk("Virtual Bus VIO TCE table failed.\n");
69         vio_bus_device.dev.archdata.dma_ops = &dma_iommu_ops;
70         vio_bus_device.dev.archdata.dma_data = &vio_iommu_table;
71 }
72 #else
73 static void __init iommu_vio_init(void)
74 {
75 }
76 #endif
77
78 static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
79 {
80 #ifdef CONFIG_PPC_ISERIES
81         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
82                 if (strcmp(dev->type, "network") == 0)
83                         return &veth_iommu_table;
84                 return &vio_iommu_table;
85         } else
86 #endif
87         {
88                 const unsigned char *dma_window;
89                 struct iommu_table *tbl;
90                 unsigned long offset, size;
91
92                 dma_window = of_get_property(dev->dev.archdata.of_node,
93                                           "ibm,my-dma-window", NULL);
94                 if (!dma_window)
95                         return NULL;
96
97                 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
98
99                 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
100                                     &tbl->it_index, &offset, &size);
101
102                 /* TCE table size - measured in tce entries */
103                 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
104                 /* offset for VIO should always be 0 */
105                 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
106                 tbl->it_busno = 0;
107                 tbl->it_type = TCE_VB;
108
109                 return iommu_init_table(tbl, -1);
110         }
111 }
112
113 /**
114  * vio_match_device: - Tell if a VIO device has a matching
115  *                      VIO device id structure.
116  * @ids:        array of VIO device id structures to search in
117  * @dev:        the VIO device structure to match against
118  *
119  * Used by a driver to check whether a VIO device present in the
120  * system is in its list of supported devices. Returns the matching
121  * vio_device_id structure or NULL if there is no match.
122  */
123 static const struct vio_device_id *vio_match_device(
124                 const struct vio_device_id *ids, const struct vio_dev *dev)
125 {
126         while (ids->type[0] != '\0') {
127                 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
128                     of_device_is_compatible(dev->dev.archdata.of_node,
129                                          ids->compat))
130                         return ids;
131                 ids++;
132         }
133         return NULL;
134 }
135
136 /*
137  * Convert from struct device to struct vio_dev and pass to driver.
138  * dev->driver has already been set by generic code because vio_bus_match
139  * succeeded.
140  */
141 static int vio_bus_probe(struct device *dev)
142 {
143         struct vio_dev *viodev = to_vio_dev(dev);
144         struct vio_driver *viodrv = to_vio_driver(dev->driver);
145         const struct vio_device_id *id;
146         int error = -ENODEV;
147
148         if (!viodrv->probe)
149                 return error;
150
151         id = vio_match_device(viodrv->id_table, viodev);
152         if (id)
153                 error = viodrv->probe(viodev, id);
154
155         return error;
156 }
157
158 /* convert from struct device to struct vio_dev and pass to driver. */
159 static int vio_bus_remove(struct device *dev)
160 {
161         struct vio_dev *viodev = to_vio_dev(dev);
162         struct vio_driver *viodrv = to_vio_driver(dev->driver);
163
164         if (viodrv->remove)
165                 return viodrv->remove(viodev);
166
167         /* driver can't remove */
168         return 1;
169 }
170
171 /**
172  * vio_register_driver: - Register a new vio driver
173  * @drv:        The vio_driver structure to be registered.
174  */
175 int vio_register_driver(struct vio_driver *viodrv)
176 {
177         printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
178                 viodrv->driver.name);
179
180         /* fill in 'struct driver' fields */
181         viodrv->driver.bus = &vio_bus_type;
182
183         return driver_register(&viodrv->driver);
184 }
185 EXPORT_SYMBOL(vio_register_driver);
186
187 /**
188  * vio_unregister_driver - Remove registration of vio driver.
189  * @driver:     The vio_driver struct to be removed form registration
190  */
191 void vio_unregister_driver(struct vio_driver *viodrv)
192 {
193         driver_unregister(&viodrv->driver);
194 }
195 EXPORT_SYMBOL(vio_unregister_driver);
196
197 /* vio_dev refcount hit 0 */
198 static void __devinit vio_dev_release(struct device *dev)
199 {
200         /* XXX should free TCE table */
201         of_node_put(dev->archdata.of_node);
202         kfree(to_vio_dev(dev));
203 }
204
205 /**
206  * vio_register_device_node: - Register a new vio device.
207  * @of_node:    The OF node for this device.
208  *
209  * Creates and initializes a vio_dev structure from the data in
210  * of_node and adds it to the list of virtual devices.
211  * Returns a pointer to the created vio_dev or NULL if node has
212  * NULL device_type or compatible fields.
213  */
214 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
215 {
216         struct vio_dev *viodev;
217         const unsigned int *unit_address;
218
219         /* we need the 'device_type' property, in order to match with drivers */
220         if (of_node->type == NULL) {
221                 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
222                                 __FUNCTION__,
223                                 of_node->name ? of_node->name : "<unknown>");
224                 return NULL;
225         }
226
227         unit_address = of_get_property(of_node, "reg", NULL);
228         if (unit_address == NULL) {
229                 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
230                                 __FUNCTION__,
231                                 of_node->name ? of_node->name : "<unknown>");
232                 return NULL;
233         }
234
235         /* allocate a vio_dev for this node */
236         viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
237         if (viodev == NULL)
238                 return NULL;
239
240         viodev->irq = irq_of_parse_and_map(of_node, 0);
241
242         snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
243         viodev->name = of_node->name;
244         viodev->type = of_node->type;
245         viodev->unit_address = *unit_address;
246         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
247                 unit_address = of_get_property(of_node,
248                                 "linux,unit_address", NULL);
249                 if (unit_address != NULL)
250                         viodev->unit_address = *unit_address;
251         }
252         viodev->dev.archdata.of_node = of_node_get(of_node);
253         viodev->dev.archdata.dma_ops = &dma_iommu_ops;
254         viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
255         viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
256
257         /* init generic 'struct device' fields: */
258         viodev->dev.parent = &vio_bus_device.dev;
259         viodev->dev.bus = &vio_bus_type;
260         viodev->dev.release = vio_dev_release;
261
262         /* register with generic device framework */
263         if (device_register(&viodev->dev)) {
264                 printk(KERN_ERR "%s: failed to register device %s\n",
265                                 __FUNCTION__, viodev->dev.bus_id);
266                 /* XXX free TCE table */
267                 kfree(viodev);
268                 return NULL;
269         }
270
271         return viodev;
272 }
273 EXPORT_SYMBOL(vio_register_device_node);
274
275 /**
276  * vio_bus_init: - Initialize the virtual IO bus
277  */
278 static int __init vio_bus_init(void)
279 {
280         int err;
281         struct device_node *node_vroot;
282
283         if (firmware_has_feature(FW_FEATURE_ISERIES))
284                 iommu_vio_init();
285
286         err = bus_register(&vio_bus_type);
287         if (err) {
288                 printk(KERN_ERR "failed to register VIO bus\n");
289                 return err;
290         }
291
292         /*
293          * The fake parent of all vio devices, just to give us
294          * a nice directory
295          */
296         err = device_register(&vio_bus_device.dev);
297         if (err) {
298                 printk(KERN_WARNING "%s: device_register returned %i\n",
299                                 __FUNCTION__, err);
300                 return err;
301         }
302
303         node_vroot = of_find_node_by_name(NULL, "vdevice");
304         if (node_vroot) {
305                 struct device_node *of_node;
306
307                 /*
308                  * Create struct vio_devices for each virtual device in
309                  * the device tree. Drivers will associate with them later.
310                  */
311                 for (of_node = node_vroot->child; of_node != NULL;
312                                 of_node = of_node->sibling)
313                         vio_register_device_node(of_node);
314                 of_node_put(node_vroot);
315         }
316
317         return 0;
318 }
319 __initcall(vio_bus_init);
320
321 static ssize_t name_show(struct device *dev,
322                 struct device_attribute *attr, char *buf)
323 {
324         return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
325 }
326
327 static ssize_t devspec_show(struct device *dev,
328                 struct device_attribute *attr, char *buf)
329 {
330         struct device_node *of_node = dev->archdata.of_node;
331
332         return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
333 }
334
335 static struct device_attribute vio_dev_attrs[] = {
336         __ATTR_RO(name),
337         __ATTR_RO(devspec),
338         __ATTR_NULL
339 };
340
341 void __devinit vio_unregister_device(struct vio_dev *viodev)
342 {
343         device_unregister(&viodev->dev);
344 }
345 EXPORT_SYMBOL(vio_unregister_device);
346
347 static int vio_bus_match(struct device *dev, struct device_driver *drv)
348 {
349         const struct vio_dev *vio_dev = to_vio_dev(dev);
350         struct vio_driver *vio_drv = to_vio_driver(drv);
351         const struct vio_device_id *ids = vio_drv->id_table;
352
353         return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
354 }
355
356 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
357                         char *buffer, int buffer_size)
358 {
359         const struct vio_dev *vio_dev = to_vio_dev(dev);
360         struct device_node *dn;
361         const char *cp;
362         int length;
363
364         if (!num_envp)
365                 return -ENOMEM;
366
367         dn = dev->archdata.of_node;
368         if (!dn)
369                 return -ENODEV;
370         cp = of_get_property(dn, "compatible", &length);
371         if (!cp)
372                 return -ENODEV;
373
374         envp[0] = buffer;
375         length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
376                                 vio_dev->type, cp);
377         if ((buffer_size - length) <= 0)
378                 return -ENOMEM;
379         envp[1] = NULL;
380         return 0;
381 }
382
383 static struct bus_type vio_bus_type = {
384         .name = "vio",
385         .dev_attrs = vio_dev_attrs,
386         .uevent = vio_hotplug,
387         .match = vio_bus_match,
388         .probe = vio_bus_probe,
389         .remove = vio_bus_remove,
390 };
391
392 /**
393  * vio_get_attribute: - get attribute for virtual device
394  * @vdev:       The vio device to get property.
395  * @which:      The property/attribute to be extracted.
396  * @length:     Pointer to length of returned data size (unused if NULL).
397  *
398  * Calls prom.c's of_get_property() to return the value of the
399  * attribute specified by @which
400 */
401 const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
402 {
403         return of_get_property(vdev->dev.archdata.of_node, which, length);
404 }
405 EXPORT_SYMBOL(vio_get_attribute);
406
407 #ifdef CONFIG_PPC_PSERIES
408 /* vio_find_name() - internal because only vio.c knows how we formatted the
409  * kobject name
410  * XXX once vio_bus_type.devices is actually used as a kset in
411  * drivers/base/bus.c, this function should be removed in favor of
412  * "device_find(kobj_name, &vio_bus_type)"
413  */
414 static struct vio_dev *vio_find_name(const char *kobj_name)
415 {
416         struct kobject *found;
417
418         found = kset_find_obj(&devices_subsys, kobj_name);
419         if (!found)
420                 return NULL;
421
422         return to_vio_dev(container_of(found, struct device, kobj));
423 }
424
425 /**
426  * vio_find_node - find an already-registered vio_dev
427  * @vnode: device_node of the virtual device we're looking for
428  */
429 struct vio_dev *vio_find_node(struct device_node *vnode)
430 {
431         const uint32_t *unit_address;
432         char kobj_name[BUS_ID_SIZE];
433
434         /* construct the kobject name from the device node */
435         unit_address = of_get_property(vnode, "reg", NULL);
436         if (!unit_address)
437                 return NULL;
438         snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
439
440         return vio_find_name(kobj_name);
441 }
442 EXPORT_SYMBOL(vio_find_node);
443
444 int vio_enable_interrupts(struct vio_dev *dev)
445 {
446         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
447         if (rc != H_SUCCESS)
448                 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
449         return rc;
450 }
451 EXPORT_SYMBOL(vio_enable_interrupts);
452
453 int vio_disable_interrupts(struct vio_dev *dev)
454 {
455         int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
456         if (rc != H_SUCCESS)
457                 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
458         return rc;
459 }
460 EXPORT_SYMBOL(vio_disable_interrupts);
461 #endif /* CONFIG_PPC_PSERIES */