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