blob: 971020cf3f7d24f90bd6d33edaf454e166fa7df7 [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
16#include <linux/init.h>
17#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mm.h>
20#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/iommu.h>
22#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/vio.h>
Olaf Hering143dcec2005-11-08 21:34:36 -080024#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static const struct vio_device_id *vio_match_device(
27 const struct vio_device_id *, const struct vio_dev *);
28
Stephen Rothwell3e494c82005-07-12 17:40:17 +100029struct vio_dev vio_bus_device = { /* fake "parent" device */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070030 .name = vio_bus_device.dev.bus_id,
31 .type = "",
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070032 .dev.bus_id = "vio",
33 .dev.bus = &vio_bus_type,
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stephen Rothwell71d276d2005-08-17 16:41:44 +100036static struct vio_bus_ops vio_bus_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100038/*
39 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100041 * succeeded.
42 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int vio_bus_probe(struct device *dev)
44{
45 struct vio_dev *viodev = to_vio_dev(dev);
46 struct vio_driver *viodrv = to_vio_driver(dev->driver);
47 const struct vio_device_id *id;
48 int error = -ENODEV;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (!viodrv->probe)
51 return error;
52
53 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100054 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 return error;
58}
59
60/* convert from struct device to struct vio_dev and pass to driver. */
61static int vio_bus_remove(struct device *dev)
62{
63 struct vio_dev *viodev = to_vio_dev(dev);
64 struct vio_driver *viodrv = to_vio_driver(dev->driver);
65
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100066 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* driver can't remove */
70 return 1;
71}
72
Stephen Rothwell34060102005-10-24 17:40:23 +100073/* convert from struct device to struct vio_dev and pass to driver. */
74static void vio_bus_shutdown(struct device *dev)
75{
76 struct vio_dev *viodev = to_vio_dev(dev);
77 struct vio_driver *viodrv = to_vio_driver(dev->driver);
78
Russell King2f53a802006-01-05 14:36:47 +000079 if (dev->driver && viodrv->shutdown)
Stephen Rothwell34060102005-10-24 17:40:23 +100080 viodrv->shutdown(viodev);
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/**
84 * vio_register_driver: - Register a new vio driver
85 * @drv: The vio_driver structure to be registered.
86 */
87int vio_register_driver(struct vio_driver *viodrv)
88{
89 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +100090 viodrv->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* fill in 'struct driver' fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 viodrv->driver.bus = &vio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 return driver_register(&viodrv->driver);
96}
97EXPORT_SYMBOL(vio_register_driver);
98
99/**
100 * vio_unregister_driver - Remove registration of vio driver.
101 * @driver: The vio_driver struct to be removed form registration
102 */
103void vio_unregister_driver(struct vio_driver *viodrv)
104{
105 driver_unregister(&viodrv->driver);
106}
107EXPORT_SYMBOL(vio_unregister_driver);
108
109/**
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000110 * vio_match_device: - Tell if a VIO device has a matching
111 * VIO device id structure.
112 * @ids: array of VIO device id structures to search in
113 * @dev: the VIO device structure to match against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
115 * Used by a driver to check whether a VIO device present in the
116 * system is in its list of supported devices. Returns the matching
117 * vio_device_id structure or NULL if there is no match.
118 */
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000119static const struct vio_device_id *vio_match_device(
120 const struct vio_device_id *ids, const struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000122 while (ids->type[0] != '\0') {
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000123 if (vio_bus_ops.match(ids, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return ids;
125 ids++;
126 }
127 return NULL;
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/**
131 * vio_bus_init: - Initialize the virtual IO bus
132 */
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000133int __init vio_bus_init(struct vio_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 int err;
136
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000137 vio_bus_ops = *ops;
Stephen Rothwell63122362005-07-12 17:45:27 +1000138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 err = bus_register(&vio_bus_type);
140 if (err) {
141 printk(KERN_ERR "failed to register VIO bus\n");
142 return err;
143 }
144
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000145 /*
146 * The fake parent of all vio devices, just to give us
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000147 * a nice directory
148 */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700149 err = device_register(&vio_bus_device.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (err) {
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000151 printk(KERN_WARNING "%s: device_register returned %i\n",
152 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return err;
154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* vio_dev refcount hit 0 */
160static void __devinit vio_dev_release(struct device *dev)
161{
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000162 if (vio_bus_ops.release_device)
163 vio_bus_ops.release_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 kfree(to_vio_dev(dev));
165}
166
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000167static ssize_t viodev_show_name(struct device *dev,
168 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
171}
172DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
173
Stephen Rothwellb877b902005-08-17 16:40:12 +1000174struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* init generic 'struct device' fields: */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700177 viodev->dev.parent = &vio_bus_device.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 viodev->dev.bus = &vio_bus_type;
179 viodev->dev.release = vio_dev_release;
180
181 /* register with generic device framework */
182 if (device_register(&viodev->dev)) {
183 printk(KERN_ERR "%s: failed to register device %s\n",
184 __FUNCTION__, viodev->dev.bus_id);
185 return NULL;
186 }
187 device_create_file(&viodev->dev, &dev_attr_name);
188
189 return viodev;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192void __devinit vio_unregister_device(struct vio_dev *viodev)
193{
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000194 if (vio_bus_ops.unregister_device)
195 vio_bus_ops.unregister_device(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 device_remove_file(&viodev->dev, &dev_attr_name);
197 device_unregister(&viodev->dev);
198}
199EXPORT_SYMBOL(vio_unregister_device);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
202 size_t size, enum dma_data_direction direction)
203{
204 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
Olof Johansson7daa4112006-04-12 21:05:59 -0500205 ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
209 size_t size, enum dma_data_direction direction)
210{
211 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
212 direction);
213}
214
215static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
216 int nelems, enum dma_data_direction direction)
217{
218 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
Olof Johansson7daa4112006-04-12 21:05:59 -0500219 nelems, ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
223 int nelems, enum dma_data_direction direction)
224{
225 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
226}
227
228static void *vio_alloc_coherent(struct device *dev, size_t size,
Al Virodd0fc662005-10-07 07:46:04 +0100229 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
Olof Johansson7daa4112006-04-12 21:05:59 -0500232 dma_handle, ~0ul, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static void vio_free_coherent(struct device *dev, size_t size,
236 void *vaddr, dma_addr_t dma_handle)
237{
238 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
239 dma_handle);
240}
241
242static int vio_dma_supported(struct device *dev, u64 mask)
243{
244 return 1;
245}
246
247struct dma_mapping_ops vio_dma_ops = {
248 .alloc_coherent = vio_alloc_coherent,
249 .free_coherent = vio_free_coherent,
250 .map_single = vio_map_single,
251 .unmap_single = vio_unmap_single,
252 .map_sg = vio_map_sg,
253 .unmap_sg = vio_unmap_sg,
254 .dma_supported = vio_dma_supported,
255};
256
257static int vio_bus_match(struct device *dev, struct device_driver *drv)
258{
259 const struct vio_dev *vio_dev = to_vio_dev(dev);
260 struct vio_driver *vio_drv = to_vio_driver(drv);
261 const struct vio_device_id *ids = vio_drv->id_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000263 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Olaf Hering143dcec2005-11-08 21:34:36 -0800266static int vio_hotplug(struct device *dev, char **envp, int num_envp,
267 char *buffer, int buffer_size)
268{
269 const struct vio_dev *vio_dev = to_vio_dev(dev);
270 char *cp;
271 int length;
272
273 if (!num_envp)
274 return -ENOMEM;
275
276 if (!vio_dev->dev.platform_data)
277 return -ENODEV;
278 cp = (char *)get_property(vio_dev->dev.platform_data, "compatible", &length);
279 if (!cp)
280 return -ENODEV;
281
282 envp[0] = buffer;
283 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
284 vio_dev->type, cp);
285 if (buffer_size - length <= 0)
286 return -ENOMEM;
287 envp[1] = NULL;
288 return 0;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291struct bus_type vio_bus_type = {
292 .name = "vio",
Kay Sievers312c0042005-11-16 09:00:00 +0100293 .uevent = vio_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .match = vio_bus_match,
Russell King2f53a802006-01-05 14:36:47 +0000295 .probe = vio_bus_probe,
296 .remove = vio_bus_remove,
297 .shutdown = vio_bus_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};