blob: 1d7b272b373789355326731b311604482a81ad8e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM PowerPC Virtual I/O Infrastructure Support.
3 *
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +10004 * Copyright (c) 2003-2005 IBM Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Dave Engebretsen engebret@us.ibm.com
6 * Santiago Leon santil@us.ibm.com
7 * Hollis Blanchard <hollisb@us.ibm.com>
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +10008 * Stephen Rothwell
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 Rothwellc7f0e8c2006-04-27 17:23:32 +100016#include <linux/types.h>
17#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/dma-mapping.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100023#include <linux/kobject.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/iommu.h>
26#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/vio.h>
Olaf Hering143dcec2005-11-08 21:34:36 -080028#include <asm/prom.h>
Stephen Rothwelle10fa772006-04-27 17:18:21 +100029#include <asm/firmware.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100030#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 Torvalds1da177e2005-04-16 15:20:36 -070039
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070040extern struct kset devices_subsys; /* needed for vio_find_name() */
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100041
Stephen Rothwell6fccab22007-09-21 14:32:05 +100042static struct bus_type vio_bus_type;
43
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100044static struct vio_dev vio_bus_device = { /* fake "parent" device */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070045 .name = vio_bus_device.dev.bus_id,
46 .type = "",
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070047 .dev.bus_id = "vio",
48 .dev.bus = &vio_bus_type,
49};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100051#ifdef CONFIG_PPC_ISERIES
52struct device *iSeries_vio_dev = &vio_bus_device.dev;
53EXPORT_SYMBOL(iSeries_vio_dev);
54
55static struct iommu_table veth_iommu_table;
56static struct iommu_table vio_iommu_table;
57
58static void __init iommu_vio_init(void)
59{
60 iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);
61 veth_iommu_table.it_size /= 2;
62 vio_iommu_table = veth_iommu_table;
63 vio_iommu_table.it_offset += veth_iommu_table.it_size;
64
Anton Blanchardca1588e2006-06-10 20:58:08 +100065 if (!iommu_init_table(&veth_iommu_table, -1))
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100066 printk("Virtual Bus VETH TCE table failed.\n");
Anton Blanchardca1588e2006-06-10 20:58:08 +100067 if (!iommu_init_table(&vio_iommu_table, -1))
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100068 printk("Virtual Bus VIO TCE table failed.\n");
Stephen Rothwellc8680782007-09-21 14:31:02 +100069 vio_bus_device.dev.archdata.dma_ops = &dma_iommu_ops;
70 vio_bus_device.dev.archdata.dma_data = &vio_iommu_table;
71}
72#else
73static void __init iommu_vio_init(void)
74{
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100075}
76#endif
77
78static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
79{
80#ifdef CONFIG_PPC_ISERIES
81 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
Stephen Rothwell1dc461f2006-05-18 17:21:26 +100082 if (strcmp(dev->type, "network") == 0)
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100083 return &veth_iommu_table;
84 return &vio_iommu_table;
85 } else
86#endif
87 {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100088 const unsigned char *dma_window;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100089 struct iommu_table *tbl;
90 unsigned long offset, size;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100091
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100092 dma_window = of_get_property(dev->dev.archdata.of_node,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110093 "ibm,my-dma-window", NULL);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100094 if (!dma_window)
95 return NULL;
96
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100097 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100098
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110099 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
100 &tbl->it_index, &offset, &size);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000101
102 /* TCE table size - measured in tce entries */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100103 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000104 /* offset for VIO should always be 0 */
Linas Vepstas5d2efba2006-10-30 16:15:59 +1100105 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +1000106 tbl->it_busno = 0;
107 tbl->it_type = TCE_VB;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000108
Anton Blanchardca1588e2006-06-10 20:58:08 +1000109 return iommu_init_table(tbl, -1);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000110 }
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000113/**
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 */
123static 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 Rothwelldd721ff2006-04-27 17:21:46 +1000127 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000128 of_device_is_compatible(dev->dev.archdata.of_node,
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100129 ids->compat))
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000130 return ids;
131 ids++;
132 }
133 return NULL;
134}
135
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000136/*
137 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000139 * succeeded.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int vio_bus_probe(struct device *dev)
142{
143 struct vio_dev *viodev = to_vio_dev(dev);
144 struct vio_driver *viodrv = to_vio_driver(dev->driver);
145 const struct vio_device_id *id;
146 int error = -ENODEV;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!viodrv->probe)
149 return error;
150
151 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000152 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return error;
156}
157
158/* convert from struct device to struct vio_dev and pass to driver. */
159static int vio_bus_remove(struct device *dev)
160{
161 struct vio_dev *viodev = to_vio_dev(dev);
162 struct vio_driver *viodrv = to_vio_driver(dev->driver);
163
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000164 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* driver can't remove */
168 return 1;
169}
170
Stephen Rothwell34060102005-10-24 17:40:23 +1000171/* convert from struct device to struct vio_dev and pass to driver. */
172static void vio_bus_shutdown(struct device *dev)
173{
174 struct vio_dev *viodev = to_vio_dev(dev);
175 struct vio_driver *viodrv = to_vio_driver(dev->driver);
176
Russell King2f53a802006-01-05 14:36:47 +0000177 if (dev->driver && viodrv->shutdown)
Stephen Rothwell34060102005-10-24 17:40:23 +1000178 viodrv->shutdown(viodev);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * vio_register_driver: - Register a new vio driver
183 * @drv: The vio_driver structure to be registered.
184 */
185int vio_register_driver(struct vio_driver *viodrv)
186{
187 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000188 viodrv->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* fill in 'struct driver' fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 viodrv->driver.bus = &vio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return driver_register(&viodrv->driver);
194}
195EXPORT_SYMBOL(vio_register_driver);
196
197/**
198 * vio_unregister_driver - Remove registration of vio driver.
199 * @driver: The vio_driver struct to be removed form registration
200 */
201void vio_unregister_driver(struct vio_driver *viodrv)
202{
203 driver_unregister(&viodrv->driver);
204}
205EXPORT_SYMBOL(vio_unregister_driver);
206
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000207/* vio_dev refcount hit 0 */
208static void __devinit vio_dev_release(struct device *dev)
209{
Mariusz Kozlowski6690fae2007-01-02 12:38:36 +0100210 /* XXX should free TCE table */
211 of_node_put(dev->archdata.of_node);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000212 kfree(to_vio_dev(dev));
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/**
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000216 * vio_register_device_node: - Register a new vio device.
217 * @of_node: The OF node for this device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000219 * Creates and initializes a vio_dev structure from the data in
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100220 * of_node and adds it to the list of virtual devices.
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000221 * Returns a pointer to the created vio_dev or NULL if node has
222 * NULL device_type or compatible fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000224struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000226 struct vio_dev *viodev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000227 const unsigned int *unit_address;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000228
229 /* we need the 'device_type' property, in order to match with drivers */
230 if (of_node->type == NULL) {
231 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
232 __FUNCTION__,
233 of_node->name ? of_node->name : "<unknown>");
234 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000236
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000237 unit_address = of_get_property(of_node, "reg", NULL);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000238 if (unit_address == NULL) {
239 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
240 __FUNCTION__,
241 of_node->name ? of_node->name : "<unknown>");
242 return NULL;
243 }
244
245 /* allocate a vio_dev for this node */
246 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
247 if (viodev == NULL)
248 return NULL;
249
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000250 viodev->irq = irq_of_parse_and_map(of_node, 0);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000251
252 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
253 viodev->name = of_node->name;
254 viodev->type = of_node->type;
255 viodev->unit_address = *unit_address;
256 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000257 unit_address = of_get_property(of_node,
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000258 "linux,unit_address", NULL);
259 if (unit_address != NULL)
260 viodev->unit_address = *unit_address;
261 }
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100262 viodev->dev.archdata.of_node = of_node_get(of_node);
263 viodev->dev.archdata.dma_ops = &dma_iommu_ops;
264 viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
265 viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000266
267 /* init generic 'struct device' fields: */
268 viodev->dev.parent = &vio_bus_device.dev;
269 viodev->dev.bus = &vio_bus_type;
270 viodev->dev.release = vio_dev_release;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000271
272 /* register with generic device framework */
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000273 if (device_register(&viodev->dev)) {
274 printk(KERN_ERR "%s: failed to register device %s\n",
275 __FUNCTION__, viodev->dev.bus_id);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000276 /* XXX free TCE table */
277 kfree(viodev);
278 return NULL;
279 }
280
281 return viodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000283EXPORT_SYMBOL(vio_register_device_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/**
286 * vio_bus_init: - Initialize the virtual IO bus
287 */
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000288static int __init vio_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int err;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000291 struct device_node *node_vroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Stephen Rothwellc8680782007-09-21 14:31:02 +1000293 if (firmware_has_feature(FW_FEATURE_ISERIES))
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000294 iommu_vio_init();
Stephen Rothwell63122362005-07-12 17:45:27 +1000295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 err = bus_register(&vio_bus_type);
297 if (err) {
298 printk(KERN_ERR "failed to register VIO bus\n");
299 return err;
300 }
301
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000302 /*
303 * The fake parent of all vio devices, just to give us
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000304 * a nice directory
305 */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700306 err = device_register(&vio_bus_device.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (err) {
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000308 printk(KERN_WARNING "%s: device_register returned %i\n",
309 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return err;
311 }
312
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000313 node_vroot = of_find_node_by_name(NULL, "vdevice");
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000314 if (node_vroot) {
315 struct device_node *of_node;
316
317 /*
318 * Create struct vio_devices for each virtual device in
319 * the device tree. Drivers will associate with them later.
320 */
321 for (of_node = node_vroot->child; of_node != NULL;
Stephen Rothwellc5467262007-09-21 14:29:28 +1000322 of_node = of_node->sibling)
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000323 vio_register_device_node(of_node);
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000324 of_node_put(node_vroot);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000329__initcall(vio_bus_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000331static ssize_t name_show(struct device *dev,
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000332 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
335}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000336
337static ssize_t devspec_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100340 struct device_node *of_node = dev->archdata.of_node;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000341
342 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
343}
344
345static struct device_attribute vio_dev_attrs[] = {
346 __ATTR_RO(name),
347 __ATTR_RO(devspec),
348 __ATTR_NULL
349};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351void __devinit vio_unregister_device(struct vio_dev *viodev)
352{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 device_unregister(&viodev->dev);
354}
355EXPORT_SYMBOL(vio_unregister_device);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static 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 Torvalds1da177e2005-04-16 15:20:36 -0700362
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000363 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Olaf Hering143dcec2005-11-08 21:34:36 -0800366static 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 Herrenschmidt12d04ee2006-11-11 17:25:02 +1100370 struct device_node *dn;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000371 const char *cp;
Olaf Hering143dcec2005-11-08 21:34:36 -0800372 int length;
373
374 if (!num_envp)
375 return -ENOMEM;
376
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100377 dn = dev->archdata.of_node;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000378 if (!dn)
Olaf Hering143dcec2005-11-08 21:34:36 -0800379 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000380 cp = of_get_property(dn, "compatible", &length);
Olaf Hering143dcec2005-11-08 21:34:36 -0800381 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 Rothwelle10fa772006-04-27 17:18:21 +1000387 if ((buffer_size - length) <= 0)
Olaf Hering143dcec2005-11-08 21:34:36 -0800388 return -ENOMEM;
389 envp[1] = NULL;
390 return 0;
391}
392
Stephen Rothwell6fccab22007-09-21 14:32:05 +1000393static struct bus_type vio_bus_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .name = "vio",
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000395 .dev_attrs = vio_dev_attrs,
Kay Sievers312c0042005-11-16 09:00:00 +0100396 .uevent = vio_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .match = vio_bus_match,
Russell King2f53a802006-01-05 14:36:47 +0000398 .probe = vio_bus_probe,
399 .remove = vio_bus_remove,
400 .shutdown = vio_bus_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000402
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 *
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000409 * Calls prom.c's of_get_property() to return the value of the
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000410 * attribute specified by @which
411*/
412const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
413{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000414 return of_get_property(vdev->dev.archdata.of_node, which, length);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000415}
416EXPORT_SYMBOL(vio_get_attribute);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000417
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 */
425static struct vio_dev *vio_find_name(const char *kobj_name)
426{
427 struct kobject *found;
428
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700429 found = kset_find_obj(&devices_subsys, kobj_name);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000430 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 */
440struct vio_dev *vio_find_node(struct device_node *vnode)
441{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000442 const uint32_t *unit_address;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000443 char kobj_name[BUS_ID_SIZE];
444
445 /* construct the kobject name from the device node */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000446 unit_address = of_get_property(vnode, "reg", NULL);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000447 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}
453EXPORT_SYMBOL(vio_find_node);
454
455int 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}
462EXPORT_SYMBOL(vio_enable_interrupts);
463
464int 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}
471EXPORT_SYMBOL(vio_disable_interrupts);
472#endif /* CONFIG_PPC_PSERIES */