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/init.h>
17 #include <linux/console.h>
18 #include <linux/module.h>
20 #include <linux/dma-mapping.h>
21 #include <asm/iommu.h>
25 #include <asm/firmware.h>
27 struct vio_dev vio_bus_device = { /* fake "parent" device */
28 .name = vio_bus_device.dev.bus_id,
31 .dev.bus = &vio_bus_type,
34 static struct vio_bus_ops vio_bus_ops;
37 * vio_match_device: - Tell if a VIO device has a matching
38 * VIO device id structure.
39 * @ids: array of VIO device id structures to search in
40 * @dev: the VIO device structure to match against
42 * Used by a driver to check whether a VIO device present in the
43 * system is in its list of supported devices. Returns the matching
44 * vio_device_id structure or NULL if there is no match.
46 static const struct vio_device_id *vio_match_device(
47 const struct vio_device_id *ids, const struct vio_dev *dev)
49 while (ids->type[0] != '\0') {
50 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
51 device_is_compatible(dev->dev.platform_data, ids->compat))
59 * Convert from struct device to struct vio_dev and pass to driver.
60 * dev->driver has already been set by generic code because vio_bus_match
63 static int vio_bus_probe(struct device *dev)
65 struct vio_dev *viodev = to_vio_dev(dev);
66 struct vio_driver *viodrv = to_vio_driver(dev->driver);
67 const struct vio_device_id *id;
73 id = vio_match_device(viodrv->id_table, viodev);
75 error = viodrv->probe(viodev, id);
80 /* convert from struct device to struct vio_dev and pass to driver. */
81 static int vio_bus_remove(struct device *dev)
83 struct vio_dev *viodev = to_vio_dev(dev);
84 struct vio_driver *viodrv = to_vio_driver(dev->driver);
87 return viodrv->remove(viodev);
89 /* driver can't remove */
93 /* convert from struct device to struct vio_dev and pass to driver. */
94 static void vio_bus_shutdown(struct device *dev)
96 struct vio_dev *viodev = to_vio_dev(dev);
97 struct vio_driver *viodrv = to_vio_driver(dev->driver);
99 if (dev->driver && viodrv->shutdown)
100 viodrv->shutdown(viodev);
104 * vio_register_driver: - Register a new vio driver
105 * @drv: The vio_driver structure to be registered.
107 int vio_register_driver(struct vio_driver *viodrv)
109 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
110 viodrv->driver.name);
112 /* fill in 'struct driver' fields */
113 viodrv->driver.bus = &vio_bus_type;
115 return driver_register(&viodrv->driver);
117 EXPORT_SYMBOL(vio_register_driver);
120 * vio_unregister_driver - Remove registration of vio driver.
121 * @driver: The vio_driver struct to be removed form registration
123 void vio_unregister_driver(struct vio_driver *viodrv)
125 driver_unregister(&viodrv->driver);
127 EXPORT_SYMBOL(vio_unregister_driver);
130 * vio_register_device_node: - Register a new vio device.
131 * @of_node: The OF node for this device.
133 * Creates and initializes a vio_dev structure from the data in
134 * of_node (dev.platform_data) and adds it to the list of virtual devices.
135 * Returns a pointer to the created vio_dev or NULL if node has
136 * NULL device_type or compatible fields.
138 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
140 struct vio_dev *viodev;
141 unsigned int *unit_address;
144 /* we need the 'device_type' property, in order to match with drivers */
145 if (of_node->type == NULL) {
146 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
148 of_node->name ? of_node->name : "<unknown>");
152 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
153 if (unit_address == NULL) {
154 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
156 of_node->name ? of_node->name : "<unknown>");
160 /* allocate a vio_dev for this node */
161 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
165 viodev->dev.platform_data = of_node_get(of_node);
167 viodev->irq = NO_IRQ;
168 irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
170 int virq = virt_irq_create_mapping(*irq_p);
171 if (virq == NO_IRQ) {
172 printk(KERN_ERR "Unable to allocate interrupt "
173 "number for %s\n", of_node->full_name);
175 viodev->irq = irq_offset_up(virq);
178 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
179 viodev->name = of_node->name;
180 viodev->type = of_node->type;
181 viodev->unit_address = *unit_address;
182 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
183 unit_address = (unsigned int *)get_property(of_node,
184 "linux,unit_address", NULL);
185 if (unit_address != NULL)
186 viodev->unit_address = *unit_address;
188 viodev->iommu_table = vio_bus_ops.build_iommu_table(viodev);
190 /* register with generic device framework */
191 if (vio_register_device(viodev) == NULL) {
192 /* XXX free TCE table */
199 EXPORT_SYMBOL(vio_register_device_node);
202 * vio_bus_init: - Initialize the virtual IO bus
204 int __init vio_bus_init(struct vio_bus_ops *ops)
207 struct device_node *node_vroot;
211 err = bus_register(&vio_bus_type);
213 printk(KERN_ERR "failed to register VIO bus\n");
218 * The fake parent of all vio devices, just to give us
221 err = device_register(&vio_bus_device.dev);
223 printk(KERN_WARNING "%s: device_register returned %i\n",
228 node_vroot = find_devices("vdevice");
230 struct device_node *of_node;
233 * Create struct vio_devices for each virtual device in
234 * the device tree. Drivers will associate with them later.
236 for (of_node = node_vroot->child; of_node != NULL;
237 of_node = of_node->sibling) {
238 printk(KERN_DEBUG "%s: processing %p\n",
239 __FUNCTION__, of_node);
240 vio_register_device_node(of_node);
247 /* vio_dev refcount hit 0 */
248 static void __devinit vio_dev_release(struct device *dev)
250 if (dev->platform_data) {
251 /* XXX free TCE table */
252 of_node_put(dev->platform_data);
254 kfree(to_vio_dev(dev));
257 static ssize_t name_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
263 static ssize_t devspec_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
266 struct device_node *of_node = dev->platform_data;
268 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
271 static struct device_attribute vio_dev_attrs[] = {
277 struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
279 /* init generic 'struct device' fields: */
280 viodev->dev.parent = &vio_bus_device.dev;
281 viodev->dev.bus = &vio_bus_type;
282 viodev->dev.release = vio_dev_release;
284 /* register with generic device framework */
285 if (device_register(&viodev->dev)) {
286 printk(KERN_ERR "%s: failed to register device %s\n",
287 __FUNCTION__, viodev->dev.bus_id);
294 void __devinit vio_unregister_device(struct vio_dev *viodev)
296 device_unregister(&viodev->dev);
298 EXPORT_SYMBOL(vio_unregister_device);
300 static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
301 size_t size, enum dma_data_direction direction)
303 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
307 static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
308 size_t size, enum dma_data_direction direction)
310 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
314 static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
315 int nelems, enum dma_data_direction direction)
317 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
318 nelems, ~0ul, direction);
321 static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
322 int nelems, enum dma_data_direction direction)
324 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
327 static void *vio_alloc_coherent(struct device *dev, size_t size,
328 dma_addr_t *dma_handle, gfp_t flag)
330 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
331 dma_handle, ~0ul, flag);
334 static void vio_free_coherent(struct device *dev, size_t size,
335 void *vaddr, dma_addr_t dma_handle)
337 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
341 static int vio_dma_supported(struct device *dev, u64 mask)
346 struct dma_mapping_ops vio_dma_ops = {
347 .alloc_coherent = vio_alloc_coherent,
348 .free_coherent = vio_free_coherent,
349 .map_single = vio_map_single,
350 .unmap_single = vio_unmap_single,
351 .map_sg = vio_map_sg,
352 .unmap_sg = vio_unmap_sg,
353 .dma_supported = vio_dma_supported,
356 static int vio_bus_match(struct device *dev, struct device_driver *drv)
358 const struct vio_dev *vio_dev = to_vio_dev(dev);
359 struct vio_driver *vio_drv = to_vio_driver(drv);
360 const struct vio_device_id *ids = vio_drv->id_table;
362 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
365 static int vio_hotplug(struct device *dev, char **envp, int num_envp,
366 char *buffer, int buffer_size)
368 const struct vio_dev *vio_dev = to_vio_dev(dev);
369 struct device_node *dn = dev->platform_data;
378 cp = (char *)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 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 get_property(vdev->dev.platform_data, which, length);
414 EXPORT_SYMBOL(vio_get_attribute);