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 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 40 | extern struct kset devices_subsys; /* needed for vio_find_name() */ |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 41 | |
Stephen Rothwell | 6fccab2 | 2007-09-21 14:32:05 +1000 | [diff] [blame] | 42 | static struct bus_type vio_bus_type; |
| 43 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 44 | static struct vio_dev vio_bus_device = { /* fake "parent" device */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 45 | .name = vio_bus_device.dev.bus_id, |
| 46 | .type = "", |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 47 | .dev.bus_id = "vio", |
| 48 | .dev.bus = &vio_bus_type, |
| 49 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 51 | static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev) |
| 52 | { |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 53 | const unsigned char *dma_window; |
| 54 | struct iommu_table *tbl; |
| 55 | unsigned long offset, size; |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 56 | |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 57 | if (firmware_has_feature(FW_FEATURE_ISERIES)) |
| 58 | return vio_build_iommu_table_iseries(dev); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 59 | |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 60 | dma_window = of_get_property(dev->dev.archdata.of_node, |
| 61 | "ibm,my-dma-window", NULL); |
| 62 | if (!dma_window) |
| 63 | return NULL; |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 64 | |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 65 | tbl = kmalloc(sizeof(*tbl), GFP_KERNEL); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 66 | |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 67 | of_parse_dma_window(dev->dev.archdata.of_node, dma_window, |
| 68 | &tbl->it_index, &offset, &size); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 69 | |
Stephen Rothwell | dd9b67a | 2007-10-11 14:55:02 +1000 | [diff] [blame^] | 70 | /* TCE table size - measured in tce entries */ |
| 71 | tbl->it_size = size >> IOMMU_PAGE_SHIFT; |
| 72 | /* offset for VIO should always be 0 */ |
| 73 | tbl->it_offset = offset >> IOMMU_PAGE_SHIFT; |
| 74 | tbl->it_busno = 0; |
| 75 | tbl->it_type = TCE_VB; |
| 76 | |
| 77 | return iommu_init_table(tbl, -1); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 80 | /** |
| 81 | * vio_match_device: - Tell if a VIO device has a matching |
| 82 | * VIO device id structure. |
| 83 | * @ids: array of VIO device id structures to search in |
| 84 | * @dev: the VIO device structure to match against |
| 85 | * |
| 86 | * Used by a driver to check whether a VIO device present in the |
| 87 | * system is in its list of supported devices. Returns the matching |
| 88 | * vio_device_id structure or NULL if there is no match. |
| 89 | */ |
| 90 | static const struct vio_device_id *vio_match_device( |
| 91 | const struct vio_device_id *ids, const struct vio_dev *dev) |
| 92 | { |
| 93 | while (ids->type[0] != '\0') { |
Stephen Rothwell | dd721ff | 2006-04-27 17:21:46 +1000 | [diff] [blame] | 94 | if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) && |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 95 | of_device_is_compatible(dev->dev.archdata.of_node, |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 96 | ids->compat)) |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 97 | return ids; |
| 98 | ids++; |
| 99 | } |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 103 | /* |
| 104 | * Convert from struct device to struct vio_dev and pass to driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * 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] | 106 | * succeeded. |
| 107 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static int vio_bus_probe(struct device *dev) |
| 109 | { |
| 110 | struct vio_dev *viodev = to_vio_dev(dev); |
| 111 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 112 | const struct vio_device_id *id; |
| 113 | int error = -ENODEV; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (!viodrv->probe) |
| 116 | return error; |
| 117 | |
| 118 | id = vio_match_device(viodrv->id_table, viodev); |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 119 | if (id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | error = viodrv->probe(viodev, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return error; |
| 123 | } |
| 124 | |
| 125 | /* convert from struct device to struct vio_dev and pass to driver. */ |
| 126 | static int vio_bus_remove(struct device *dev) |
| 127 | { |
| 128 | struct vio_dev *viodev = to_vio_dev(dev); |
| 129 | struct vio_driver *viodrv = to_vio_driver(dev->driver); |
| 130 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 131 | if (viodrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return viodrv->remove(viodev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* driver can't remove */ |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * vio_register_driver: - Register a new vio driver |
| 140 | * @drv: The vio_driver structure to be registered. |
| 141 | */ |
| 142 | int vio_register_driver(struct vio_driver *viodrv) |
| 143 | { |
| 144 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, |
Stephen Rothwell | 6fdf539 | 2005-10-24 14:53:21 +1000 | [diff] [blame] | 145 | viodrv->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* fill in 'struct driver' fields */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | viodrv->driver.bus = &vio_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | return driver_register(&viodrv->driver); |
| 151 | } |
| 152 | EXPORT_SYMBOL(vio_register_driver); |
| 153 | |
| 154 | /** |
| 155 | * vio_unregister_driver - Remove registration of vio driver. |
| 156 | * @driver: The vio_driver struct to be removed form registration |
| 157 | */ |
| 158 | void vio_unregister_driver(struct vio_driver *viodrv) |
| 159 | { |
| 160 | driver_unregister(&viodrv->driver); |
| 161 | } |
| 162 | EXPORT_SYMBOL(vio_unregister_driver); |
| 163 | |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 164 | /* vio_dev refcount hit 0 */ |
| 165 | static void __devinit vio_dev_release(struct device *dev) |
| 166 | { |
Mariusz Kozlowski | 6690fae | 2007-01-02 12:38:36 +0100 | [diff] [blame] | 167 | /* XXX should free TCE table */ |
| 168 | of_node_put(dev->archdata.of_node); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 169 | kfree(to_vio_dev(dev)); |
| 170 | } |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | /** |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 173 | * vio_register_device_node: - Register a new vio device. |
| 174 | * @of_node: The OF node for this device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 176 | * Creates and initializes a vio_dev structure from the data in |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 177 | * of_node and adds it to the list of virtual devices. |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 178 | * Returns a pointer to the created vio_dev or NULL if node has |
| 179 | * NULL device_type or compatible fields. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | */ |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 181 | 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] | 182 | { |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 183 | struct vio_dev *viodev; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 184 | const unsigned int *unit_address; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 185 | |
| 186 | /* we need the 'device_type' property, in order to match with drivers */ |
| 187 | if (of_node->type == NULL) { |
| 188 | printk(KERN_WARNING "%s: node %s missing 'device_type'\n", |
| 189 | __FUNCTION__, |
| 190 | of_node->name ? of_node->name : "<unknown>"); |
| 191 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 193 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 194 | unit_address = of_get_property(of_node, "reg", NULL); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 195 | if (unit_address == NULL) { |
| 196 | printk(KERN_WARNING "%s: node %s missing 'reg'\n", |
| 197 | __FUNCTION__, |
| 198 | of_node->name ? of_node->name : "<unknown>"); |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | /* allocate a vio_dev for this node */ |
| 203 | viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL); |
| 204 | if (viodev == NULL) |
| 205 | return NULL; |
| 206 | |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 207 | viodev->irq = irq_of_parse_and_map(of_node, 0); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 208 | |
| 209 | snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address); |
| 210 | viodev->name = of_node->name; |
| 211 | viodev->type = of_node->type; |
| 212 | viodev->unit_address = *unit_address; |
| 213 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 214 | unit_address = of_get_property(of_node, |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 215 | "linux,unit_address", NULL); |
| 216 | if (unit_address != NULL) |
| 217 | viodev->unit_address = *unit_address; |
| 218 | } |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 219 | viodev->dev.archdata.of_node = of_node_get(of_node); |
| 220 | viodev->dev.archdata.dma_ops = &dma_iommu_ops; |
| 221 | viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev); |
| 222 | viodev->dev.archdata.numa_node = of_node_to_nid(of_node); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 223 | |
| 224 | /* init generic 'struct device' fields: */ |
| 225 | viodev->dev.parent = &vio_bus_device.dev; |
| 226 | viodev->dev.bus = &vio_bus_type; |
| 227 | viodev->dev.release = vio_dev_release; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 228 | |
| 229 | /* register with generic device framework */ |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 230 | if (device_register(&viodev->dev)) { |
| 231 | printk(KERN_ERR "%s: failed to register device %s\n", |
| 232 | __FUNCTION__, viodev->dev.bus_id); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 233 | /* XXX free TCE table */ |
| 234 | kfree(viodev); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | return viodev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 240 | EXPORT_SYMBOL(vio_register_device_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /** |
| 243 | * vio_bus_init: - Initialize the virtual IO bus |
| 244 | */ |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 245 | static int __init vio_bus_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | int err; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 248 | struct device_node *node_vroot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Stephen Rothwell | c868078 | 2007-09-21 14:31:02 +1000 | [diff] [blame] | 250 | if (firmware_has_feature(FW_FEATURE_ISERIES)) |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 251 | iommu_vio_init(); |
Stephen Rothwell | 6312236 | 2005-07-12 17:45:27 +1000 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | err = bus_register(&vio_bus_type); |
| 254 | if (err) { |
| 255 | printk(KERN_ERR "failed to register VIO bus\n"); |
| 256 | return err; |
| 257 | } |
| 258 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 259 | /* |
| 260 | * The fake parent of all vio devices, just to give us |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 261 | * a nice directory |
| 262 | */ |
Stephen Rothwell | ac5b33c | 2005-06-21 17:15:54 -0700 | [diff] [blame] | 263 | err = device_register(&vio_bus_device.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (err) { |
Stephen Rothwell | 3e494c8 | 2005-07-12 17:40:17 +1000 | [diff] [blame] | 265 | printk(KERN_WARNING "%s: device_register returned %i\n", |
| 266 | __FUNCTION__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return err; |
| 268 | } |
| 269 | |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 270 | node_vroot = of_find_node_by_name(NULL, "vdevice"); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 271 | if (node_vroot) { |
| 272 | struct device_node *of_node; |
| 273 | |
| 274 | /* |
| 275 | * Create struct vio_devices for each virtual device in |
| 276 | * the device tree. Drivers will associate with them later. |
| 277 | */ |
| 278 | for (of_node = node_vroot->child; of_node != NULL; |
Stephen Rothwell | c546726 | 2007-09-21 14:29:28 +1000 | [diff] [blame] | 279 | of_node = of_node->sibling) |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 280 | vio_register_device_node(of_node); |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 281 | of_node_put(node_vroot); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 282 | } |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 286 | __initcall(vio_bus_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 288 | static ssize_t name_show(struct device *dev, |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 289 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
| 292 | } |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 293 | |
| 294 | static ssize_t devspec_show(struct device *dev, |
| 295 | struct device_attribute *attr, char *buf) |
| 296 | { |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 297 | struct device_node *of_node = dev->archdata.of_node; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 298 | |
| 299 | return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none"); |
| 300 | } |
| 301 | |
| 302 | static struct device_attribute vio_dev_attrs[] = { |
| 303 | __ATTR_RO(name), |
| 304 | __ATTR_RO(devspec), |
| 305 | __ATTR_NULL |
| 306 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
| 309 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | device_unregister(&viodev->dev); |
| 311 | } |
| 312 | EXPORT_SYMBOL(vio_unregister_device); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | static int vio_bus_match(struct device *dev, struct device_driver *drv) |
| 315 | { |
| 316 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
| 317 | struct vio_driver *vio_drv = to_vio_driver(drv); |
| 318 | const struct vio_device_id *ids = vio_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Stephen Rothwell | 5c0b4b8 | 2005-08-17 16:37:35 +1000 | [diff] [blame] | 320 | return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 323 | static int vio_hotplug(struct device *dev, char **envp, int num_envp, |
| 324 | char *buffer, int buffer_size) |
| 325 | { |
| 326 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 327 | struct device_node *dn; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 328 | const char *cp; |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 329 | int length; |
| 330 | |
| 331 | if (!num_envp) |
| 332 | return -ENOMEM; |
| 333 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 334 | dn = dev->archdata.of_node; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 335 | if (!dn) |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 336 | return -ENODEV; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 337 | cp = of_get_property(dn, "compatible", &length); |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 338 | if (!cp) |
| 339 | return -ENODEV; |
| 340 | |
| 341 | envp[0] = buffer; |
| 342 | length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s", |
| 343 | vio_dev->type, cp); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 344 | if ((buffer_size - length) <= 0) |
Olaf Hering | 143dcec | 2005-11-08 21:34:36 -0800 | [diff] [blame] | 345 | return -ENOMEM; |
| 346 | envp[1] = NULL; |
| 347 | return 0; |
| 348 | } |
| 349 | |
Stephen Rothwell | 6fccab2 | 2007-09-21 14:32:05 +1000 | [diff] [blame] | 350 | static struct bus_type vio_bus_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | .name = "vio", |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 352 | .dev_attrs = vio_dev_attrs, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 353 | .uevent = vio_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | .match = vio_bus_match, |
Russell King | 2f53a80 | 2006-01-05 14:36:47 +0000 | [diff] [blame] | 355 | .probe = vio_bus_probe, |
| 356 | .remove = vio_bus_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | }; |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 358 | |
| 359 | /** |
| 360 | * vio_get_attribute: - get attribute for virtual device |
| 361 | * @vdev: The vio device to get property. |
| 362 | * @which: The property/attribute to be extracted. |
| 363 | * @length: Pointer to length of returned data size (unused if NULL). |
| 364 | * |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 365 | * Calls prom.c's of_get_property() to return the value of the |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 366 | * attribute specified by @which |
| 367 | */ |
| 368 | const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length) |
| 369 | { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 370 | return of_get_property(vdev->dev.archdata.of_node, which, length); |
Stephen Rothwell | e10fa77 | 2006-04-27 17:18:21 +1000 | [diff] [blame] | 371 | } |
| 372 | EXPORT_SYMBOL(vio_get_attribute); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 373 | |
| 374 | #ifdef CONFIG_PPC_PSERIES |
| 375 | /* vio_find_name() - internal because only vio.c knows how we formatted the |
| 376 | * kobject name |
| 377 | * XXX once vio_bus_type.devices is actually used as a kset in |
| 378 | * drivers/base/bus.c, this function should be removed in favor of |
| 379 | * "device_find(kobj_name, &vio_bus_type)" |
| 380 | */ |
| 381 | static struct vio_dev *vio_find_name(const char *kobj_name) |
| 382 | { |
| 383 | struct kobject *found; |
| 384 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 385 | found = kset_find_obj(&devices_subsys, kobj_name); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 386 | if (!found) |
| 387 | return NULL; |
| 388 | |
| 389 | return to_vio_dev(container_of(found, struct device, kobj)); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * vio_find_node - find an already-registered vio_dev |
| 394 | * @vnode: device_node of the virtual device we're looking for |
| 395 | */ |
| 396 | struct vio_dev *vio_find_node(struct device_node *vnode) |
| 397 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 398 | const uint32_t *unit_address; |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 399 | char kobj_name[BUS_ID_SIZE]; |
| 400 | |
| 401 | /* construct the kobject name from the device node */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 402 | unit_address = of_get_property(vnode, "reg", NULL); |
Stephen Rothwell | c7f0e8c | 2006-04-27 17:23:32 +1000 | [diff] [blame] | 403 | if (!unit_address) |
| 404 | return NULL; |
| 405 | snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address); |
| 406 | |
| 407 | return vio_find_name(kobj_name); |
| 408 | } |
| 409 | EXPORT_SYMBOL(vio_find_node); |
| 410 | |
| 411 | int vio_enable_interrupts(struct vio_dev *dev) |
| 412 | { |
| 413 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE); |
| 414 | if (rc != H_SUCCESS) |
| 415 | printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc); |
| 416 | return rc; |
| 417 | } |
| 418 | EXPORT_SYMBOL(vio_enable_interrupts); |
| 419 | |
| 420 | int vio_disable_interrupts(struct vio_dev *dev) |
| 421 | { |
| 422 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE); |
| 423 | if (rc != H_SUCCESS) |
| 424 | printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc); |
| 425 | return rc; |
| 426 | } |
| 427 | EXPORT_SYMBOL(vio_disable_interrupts); |
| 428 | #endif /* CONFIG_PPC_PSERIES */ |