blob: cdf5867838a6b6da14d54e173d88cc8de4390855 [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
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100040extern struct subsystem devices_subsys; /* needed for vio_find_name() */
41
42static struct vio_dev vio_bus_device = { /* fake "parent" device */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070043 .name = vio_bus_device.dev.bus_id,
44 .type = "",
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070045 .dev.bus_id = "vio",
46 .dev.bus = &vio_bus_type,
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100049#ifdef CONFIG_PPC_ISERIES
50struct device *iSeries_vio_dev = &vio_bus_device.dev;
51EXPORT_SYMBOL(iSeries_vio_dev);
52
53static struct iommu_table veth_iommu_table;
54static struct iommu_table vio_iommu_table;
55
56static 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 Blanchardca1588e2006-06-10 20:58:08 +100063 if (!iommu_init_table(&veth_iommu_table, -1))
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100064 printk("Virtual Bus VETH TCE table failed.\n");
Anton Blanchardca1588e2006-06-10 20:58:08 +100065 if (!iommu_init_table(&vio_iommu_table, -1))
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100066 printk("Virtual Bus VIO TCE table failed.\n");
67}
68#endif
69
70static 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 Rothwell1dc461f2006-05-18 17:21:26 +100074 if (strcmp(dev->type, "network") == 0)
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100075 return &veth_iommu_table;
76 return &vio_iommu_table;
77 } else
78#endif
79 {
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100080 unsigned char *dma_window;
81 struct iommu_table *tbl;
82 unsigned long offset, size;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100083
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100084 dma_window = get_property(dev->dev.platform_data,
85 "ibm,my-dma-window", NULL);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100086 if (!dma_window)
87 return NULL;
88
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100089 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100090
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100091 of_parse_dma_window(dev->dev.platform_data, dma_window,
92 &tbl->it_index, &offset, &size);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100093
94 /* TCE table size - measured in tce entries */
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100095 tbl->it_size = size >> PAGE_SHIFT;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100096 /* offset for VIO should always be 0 */
Jeremy Kerr4c76e0b2006-05-18 18:06:37 +100097 tbl->it_offset = offset >> PAGE_SHIFT;
98 tbl->it_busno = 0;
99 tbl->it_type = TCE_VB;
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000100
Anton Blanchardca1588e2006-06-10 20:58:08 +1000101 return iommu_init_table(tbl, -1);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000102 }
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000105/**
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 */
115static 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 Rothwelldd721ff2006-04-27 17:21:46 +1000119 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
120 device_is_compatible(dev->dev.platform_data, ids->compat))
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000121 return ids;
122 ids++;
123 }
124 return NULL;
125}
126
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000127/*
128 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000130 * succeeded.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int vio_bus_probe(struct device *dev)
133{
134 struct vio_dev *viodev = to_vio_dev(dev);
135 struct vio_driver *viodrv = to_vio_driver(dev->driver);
136 const struct vio_device_id *id;
137 int error = -ENODEV;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!viodrv->probe)
140 return error;
141
142 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000143 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return error;
147}
148
149/* convert from struct device to struct vio_dev and pass to driver. */
150static int vio_bus_remove(struct device *dev)
151{
152 struct vio_dev *viodev = to_vio_dev(dev);
153 struct vio_driver *viodrv = to_vio_driver(dev->driver);
154
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000155 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* driver can't remove */
159 return 1;
160}
161
Stephen Rothwell34060102005-10-24 17:40:23 +1000162/* convert from struct device to struct vio_dev and pass to driver. */
163static void vio_bus_shutdown(struct device *dev)
164{
165 struct vio_dev *viodev = to_vio_dev(dev);
166 struct vio_driver *viodrv = to_vio_driver(dev->driver);
167
Russell King2f53a802006-01-05 14:36:47 +0000168 if (dev->driver && viodrv->shutdown)
Stephen Rothwell34060102005-10-24 17:40:23 +1000169 viodrv->shutdown(viodev);
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/**
173 * vio_register_driver: - Register a new vio driver
174 * @drv: The vio_driver structure to be registered.
175 */
176int vio_register_driver(struct vio_driver *viodrv)
177{
178 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000179 viodrv->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* fill in 'struct driver' fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 viodrv->driver.bus = &vio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 return driver_register(&viodrv->driver);
185}
186EXPORT_SYMBOL(vio_register_driver);
187
188/**
189 * vio_unregister_driver - Remove registration of vio driver.
190 * @driver: The vio_driver struct to be removed form registration
191 */
192void vio_unregister_driver(struct vio_driver *viodrv)
193{
194 driver_unregister(&viodrv->driver);
195}
196EXPORT_SYMBOL(vio_unregister_driver);
197
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000198/* vio_dev refcount hit 0 */
199static void __devinit vio_dev_release(struct device *dev)
200{
201 if (dev->platform_data) {
202 /* XXX free TCE table */
203 of_node_put(dev->platform_data);
204 }
205 kfree(to_vio_dev(dev));
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/**
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000209 * vio_register_device_node: - Register a new vio device.
210 * @of_node: The OF node for this device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 *
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000212 * Creates and initializes a vio_dev structure from the data in
213 * of_node (dev.platform_data) and adds it to the list of virtual devices.
214 * Returns a pointer to the created vio_dev or NULL if node has
215 * NULL device_type or compatible fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000217struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000219 struct vio_dev *viodev;
220 unsigned int *unit_address;
221 unsigned int *irq_p;
222
223 /* we need the 'device_type' property, in order to match with drivers */
224 if (of_node->type == NULL) {
225 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
226 __FUNCTION__,
227 of_node->name ? of_node->name : "<unknown>");
228 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000230
231 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
232 if (unit_address == NULL) {
233 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
234 __FUNCTION__,
235 of_node->name ? of_node->name : "<unknown>");
236 return NULL;
237 }
238
239 /* allocate a vio_dev for this node */
240 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
241 if (viodev == NULL)
242 return NULL;
243
244 viodev->dev.platform_data = of_node_get(of_node);
245
246 viodev->irq = NO_IRQ;
247 irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
248 if (irq_p) {
249 int virq = virt_irq_create_mapping(*irq_p);
250 if (virq == NO_IRQ) {
251 printk(KERN_ERR "Unable to allocate interrupt "
252 "number for %s\n", of_node->full_name);
253 } else
254 viodev->irq = irq_offset_up(virq);
255 }
256
257 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
258 viodev->name = of_node->name;
259 viodev->type = of_node->type;
260 viodev->unit_address = *unit_address;
261 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
262 unit_address = (unsigned int *)get_property(of_node,
263 "linux,unit_address", NULL);
264 if (unit_address != NULL)
265 viodev->unit_address = *unit_address;
266 }
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000267 viodev->iommu_table = vio_build_iommu_table(viodev);
268
269 /* init generic 'struct device' fields: */
270 viodev->dev.parent = &vio_bus_device.dev;
271 viodev->dev.bus = &vio_bus_type;
272 viodev->dev.release = vio_dev_release;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000273
274 /* register with generic device framework */
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000275 if (device_register(&viodev->dev)) {
276 printk(KERN_ERR "%s: failed to register device %s\n",
277 __FUNCTION__, viodev->dev.bus_id);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000278 /* XXX free TCE table */
279 kfree(viodev);
280 return NULL;
281 }
282
283 return viodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000285EXPORT_SYMBOL(vio_register_device_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/**
288 * vio_bus_init: - Initialize the virtual IO bus
289 */
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000290static int __init vio_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int err;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000293 struct device_node *node_vroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000295#ifdef CONFIG_PPC_ISERIES
296 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
297 iommu_vio_init();
298 vio_bus_device.iommu_table = &vio_iommu_table;
299 iSeries_vio_dev = &vio_bus_device.dev;
300 }
301#endif
Stephen Rothwell63122362005-07-12 17:45:27 +1000302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 err = bus_register(&vio_bus_type);
304 if (err) {
305 printk(KERN_ERR "failed to register VIO bus\n");
306 return err;
307 }
308
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000309 /*
310 * The fake parent of all vio devices, just to give us
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000311 * a nice directory
312 */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700313 err = device_register(&vio_bus_device.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (err) {
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000315 printk(KERN_WARNING "%s: device_register returned %i\n",
316 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return err;
318 }
319
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000320 node_vroot = find_devices("vdevice");
321 if (node_vroot) {
322 struct device_node *of_node;
323
324 /*
325 * Create struct vio_devices for each virtual device in
326 * the device tree. Drivers will associate with them later.
327 */
328 for (of_node = node_vroot->child; of_node != NULL;
329 of_node = of_node->sibling) {
330 printk(KERN_DEBUG "%s: processing %p\n",
331 __FUNCTION__, of_node);
332 vio_register_device_node(of_node);
333 }
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000338__initcall(vio_bus_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000340static ssize_t name_show(struct device *dev,
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000341 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
344}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000345
346static ssize_t devspec_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 struct device_node *of_node = dev->platform_data;
350
351 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
352}
353
354static struct device_attribute vio_dev_attrs[] = {
355 __ATTR_RO(name),
356 __ATTR_RO(devspec),
357 __ATTR_NULL
358};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360void __devinit vio_unregister_device(struct vio_dev *viodev)
361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 device_unregister(&viodev->dev);
363}
364EXPORT_SYMBOL(vio_unregister_device);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
367 size_t size, enum dma_data_direction direction)
368{
369 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
Olof Johansson7daa4112006-04-12 21:05:59 -0500370 ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
374 size_t size, enum dma_data_direction direction)
375{
376 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
377 direction);
378}
379
380static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
381 int nelems, enum dma_data_direction direction)
382{
383 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
Olof Johansson7daa4112006-04-12 21:05:59 -0500384 nelems, ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
388 int nelems, enum dma_data_direction direction)
389{
390 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
391}
392
393static void *vio_alloc_coherent(struct device *dev, size_t size,
Al Virodd0fc662005-10-07 07:46:04 +0100394 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
Christoph Hellwig8eb6c6e2006-06-06 16:11:35 +0200397 dma_handle, ~0ul, flag, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static void vio_free_coherent(struct device *dev, size_t size,
401 void *vaddr, dma_addr_t dma_handle)
402{
403 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
404 dma_handle);
405}
406
407static int vio_dma_supported(struct device *dev, u64 mask)
408{
409 return 1;
410}
411
412struct dma_mapping_ops vio_dma_ops = {
413 .alloc_coherent = vio_alloc_coherent,
414 .free_coherent = vio_free_coherent,
415 .map_single = vio_map_single,
416 .unmap_single = vio_unmap_single,
417 .map_sg = vio_map_sg,
418 .unmap_sg = vio_unmap_sg,
419 .dma_supported = vio_dma_supported,
420};
421
422static int vio_bus_match(struct device *dev, struct device_driver *drv)
423{
424 const struct vio_dev *vio_dev = to_vio_dev(dev);
425 struct vio_driver *vio_drv = to_vio_driver(drv);
426 const struct vio_device_id *ids = vio_drv->id_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000428 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Olaf Hering143dcec2005-11-08 21:34:36 -0800431static int vio_hotplug(struct device *dev, char **envp, int num_envp,
432 char *buffer, int buffer_size)
433{
434 const struct vio_dev *vio_dev = to_vio_dev(dev);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000435 struct device_node *dn = dev->platform_data;
Olaf Hering143dcec2005-11-08 21:34:36 -0800436 char *cp;
437 int length;
438
439 if (!num_envp)
440 return -ENOMEM;
441
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000442 if (!dn)
Olaf Hering143dcec2005-11-08 21:34:36 -0800443 return -ENODEV;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000444 cp = (char *)get_property(dn, "compatible", &length);
Olaf Hering143dcec2005-11-08 21:34:36 -0800445 if (!cp)
446 return -ENODEV;
447
448 envp[0] = buffer;
449 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
450 vio_dev->type, cp);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000451 if ((buffer_size - length) <= 0)
Olaf Hering143dcec2005-11-08 21:34:36 -0800452 return -ENOMEM;
453 envp[1] = NULL;
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457struct bus_type vio_bus_type = {
458 .name = "vio",
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000459 .dev_attrs = vio_dev_attrs,
Kay Sievers312c0042005-11-16 09:00:00 +0100460 .uevent = vio_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .match = vio_bus_match,
Russell King2f53a802006-01-05 14:36:47 +0000462 .probe = vio_bus_probe,
463 .remove = vio_bus_remove,
464 .shutdown = vio_bus_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465};
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000466
467/**
468 * vio_get_attribute: - get attribute for virtual device
469 * @vdev: The vio device to get property.
470 * @which: The property/attribute to be extracted.
471 * @length: Pointer to length of returned data size (unused if NULL).
472 *
473 * Calls prom.c's get_property() to return the value of the
474 * attribute specified by @which
475*/
476const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
477{
478 return get_property(vdev->dev.platform_data, which, length);
479}
480EXPORT_SYMBOL(vio_get_attribute);
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +1000481
482#ifdef CONFIG_PPC_PSERIES
483/* vio_find_name() - internal because only vio.c knows how we formatted the
484 * kobject name
485 * XXX once vio_bus_type.devices is actually used as a kset in
486 * drivers/base/bus.c, this function should be removed in favor of
487 * "device_find(kobj_name, &vio_bus_type)"
488 */
489static struct vio_dev *vio_find_name(const char *kobj_name)
490{
491 struct kobject *found;
492
493 found = kset_find_obj(&devices_subsys.kset, kobj_name);
494 if (!found)
495 return NULL;
496
497 return to_vio_dev(container_of(found, struct device, kobj));
498}
499
500/**
501 * vio_find_node - find an already-registered vio_dev
502 * @vnode: device_node of the virtual device we're looking for
503 */
504struct vio_dev *vio_find_node(struct device_node *vnode)
505{
506 uint32_t *unit_address;
507 char kobj_name[BUS_ID_SIZE];
508
509 /* construct the kobject name from the device node */
510 unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
511 if (!unit_address)
512 return NULL;
513 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
514
515 return vio_find_name(kobj_name);
516}
517EXPORT_SYMBOL(vio_find_node);
518
519int vio_enable_interrupts(struct vio_dev *dev)
520{
521 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
522 if (rc != H_SUCCESS)
523 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
524 return rc;
525}
526EXPORT_SYMBOL(vio_enable_interrupts);
527
528int vio_disable_interrupts(struct vio_dev *dev)
529{
530 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
531 if (rc != H_SUCCESS)
532 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
533 return rc;
534}
535EXPORT_SYMBOL(vio_disable_interrupts);
536#endif /* CONFIG_PPC_PSERIES */