Commit | Line | Data |
---|---|---|
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 | |
25 | static const struct vio_device_id *vio_match_device( | |
26 | const struct vio_device_id *, const struct vio_dev *); | |
27 | ||
3e494c80 | 28 | struct 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 | |
71d276d7 | 35 | static struct vio_bus_ops vio_bus_ops; |
1da177e4 | 36 | |
5c0b4b87 SR |
37 | /* |
38 | * Convert from struct device to struct vio_dev and pass to driver. | |
1da177e4 | 39 | * dev->driver has already been set by generic code because vio_bus_match |
5c0b4b87 SR |
40 | * succeeded. |
41 | */ | |
1da177e4 LT |
42 | static 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 | ||
1da177e4 LT |
49 | if (!viodrv->probe) |
50 | return error; | |
51 | ||
52 | id = vio_match_device(viodrv->id_table, viodev); | |
5c0b4b87 | 53 | if (id) |
1da177e4 | 54 | error = viodrv->probe(viodev, id); |
1da177e4 LT |
55 | |
56 | return error; | |
57 | } | |
58 | ||
59 | /* convert from struct device to struct vio_dev and pass to driver. */ | |
60 | static 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 | ||
5c0b4b87 | 65 | if (viodrv->remove) |
1da177e4 | 66 | return viodrv->remove(viodev); |
1da177e4 LT |
67 | |
68 | /* driver can't remove */ | |
69 | return 1; | |
70 | } | |
71 | ||
34060104 SR |
72 | /* convert from struct device to struct vio_dev and pass to driver. */ |
73 | static void vio_bus_shutdown(struct device *dev) | |
74 | { | |
75 | struct vio_dev *viodev = to_vio_dev(dev); | |
76 | struct vio_driver *viodrv = to_vio_driver(dev->driver); | |
77 | ||
78 | if (viodrv->shutdown) | |
79 | viodrv->shutdown(viodev); | |
80 | } | |
81 | ||
1da177e4 LT |
82 | /** |
83 | * vio_register_driver: - Register a new vio driver | |
84 | * @drv: The vio_driver structure to be registered. | |
85 | */ | |
86 | int vio_register_driver(struct vio_driver *viodrv) | |
87 | { | |
88 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, | |
6fdf5392 | 89 | viodrv->driver.name); |
1da177e4 LT |
90 | |
91 | /* fill in 'struct driver' fields */ | |
1da177e4 LT |
92 | viodrv->driver.bus = &vio_bus_type; |
93 | viodrv->driver.probe = vio_bus_probe; | |
94 | viodrv->driver.remove = vio_bus_remove; | |
34060104 | 95 | viodrv->driver.shutdown = vio_bus_shutdown; |
1da177e4 LT |
96 | |
97 | return driver_register(&viodrv->driver); | |
98 | } | |
99 | EXPORT_SYMBOL(vio_register_driver); | |
100 | ||
101 | /** | |
102 | * vio_unregister_driver - Remove registration of vio driver. | |
103 | * @driver: The vio_driver struct to be removed form registration | |
104 | */ | |
105 | void vio_unregister_driver(struct vio_driver *viodrv) | |
106 | { | |
107 | driver_unregister(&viodrv->driver); | |
108 | } | |
109 | EXPORT_SYMBOL(vio_unregister_driver); | |
110 | ||
111 | /** | |
5c0b4b87 SR |
112 | * vio_match_device: - Tell if a VIO device has a matching |
113 | * VIO device id structure. | |
114 | * @ids: array of VIO device id structures to search in | |
115 | * @dev: the VIO device structure to match against | |
1da177e4 LT |
116 | * |
117 | * Used by a driver to check whether a VIO device present in the | |
118 | * system is in its list of supported devices. Returns the matching | |
119 | * vio_device_id structure or NULL if there is no match. | |
120 | */ | |
5c0b4b87 SR |
121 | static const struct vio_device_id *vio_match_device( |
122 | const struct vio_device_id *ids, const struct vio_dev *dev) | |
1da177e4 | 123 | { |
fb120da6 | 124 | while (ids->type[0] != '\0') { |
71d276d7 | 125 | if (vio_bus_ops.match(ids, dev)) |
1da177e4 LT |
126 | return ids; |
127 | ids++; | |
128 | } | |
129 | return NULL; | |
130 | } | |
131 | ||
1da177e4 LT |
132 | /** |
133 | * vio_bus_init: - Initialize the virtual IO bus | |
134 | */ | |
71d276d7 | 135 | int __init vio_bus_init(struct vio_bus_ops *ops) |
1da177e4 LT |
136 | { |
137 | int err; | |
138 | ||
71d276d7 | 139 | vio_bus_ops = *ops; |
6312236f | 140 | |
1da177e4 LT |
141 | err = bus_register(&vio_bus_type); |
142 | if (err) { | |
143 | printk(KERN_ERR "failed to register VIO bus\n"); | |
144 | return err; | |
145 | } | |
146 | ||
5c0b4b87 SR |
147 | /* |
148 | * The fake parent of all vio devices, just to give us | |
3e494c80 SR |
149 | * a nice directory |
150 | */ | |
ac5b33c9 | 151 | err = device_register(&vio_bus_device.dev); |
1da177e4 | 152 | if (err) { |
3e494c80 SR |
153 | printk(KERN_WARNING "%s: device_register returned %i\n", |
154 | __FUNCTION__, err); | |
1da177e4 LT |
155 | return err; |
156 | } | |
157 | ||
3e494c80 SR |
158 | return 0; |
159 | } | |
160 | ||
1da177e4 LT |
161 | /* vio_dev refcount hit 0 */ |
162 | static void __devinit vio_dev_release(struct device *dev) | |
163 | { | |
71d276d7 SR |
164 | if (vio_bus_ops.release_device) |
165 | vio_bus_ops.release_device(dev); | |
1da177e4 LT |
166 | kfree(to_vio_dev(dev)); |
167 | } | |
168 | ||
5c0b4b87 SR |
169 | static ssize_t viodev_show_name(struct device *dev, |
170 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
171 | { |
172 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); | |
173 | } | |
174 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); | |
175 | ||
b877b90f | 176 | struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev) |
1da177e4 | 177 | { |
1da177e4 | 178 | /* init generic 'struct device' fields: */ |
ac5b33c9 | 179 | viodev->dev.parent = &vio_bus_device.dev; |
1da177e4 LT |
180 | viodev->dev.bus = &vio_bus_type; |
181 | viodev->dev.release = vio_dev_release; | |
182 | ||
183 | /* register with generic device framework */ | |
184 | if (device_register(&viodev->dev)) { | |
185 | printk(KERN_ERR "%s: failed to register device %s\n", | |
186 | __FUNCTION__, viodev->dev.bus_id); | |
187 | return NULL; | |
188 | } | |
189 | device_create_file(&viodev->dev, &dev_attr_name); | |
190 | ||
191 | return viodev; | |
192 | } | |
193 | ||
1da177e4 LT |
194 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
195 | { | |
71d276d7 SR |
196 | if (vio_bus_ops.unregister_device) |
197 | vio_bus_ops.unregister_device(viodev); | |
1da177e4 LT |
198 | device_remove_file(&viodev->dev, &dev_attr_name); |
199 | device_unregister(&viodev->dev); | |
200 | } | |
201 | EXPORT_SYMBOL(vio_unregister_device); | |
202 | ||
1da177e4 LT |
203 | static dma_addr_t vio_map_single(struct device *dev, void *vaddr, |
204 | size_t size, enum dma_data_direction direction) | |
205 | { | |
206 | return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size, | |
207 | direction); | |
208 | } | |
209 | ||
210 | static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle, | |
211 | size_t size, enum dma_data_direction direction) | |
212 | { | |
213 | iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size, | |
214 | direction); | |
215 | } | |
216 | ||
217 | static int vio_map_sg(struct device *dev, struct scatterlist *sglist, | |
218 | int nelems, enum dma_data_direction direction) | |
219 | { | |
220 | return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist, | |
221 | nelems, direction); | |
222 | } | |
223 | ||
224 | static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist, | |
225 | int nelems, enum dma_data_direction direction) | |
226 | { | |
227 | iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction); | |
228 | } | |
229 | ||
230 | static void *vio_alloc_coherent(struct device *dev, size_t size, | |
dd0fc66f | 231 | dma_addr_t *dma_handle, gfp_t flag) |
1da177e4 LT |
232 | { |
233 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, | |
234 | dma_handle, flag); | |
235 | } | |
236 | ||
237 | static void vio_free_coherent(struct device *dev, size_t size, | |
238 | void *vaddr, dma_addr_t dma_handle) | |
239 | { | |
240 | iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr, | |
241 | dma_handle); | |
242 | } | |
243 | ||
244 | static int vio_dma_supported(struct device *dev, u64 mask) | |
245 | { | |
246 | return 1; | |
247 | } | |
248 | ||
249 | struct dma_mapping_ops vio_dma_ops = { | |
250 | .alloc_coherent = vio_alloc_coherent, | |
251 | .free_coherent = vio_free_coherent, | |
252 | .map_single = vio_map_single, | |
253 | .unmap_single = vio_unmap_single, | |
254 | .map_sg = vio_map_sg, | |
255 | .unmap_sg = vio_unmap_sg, | |
256 | .dma_supported = vio_dma_supported, | |
257 | }; | |
258 | ||
259 | static int vio_bus_match(struct device *dev, struct device_driver *drv) | |
260 | { | |
261 | const struct vio_dev *vio_dev = to_vio_dev(dev); | |
262 | struct vio_driver *vio_drv = to_vio_driver(drv); | |
263 | const struct vio_device_id *ids = vio_drv->id_table; | |
1da177e4 | 264 | |
5c0b4b87 | 265 | return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL); |
1da177e4 LT |
266 | } |
267 | ||
268 | struct bus_type vio_bus_type = { | |
269 | .name = "vio", | |
270 | .match = vio_bus_match, | |
271 | }; |