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 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 40 | /* |
| 41 | * Convert from struct device to struct vio_dev and pass to driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * 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^] | 43 | * succeeded. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static int vio_bus_probe(struct device *dev) |
| 46 | { |
| 47 | struct vio_dev *viodev = to_vio_dev(dev); |
| 48 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 49 | const struct vio_device_id *id; |
| 50 | int error = -ENODEV; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (!viodrv->probe) |
| 53 | return error; |
| 54 | |
| 55 | id = vio_match_device(viodrv->id_table, viodev); |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 56 | if (id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | error = viodrv->probe(viodev, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | return error; |
| 60 | } |
| 61 | |
| 62 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 63 | static int vio_bus_remove(struct device *dev) |
| 64 | { |
| 65 | struct vio_dev *viodev = to_vio_dev(dev); |
| 66 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 67 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 68 | if (viodrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return viodrv->remove(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /** |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 105 | * vio_match_device: - Tell if a VIO device has a matching |
| 106 | * VIO device id structure. |
| 107 | * @ids: array of VIO device id structures to search in |
| 108 | * @dev: the VIO device structure to match against |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * |
| 110 | * Used by a driver to check whether a VIO device present in the |
| 111 | * system is in its list of supported devices. Returns the matching |
| 112 | * vio_device_id structure or NULL if there is no match. |
| 113 | */ |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 114 | static const struct vio_device_id *vio_match_device( |
| 115 | const struct vio_device_id *ids, const struct vio_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | while (ids->type) { |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 118 | if (is_match(ids, dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return ids; |
| 120 | ids++; |
| 121 | } |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /** |
| 126 | * vio_bus_init: - Initialize the virtual IO bus |
| 127 | */ |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 128 | 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] | 129 | const struct vio_dev *dev), |
| 130 | void (*unregister_dev)(struct vio_dev *), |
| 131 | void (*release_dev)(struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | int err; |
| 134 | |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 135 | is_match = match_func; |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame] | 136 | unregister_device_callback = unregister_dev; |
| 137 | release_device_callback = release_dev; |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | err = bus_register(&vio_bus_type); |
| 140 | if (err) { |
| 141 | printk(KERN_ERR "failed to register VIO bus\n"); |
| 142 | return err; |
| 143 | } |
| 144 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 145 | /* |
| 146 | * The fake parent of all vio devices, just to give us |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 147 | * a nice directory |
| 148 | */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 149 | err = device_register(&vio_bus_device.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (err) { |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 151 | printk(KERN_WARNING "%s: device_register returned %i\n", |
| 152 | __FUNCTION__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return err; |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* vio_dev refcount hit 0 */ |
| 160 | static void __devinit vio_dev_release(struct device *dev) |
| 161 | { |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame] | 162 | if (release_device_callback) |
| 163 | release_device_callback(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | kfree(to_vio_dev(dev)); |
| 165 | } |
| 166 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 167 | static ssize_t viodev_show_name(struct device *dev, |
| 168 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
| 171 | } |
| 172 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); |
| 173 | |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 174 | struct vio_dev * __devinit vio_register_device_common( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | struct vio_dev *viodev, char *name, char *type, |
| 176 | uint32_t unit_address, struct iommu_table *iommu_table) |
| 177 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | viodev->name = name; |
| 179 | viodev->type = type; |
| 180 | viodev->unit_address = unit_address; |
| 181 | viodev->iommu_table = iommu_table; |
| 182 | /* init generic 'struct device' fields: */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 183 | viodev->dev.parent = &vio_bus_device.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | viodev->dev.bus = &vio_bus_type; |
| 185 | viodev->dev.release = vio_dev_release; |
| 186 | |
| 187 | /* register with generic device framework */ |
| 188 | if (device_register(&viodev->dev)) { |
| 189 | printk(KERN_ERR "%s: failed to register device %s\n", |
| 190 | __FUNCTION__, viodev->dev.bus_id); |
| 191 | return NULL; |
| 192 | } |
| 193 | device_create_file(&viodev->dev, &dev_attr_name); |
| 194 | |
| 195 | return viodev; |
| 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
| 199 | { |
Stephen Rothwell | 19dbd0f | 2005-07-12 17:50:26 +1000 | [diff] [blame] | 200 | if (unregister_device_callback) |
| 201 | unregister_device_callback(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | device_remove_file(&viodev->dev, &dev_attr_name); |
| 203 | device_unregister(&viodev->dev); |
| 204 | } |
| 205 | EXPORT_SYMBOL(vio_unregister_device); |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | static dma_addr_t vio_map_single(struct device *dev, void *vaddr, |
| 208 | size_t size, enum dma_data_direction direction) |
| 209 | { |
| 210 | return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size, |
| 211 | direction); |
| 212 | } |
| 213 | |
| 214 | static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle, |
| 215 | size_t size, enum dma_data_direction direction) |
| 216 | { |
| 217 | iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size, |
| 218 | direction); |
| 219 | } |
| 220 | |
| 221 | static int vio_map_sg(struct device *dev, struct scatterlist *sglist, |
| 222 | int nelems, enum dma_data_direction direction) |
| 223 | { |
| 224 | return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist, |
| 225 | nelems, direction); |
| 226 | } |
| 227 | |
| 228 | static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 229 | int nelems, enum dma_data_direction direction) |
| 230 | { |
| 231 | iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction); |
| 232 | } |
| 233 | |
| 234 | static void *vio_alloc_coherent(struct device *dev, size_t size, |
| 235 | dma_addr_t *dma_handle, unsigned int __nocast flag) |
| 236 | { |
| 237 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, |
| 238 | dma_handle, flag); |
| 239 | } |
| 240 | |
| 241 | static void vio_free_coherent(struct device *dev, size_t size, |
| 242 | void *vaddr, dma_addr_t dma_handle) |
| 243 | { |
| 244 | iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr, |
| 245 | dma_handle); |
| 246 | } |
| 247 | |
| 248 | static int vio_dma_supported(struct device *dev, u64 mask) |
| 249 | { |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | struct dma_mapping_ops vio_dma_ops = { |
| 254 | .alloc_coherent = vio_alloc_coherent, |
| 255 | .free_coherent = vio_free_coherent, |
| 256 | .map_single = vio_map_single, |
| 257 | .unmap_single = vio_unmap_single, |
| 258 | .map_sg = vio_map_sg, |
| 259 | .unmap_sg = vio_unmap_sg, |
| 260 | .dma_supported = vio_dma_supported, |
| 261 | }; |
| 262 | |
| 263 | static int vio_bus_match(struct device *dev, struct device_driver *drv) |
| 264 | { |
| 265 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
| 266 | struct vio_driver *vio_drv = to_vio_driver(drv); |
| 267 | const struct vio_device_id *ids = vio_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame^] | 269 | return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | struct bus_type vio_bus_type = { |
| 273 | .name = "vio", |
| 274 | .match = vio_bus_match, |
| 275 | }; |