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