blob: 19529297a2dca7c827b64dddda6f94912b460c17 [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>
Stephen Rothwelle10fa772006-04-27 17:18:21 +100025#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Stephen Rothwell3e494c82005-07-12 17:40:17 +100027struct vio_dev vio_bus_device = { /* fake "parent" device */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070028 .name = vio_bus_device.dev.bus_id,
29 .type = "",
Stephen Rothwellac5b33c2005-06-21 17:15:54 -070030 .dev.bus_id = "vio",
31 .dev.bus = &vio_bus_type,
32};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Stephen Rothwell71d276d2005-08-17 16:41:44 +100034static struct vio_bus_ops vio_bus_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stephen Rothwelle10fa772006-04-27 17:18:21 +100036/**
37 * vio_match_device: - Tell if a VIO device has a matching
38 * VIO device id structure.
39 * @ids: array of VIO device id structures to search in
40 * @dev: the VIO device structure to match against
41 *
42 * Used by a driver to check whether a VIO device present in the
43 * system is in its list of supported devices. Returns the matching
44 * vio_device_id structure or NULL if there is no match.
45 */
46static const struct vio_device_id *vio_match_device(
47 const struct vio_device_id *ids, const struct vio_dev *dev)
48{
49 while (ids->type[0] != '\0') {
Stephen Rothwelldd721ff2006-04-27 17:21:46 +100050 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
51 device_is_compatible(dev->dev.platform_data, ids->compat))
Stephen Rothwelle10fa772006-04-27 17:18:21 +100052 return ids;
53 ids++;
54 }
55 return NULL;
56}
57
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100058/*
59 * Convert from struct device to struct vio_dev and pass to driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * dev->driver has already been set by generic code because vio_bus_match
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100061 * succeeded.
62 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int vio_bus_probe(struct device *dev)
64{
65 struct vio_dev *viodev = to_vio_dev(dev);
66 struct vio_driver *viodrv = to_vio_driver(dev->driver);
67 const struct vio_device_id *id;
68 int error = -ENODEV;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (!viodrv->probe)
71 return error;
72
73 id = vio_match_device(viodrv->id_table, viodev);
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100074 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 error = viodrv->probe(viodev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return error;
78}
79
80/* convert from struct device to struct vio_dev and pass to driver. */
81static int vio_bus_remove(struct device *dev)
82{
83 struct vio_dev *viodev = to_vio_dev(dev);
84 struct vio_driver *viodrv = to_vio_driver(dev->driver);
85
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +100086 if (viodrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return viodrv->remove(viodev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 /* driver can't remove */
90 return 1;
91}
92
Stephen Rothwell34060102005-10-24 17:40:23 +100093/* convert from struct device to struct vio_dev and pass to driver. */
94static void vio_bus_shutdown(struct device *dev)
95{
96 struct vio_dev *viodev = to_vio_dev(dev);
97 struct vio_driver *viodrv = to_vio_driver(dev->driver);
98
Russell King2f53a802006-01-05 14:36:47 +000099 if (dev->driver && viodrv->shutdown)
Stephen Rothwell34060102005-10-24 17:40:23 +1000100 viodrv->shutdown(viodev);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/**
104 * vio_register_driver: - Register a new vio driver
105 * @drv: The vio_driver structure to be registered.
106 */
107int vio_register_driver(struct vio_driver *viodrv)
108{
109 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
Stephen Rothwell6fdf5392005-10-24 14:53:21 +1000110 viodrv->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* fill in 'struct driver' fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 viodrv->driver.bus = &vio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return driver_register(&viodrv->driver);
116}
117EXPORT_SYMBOL(vio_register_driver);
118
119/**
120 * vio_unregister_driver - Remove registration of vio driver.
121 * @driver: The vio_driver struct to be removed form registration
122 */
123void vio_unregister_driver(struct vio_driver *viodrv)
124{
125 driver_unregister(&viodrv->driver);
126}
127EXPORT_SYMBOL(vio_unregister_driver);
128
129/**
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000130 * vio_register_device_node: - Register a new vio device.
131 * @of_node: The OF node for this device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000133 * Creates and initializes a vio_dev structure from the data in
134 * of_node (dev.platform_data) and adds it to the list of virtual devices.
135 * Returns a pointer to the created vio_dev or NULL if node has
136 * NULL device_type or compatible fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000138struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000140 struct vio_dev *viodev;
141 unsigned int *unit_address;
142 unsigned int *irq_p;
143
144 /* we need the 'device_type' property, in order to match with drivers */
145 if (of_node->type == NULL) {
146 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
147 __FUNCTION__,
148 of_node->name ? of_node->name : "<unknown>");
149 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000151
152 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
153 if (unit_address == NULL) {
154 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
155 __FUNCTION__,
156 of_node->name ? of_node->name : "<unknown>");
157 return NULL;
158 }
159
160 /* allocate a vio_dev for this node */
161 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
162 if (viodev == NULL)
163 return NULL;
164
165 viodev->dev.platform_data = of_node_get(of_node);
166
167 viodev->irq = NO_IRQ;
168 irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
169 if (irq_p) {
170 int virq = virt_irq_create_mapping(*irq_p);
171 if (virq == NO_IRQ) {
172 printk(KERN_ERR "Unable to allocate interrupt "
173 "number for %s\n", of_node->full_name);
174 } else
175 viodev->irq = irq_offset_up(virq);
176 }
177
178 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
179 viodev->name = of_node->name;
180 viodev->type = of_node->type;
181 viodev->unit_address = *unit_address;
182 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
183 unit_address = (unsigned int *)get_property(of_node,
184 "linux,unit_address", NULL);
185 if (unit_address != NULL)
186 viodev->unit_address = *unit_address;
187 }
188 viodev->iommu_table = vio_bus_ops.build_iommu_table(viodev);
189
190 /* register with generic device framework */
191 if (vio_register_device(viodev) == NULL) {
192 /* XXX free TCE table */
193 kfree(viodev);
194 return NULL;
195 }
196
197 return viodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000199EXPORT_SYMBOL(vio_register_device_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/**
202 * vio_bus_init: - Initialize the virtual IO bus
203 */
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000204int __init vio_bus_init(struct vio_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int err;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000207 struct device_node *node_vroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Stephen Rothwell71d276d2005-08-17 16:41:44 +1000209 vio_bus_ops = *ops;
Stephen Rothwell63122362005-07-12 17:45:27 +1000210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 err = bus_register(&vio_bus_type);
212 if (err) {
213 printk(KERN_ERR "failed to register VIO bus\n");
214 return err;
215 }
216
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000217 /*
218 * The fake parent of all vio devices, just to give us
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000219 * a nice directory
220 */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700221 err = device_register(&vio_bus_device.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (err) {
Stephen Rothwell3e494c82005-07-12 17:40:17 +1000223 printk(KERN_WARNING "%s: device_register returned %i\n",
224 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
226 }
227
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000228 node_vroot = find_devices("vdevice");
229 if (node_vroot) {
230 struct device_node *of_node;
231
232 /*
233 * Create struct vio_devices for each virtual device in
234 * the device tree. Drivers will associate with them later.
235 */
236 for (of_node = node_vroot->child; of_node != NULL;
237 of_node = of_node->sibling) {
238 printk(KERN_DEBUG "%s: processing %p\n",
239 __FUNCTION__, of_node);
240 vio_register_device_node(of_node);
241 }
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* vio_dev refcount hit 0 */
248static void __devinit vio_dev_release(struct device *dev)
249{
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000250 if (dev->platform_data) {
251 /* XXX free TCE table */
252 of_node_put(dev->platform_data);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kfree(to_vio_dev(dev));
255}
256
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000257static ssize_t name_show(struct device *dev,
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000258 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
261}
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000262
263static ssize_t devspec_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
266 struct device_node *of_node = dev->platform_data;
267
268 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
269}
270
271static struct device_attribute vio_dev_attrs[] = {
272 __ATTR_RO(name),
273 __ATTR_RO(devspec),
274 __ATTR_NULL
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Stephen Rothwellb877b902005-08-17 16:40:12 +1000277struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* init generic 'struct device' fields: */
Stephen Rothwellac5b33c2005-06-21 17:15:54 -0700280 viodev->dev.parent = &vio_bus_device.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 viodev->dev.bus = &vio_bus_type;
282 viodev->dev.release = vio_dev_release;
283
284 /* register with generic device framework */
285 if (device_register(&viodev->dev)) {
286 printk(KERN_ERR "%s: failed to register device %s\n",
287 __FUNCTION__, viodev->dev.bus_id);
288 return NULL;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return viodev;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294void __devinit vio_unregister_device(struct vio_dev *viodev)
295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 device_unregister(&viodev->dev);
297}
298EXPORT_SYMBOL(vio_unregister_device);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
301 size_t size, enum dma_data_direction direction)
302{
303 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
Olof Johansson7daa4112006-04-12 21:05:59 -0500304 ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
308 size_t size, enum dma_data_direction direction)
309{
310 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
311 direction);
312}
313
314static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
315 int nelems, enum dma_data_direction direction)
316{
317 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
Olof Johansson7daa4112006-04-12 21:05:59 -0500318 nelems, ~0ul, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
322 int nelems, enum dma_data_direction direction)
323{
324 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
325}
326
327static void *vio_alloc_coherent(struct device *dev, size_t size,
Al Virodd0fc662005-10-07 07:46:04 +0100328 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
Olof Johansson7daa4112006-04-12 21:05:59 -0500331 dma_handle, ~0ul, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334static void vio_free_coherent(struct device *dev, size_t size,
335 void *vaddr, dma_addr_t dma_handle)
336{
337 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
338 dma_handle);
339}
340
341static int vio_dma_supported(struct device *dev, u64 mask)
342{
343 return 1;
344}
345
346struct dma_mapping_ops vio_dma_ops = {
347 .alloc_coherent = vio_alloc_coherent,
348 .free_coherent = vio_free_coherent,
349 .map_single = vio_map_single,
350 .unmap_single = vio_unmap_single,
351 .map_sg = vio_map_sg,
352 .unmap_sg = vio_unmap_sg,
353 .dma_supported = vio_dma_supported,
354};
355
356static int vio_bus_match(struct device *dev, struct device_driver *drv)
357{
358 const struct vio_dev *vio_dev = to_vio_dev(dev);
359 struct vio_driver *vio_drv = to_vio_driver(drv);
360 const struct vio_device_id *ids = vio_drv->id_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Stephen Rothwell5c0b4b82005-08-17 16:37:35 +1000362 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Olaf Hering143dcec2005-11-08 21:34:36 -0800365static int vio_hotplug(struct device *dev, char **envp, int num_envp,
366 char *buffer, int buffer_size)
367{
368 const struct vio_dev *vio_dev = to_vio_dev(dev);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000369 struct device_node *dn = dev->platform_data;
Olaf Hering143dcec2005-11-08 21:34:36 -0800370 char *cp;
371 int length;
372
373 if (!num_envp)
374 return -ENOMEM;
375
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000376 if (!dn)
Olaf Hering143dcec2005-11-08 21:34:36 -0800377 return -ENODEV;
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000378 cp = (char *)get_property(dn, "compatible", &length);
Olaf Hering143dcec2005-11-08 21:34:36 -0800379 if (!cp)
380 return -ENODEV;
381
382 envp[0] = buffer;
383 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
384 vio_dev->type, cp);
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000385 if ((buffer_size - length) <= 0)
Olaf Hering143dcec2005-11-08 21:34:36 -0800386 return -ENOMEM;
387 envp[1] = NULL;
388 return 0;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391struct bus_type vio_bus_type = {
392 .name = "vio",
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000393 .dev_attrs = vio_dev_attrs,
Kay Sievers312c0042005-11-16 09:00:00 +0100394 .uevent = vio_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .match = vio_bus_match,
Russell King2f53a802006-01-05 14:36:47 +0000396 .probe = vio_bus_probe,
397 .remove = vio_bus_remove,
398 .shutdown = vio_bus_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
Stephen Rothwelle10fa772006-04-27 17:18:21 +1000400
401/**
402 * vio_get_attribute: - get attribute for virtual device
403 * @vdev: The vio device to get property.
404 * @which: The property/attribute to be extracted.
405 * @length: Pointer to length of returned data size (unused if NULL).
406 *
407 * Calls prom.c's get_property() to return the value of the
408 * attribute specified by @which
409*/
410const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
411{
412 return get_property(vdev->dev.platform_data, which, length);
413}
414EXPORT_SYMBOL(vio_get_attribute);