[PATCH] fix iSeries build for gcc-3.4
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / ppc64 / kernel / vio.c
CommitLineData
1da177e4
LT
1/*
2 * IBM PowerPC Virtual I/O Infrastructure Support.
3 *
19dbd0f6 4 * Copyright (c) 2003-2005 IBM Corp.
1da177e4
LT
5 * Dave Engebretsen engebret@us.ibm.com
6 * Santiago Leon santil@us.ibm.com
7 * Hollis Blanchard <hollisb@us.ibm.com>
19dbd0f6 8 * Stephen Rothwell
1da177e4
LT
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
16#include <linux/init.h>
17#include <linux/console.h>
1da177e4 18#include <linux/module.h>
1da177e4
LT
19#include <linux/mm.h>
20#include <linux/dma-mapping.h>
1da177e4
LT
21#include <asm/iommu.h>
22#include <asm/dma.h>
1da177e4 23#include <asm/vio.h>
1da177e4
LT
24
25static const struct vio_device_id *vio_match_device(
26 const struct vio_device_id *, const struct vio_dev *);
27
3e494c80 28struct vio_dev vio_bus_device = { /* fake "parent" device */
ac5b33c9
SR
29 .name = vio_bus_device.dev.bus_id,
30 .type = "",
ac5b33c9
SR
31 .dev.bus_id = "vio",
32 .dev.bus = &vio_bus_type,
1da177e4 33};
ac5b33c9 34
6312236f
SR
35static int (*is_match)(const struct vio_device_id *id,
36 const struct vio_dev *dev);
19dbd0f6
SR
37static void (*unregister_device_callback)(struct vio_dev *dev);
38static void (*release_device_callback)(struct device *dev);
1da177e4
LT
39
40/* convert from struct device to struct vio_dev and pass to driver.
41 * dev->driver has already been set by generic code because vio_bus_match
42 * succeeded. */
43static 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
1da177e4
LT
50 if (!viodrv->probe)
51 return error;
52
53 id = vio_match_device(viodrv->id_table, viodev);
54 if (id) {
55 error = viodrv->probe(viodev, id);
56 }
57
58 return error;
59}
60
61/* convert from struct device to struct vio_dev and pass to driver. */
62static int vio_bus_remove(struct device *dev)
63{
64 struct vio_dev *viodev = to_vio_dev(dev);
65 struct vio_driver *viodrv = to_vio_driver(dev->driver);
66
1da177e4
LT
67 if (viodrv->remove) {
68 return viodrv->remove(viodev);
69 }
70
71 /* driver can't remove */
72 return 1;
73}
74
75/**
76 * vio_register_driver: - Register a new vio driver
77 * @drv: The vio_driver structure to be registered.
78 */
79int vio_register_driver(struct vio_driver *viodrv)
80{
81 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
82 viodrv->name);
83
84 /* fill in 'struct driver' fields */
85 viodrv->driver.name = viodrv->name;
86 viodrv->driver.bus = &vio_bus_type;
87 viodrv->driver.probe = vio_bus_probe;
88 viodrv->driver.remove = vio_bus_remove;
89
90 return driver_register(&viodrv->driver);
91}
92EXPORT_SYMBOL(vio_register_driver);
93
94/**
95 * vio_unregister_driver - Remove registration of vio driver.
96 * @driver: The vio_driver struct to be removed form registration
97 */
98void vio_unregister_driver(struct vio_driver *viodrv)
99{
100 driver_unregister(&viodrv->driver);
101}
102EXPORT_SYMBOL(vio_unregister_driver);
103
104/**
105 * vio_match_device: - Tell if a VIO device has a matching VIO device id structure.
106 * @ids: array of VIO device id structures to search in
107 * @dev: the VIO device structure to match against
108 *
109 * Used by a driver to check whether a VIO device present in the
110 * system is in its list of supported devices. Returns the matching
111 * vio_device_id structure or NULL if there is no match.
112 */
113static const struct vio_device_id * vio_match_device(const struct vio_device_id *ids,
114 const struct vio_dev *dev)
115{
1da177e4 116 while (ids->type) {
6312236f 117 if (is_match(ids, dev))
1da177e4
LT
118 return ids;
119 ids++;
120 }
121 return NULL;
122}
123
1da177e4
LT
124/**
125 * vio_bus_init: - Initialize the virtual IO bus
126 */
6312236f 127int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
19dbd0f6
SR
128 const struct vio_dev *dev),
129 void (*unregister_dev)(struct vio_dev *),
130 void (*release_dev)(struct device *))
1da177e4
LT
131{
132 int err;
133
6312236f 134 is_match = match_func;
19dbd0f6
SR
135 unregister_device_callback = unregister_dev;
136 release_device_callback = release_dev;
6312236f 137
1da177e4
LT
138 err = bus_register(&vio_bus_type);
139 if (err) {
140 printk(KERN_ERR "failed to register VIO bus\n");
141 return err;
142 }
143
3e494c80
SR
144 /* the fake parent of all vio devices, just to give us
145 * a nice directory
146 */
ac5b33c9 147 err = device_register(&vio_bus_device.dev);
1da177e4 148 if (err) {
3e494c80
SR
149 printk(KERN_WARNING "%s: device_register returned %i\n",
150 __FUNCTION__, err);
1da177e4
LT
151 return err;
152 }
153
3e494c80
SR
154 return 0;
155}
156
1da177e4
LT
157/* vio_dev refcount hit 0 */
158static void __devinit vio_dev_release(struct device *dev)
159{
19dbd0f6
SR
160 if (release_device_callback)
161 release_device_callback(dev);
1da177e4
LT
162 kfree(to_vio_dev(dev));
163}
164
ff381d22 165static ssize_t viodev_show_name(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
166{
167 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
168}
169DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
170
3e494c80 171struct vio_dev * __devinit vio_register_device_common(
1da177e4
LT
172 struct vio_dev *viodev, char *name, char *type,
173 uint32_t unit_address, struct iommu_table *iommu_table)
174{
1da177e4
LT
175 viodev->name = name;
176 viodev->type = type;
177 viodev->unit_address = unit_address;
178 viodev->iommu_table = iommu_table;
179 /* init generic 'struct device' fields: */
ac5b33c9 180 viodev->dev.parent = &vio_bus_device.dev;
1da177e4
LT
181 viodev->dev.bus = &vio_bus_type;
182 viodev->dev.release = vio_dev_release;
183
184 /* register with generic device framework */
185 if (device_register(&viodev->dev)) {
186 printk(KERN_ERR "%s: failed to register device %s\n",
187 __FUNCTION__, viodev->dev.bus_id);
188 return NULL;
189 }
190 device_create_file(&viodev->dev, &dev_attr_name);
191
192 return viodev;
193}
194
1da177e4
LT
195void __devinit vio_unregister_device(struct vio_dev *viodev)
196{
19dbd0f6
SR
197 if (unregister_device_callback)
198 unregister_device_callback(viodev);
1da177e4
LT
199 device_remove_file(&viodev->dev, &dev_attr_name);
200 device_unregister(&viodev->dev);
201}
202EXPORT_SYMBOL(vio_unregister_device);
203
1da177e4
LT
204static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
205 size_t size, enum dma_data_direction direction)
206{
207 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
208 direction);
209}
210
211static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
212 size_t size, enum dma_data_direction direction)
213{
214 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
215 direction);
216}
217
218static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
219 int nelems, enum dma_data_direction direction)
220{
221 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
222 nelems, direction);
223}
224
225static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
226 int nelems, enum dma_data_direction direction)
227{
228 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
229}
230
231static void *vio_alloc_coherent(struct device *dev, size_t size,
232 dma_addr_t *dma_handle, unsigned int __nocast flag)
233{
234 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
235 dma_handle, flag);
236}
237
238static void vio_free_coherent(struct device *dev, size_t size,
239 void *vaddr, dma_addr_t dma_handle)
240{
241 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
242 dma_handle);
243}
244
245static int vio_dma_supported(struct device *dev, u64 mask)
246{
247 return 1;
248}
249
250struct dma_mapping_ops vio_dma_ops = {
251 .alloc_coherent = vio_alloc_coherent,
252 .free_coherent = vio_free_coherent,
253 .map_single = vio_map_single,
254 .unmap_single = vio_unmap_single,
255 .map_sg = vio_map_sg,
256 .unmap_sg = vio_unmap_sg,
257 .dma_supported = vio_dma_supported,
258};
259
260static int vio_bus_match(struct device *dev, struct device_driver *drv)
261{
262 const struct vio_dev *vio_dev = to_vio_dev(dev);
263 struct vio_driver *vio_drv = to_vio_driver(drv);
264 const struct vio_device_id *ids = vio_drv->id_table;
265 const struct vio_device_id *found_id;
266
1da177e4
LT
267 if (!ids)
268 return 0;
269
270 found_id = vio_match_device(ids, vio_dev);
271 if (found_id)
272 return 1;
273
274 return 0;
275}
276
277struct bus_type vio_bus_type = {
278 .name = "vio",
279 .match = vio_bus_match,
280};