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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static const struct vio_device_id *vio_match_device( |
| 26 | const struct vio_device_id *, const struct vio_dev *); |
| 27 | |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 28 | struct vio_dev vio_bus_device = { /* fake "parent" device */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 29 | .name = vio_bus_device.dev.bus_id, |
| 30 | .type = "", |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 31 | .dev.bus_id = "vio", |
| 32 | .dev.bus = &vio_bus_type, |
| 33 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 35 | static int (*is_match)(const struct vio_device_id *id, |
| 36 | const struct vio_dev *dev); |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame^] | 37 | static void (*unregister_device_callback)(struct vio_dev *dev); |
| 38 | static void (*release_device_callback)(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* convert from struct device to struct vio_dev and pass to driver. |
| 41 | * dev->driver has already been set by generic code because vio_bus_match |
| 42 | * succeeded. */ |
| 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); |
| 54 | if (id) { |
| 55 | error = viodrv->probe(viodev, id); |
| 56 | } |
| 57 | |
| 58 | return error; |
| 59 | } |
| 60 | |
| 61 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 62 | static int vio_bus_remove(struct device *dev) |
| 63 | { |
| 64 | struct vio_dev *viodev = to_vio_dev(dev); |
| 65 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (viodrv->remove) { |
| 68 | return viodrv->remove(viodev); |
| 69 | } |
| 70 | |
| 71 | /* driver can't remove */ |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * vio_register_driver: - Register a new vio driver |
| 77 | * @drv: The vio_driver structure to be registered. |
| 78 | */ |
| 79 | int vio_register_driver(struct vio_driver *viodrv) |
| 80 | { |
| 81 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, |
| 82 | viodrv->name); |
| 83 | |
| 84 | /* fill in 'struct driver' fields */ |
| 85 | viodrv->driver.name = viodrv->name; |
| 86 | viodrv->driver.bus = &vio_bus_type; |
| 87 | viodrv->driver.probe = vio_bus_probe; |
| 88 | viodrv->driver.remove = vio_bus_remove; |
| 89 | |
| 90 | return driver_register(&viodrv->driver); |
| 91 | } |
| 92 | EXPORT_SYMBOL(vio_register_driver); |
| 93 | |
| 94 | /** |
| 95 | * vio_unregister_driver - Remove registration of vio driver. |
| 96 | * @driver: The vio_driver struct to be removed form registration |
| 97 | */ |
| 98 | void vio_unregister_driver(struct vio_driver *viodrv) |
| 99 | { |
| 100 | driver_unregister(&viodrv->driver); |
| 101 | } |
| 102 | EXPORT_SYMBOL(vio_unregister_driver); |
| 103 | |
| 104 | /** |
| 105 | * vio_match_device: - Tell if a VIO device has a matching VIO device id structure. |
| 106 | * @ids: array of VIO device id structures to search in |
| 107 | * @dev: the VIO device structure to match against |
| 108 | * |
| 109 | * Used by a driver to check whether a VIO device present in the |
| 110 | * system is in its list of supported devices. Returns the matching |
| 111 | * vio_device_id structure or NULL if there is no match. |
| 112 | */ |
| 113 | static const struct vio_device_id * vio_match_device(const struct vio_device_id *ids, |
| 114 | const struct vio_dev *dev) |
| 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | while (ids->type) { |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 117 | if (is_match(ids, dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return ids; |
| 119 | ids++; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /** |
| 125 | * vio_bus_init: - Initialize the virtual IO bus |
| 126 | */ |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 127 | int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id, |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame^] | 128 | const struct vio_dev *dev), |
| 129 | void (*unregister_dev)(struct vio_dev *), |
| 130 | void (*release_dev)(struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | int err; |
| 133 | |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 134 | is_match = match_func; |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame^] | 135 | unregister_device_callback = unregister_dev; |
| 136 | release_device_callback = release_dev; |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | err = bus_register(&vio_bus_type); |
| 139 | if (err) { |
| 140 | printk(KERN_ERR "failed to register VIO bus\n"); |
| 141 | return err; |
| 142 | } |
| 143 | |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 144 | /* the fake parent of all vio devices, just to give us |
| 145 | * a nice directory |
| 146 | */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 147 | err = device_register(&vio_bus_device.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (err) { |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 149 | printk(KERN_WARNING "%s: device_register returned %i\n", |
| 150 | __FUNCTION__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return err; |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* vio_dev refcount hit 0 */ |
| 158 | static void __devinit vio_dev_release(struct device *dev) |
| 159 | { |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame^] | 160 | if (release_device_callback) |
| 161 | release_device_callback(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | kfree(to_vio_dev(dev)); |
| 163 | } |
| 164 | |
Yani Ioannou | ff381d2 | 2005-05-17 06:40:51 -0400 | [diff] [blame] | 165 | static ssize_t viodev_show_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
| 168 | } |
| 169 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); |
| 170 | |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 171 | struct vio_dev * __devinit vio_register_device_common( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | struct vio_dev *viodev, char *name, char *type, |
| 173 | uint32_t unit_address, struct iommu_table *iommu_table) |
| 174 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | viodev->name = name; |
| 176 | viodev->type = type; |
| 177 | viodev->unit_address = unit_address; |
| 178 | viodev->iommu_table = iommu_table; |
| 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 | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame^] | 197 | if (unregister_device_callback) |
| 198 | unregister_device_callback(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, |
| 232 | dma_addr_t *dma_handle, unsigned int __nocast flag) |
| 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; |
| 265 | const struct vio_device_id *found_id; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (!ids) |
| 268 | return 0; |
| 269 | |
| 270 | found_id = vio_match_device(ids, vio_dev); |
| 271 | if (found_id) |
| 272 | return 1; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | struct bus_type vio_bus_type = { |
| 278 | .name = "vio", |
| 279 | .match = vio_bus_match, |
| 280 | }; |