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 bus_type vio_bus_type;
44 static struct vio_dev vio_bus_device = { /* fake "parent" device */
45 .name = vio_bus_device.dev.bus_id,
48 .dev.bus = &vio_bus_type,
51 #ifdef CONFIG_PPC_ISERIES
52 struct device *iSeries_vio_dev = &vio_bus_device.dev;
53 EXPORT_SYMBOL(iSeries_vio_dev);
55 static struct iommu_table veth_iommu_table;
56 static struct iommu_table vio_iommu_table;
58 static void __init iommu_vio_init(void)
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;
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;
73 static void __init iommu_vio_init(void)
78 static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
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;
88 const unsigned char *dma_window;
89 struct iommu_table *tbl;
90 unsigned long offset, size;
92 dma_window = of_get_property(dev->dev.archdata.of_node,
93 "ibm,my-dma-window", NULL);
97 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
99 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
100 &tbl->it_index, &offset, &size);
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;
107 tbl->it_type = TCE_VB;
109 return iommu_init_table(tbl, -1);
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
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.
123 static const struct vio_device_id *vio_match_device(
124 const struct vio_device_id *ids, const struct vio_dev *dev)
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,
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
141 static int vio_bus_probe(struct device *dev)
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;
151 id = vio_match_device(viodrv->id_table, viodev);
153 error = viodrv->probe(viodev, id);
158 /* convert from struct device to struct vio_dev and pass to driver. */
159 static int vio_bus_remove(struct device *dev)
161 struct vio_dev *viodev = to_vio_dev(dev);
162 struct vio_driver *viodrv = to_vio_driver(dev->driver);
165 return viodrv->remove(viodev);
167 /* driver can't remove */
171 /* convert from struct device to struct vio_dev and pass to driver. */
172 static void vio_bus_shutdown(struct device *dev)
174 struct vio_dev *viodev = to_vio_dev(dev);
175 struct vio_driver *viodrv = to_vio_driver(dev->driver);
177 if (dev->driver && viodrv->shutdown)
178 viodrv->shutdown(viodev);
182 * vio_register_driver: - Register a new vio driver
183 * @drv: The vio_driver structure to be registered.
185 int vio_register_driver(struct vio_driver *viodrv)
187 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
188 viodrv->driver.name);
190 /* fill in 'struct driver' fields */
191 viodrv->driver.bus = &vio_bus_type;
193 return driver_register(&viodrv->driver);
195 EXPORT_SYMBOL(vio_register_driver);
198 * vio_unregister_driver - Remove registration of vio driver.
199 * @driver: The vio_driver struct to be removed form registration
201 void vio_unregister_driver(struct vio_driver *viodrv)
203 driver_unregister(&viodrv->driver);
205 EXPORT_SYMBOL(vio_unregister_driver);
207 /* vio_dev refcount hit 0 */
208 static void __devinit vio_dev_release(struct device *dev)
210 /* XXX should free TCE table */
211 of_node_put(dev->archdata.of_node);
212 kfree(to_vio_dev(dev));
216 * vio_register_device_node: - Register a new vio device.
217 * @of_node: The OF node for this device.
219 * Creates and initializes a vio_dev structure from the data in
220 * of_node and adds it to the list of virtual devices.
221 * Returns a pointer to the created vio_dev or NULL if node has
222 * NULL device_type or compatible fields.
224 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
226 struct vio_dev *viodev;
227 const unsigned int *unit_address;
229 /* we need the 'device_type' property, in order to match with drivers */
230 if (of_node->type == NULL) {
231 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
233 of_node->name ? of_node->name : "<unknown>");
237 unit_address = of_get_property(of_node, "reg", NULL);
238 if (unit_address == NULL) {
239 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
241 of_node->name ? of_node->name : "<unknown>");
245 /* allocate a vio_dev for this node */
246 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
250 viodev->irq = irq_of_parse_and_map(of_node, 0);
252 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
253 viodev->name = of_node->name;
254 viodev->type = of_node->type;
255 viodev->unit_address = *unit_address;
256 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
257 unit_address = of_get_property(of_node,
258 "linux,unit_address", NULL);
259 if (unit_address != NULL)
260 viodev->unit_address = *unit_address;
262 viodev->dev.archdata.of_node = of_node_get(of_node);
263 viodev->dev.archdata.dma_ops = &dma_iommu_ops;
264 viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
265 viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
267 /* init generic 'struct device' fields: */
268 viodev->dev.parent = &vio_bus_device.dev;
269 viodev->dev.bus = &vio_bus_type;
270 viodev->dev.release = vio_dev_release;
272 /* register with generic device framework */
273 if (device_register(&viodev->dev)) {
274 printk(KERN_ERR "%s: failed to register device %s\n",
275 __FUNCTION__, viodev->dev.bus_id);
276 /* XXX free TCE table */
283 EXPORT_SYMBOL(vio_register_device_node);
286 * vio_bus_init: - Initialize the virtual IO bus
288 static int __init vio_bus_init(void)
291 struct device_node *node_vroot;
293 if (firmware_has_feature(FW_FEATURE_ISERIES))
296 err = bus_register(&vio_bus_type);
298 printk(KERN_ERR "failed to register VIO bus\n");
303 * The fake parent of all vio devices, just to give us
306 err = device_register(&vio_bus_device.dev);
308 printk(KERN_WARNING "%s: device_register returned %i\n",
313 node_vroot = of_find_node_by_name(NULL, "vdevice");
315 struct device_node *of_node;
318 * Create struct vio_devices for each virtual device in
319 * the device tree. Drivers will associate with them later.
321 for (of_node = node_vroot->child; of_node != NULL;
322 of_node = of_node->sibling)
323 vio_register_device_node(of_node);
324 of_node_put(node_vroot);
329 __initcall(vio_bus_init);
331 static ssize_t name_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
337 static ssize_t devspec_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct device_node *of_node = dev->archdata.of_node;
342 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
345 static struct device_attribute vio_dev_attrs[] = {
351 void __devinit vio_unregister_device(struct vio_dev *viodev)
353 device_unregister(&viodev->dev);
355 EXPORT_SYMBOL(vio_unregister_device);
357 static int vio_bus_match(struct device *dev, struct device_driver *drv)
359 const struct vio_dev *vio_dev = to_vio_dev(dev);
360 struct vio_driver *vio_drv = to_vio_driver(drv);
361 const struct vio_device_id *ids = vio_drv->id_table;
363 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
366 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
367 char *buffer, int buffer_size)
369 const struct vio_dev *vio_dev = to_vio_dev(dev);
370 struct device_node *dn;
377 dn = dev->archdata.of_node;
380 cp = of_get_property(dn, "compatible", &length);
385 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
387 if ((buffer_size - length) <= 0)
393 static struct bus_type vio_bus_type = {
395 .dev_attrs = vio_dev_attrs,
396 .uevent = vio_hotplug,
397 .match = vio_bus_match,
398 .probe = vio_bus_probe,
399 .remove = vio_bus_remove,
400 .shutdown = vio_bus_shutdown,
404 * vio_get_attribute: - get attribute for virtual device
405 * @vdev: The vio device to get property.
406 * @which: The property/attribute to be extracted.
407 * @length: Pointer to length of returned data size (unused if NULL).
409 * Calls prom.c's of_get_property() to return the value of the
410 * attribute specified by @which
412 const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
414 return of_get_property(vdev->dev.archdata.of_node, which, length);
416 EXPORT_SYMBOL(vio_get_attribute);
418 #ifdef CONFIG_PPC_PSERIES
419 /* vio_find_name() - internal because only vio.c knows how we formatted the
421 * XXX once vio_bus_type.devices is actually used as a kset in
422 * drivers/base/bus.c, this function should be removed in favor of
423 * "device_find(kobj_name, &vio_bus_type)"
425 static struct vio_dev *vio_find_name(const char *kobj_name)
427 struct kobject *found;
429 found = kset_find_obj(&devices_subsys, kobj_name);
433 return to_vio_dev(container_of(found, struct device, kobj));
437 * vio_find_node - find an already-registered vio_dev
438 * @vnode: device_node of the virtual device we're looking for
440 struct vio_dev *vio_find_node(struct device_node *vnode)
442 const uint32_t *unit_address;
443 char kobj_name[BUS_ID_SIZE];
445 /* construct the kobject name from the device node */
446 unit_address = of_get_property(vnode, "reg", NULL);
449 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
451 return vio_find_name(kobj_name);
453 EXPORT_SYMBOL(vio_find_node);
455 int vio_enable_interrupts(struct vio_dev *dev)
457 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
459 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
462 EXPORT_SYMBOL(vio_enable_interrupts);
464 int vio_disable_interrupts(struct vio_dev *dev)
466 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
468 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
471 EXPORT_SYMBOL(vio_disable_interrupts);
472 #endif /* CONFIG_PPC_PSERIES */