Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IBM PowerPC Virtual I/O Infrastructure Support. |
| 3 | * |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 IBM Corp. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Dave Engebretsen engebret@us.ibm.com |
| 6 | * Santiago Leon santil@us.ibm.com |
| 7 | * Hollis Blanchard <hollisb@us.ibm.com> |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame] | 8 | * Stephen Rothwell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/init.h> |
| 17 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mm.h> |
| 20 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/iommu.h> |
| 22 | #include <asm/dma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/vio.h> |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 24 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static const struct vio_device_id *vio_match_device( |
| 27 | const struct vio_device_id *, const struct vio_dev *); |
| 28 | |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 29 | struct vio_dev vio_bus_device = { /* fake "parent" device */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 30 | .name = vio_bus_device.dev.bus_id, |
| 31 | .type = "", |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 32 | .dev.bus_id = "vio", |
| 33 | .dev.bus = &vio_bus_type, |
| 34 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 36 | static struct vio_bus_ops vio_bus_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 38 | /* |
| 39 | * Convert from struct device to struct vio_dev and pass to driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * dev->driver has already been set by generic code because vio_bus_match |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 41 | * succeeded. |
| 42 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static int vio_bus_probe(struct device *dev) |
| 44 | { |
| 45 | struct vio_dev *viodev = to_vio_dev(dev); |
| 46 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 47 | const struct vio_device_id *id; |
| 48 | int error = -ENODEV; |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | if (!viodrv->probe) |
| 51 | return error; |
| 52 | |
| 53 | id = vio_match_device(viodrv->id_table, viodev); |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 54 | if (id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | error = viodrv->probe(viodev, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | return error; |
| 58 | } |
| 59 | |
| 60 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 61 | static int vio_bus_remove(struct device *dev) |
| 62 | { |
| 63 | struct vio_dev *viodev = to_vio_dev(dev); |
| 64 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 65 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 66 | if (viodrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return viodrv->remove(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* driver can't remove */ |
| 70 | return 1; |
| 71 | } |
| 72 | |
Stephen Rothwell | 3406010 | 2005-10-24 17:40:23 +1000 | [diff] [blame] | 73 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 74 | static void vio_bus_shutdown(struct device *dev) |
| 75 | { |
| 76 | struct vio_dev *viodev = to_vio_dev(dev); |
| 77 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 78 | |
| 79 | if (viodrv->shutdown) |
| 80 | viodrv->shutdown(viodev); |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /** |
| 84 | * vio_register_driver: - Register a new vio driver |
| 85 | * @drv: The vio_driver structure to be registered. |
| 86 | */ |
| 87 | int vio_register_driver(struct vio_driver *viodrv) |
| 88 | { |
| 89 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 90 | viodrv->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* fill in 'struct driver' fields */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | viodrv->driver.bus = &vio_bus_type; |
| 94 | viodrv->driver.probe = vio_bus_probe; |
| 95 | viodrv->driver.remove = vio_bus_remove; |
Stephen Rothwell | 3406010 | 2005-10-24 17:40:23 +1000 | [diff] [blame] | 96 | viodrv->driver.shutdown = vio_bus_shutdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | return driver_register(&viodrv->driver); |
| 99 | } |
| 100 | EXPORT_SYMBOL(vio_register_driver); |
| 101 | |
| 102 | /** |
| 103 | * vio_unregister_driver - Remove registration of vio driver. |
| 104 | * @driver: The vio_driver struct to be removed form registration |
| 105 | */ |
| 106 | void vio_unregister_driver(struct vio_driver *viodrv) |
| 107 | { |
| 108 | driver_unregister(&viodrv->driver); |
| 109 | } |
| 110 | EXPORT_SYMBOL(vio_unregister_driver); |
| 111 | |
| 112 | /** |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 113 | * vio_match_device: - Tell if a VIO device has a matching |
| 114 | * VIO device id structure. |
| 115 | * @ids: array of VIO device id structures to search in |
| 116 | * @dev: the VIO device structure to match against |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * |
| 118 | * Used by a driver to check whether a VIO device present in the |
| 119 | * system is in its list of supported devices. Returns the matching |
| 120 | * vio_device_id structure or NULL if there is no match. |
| 121 | */ |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 122 | static const struct vio_device_id *vio_match_device( |
| 123 | const struct vio_device_id *ids, const struct vio_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 125 | while (ids->type[0] != '\0') { |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 126 | if (vio_bus_ops.match(ids, dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return ids; |
| 128 | ids++; |
| 129 | } |
| 130 | return NULL; |
| 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /** |
| 134 | * vio_bus_init: - Initialize the virtual IO bus |
| 135 | */ |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 136 | int __init vio_bus_init(struct vio_bus_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | int err; |
| 139 | |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 140 | vio_bus_ops = *ops; |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | err = bus_register(&vio_bus_type); |
| 143 | if (err) { |
| 144 | printk(KERN_ERR "failed to register VIO bus\n"); |
| 145 | return err; |
| 146 | } |
| 147 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 148 | /* |
| 149 | * The fake parent of all vio devices, just to give us |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 150 | * a nice directory |
| 151 | */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 152 | err = device_register(&vio_bus_device.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (err) { |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 154 | printk(KERN_WARNING "%s: device_register returned %i\n", |
| 155 | __FUNCTION__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return err; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* vio_dev refcount hit 0 */ |
| 163 | static void __devinit vio_dev_release(struct device *dev) |
| 164 | { |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 165 | if (vio_bus_ops.release_device) |
| 166 | vio_bus_ops.release_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | kfree(to_vio_dev(dev)); |
| 168 | } |
| 169 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 170 | static ssize_t viodev_show_name(struct device *dev, |
| 171 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
| 174 | } |
| 175 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); |
| 176 | |
Stephen Rothwell | b877b90 | 2005-08-17 16:40:12 +1000 | [diff] [blame] | 177 | struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* init generic 'struct device' fields: */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 180 | viodev->dev.parent = &vio_bus_device.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | viodev->dev.bus = &vio_bus_type; |
| 182 | viodev->dev.release = vio_dev_release; |
| 183 | |
| 184 | /* register with generic device framework */ |
| 185 | if (device_register(&viodev->dev)) { |
| 186 | printk(KERN_ERR "%s: failed to register device %s\n", |
| 187 | __FUNCTION__, viodev->dev.bus_id); |
| 188 | return NULL; |
| 189 | } |
| 190 | device_create_file(&viodev->dev, &dev_attr_name); |
| 191 | |
| 192 | return viodev; |
| 193 | } |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
| 196 | { |
Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 197 | if (vio_bus_ops.unregister_device) |
| 198 | vio_bus_ops.unregister_device(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | device_remove_file(&viodev->dev, &dev_attr_name); |
| 200 | device_unregister(&viodev->dev); |
| 201 | } |
| 202 | EXPORT_SYMBOL(vio_unregister_device); |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | static dma_addr_t vio_map_single(struct device *dev, void *vaddr, |
| 205 | size_t size, enum dma_data_direction direction) |
| 206 | { |
| 207 | return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size, |
| 208 | direction); |
| 209 | } |
| 210 | |
| 211 | static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle, |
| 212 | size_t size, enum dma_data_direction direction) |
| 213 | { |
| 214 | iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size, |
| 215 | direction); |
| 216 | } |
| 217 | |
| 218 | static int vio_map_sg(struct device *dev, struct scatterlist *sglist, |
| 219 | int nelems, enum dma_data_direction direction) |
| 220 | { |
| 221 | return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist, |
| 222 | nelems, direction); |
| 223 | } |
| 224 | |
| 225 | static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 226 | int nelems, enum dma_data_direction direction) |
| 227 | { |
| 228 | iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction); |
| 229 | } |
| 230 | |
| 231 | static void *vio_alloc_coherent(struct device *dev, size_t size, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 232 | dma_addr_t *dma_handle, gfp_t flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, |
| 235 | dma_handle, flag); |
| 236 | } |
| 237 | |
| 238 | static void vio_free_coherent(struct device *dev, size_t size, |
| 239 | void *vaddr, dma_addr_t dma_handle) |
| 240 | { |
| 241 | iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr, |
| 242 | dma_handle); |
| 243 | } |
| 244 | |
| 245 | static int vio_dma_supported(struct device *dev, u64 mask) |
| 246 | { |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | struct dma_mapping_ops vio_dma_ops = { |
| 251 | .alloc_coherent = vio_alloc_coherent, |
| 252 | .free_coherent = vio_free_coherent, |
| 253 | .map_single = vio_map_single, |
| 254 | .unmap_single = vio_unmap_single, |
| 255 | .map_sg = vio_map_sg, |
| 256 | .unmap_sg = vio_unmap_sg, |
| 257 | .dma_supported = vio_dma_supported, |
| 258 | }; |
| 259 | |
| 260 | static int vio_bus_match(struct device *dev, struct device_driver *drv) |
| 261 | { |
| 262 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
| 263 | struct vio_driver *vio_drv = to_vio_driver(drv); |
| 264 | const struct vio_device_id *ids = vio_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 266 | return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 269 | static int vio_hotplug(struct device *dev, char **envp, int num_envp, |
| 270 | char *buffer, int buffer_size) |
| 271 | { |
| 272 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
| 273 | char *cp; |
| 274 | int length; |
| 275 | |
| 276 | if (!num_envp) |
| 277 | return -ENOMEM; |
| 278 | |
| 279 | if (!vio_dev->dev.platform_data) |
| 280 | return -ENODEV; |
| 281 | cp = (char *)get_property(vio_dev->dev.platform_data, "compatible", &length); |
| 282 | if (!cp) |
| 283 | return -ENODEV; |
| 284 | |
| 285 | envp[0] = buffer; |
| 286 | length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s", |
| 287 | vio_dev->type, cp); |
| 288 | if (buffer_size - length <= 0) |
| 289 | return -ENOMEM; |
| 290 | envp[1] = NULL; |
| 291 | return 0; |
| 292 | } |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | struct bus_type vio_bus_type = { |
| 295 | .name = "vio", |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame^] | 296 | .uevent = vio_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | .match = vio_bus_match, |
| 298 | }; |