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 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/dma-mapping.h> |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 23 | #include <linux/kobject.h> |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/iommu.h> |
| 26 | #include <asm/dma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/vio.h> |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 28 | #include <asm/prom.h> |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 29 | #include <asm/firmware.h> |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 30 | #include <asm/tce.h> |
| 31 | #include <asm/abs_addr.h> |
| 32 | #include <asm/page.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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 40 | extern struct subsystem devices_subsys; /* needed for vio_find_name() */ |
| 41 | |
| 42 | static struct vio_dev vio_bus_device = { /* fake "parent" device */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 43 | .name = vio_bus_device.dev.bus_id, |
| 44 | .type = "", |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 45 | .dev.bus_id = "vio", |
| 46 | .dev.bus = &vio_bus_type, |
| 47 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 49 | #ifdef CONFIG_PPC_ISERIES |
| 50 | struct device *iSeries_vio_dev = &vio_bus_device.dev; |
| 51 | EXPORT_SYMBOL(iSeries_vio_dev); |
| 52 | |
| 53 | static struct iommu_table veth_iommu_table; |
| 54 | static struct iommu_table vio_iommu_table; |
| 55 | |
| 56 | static void __init iommu_vio_init(void) |
| 57 | { |
| 58 | iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table); |
| 59 | veth_iommu_table.it_size /= 2; |
| 60 | vio_iommu_table = veth_iommu_table; |
| 61 | vio_iommu_table.it_offset += veth_iommu_table.it_size; |
| 62 | |
| 63 | if (!iommu_init_table(&veth_iommu_table)) |
| 64 | printk("Virtual Bus VETH TCE table failed.\n"); |
| 65 | if (!iommu_init_table(&vio_iommu_table)) |
| 66 | printk("Virtual Bus VIO TCE table failed.\n"); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev) |
| 71 | { |
| 72 | #ifdef CONFIG_PPC_ISERIES |
| 73 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { |
| 74 | if (strcmp(dev->type, "vlan") == 0) |
| 75 | return &veth_iommu_table; |
| 76 | return &vio_iommu_table; |
| 77 | } else |
| 78 | #endif |
| 79 | { |
| 80 | unsigned int *dma_window; |
| 81 | struct iommu_table *newTceTable; |
| 82 | unsigned long offset; |
| 83 | int dma_window_property_size; |
| 84 | |
| 85 | dma_window = (unsigned int *)get_property( |
| 86 | dev->dev.platform_data, "ibm,my-dma-window", |
| 87 | &dma_window_property_size); |
| 88 | if (!dma_window) |
| 89 | return NULL; |
| 90 | |
| 91 | newTceTable = kmalloc(sizeof(struct iommu_table), GFP_KERNEL); |
| 92 | |
| 93 | /* |
| 94 | * There should be some code to extract the phys-encoded |
| 95 | * offset using prom_n_addr_cells(). However, according to |
| 96 | * a comment on earlier versions, it's always zero, so we |
| 97 | * don't bother |
| 98 | */ |
| 99 | offset = dma_window[1] >> PAGE_SHIFT; |
| 100 | |
| 101 | /* TCE table size - measured in tce entries */ |
| 102 | newTceTable->it_size = dma_window[4] >> PAGE_SHIFT; |
| 103 | /* offset for VIO should always be 0 */ |
| 104 | newTceTable->it_offset = offset; |
| 105 | newTceTable->it_busno = 0; |
| 106 | newTceTable->it_index = (unsigned long)dma_window[0]; |
| 107 | newTceTable->it_type = TCE_VB; |
| 108 | |
| 109 | return iommu_init_table(newTceTable); |
| 110 | } |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 113 | /** |
| 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 |
| 118 | * |
| 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. |
| 122 | */ |
| 123 | static const struct vio_device_id *vio_match_device( |
| 124 | const struct vio_device_id *ids, const struct vio_dev *dev) |
| 125 | { |
| 126 | while (ids->type[0] != '\0') { |
Stephen Rothwell | dd721ff | 2006-04-27 17:21:46 +1000 | [diff] [blame] | 127 | if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) && |
| 128 | device_is_compatible(dev->dev.platform_data, ids->compat)) |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 129 | return ids; |
| 130 | ids++; |
| 131 | } |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 135 | /* |
| 136 | * Convert from struct device to struct vio_dev and pass to driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * 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] | 138 | * succeeded. |
| 139 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | static int vio_bus_probe(struct device *dev) |
| 141 | { |
| 142 | struct vio_dev *viodev = to_vio_dev(dev); |
| 143 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 144 | const struct vio_device_id *id; |
| 145 | int error = -ENODEV; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (!viodrv->probe) |
| 148 | return error; |
| 149 | |
| 150 | id = vio_match_device(viodrv->id_table, viodev); |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 151 | if (id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | error = viodrv->probe(viodev, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 158 | static int vio_bus_remove(struct device *dev) |
| 159 | { |
| 160 | struct vio_dev *viodev = to_vio_dev(dev); |
| 161 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 162 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 163 | if (viodrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return viodrv->remove(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* driver can't remove */ |
| 167 | return 1; |
| 168 | } |
| 169 | |
Stephen Rothwell | 3406010 | 2005-10-24 17:40:23 +1000 | [diff] [blame] | 170 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 171 | static void vio_bus_shutdown(struct device *dev) |
| 172 | { |
| 173 | struct vio_dev *viodev = to_vio_dev(dev); |
| 174 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 175 | |
Russell King | 2f53a80 | 2006-01-05 14:36:47 +0000 | [diff] [blame] | 176 | if (dev->driver && viodrv->shutdown) |
Stephen Rothwell | 3406010 | 2005-10-24 17:40:23 +1000 | [diff] [blame] | 177 | viodrv->shutdown(viodev); |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /** |
| 181 | * vio_register_driver: - Register a new vio driver |
| 182 | * @drv: The vio_driver structure to be registered. |
| 183 | */ |
| 184 | int vio_register_driver(struct vio_driver *viodrv) |
| 185 | { |
| 186 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 187 | viodrv->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* fill in 'struct driver' fields */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | viodrv->driver.bus = &vio_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | return driver_register(&viodrv->driver); |
| 193 | } |
| 194 | EXPORT_SYMBOL(vio_register_driver); |
| 195 | |
| 196 | /** |
| 197 | * vio_unregister_driver - Remove registration of vio driver. |
| 198 | * @driver: The vio_driver struct to be removed form registration |
| 199 | */ |
| 200 | void vio_unregister_driver(struct vio_driver *viodrv) |
| 201 | { |
| 202 | driver_unregister(&viodrv->driver); |
| 203 | } |
| 204 | EXPORT_SYMBOL(vio_unregister_driver); |
| 205 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 206 | /* vio_dev refcount hit 0 */ |
| 207 | static void __devinit vio_dev_release(struct device *dev) |
| 208 | { |
| 209 | if (dev->platform_data) { |
| 210 | /* XXX free TCE table */ |
| 211 | of_node_put(dev->platform_data); |
| 212 | } |
| 213 | kfree(to_vio_dev(dev)); |
| 214 | } |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /** |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 217 | * vio_register_device_node: - Register a new vio device. |
| 218 | * @of_node: The OF node for this device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 220 | * Creates and initializes a vio_dev structure from the data in |
| 221 | * of_node (dev.platform_data) and adds it to the list of virtual devices. |
| 222 | * Returns a pointer to the created vio_dev or NULL if node has |
| 223 | * NULL device_type or compatible fields. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | */ |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 225 | struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 227 | struct vio_dev *viodev; |
| 228 | unsigned int *unit_address; |
| 229 | unsigned int *irq_p; |
| 230 | |
| 231 | /* we need the 'device_type' property, in order to match with drivers */ |
| 232 | if (of_node->type == NULL) { |
| 233 | printk(KERN_WARNING "%s: node %s missing 'device_type'\n", |
| 234 | __FUNCTION__, |
| 235 | of_node->name ? of_node->name : "<unknown>"); |
| 236 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 238 | |
| 239 | unit_address = (unsigned int *)get_property(of_node, "reg", NULL); |
| 240 | if (unit_address == NULL) { |
| 241 | printk(KERN_WARNING "%s: node %s missing 'reg'\n", |
| 242 | __FUNCTION__, |
| 243 | of_node->name ? of_node->name : "<unknown>"); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | /* allocate a vio_dev for this node */ |
| 248 | viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL); |
| 249 | if (viodev == NULL) |
| 250 | return NULL; |
| 251 | |
| 252 | viodev->dev.platform_data = of_node_get(of_node); |
| 253 | |
| 254 | viodev->irq = NO_IRQ; |
| 255 | irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL); |
| 256 | if (irq_p) { |
| 257 | int virq = virt_irq_create_mapping(*irq_p); |
| 258 | if (virq == NO_IRQ) { |
| 259 | printk(KERN_ERR "Unable to allocate interrupt " |
| 260 | "number for %s\n", of_node->full_name); |
| 261 | } else |
| 262 | viodev->irq = irq_offset_up(virq); |
| 263 | } |
| 264 | |
| 265 | snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address); |
| 266 | viodev->name = of_node->name; |
| 267 | viodev->type = of_node->type; |
| 268 | viodev->unit_address = *unit_address; |
| 269 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { |
| 270 | unit_address = (unsigned int *)get_property(of_node, |
| 271 | "linux,unit_address", NULL); |
| 272 | if (unit_address != NULL) |
| 273 | viodev->unit_address = *unit_address; |
| 274 | } |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 275 | viodev->iommu_table = vio_build_iommu_table(viodev); |
| 276 | |
| 277 | /* init generic 'struct device' fields: */ |
| 278 | viodev->dev.parent = &vio_bus_device.dev; |
| 279 | viodev->dev.bus = &vio_bus_type; |
| 280 | viodev->dev.release = vio_dev_release; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 281 | |
| 282 | /* register with generic device framework */ |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 283 | if (device_register(&viodev->dev)) { |
| 284 | printk(KERN_ERR "%s: failed to register device %s\n", |
| 285 | __FUNCTION__, viodev->dev.bus_id); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 286 | /* XXX free TCE table */ |
| 287 | kfree(viodev); |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | return viodev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 293 | EXPORT_SYMBOL(vio_register_device_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /** |
| 296 | * vio_bus_init: - Initialize the virtual IO bus |
| 297 | */ |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 298 | static int __init vio_bus_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | int err; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 301 | struct device_node *node_vroot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 303 | #ifdef CONFIG_PPC_ISERIES |
| 304 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { |
| 305 | iommu_vio_init(); |
| 306 | vio_bus_device.iommu_table = &vio_iommu_table; |
| 307 | iSeries_vio_dev = &vio_bus_device.dev; |
| 308 | } |
| 309 | #endif |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | err = bus_register(&vio_bus_type); |
| 312 | if (err) { |
| 313 | printk(KERN_ERR "failed to register VIO bus\n"); |
| 314 | return err; |
| 315 | } |
| 316 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 317 | /* |
| 318 | * The fake parent of all vio devices, just to give us |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 319 | * a nice directory |
| 320 | */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 321 | err = device_register(&vio_bus_device.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (err) { |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 323 | printk(KERN_WARNING "%s: device_register returned %i\n", |
| 324 | __FUNCTION__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return err; |
| 326 | } |
| 327 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 328 | node_vroot = find_devices("vdevice"); |
| 329 | if (node_vroot) { |
| 330 | struct device_node *of_node; |
| 331 | |
| 332 | /* |
| 333 | * Create struct vio_devices for each virtual device in |
| 334 | * the device tree. Drivers will associate with them later. |
| 335 | */ |
| 336 | for (of_node = node_vroot->child; of_node != NULL; |
| 337 | of_node = of_node->sibling) { |
| 338 | printk(KERN_DEBUG "%s: processing %p\n", |
| 339 | __FUNCTION__, of_node); |
| 340 | vio_register_device_node(of_node); |
| 341 | } |
| 342 | } |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return 0; |
| 345 | } |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 346 | __initcall(vio_bus_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 348 | static ssize_t name_show(struct device *dev, |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 349 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
| 351 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
| 352 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 353 | |
| 354 | static ssize_t devspec_show(struct device *dev, |
| 355 | struct device_attribute *attr, char *buf) |
| 356 | { |
| 357 | struct device_node *of_node = dev->platform_data; |
| 358 | |
| 359 | return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none"); |
| 360 | } |
| 361 | |
| 362 | static struct device_attribute vio_dev_attrs[] = { |
| 363 | __ATTR_RO(name), |
| 364 | __ATTR_RO(devspec), |
| 365 | __ATTR_NULL |
| 366 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
| 369 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | device_unregister(&viodev->dev); |
| 371 | } |
| 372 | EXPORT_SYMBOL(vio_unregister_device); |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | static dma_addr_t vio_map_single(struct device *dev, void *vaddr, |
| 375 | size_t size, enum dma_data_direction direction) |
| 376 | { |
| 377 | return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size, |
Olof Johansson | 7daa411 | 2006-04-12 21:05:59 -0500 | [diff] [blame] | 378 | ~0ul, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle, |
| 382 | size_t size, enum dma_data_direction direction) |
| 383 | { |
| 384 | iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size, |
| 385 | direction); |
| 386 | } |
| 387 | |
| 388 | static int vio_map_sg(struct device *dev, struct scatterlist *sglist, |
| 389 | int nelems, enum dma_data_direction direction) |
| 390 | { |
| 391 | return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist, |
Olof Johansson | 7daa411 | 2006-04-12 21:05:59 -0500 | [diff] [blame] | 392 | nelems, ~0ul, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 396 | int nelems, enum dma_data_direction direction) |
| 397 | { |
| 398 | iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction); |
| 399 | } |
| 400 | |
| 401 | static void *vio_alloc_coherent(struct device *dev, size_t size, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 402 | dma_addr_t *dma_handle, gfp_t flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, |
Olof Johansson | 7daa411 | 2006-04-12 21:05:59 -0500 | [diff] [blame] | 405 | dma_handle, ~0ul, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static void vio_free_coherent(struct device *dev, size_t size, |
| 409 | void *vaddr, dma_addr_t dma_handle) |
| 410 | { |
| 411 | iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr, |
| 412 | dma_handle); |
| 413 | } |
| 414 | |
| 415 | static int vio_dma_supported(struct device *dev, u64 mask) |
| 416 | { |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | struct dma_mapping_ops vio_dma_ops = { |
| 421 | .alloc_coherent = vio_alloc_coherent, |
| 422 | .free_coherent = vio_free_coherent, |
| 423 | .map_single = vio_map_single, |
| 424 | .unmap_single = vio_unmap_single, |
| 425 | .map_sg = vio_map_sg, |
| 426 | .unmap_sg = vio_unmap_sg, |
| 427 | .dma_supported = vio_dma_supported, |
| 428 | }; |
| 429 | |
| 430 | static int vio_bus_match(struct device *dev, struct device_driver *drv) |
| 431 | { |
| 432 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
| 433 | struct vio_driver *vio_drv = to_vio_driver(drv); |
| 434 | const struct vio_device_id *ids = vio_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 436 | return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 439 | static int vio_hotplug(struct device *dev, char **envp, int num_envp, |
| 440 | char *buffer, int buffer_size) |
| 441 | { |
| 442 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 443 | struct device_node *dn = dev->platform_data; |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 444 | char *cp; |
| 445 | int length; |
| 446 | |
| 447 | if (!num_envp) |
| 448 | return -ENOMEM; |
| 449 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 450 | if (!dn) |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 451 | return -ENODEV; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 452 | cp = (char *)get_property(dn, "compatible", &length); |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 453 | if (!cp) |
| 454 | return -ENODEV; |
| 455 | |
| 456 | envp[0] = buffer; |
| 457 | length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s", |
| 458 | vio_dev->type, cp); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 459 | if ((buffer_size - length) <= 0) |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 460 | return -ENOMEM; |
| 461 | envp[1] = NULL; |
| 462 | return 0; |
| 463 | } |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | struct bus_type vio_bus_type = { |
| 466 | .name = "vio", |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 467 | .dev_attrs = vio_dev_attrs, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 468 | .uevent = vio_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | .match = vio_bus_match, |
Russell King | 2f53a80 | 2006-01-05 14:36:47 +0000 | [diff] [blame] | 470 | .probe = vio_bus_probe, |
| 471 | .remove = vio_bus_remove, |
| 472 | .shutdown = vio_bus_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | }; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * vio_get_attribute: - get attribute for virtual device |
| 477 | * @vdev: The vio device to get property. |
| 478 | * @which: The property/attribute to be extracted. |
| 479 | * @length: Pointer to length of returned data size (unused if NULL). |
| 480 | * |
| 481 | * Calls prom.c's get_property() to return the value of the |
| 482 | * attribute specified by @which |
| 483 | */ |
| 484 | const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length) |
| 485 | { |
| 486 | return get_property(vdev->dev.platform_data, which, length); |
| 487 | } |
| 488 | EXPORT_SYMBOL(vio_get_attribute); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame^] | 489 | |
| 490 | #ifdef CONFIG_PPC_PSERIES |
| 491 | /* vio_find_name() - internal because only vio.c knows how we formatted the |
| 492 | * kobject name |
| 493 | * XXX once vio_bus_type.devices is actually used as a kset in |
| 494 | * drivers/base/bus.c, this function should be removed in favor of |
| 495 | * "device_find(kobj_name, &vio_bus_type)" |
| 496 | */ |
| 497 | static struct vio_dev *vio_find_name(const char *kobj_name) |
| 498 | { |
| 499 | struct kobject *found; |
| 500 | |
| 501 | found = kset_find_obj(&devices_subsys.kset, kobj_name); |
| 502 | if (!found) |
| 503 | return NULL; |
| 504 | |
| 505 | return to_vio_dev(container_of(found, struct device, kobj)); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * vio_find_node - find an already-registered vio_dev |
| 510 | * @vnode: device_node of the virtual device we're looking for |
| 511 | */ |
| 512 | struct vio_dev *vio_find_node(struct device_node *vnode) |
| 513 | { |
| 514 | uint32_t *unit_address; |
| 515 | char kobj_name[BUS_ID_SIZE]; |
| 516 | |
| 517 | /* construct the kobject name from the device node */ |
| 518 | unit_address = (uint32_t *)get_property(vnode, "reg", NULL); |
| 519 | if (!unit_address) |
| 520 | return NULL; |
| 521 | snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address); |
| 522 | |
| 523 | return vio_find_name(kobj_name); |
| 524 | } |
| 525 | EXPORT_SYMBOL(vio_find_node); |
| 526 | |
| 527 | int vio_enable_interrupts(struct vio_dev *dev) |
| 528 | { |
| 529 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE); |
| 530 | if (rc != H_SUCCESS) |
| 531 | printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc); |
| 532 | return rc; |
| 533 | } |
| 534 | EXPORT_SYMBOL(vio_enable_interrupts); |
| 535 | |
| 536 | int vio_disable_interrupts(struct vio_dev *dev) |
| 537 | { |
| 538 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE); |
| 539 | if (rc != H_SUCCESS) |
| 540 | printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc); |
| 541 | return rc; |
| 542 | } |
| 543 | EXPORT_SYMBOL(vio_disable_interrupts); |
| 544 | #endif /* CONFIG_PPC_PSERIES */ |