2 * IBM PowerPC Virtual I/O Infrastructure Support.
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>
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.
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>
22 #include <linux/dma-mapping.h>
23 #include <linux/kobject.h>
25 #include <asm/iommu.h>
29 #include <asm/firmware.h>
31 #include <asm/abs_addr.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>
40 extern struct kset devices_subsys; /* needed for vio_find_name() */
42 static struct vio_dev vio_bus_device = { /* fake "parent" device */
43 .name = vio_bus_device.dev.bus_id,
46 .dev.bus = &vio_bus_type,
49 #ifdef CONFIG_PPC_ISERIES
50 struct device *iSeries_vio_dev = &vio_bus_device.dev;
51 EXPORT_SYMBOL(iSeries_vio_dev);
53 static struct iommu_table veth_iommu_table;
54 static struct iommu_table vio_iommu_table;
56 static void __init iommu_vio_init(void)
58 iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);
59 veth_iommu_table.it_size /= 2;
60 vio_iommu_table = veth_iommu_table;
61 vio_iommu_table.it_offset += veth_iommu_table.it_size;
63 if (!iommu_init_table(&veth_iommu_table, -1))
64 printk("Virtual Bus VETH TCE table failed.\n");
65 if (!iommu_init_table(&vio_iommu_table, -1))
66 printk("Virtual Bus VIO TCE table failed.\n");
70 static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
72 #ifdef CONFIG_PPC_ISERIES
73 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
74 if (strcmp(dev->type, "network") == 0)
75 return &veth_iommu_table;
76 return &vio_iommu_table;
80 const unsigned char *dma_window;
81 struct iommu_table *tbl;
82 unsigned long offset, size;
84 dma_window = of_get_property(dev->dev.archdata.of_node,
85 "ibm,my-dma-window", NULL);
89 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
91 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
92 &tbl->it_index, &offset, &size);
94 /* TCE table size - measured in tce entries */
95 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
96 /* offset for VIO should always be 0 */
97 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
99 tbl->it_type = TCE_VB;
101 return iommu_init_table(tbl, -1);
106 * vio_match_device: - Tell if a VIO device has a matching
107 * VIO device id structure.
108 * @ids: array of VIO device id structures to search in
109 * @dev: the VIO device structure to match against
111 * Used by a driver to check whether a VIO device present in the
112 * system is in its list of supported devices. Returns the matching
113 * vio_device_id structure or NULL if there is no match.
115 static const struct vio_device_id *vio_match_device(
116 const struct vio_device_id *ids, const struct vio_dev *dev)
118 while (ids->type[0] != '\0') {
119 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
120 of_device_is_compatible(dev->dev.archdata.of_node,
129 * Convert from struct device to struct vio_dev and pass to driver.
130 * dev->driver has already been set by generic code because vio_bus_match
133 static int vio_bus_probe(struct device *dev)
135 struct vio_dev *viodev = to_vio_dev(dev);
136 struct vio_driver *viodrv = to_vio_driver(dev->driver);
137 const struct vio_device_id *id;
143 id = vio_match_device(viodrv->id_table, viodev);
145 error = viodrv->probe(viodev, id);
150 /* convert from struct device to struct vio_dev and pass to driver. */
151 static int vio_bus_remove(struct device *dev)
153 struct vio_dev *viodev = to_vio_dev(dev);
154 struct vio_driver *viodrv = to_vio_driver(dev->driver);
157 return viodrv->remove(viodev);
159 /* driver can't remove */
163 /* convert from struct device to struct vio_dev and pass to driver. */
164 static void vio_bus_shutdown(struct device *dev)
166 struct vio_dev *viodev = to_vio_dev(dev);
167 struct vio_driver *viodrv = to_vio_driver(dev->driver);
169 if (dev->driver && viodrv->shutdown)
170 viodrv->shutdown(viodev);
174 * vio_register_driver: - Register a new vio driver
175 * @drv: The vio_driver structure to be registered.
177 int vio_register_driver(struct vio_driver *viodrv)
179 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
180 viodrv->driver.name);
182 /* fill in 'struct driver' fields */
183 viodrv->driver.bus = &vio_bus_type;
185 return driver_register(&viodrv->driver);
187 EXPORT_SYMBOL(vio_register_driver);
190 * vio_unregister_driver - Remove registration of vio driver.
191 * @driver: The vio_driver struct to be removed form registration
193 void vio_unregister_driver(struct vio_driver *viodrv)
195 driver_unregister(&viodrv->driver);
197 EXPORT_SYMBOL(vio_unregister_driver);
199 /* vio_dev refcount hit 0 */
200 static void __devinit vio_dev_release(struct device *dev)
202 /* XXX should free TCE table */
203 of_node_put(dev->archdata.of_node);
204 kfree(to_vio_dev(dev));
208 * vio_register_device_node: - Register a new vio device.
209 * @of_node: The OF node for this device.
211 * Creates and initializes a vio_dev structure from the data in
212 * of_node and adds it to the list of virtual devices.
213 * Returns a pointer to the created vio_dev or NULL if node has
214 * NULL device_type or compatible fields.
216 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
218 struct vio_dev *viodev;
219 const unsigned int *unit_address;
221 /* we need the 'device_type' property, in order to match with drivers */
222 if (of_node->type == NULL) {
223 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
225 of_node->name ? of_node->name : "<unknown>");
229 unit_address = of_get_property(of_node, "reg", NULL);
230 if (unit_address == NULL) {
231 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
233 of_node->name ? of_node->name : "<unknown>");
237 /* allocate a vio_dev for this node */
238 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
242 viodev->irq = irq_of_parse_and_map(of_node, 0);
244 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
245 viodev->name = of_node->name;
246 viodev->type = of_node->type;
247 viodev->unit_address = *unit_address;
248 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
249 unit_address = of_get_property(of_node,
250 "linux,unit_address", NULL);
251 if (unit_address != NULL)
252 viodev->unit_address = *unit_address;
254 viodev->dev.archdata.of_node = of_node_get(of_node);
255 viodev->dev.archdata.dma_ops = &dma_iommu_ops;
256 viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
257 viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
259 /* init generic 'struct device' fields: */
260 viodev->dev.parent = &vio_bus_device.dev;
261 viodev->dev.bus = &vio_bus_type;
262 viodev->dev.release = vio_dev_release;
264 /* register with generic device framework */
265 if (device_register(&viodev->dev)) {
266 printk(KERN_ERR "%s: failed to register device %s\n",
267 __FUNCTION__, viodev->dev.bus_id);
268 /* XXX free TCE table */
275 EXPORT_SYMBOL(vio_register_device_node);
278 * vio_bus_init: - Initialize the virtual IO bus
280 static int __init vio_bus_init(void)
283 struct device_node *node_vroot;
285 #ifdef CONFIG_PPC_ISERIES
286 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
288 vio_bus_device.dev.archdata.dma_ops = &dma_iommu_ops;
289 vio_bus_device.dev.archdata.dma_data = &vio_iommu_table;
290 iSeries_vio_dev = &vio_bus_device.dev;
292 #endif /* CONFIG_PPC_ISERIES */
294 err = bus_register(&vio_bus_type);
296 printk(KERN_ERR "failed to register VIO bus\n");
301 * The fake parent of all vio devices, just to give us
304 err = device_register(&vio_bus_device.dev);
306 printk(KERN_WARNING "%s: device_register returned %i\n",
311 node_vroot = of_find_node_by_name(NULL, "vdevice");
313 struct device_node *of_node;
316 * Create struct vio_devices for each virtual device in
317 * the device tree. Drivers will associate with them later.
319 for (of_node = node_vroot->child; of_node != NULL;
320 of_node = of_node->sibling)
321 vio_register_device_node(of_node);
322 of_node_put(node_vroot);
327 __initcall(vio_bus_init);
329 static ssize_t name_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
335 static ssize_t devspec_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct device_node *of_node = dev->archdata.of_node;
340 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
343 static struct device_attribute vio_dev_attrs[] = {
349 void __devinit vio_unregister_device(struct vio_dev *viodev)
351 device_unregister(&viodev->dev);
353 EXPORT_SYMBOL(vio_unregister_device);
355 static int vio_bus_match(struct device *dev, struct device_driver *drv)
357 const struct vio_dev *vio_dev = to_vio_dev(dev);
358 struct vio_driver *vio_drv = to_vio_driver(drv);
359 const struct vio_device_id *ids = vio_drv->id_table;
361 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
364 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
365 char *buffer, int buffer_size)
367 const struct vio_dev *vio_dev = to_vio_dev(dev);
368 struct device_node *dn;
375 dn = dev->archdata.of_node;
378 cp = of_get_property(dn, "compatible", &length);
383 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
385 if ((buffer_size - length) <= 0)
391 struct bus_type vio_bus_type = {
393 .dev_attrs = vio_dev_attrs,
394 .uevent = vio_hotplug,
395 .match = vio_bus_match,
396 .probe = vio_bus_probe,
397 .remove = vio_bus_remove,
398 .shutdown = vio_bus_shutdown,
402 * vio_get_attribute: - get attribute for virtual device
403 * @vdev: The vio device to get property.
404 * @which: The property/attribute to be extracted.
405 * @length: Pointer to length of returned data size (unused if NULL).
407 * Calls prom.c's of_get_property() to return the value of the
408 * attribute specified by @which
410 const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
412 return of_get_property(vdev->dev.archdata.of_node, which, length);
414 EXPORT_SYMBOL(vio_get_attribute);
416 #ifdef CONFIG_PPC_PSERIES
417 /* vio_find_name() - internal because only vio.c knows how we formatted the
419 * XXX once vio_bus_type.devices is actually used as a kset in
420 * drivers/base/bus.c, this function should be removed in favor of
421 * "device_find(kobj_name, &vio_bus_type)"
423 static struct vio_dev *vio_find_name(const char *kobj_name)
425 struct kobject *found;
427 found = kset_find_obj(&devices_subsys, kobj_name);
431 return to_vio_dev(container_of(found, struct device, kobj));
435 * vio_find_node - find an already-registered vio_dev
436 * @vnode: device_node of the virtual device we're looking for
438 struct vio_dev *vio_find_node(struct device_node *vnode)
440 const uint32_t *unit_address;
441 char kobj_name[BUS_ID_SIZE];
443 /* construct the kobject name from the device node */
444 unit_address = of_get_property(vnode, "reg", NULL);
447 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
449 return vio_find_name(kobj_name);
451 EXPORT_SYMBOL(vio_find_node);
453 int vio_enable_interrupts(struct vio_dev *dev)
455 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
457 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
460 EXPORT_SYMBOL(vio_enable_interrupts);
462 int vio_disable_interrupts(struct vio_dev *dev)
464 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
466 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
469 EXPORT_SYMBOL(vio_disable_interrupts);
470 #endif /* CONFIG_PPC_PSERIES */