blob: 3aec492ec76721d363390ffda237202de8e71738 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static const struct vio_device_id *vio_match_device(
26 const struct vio_device_id *, const struct vio_dev *);
27
Stephen Rothwell3e494c82005-07-12 17:40:17 +100028struct vio_dev vio_bus_device = { /* fake "parent" device */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070029 .name = vio_bus_device.dev.bus_id,
30 .type = "",
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070031 .dev.bus_id = "vio",
32 .dev.bus = &vio_bus_type,
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Stephen Rothwell71d276d2005-08-17 16:41:44 +100035static struct vio_bus_ops vio_bus_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100037/*
38 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100040 * succeeded.
41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int vio_bus_probe(struct device *dev)
43{
44 struct vio_dev *viodev = to_vio_dev(dev);
45 struct vio_driver *viodrv = to_vio_driver(dev->driver);
46 const struct vio_device_id *id;
47 int error = -ENODEV;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!viodrv->probe)
50 return error;
51
52 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100053 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 return error;
57}
58
59/* convert from struct device to struct vio_dev and pass to driver. */
60static int vio_bus_remove(struct device *dev)
61{
62 struct vio_dev *viodev = to_vio_dev(dev);
63 struct vio_driver *viodrv = to_vio_driver(dev->driver);
64
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100065 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /* driver can't remove */
69 return 1;
70}
71
72/**
73 * vio_register_driver: - Register a new vio driver
74 * @drv: The vio_driver structure to be registered.
75 */
76int vio_register_driver(struct vio_driver *viodrv)
77{
78 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +100079 viodrv->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* fill in 'struct driver' fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 viodrv->driver.bus = &vio_bus_type;
83 viodrv->driver.probe = vio_bus_probe;
84 viodrv->driver.remove = vio_bus_remove;
85
86 return driver_register(&viodrv->driver);
87}
88EXPORT_SYMBOL(vio_register_driver);
89
90/**
91 * vio_unregister_driver - Remove registration of vio driver.
92 * @driver: The vio_driver struct to be removed form registration
93 */
94void vio_unregister_driver(struct vio_driver *viodrv)
95{
96 driver_unregister(&viodrv->driver);
97}
98EXPORT_SYMBOL(vio_unregister_driver);
99
100/**
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000101 * vio_match_device: - Tell if a VIO device has a matching
102 * VIO device id structure.
103 * @ids: array of VIO device id structures to search in
104 * @dev: the VIO device structure to match against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * Used by a driver to check whether a VIO device present in the
107 * system is in its list of supported devices. Returns the matching
108 * vio_device_id structure or NULL if there is no match.
109 */
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000110static const struct vio_device_id *vio_match_device(
111 const struct vio_device_id *ids, const struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000113 while (ids->type[0] != '\0') {
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000114 if (vio_bus_ops.match(ids, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ids;
116 ids++;
117 }
118 return NULL;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/**
122 * vio_bus_init: - Initialize the virtual IO bus
123 */
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000124int __init vio_bus_init(struct vio_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int err;
127
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000128 vio_bus_ops = *ops;
Stephen Rothwell63122362005-07-12 17:45:27 +1000129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 err = bus_register(&vio_bus_type);
131 if (err) {
132 printk(KERN_ERR "failed to register VIO bus\n");
133 return err;
134 }
135
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000136 /*
137 * The fake parent of all vio devices, just to give us
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000138 * a nice directory
139 */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700140 err = device_register(&vio_bus_device.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (err) {
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000142 printk(KERN_WARNING "%s: device_register returned %i\n",
143 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return err;
145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* vio_dev refcount hit 0 */
151static void __devinit vio_dev_release(struct device *dev)
152{
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000153 if (vio_bus_ops.release_device)
154 vio_bus_ops.release_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 kfree(to_vio_dev(dev));
156}
157
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000158static ssize_t viodev_show_name(struct device *dev,
159 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
162}
163DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
164
Stephen Rothwellb877b902005-08-17 16:40:12 +1000165struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* init generic 'struct device' fields: */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700168 viodev->dev.parent = &vio_bus_device.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 viodev->dev.bus = &vio_bus_type;
170 viodev->dev.release = vio_dev_release;
171
172 /* register with generic device framework */
173 if (device_register(&viodev->dev)) {
174 printk(KERN_ERR "%s: failed to register device %s\n",
175 __FUNCTION__, viodev->dev.bus_id);
176 return NULL;
177 }
178 device_create_file(&viodev->dev, &dev_attr_name);
179
180 return viodev;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183void __devinit vio_unregister_device(struct vio_dev *viodev)
184{
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000185 if (vio_bus_ops.unregister_device)
186 vio_bus_ops.unregister_device(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 device_remove_file(&viodev->dev, &dev_attr_name);
188 device_unregister(&viodev->dev);
189}
190EXPORT_SYMBOL(vio_unregister_device);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
193 size_t size, enum dma_data_direction direction)
194{
195 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
196 direction);
197}
198
199static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
200 size_t size, enum dma_data_direction direction)
201{
202 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
203 direction);
204}
205
206static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
207 int nelems, enum dma_data_direction direction)
208{
209 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
210 nelems, direction);
211}
212
213static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
214 int nelems, enum dma_data_direction direction)
215{
216 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
217}
218
219static void *vio_alloc_coherent(struct device *dev, size_t size,
Al Virodd0fc662005-10-07 07:46:04 +0100220 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
223 dma_handle, flag);
224}
225
226static void vio_free_coherent(struct device *dev, size_t size,
227 void *vaddr, dma_addr_t dma_handle)
228{
229 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
230 dma_handle);
231}
232
233static int vio_dma_supported(struct device *dev, u64 mask)
234{
235 return 1;
236}
237
238struct dma_mapping_ops vio_dma_ops = {
239 .alloc_coherent = vio_alloc_coherent,
240 .free_coherent = vio_free_coherent,
241 .map_single = vio_map_single,
242 .unmap_single = vio_unmap_single,
243 .map_sg = vio_map_sg,
244 .unmap_sg = vio_unmap_sg,
245 .dma_supported = vio_dma_supported,
246};
247
248static int vio_bus_match(struct device *dev, struct device_driver *drv)
249{
250 const struct vio_dev *vio_dev = to_vio_dev(dev);
251 struct vio_driver *vio_drv = to_vio_driver(drv);
252 const struct vio_device_id *ids = vio_drv->id_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000254 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257struct bus_type vio_bus_type = {
258 .name = "vio",
259 .match = vio_bus_match,
260};