blob: 7798f01f77b4aeacfd14586e9847deee1bf7ca74 [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 Rothwell63122362005-07-12 17:45:27 +100035static int (*is_match)(const struct vio_device_id *id,
36 const struct vio_dev *dev);
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +100037static void (*unregister_device_callback)(struct vio_dev *dev);
38static void (*release_device_callback)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100040/*
41 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100043 * succeeded.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int vio_bus_probe(struct device *dev)
46{
47 struct vio_dev *viodev = to_vio_dev(dev);
48 struct vio_driver *viodrv = to_vio_driver(dev->driver);
49 const struct vio_device_id *id;
50 int error = -ENODEV;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!viodrv->probe)
53 return error;
54
55 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100056 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 return error;
60}
61
62/* convert from struct device to struct vio_dev and pass to driver. */
63static int vio_bus_remove(struct device *dev)
64{
65 struct vio_dev *viodev = to_vio_dev(dev);
66 struct vio_driver *viodrv = to_vio_driver(dev->driver);
67
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100068 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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/**
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000105 * vio_match_device: - Tell if a VIO device has a matching
106 * VIO device id structure.
107 * @ids: array of VIO device id structures to search in
108 * @dev: the VIO device structure to match against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 * Used by a driver to check whether a VIO device present in the
111 * system is in its list of supported devices. Returns the matching
112 * vio_device_id structure or NULL if there is no match.
113 */
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000114static const struct vio_device_id *vio_match_device(
115 const struct vio_device_id *ids, const struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 while (ids->type) {
Stephen Rothwell63122362005-07-12 17:45:27 +1000118 if (is_match(ids, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ids;
120 ids++;
121 }
122 return NULL;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
126 * vio_bus_init: - Initialize the virtual IO bus
127 */
Stephen Rothwell63122362005-07-12 17:45:27 +1000128int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +1000129 const struct vio_dev *dev),
130 void (*unregister_dev)(struct vio_dev *),
131 void (*release_dev)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int err;
134
Stephen Rothwell63122362005-07-12 17:45:27 +1000135 is_match = match_func;
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +1000136 unregister_device_callback = unregister_dev;
137 release_device_callback = release_dev;
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 Rothwell19dbd0f2005-07-12 17:50:26 +1000162 if (release_device_callback)
163 release_device_callback(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 Rothwell3e494c82005-07-12 17:40:17 +1000174struct vio_dev * __devinit vio_register_device_common(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct vio_dev *viodev, char *name, char *type,
176 uint32_t unit_address, struct iommu_table *iommu_table)
177{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 viodev->name = name;
179 viodev->type = type;
180 viodev->unit_address = unit_address;
181 viodev->iommu_table = iommu_table;
182 /* init generic 'struct device' fields: */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700183 viodev->dev.parent = &vio_bus_device.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 viodev->dev.bus = &vio_bus_type;
185 viodev->dev.release = vio_dev_release;
186
187 /* register with generic device framework */
188 if (device_register(&viodev->dev)) {
189 printk(KERN_ERR "%s: failed to register device %s\n",
190 __FUNCTION__, viodev->dev.bus_id);
191 return NULL;
192 }
193 device_create_file(&viodev->dev, &dev_attr_name);
194
195 return viodev;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198void __devinit vio_unregister_device(struct vio_dev *viodev)
199{
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +1000200 if (unregister_device_callback)
201 unregister_device_callback(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 device_remove_file(&viodev->dev, &dev_attr_name);
203 device_unregister(&viodev->dev);
204}
205EXPORT_SYMBOL(vio_unregister_device);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
208 size_t size, enum dma_data_direction direction)
209{
210 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
211 direction);
212}
213
214static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
215 size_t size, enum dma_data_direction direction)
216{
217 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
218 direction);
219}
220
221static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
222 int nelems, enum dma_data_direction direction)
223{
224 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
225 nelems, direction);
226}
227
228static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
229 int nelems, enum dma_data_direction direction)
230{
231 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
232}
233
234static void *vio_alloc_coherent(struct device *dev, size_t size,
235 dma_addr_t *dma_handle, unsigned int __nocast flag)
236{
237 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
238 dma_handle, flag);
239}
240
241static void vio_free_coherent(struct device *dev, size_t size,
242 void *vaddr, dma_addr_t dma_handle)
243{
244 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
245 dma_handle);
246}
247
248static int vio_dma_supported(struct device *dev, u64 mask)
249{
250 return 1;
251}
252
253struct dma_mapping_ops vio_dma_ops = {
254 .alloc_coherent = vio_alloc_coherent,
255 .free_coherent = vio_free_coherent,
256 .map_single = vio_map_single,
257 .unmap_single = vio_unmap_single,
258 .map_sg = vio_map_sg,
259 .unmap_sg = vio_unmap_sg,
260 .dma_supported = vio_dma_supported,
261};
262
263static int vio_bus_match(struct device *dev, struct device_driver *drv)
264{
265 const struct vio_dev *vio_dev = to_vio_dev(dev);
266 struct vio_driver *vio_drv = to_vio_driver(drv);
267 const struct vio_device_id *ids = vio_drv->id_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000269 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272struct bus_type vio_bus_type = {
273 .name = "vio",
274 .match = vio_bus_match,
275};