Driver core: change add_uevent_var to use a struct
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / 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
c7f0e8cb
SR
16#include <linux/types.h>
17#include <linux/device.h>
1da177e4
LT
18#include <linux/init.h>
19#include <linux/console.h>
1da177e4 20#include <linux/module.h>
1da177e4
LT
21#include <linux/mm.h>
22#include <linux/dma-mapping.h>
c7f0e8cb
SR
23#include <linux/kobject.h>
24
1da177e4
LT
25#include <asm/iommu.h>
26#include <asm/dma.h>
1da177e4 27#include <asm/vio.h>
143dcec2 28#include <asm/prom.h>
e10fa773 29#include <asm/firmware.h>
c7f0e8cb
SR
30#include <asm/tce.h>
31#include <asm/abs_addr.h>
32#include <asm/page.h>
33#include <asm/hvcall.h>
34#include <asm/iseries/vio.h>
35#include <asm/iseries/hv_types.h>
36#include <asm/iseries/hv_lp_config.h>
37#include <asm/iseries/hv_call_xm.h>
38#include <asm/iseries/iommu.h>
39
823bccfc 40extern struct kset devices_subsys; /* needed for vio_find_name() */
c7f0e8cb 41
6fccab26
SR
42static struct bus_type vio_bus_type;
43
c7f0e8cb 44static struct vio_dev vio_bus_device = { /* fake "parent" device */
ac5b33c9
SR
45 .name = vio_bus_device.dev.bus_id,
46 .type = "",
ac5b33c9
SR
47 .dev.bus_id = "vio",
48 .dev.bus = &vio_bus_type,
1da177e4 49};
ac5b33c9 50
c7f0e8cb
SR
51static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
52{
dd9b67ab
SR
53 const unsigned char *dma_window;
54 struct iommu_table *tbl;
55 unsigned long offset, size;
56
57 if (firmware_has_feature(FW_FEATURE_ISERIES))
58 return vio_build_iommu_table_iseries(dev);
c7f0e8cb 59
dd9b67ab
SR
60 dma_window = of_get_property(dev->dev.archdata.of_node,
61 "ibm,my-dma-window", NULL);
62 if (!dma_window)
63 return NULL;
c7f0e8cb 64
dd9b67ab 65 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
c7f0e8cb 66
dd9b67ab
SR
67 of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
68 &tbl->it_index, &offset, &size);
c7f0e8cb 69
dd9b67ab
SR
70 /* TCE table size - measured in tce entries */
71 tbl->it_size = size >> IOMMU_PAGE_SHIFT;
72 /* offset for VIO should always be 0 */
73 tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
74 tbl->it_busno = 0;
75 tbl->it_type = TCE_VB;
c7f0e8cb 76
dd9b67ab 77 return iommu_init_table(tbl, -1);
c7f0e8cb 78}
1da177e4 79
e10fa773
SR
80/**
81 * vio_match_device: - Tell if a VIO device has a matching
82 * VIO device id structure.
83 * @ids: array of VIO device id structures to search in
84 * @dev: the VIO device structure to match against
85 *
86 * Used by a driver to check whether a VIO device present in the
87 * system is in its list of supported devices. Returns the matching
88 * vio_device_id structure or NULL if there is no match.
89 */
90static const struct vio_device_id *vio_match_device(
91 const struct vio_device_id *ids, const struct vio_dev *dev)
92{
93 while (ids->type[0] != '\0') {
dd721ffd 94 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
55b61fec 95 of_device_is_compatible(dev->dev.archdata.of_node,
12d04eef 96 ids->compat))
e10fa773
SR
97 return ids;
98 ids++;
99 }
100 return NULL;
101}
102
5c0b4b87
SR
103/*
104 * Convert from struct device to struct vio_dev and pass to driver.
1da177e4 105 * dev->driver has already been set by generic code because vio_bus_match
5c0b4b87
SR
106 * succeeded.
107 */
1da177e4
LT
108static int vio_bus_probe(struct device *dev)
109{
110 struct vio_dev *viodev = to_vio_dev(dev);
111 struct vio_driver *viodrv = to_vio_driver(dev->driver);
112 const struct vio_device_id *id;
113 int error = -ENODEV;
114
1da177e4
LT
115 if (!viodrv->probe)
116 return error;
117
118 id = vio_match_device(viodrv->id_table, viodev);
5c0b4b87 119 if (id)
1da177e4 120 error = viodrv->probe(viodev, id);
1da177e4
LT
121
122 return error;
123}
124
125/* convert from struct device to struct vio_dev and pass to driver. */
126static int vio_bus_remove(struct device *dev)
127{
128 struct vio_dev *viodev = to_vio_dev(dev);
129 struct vio_driver *viodrv = to_vio_driver(dev->driver);
130
5c0b4b87 131 if (viodrv->remove)
1da177e4 132 return viodrv->remove(viodev);
1da177e4
LT
133
134 /* driver can't remove */
135 return 1;
136}
137
138/**
139 * vio_register_driver: - Register a new vio driver
140 * @drv: The vio_driver structure to be registered.
141 */
142int vio_register_driver(struct vio_driver *viodrv)
143{
144 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
6fdf5392 145 viodrv->driver.name);
1da177e4
LT
146
147 /* fill in 'struct driver' fields */
1da177e4 148 viodrv->driver.bus = &vio_bus_type;
1da177e4
LT
149
150 return driver_register(&viodrv->driver);
151}
152EXPORT_SYMBOL(vio_register_driver);
153
154/**
155 * vio_unregister_driver - Remove registration of vio driver.
156 * @driver: The vio_driver struct to be removed form registration
157 */
158void vio_unregister_driver(struct vio_driver *viodrv)
159{
160 driver_unregister(&viodrv->driver);
161}
162EXPORT_SYMBOL(vio_unregister_driver);
163
c7f0e8cb
SR
164/* vio_dev refcount hit 0 */
165static void __devinit vio_dev_release(struct device *dev)
166{
6690faeb
MK
167 /* XXX should free TCE table */
168 of_node_put(dev->archdata.of_node);
c7f0e8cb
SR
169 kfree(to_vio_dev(dev));
170}
171
1da177e4 172/**
e10fa773
SR
173 * vio_register_device_node: - Register a new vio device.
174 * @of_node: The OF node for this device.
1da177e4 175 *
e10fa773 176 * Creates and initializes a vio_dev structure from the data in
12d04eef 177 * of_node and adds it to the list of virtual devices.
e10fa773
SR
178 * Returns a pointer to the created vio_dev or NULL if node has
179 * NULL device_type or compatible fields.
1da177e4 180 */
e10fa773 181struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
1da177e4 182{
e10fa773 183 struct vio_dev *viodev;
a7f67bdf 184 const unsigned int *unit_address;
e10fa773
SR
185
186 /* we need the 'device_type' property, in order to match with drivers */
187 if (of_node->type == NULL) {
188 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
189 __FUNCTION__,
190 of_node->name ? of_node->name : "<unknown>");
191 return NULL;
1da177e4 192 }
e10fa773 193
e2eb6392 194 unit_address = of_get_property(of_node, "reg", NULL);
e10fa773
SR
195 if (unit_address == NULL) {
196 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
197 __FUNCTION__,
198 of_node->name ? of_node->name : "<unknown>");
199 return NULL;
200 }
201
202 /* allocate a vio_dev for this node */
203 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
204 if (viodev == NULL)
205 return NULL;
206
0ebfff14 207 viodev->irq = irq_of_parse_and_map(of_node, 0);
e10fa773
SR
208
209 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
210 viodev->name = of_node->name;
211 viodev->type = of_node->type;
212 viodev->unit_address = *unit_address;
213 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
e2eb6392 214 unit_address = of_get_property(of_node,
e10fa773
SR
215 "linux,unit_address", NULL);
216 if (unit_address != NULL)
217 viodev->unit_address = *unit_address;
218 }
12d04eef
BH
219 viodev->dev.archdata.of_node = of_node_get(of_node);
220 viodev->dev.archdata.dma_ops = &dma_iommu_ops;
221 viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
222 viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
c7f0e8cb
SR
223
224 /* init generic 'struct device' fields: */
225 viodev->dev.parent = &vio_bus_device.dev;
226 viodev->dev.bus = &vio_bus_type;
227 viodev->dev.release = vio_dev_release;
e10fa773
SR
228
229 /* register with generic device framework */
c7f0e8cb
SR
230 if (device_register(&viodev->dev)) {
231 printk(KERN_ERR "%s: failed to register device %s\n",
232 __FUNCTION__, viodev->dev.bus_id);
e10fa773
SR
233 /* XXX free TCE table */
234 kfree(viodev);
235 return NULL;
236 }
237
238 return viodev;
1da177e4 239}
e10fa773 240EXPORT_SYMBOL(vio_register_device_node);
1da177e4 241
1da177e4
LT
242/**
243 * vio_bus_init: - Initialize the virtual IO bus
244 */
c7f0e8cb 245static int __init vio_bus_init(void)
1da177e4
LT
246{
247 int err;
e10fa773 248 struct device_node *node_vroot;
1da177e4
LT
249
250 err = bus_register(&vio_bus_type);
251 if (err) {
252 printk(KERN_ERR "failed to register VIO bus\n");
253 return err;
254 }
255
5c0b4b87
SR
256 /*
257 * The fake parent of all vio devices, just to give us
3e494c80
SR
258 * a nice directory
259 */
ac5b33c9 260 err = device_register(&vio_bus_device.dev);
1da177e4 261 if (err) {
3e494c80
SR
262 printk(KERN_WARNING "%s: device_register returned %i\n",
263 __FUNCTION__, err);
1da177e4
LT
264 return err;
265 }
266
30686ba6 267 node_vroot = of_find_node_by_name(NULL, "vdevice");
e10fa773
SR
268 if (node_vroot) {
269 struct device_node *of_node;
270
271 /*
272 * Create struct vio_devices for each virtual device in
273 * the device tree. Drivers will associate with them later.
274 */
275 for (of_node = node_vroot->child; of_node != NULL;
c5467262 276 of_node = of_node->sibling)
e10fa773 277 vio_register_device_node(of_node);
30686ba6 278 of_node_put(node_vroot);
e10fa773
SR
279 }
280
3e494c80
SR
281 return 0;
282}
c7f0e8cb 283__initcall(vio_bus_init);
1da177e4 284
e10fa773 285static ssize_t name_show(struct device *dev,
5c0b4b87 286 struct device_attribute *attr, char *buf)
1da177e4
LT
287{
288 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
289}
e10fa773
SR
290
291static ssize_t devspec_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
12d04eef 294 struct device_node *of_node = dev->archdata.of_node;
e10fa773
SR
295
296 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
297}
298
299static struct device_attribute vio_dev_attrs[] = {
300 __ATTR_RO(name),
301 __ATTR_RO(devspec),
302 __ATTR_NULL
303};
1da177e4 304
1da177e4
LT
305void __devinit vio_unregister_device(struct vio_dev *viodev)
306{
1da177e4
LT
307 device_unregister(&viodev->dev);
308}
309EXPORT_SYMBOL(vio_unregister_device);
310
1da177e4
LT
311static int vio_bus_match(struct device *dev, struct device_driver *drv)
312{
313 const struct vio_dev *vio_dev = to_vio_dev(dev);
314 struct vio_driver *vio_drv = to_vio_driver(drv);
315 const struct vio_device_id *ids = vio_drv->id_table;
1da177e4 316
5c0b4b87 317 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
1da177e4
LT
318}
319
7eff2e7a 320static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
143dcec2
OH
321{
322 const struct vio_dev *vio_dev = to_vio_dev(dev);
12d04eef 323 struct device_node *dn;
a7f67bdf 324 const char *cp;
143dcec2 325
12d04eef 326 dn = dev->archdata.of_node;
e10fa773 327 if (!dn)
143dcec2 328 return -ENODEV;
7eff2e7a 329 cp = of_get_property(dn, "compatible", NULL);
143dcec2
OH
330 if (!cp)
331 return -ENODEV;
332
7eff2e7a 333 add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
143dcec2
OH
334 return 0;
335}
336
6fccab26 337static struct bus_type vio_bus_type = {
1da177e4 338 .name = "vio",
e10fa773 339 .dev_attrs = vio_dev_attrs,
312c004d 340 .uevent = vio_hotplug,
1da177e4 341 .match = vio_bus_match,
2f53a80f
RK
342 .probe = vio_bus_probe,
343 .remove = vio_bus_remove,
1da177e4 344};
e10fa773
SR
345
346/**
347 * vio_get_attribute: - get attribute for virtual device
348 * @vdev: The vio device to get property.
349 * @which: The property/attribute to be extracted.
350 * @length: Pointer to length of returned data size (unused if NULL).
351 *
e2eb6392 352 * Calls prom.c's of_get_property() to return the value of the
e10fa773
SR
353 * attribute specified by @which
354*/
355const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
356{
e2eb6392 357 return of_get_property(vdev->dev.archdata.of_node, which, length);
e10fa773
SR
358}
359EXPORT_SYMBOL(vio_get_attribute);
c7f0e8cb
SR
360
361#ifdef CONFIG_PPC_PSERIES
362/* vio_find_name() - internal because only vio.c knows how we formatted the
363 * kobject name
364 * XXX once vio_bus_type.devices is actually used as a kset in
365 * drivers/base/bus.c, this function should be removed in favor of
366 * "device_find(kobj_name, &vio_bus_type)"
367 */
368static struct vio_dev *vio_find_name(const char *kobj_name)
369{
370 struct kobject *found;
371
823bccfc 372 found = kset_find_obj(&devices_subsys, kobj_name);
c7f0e8cb
SR
373 if (!found)
374 return NULL;
375
376 return to_vio_dev(container_of(found, struct device, kobj));
377}
378
379/**
380 * vio_find_node - find an already-registered vio_dev
381 * @vnode: device_node of the virtual device we're looking for
382 */
383struct vio_dev *vio_find_node(struct device_node *vnode)
384{
a7f67bdf 385 const uint32_t *unit_address;
c7f0e8cb
SR
386 char kobj_name[BUS_ID_SIZE];
387
388 /* construct the kobject name from the device node */
e2eb6392 389 unit_address = of_get_property(vnode, "reg", NULL);
c7f0e8cb
SR
390 if (!unit_address)
391 return NULL;
392 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
393
394 return vio_find_name(kobj_name);
395}
396EXPORT_SYMBOL(vio_find_node);
397
398int vio_enable_interrupts(struct vio_dev *dev)
399{
400 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
401 if (rc != H_SUCCESS)
402 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
403 return rc;
404}
405EXPORT_SYMBOL(vio_enable_interrupts);
406
407int vio_disable_interrupts(struct vio_dev *dev)
408{
409 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
410 if (rc != H_SUCCESS)
411 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
412 return rc;
413}
414EXPORT_SYMBOL(vio_disable_interrupts);
415#endif /* CONFIG_PPC_PSERIES */