[POWERPC] briq_panel Kconfig fix
[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
40extern struct subsystem devices_subsys; /* needed for vio_find_name() */
41
42static struct vio_dev vio_bus_device = { /* fake "parent" device */
ac5b33c9
SR
43 .name = vio_bus_device.dev.bus_id,
44 .type = "",
ac5b33c9
SR
45 .dev.bus_id = "vio",
46 .dev.bus = &vio_bus_type,
1da177e4 47};
ac5b33c9 48
c7f0e8cb
SR
49#ifdef CONFIG_PPC_ISERIES
50struct device *iSeries_vio_dev = &vio_bus_device.dev;
51EXPORT_SYMBOL(iSeries_vio_dev);
52
53static struct iommu_table veth_iommu_table;
54static struct iommu_table vio_iommu_table;
55
56static void __init iommu_vio_init(void)
57{
58 iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);
59 veth_iommu_table.it_size /= 2;
60 vio_iommu_table = veth_iommu_table;
61 vio_iommu_table.it_offset += veth_iommu_table.it_size;
62
ca1588e7 63 if (!iommu_init_table(&veth_iommu_table, -1))
c7f0e8cb 64 printk("Virtual Bus VETH TCE table failed.\n");
ca1588e7 65 if (!iommu_init_table(&vio_iommu_table, -1))
c7f0e8cb
SR
66 printk("Virtual Bus VIO TCE table failed.\n");
67}
68#endif
69
70static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
71{
72#ifdef CONFIG_PPC_ISERIES
73 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
1dc461f0 74 if (strcmp(dev->type, "network") == 0)
c7f0e8cb
SR
75 return &veth_iommu_table;
76 return &vio_iommu_table;
77 } else
78#endif
79 {
4c76e0bc
JK
80 unsigned char *dma_window;
81 struct iommu_table *tbl;
82 unsigned long offset, size;
c7f0e8cb 83
4c76e0bc
JK
84 dma_window = get_property(dev->dev.platform_data,
85 "ibm,my-dma-window", NULL);
c7f0e8cb
SR
86 if (!dma_window)
87 return NULL;
88
4c76e0bc 89 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
c7f0e8cb 90
4c76e0bc
JK
91 of_parse_dma_window(dev->dev.platform_data, dma_window,
92 &tbl->it_index, &offset, &size);
c7f0e8cb
SR
93
94 /* TCE table size - measured in tce entries */
4c76e0bc 95 tbl->it_size = size >> PAGE_SHIFT;
c7f0e8cb 96 /* offset for VIO should always be 0 */
4c76e0bc
JK
97 tbl->it_offset = offset >> PAGE_SHIFT;
98 tbl->it_busno = 0;
99 tbl->it_type = TCE_VB;
c7f0e8cb 100
ca1588e7 101 return iommu_init_table(tbl, -1);
c7f0e8cb
SR
102 }
103}
1da177e4 104
e10fa773
SR
105/**
106 * vio_match_device: - Tell if a VIO device has a matching
107 * VIO device id structure.
108 * @ids: array of VIO device id structures to search in
109 * @dev: the VIO device structure to match against
110 *
111 * Used by a driver to check whether a VIO device present in the
112 * system is in its list of supported devices. Returns the matching
113 * vio_device_id structure or NULL if there is no match.
114 */
115static const struct vio_device_id *vio_match_device(
116 const struct vio_device_id *ids, const struct vio_dev *dev)
117{
118 while (ids->type[0] != '\0') {
dd721ffd
SR
119 if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
120 device_is_compatible(dev->dev.platform_data, ids->compat))
e10fa773
SR
121 return ids;
122 ids++;
123 }
124 return NULL;
125}
126
5c0b4b87
SR
127/*
128 * Convert from struct device to struct vio_dev and pass to driver.
1da177e4 129 * dev->driver has already been set by generic code because vio_bus_match
5c0b4b87
SR
130 * succeeded.
131 */
1da177e4
LT
132static int vio_bus_probe(struct device *dev)
133{
134 struct vio_dev *viodev = to_vio_dev(dev);
135 struct vio_driver *viodrv = to_vio_driver(dev->driver);
136 const struct vio_device_id *id;
137 int error = -ENODEV;
138
1da177e4
LT
139 if (!viodrv->probe)
140 return error;
141
142 id = vio_match_device(viodrv->id_table, viodev);
5c0b4b87 143 if (id)
1da177e4 144 error = viodrv->probe(viodev, id);
1da177e4
LT
145
146 return error;
147}
148
149/* convert from struct device to struct vio_dev and pass to driver. */
150static int vio_bus_remove(struct device *dev)
151{
152 struct vio_dev *viodev = to_vio_dev(dev);
153 struct vio_driver *viodrv = to_vio_driver(dev->driver);
154
5c0b4b87 155 if (viodrv->remove)
1da177e4 156 return viodrv->remove(viodev);
1da177e4
LT
157
158 /* driver can't remove */
159 return 1;
160}
161
34060104
SR
162/* convert from struct device to struct vio_dev and pass to driver. */
163static void vio_bus_shutdown(struct device *dev)
164{
165 struct vio_dev *viodev = to_vio_dev(dev);
166 struct vio_driver *viodrv = to_vio_driver(dev->driver);
167
2f53a80f 168 if (dev->driver && viodrv->shutdown)
34060104
SR
169 viodrv->shutdown(viodev);
170}
171
1da177e4
LT
172/**
173 * vio_register_driver: - Register a new vio driver
174 * @drv: The vio_driver structure to be registered.
175 */
176int vio_register_driver(struct vio_driver *viodrv)
177{
178 printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
6fdf5392 179 viodrv->driver.name);
1da177e4
LT
180
181 /* fill in 'struct driver' fields */
1da177e4 182 viodrv->driver.bus = &vio_bus_type;
1da177e4
LT
183
184 return driver_register(&viodrv->driver);
185}
186EXPORT_SYMBOL(vio_register_driver);
187
188/**
189 * vio_unregister_driver - Remove registration of vio driver.
190 * @driver: The vio_driver struct to be removed form registration
191 */
192void vio_unregister_driver(struct vio_driver *viodrv)
193{
194 driver_unregister(&viodrv->driver);
195}
196EXPORT_SYMBOL(vio_unregister_driver);
197
c7f0e8cb
SR
198/* vio_dev refcount hit 0 */
199static void __devinit vio_dev_release(struct device *dev)
200{
201 if (dev->platform_data) {
202 /* XXX free TCE table */
203 of_node_put(dev->platform_data);
204 }
205 kfree(to_vio_dev(dev));
206}
207
1da177e4 208/**
e10fa773
SR
209 * vio_register_device_node: - Register a new vio device.
210 * @of_node: The OF node for this device.
1da177e4 211 *
e10fa773
SR
212 * Creates and initializes a vio_dev structure from the data in
213 * of_node (dev.platform_data) and adds it to the list of virtual devices.
214 * Returns a pointer to the created vio_dev or NULL if node has
215 * NULL device_type or compatible fields.
1da177e4 216 */
e10fa773 217struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
1da177e4 218{
e10fa773
SR
219 struct vio_dev *viodev;
220 unsigned int *unit_address;
e10fa773
SR
221
222 /* we need the 'device_type' property, in order to match with drivers */
223 if (of_node->type == NULL) {
224 printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
225 __FUNCTION__,
226 of_node->name ? of_node->name : "<unknown>");
227 return NULL;
1da177e4 228 }
e10fa773
SR
229
230 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
231 if (unit_address == NULL) {
232 printk(KERN_WARNING "%s: node %s missing 'reg'\n",
233 __FUNCTION__,
234 of_node->name ? of_node->name : "<unknown>");
235 return NULL;
236 }
237
238 /* allocate a vio_dev for this node */
239 viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
240 if (viodev == NULL)
241 return NULL;
242
243 viodev->dev.platform_data = of_node_get(of_node);
244
0ebfff14 245 viodev->irq = irq_of_parse_and_map(of_node, 0);
e10fa773
SR
246
247 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
248 viodev->name = of_node->name;
249 viodev->type = of_node->type;
250 viodev->unit_address = *unit_address;
251 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
252 unit_address = (unsigned int *)get_property(of_node,
253 "linux,unit_address", NULL);
254 if (unit_address != NULL)
255 viodev->unit_address = *unit_address;
256 }
c7f0e8cb
SR
257 viodev->iommu_table = vio_build_iommu_table(viodev);
258
259 /* init generic 'struct device' fields: */
260 viodev->dev.parent = &vio_bus_device.dev;
261 viodev->dev.bus = &vio_bus_type;
262 viodev->dev.release = vio_dev_release;
e10fa773
SR
263
264 /* register with generic device framework */
c7f0e8cb
SR
265 if (device_register(&viodev->dev)) {
266 printk(KERN_ERR "%s: failed to register device %s\n",
267 __FUNCTION__, viodev->dev.bus_id);
e10fa773
SR
268 /* XXX free TCE table */
269 kfree(viodev);
270 return NULL;
271 }
272
273 return viodev;
1da177e4 274}
e10fa773 275EXPORT_SYMBOL(vio_register_device_node);
1da177e4 276
1da177e4
LT
277/**
278 * vio_bus_init: - Initialize the virtual IO bus
279 */
c7f0e8cb 280static int __init vio_bus_init(void)
1da177e4
LT
281{
282 int err;
e10fa773 283 struct device_node *node_vroot;
1da177e4 284
c7f0e8cb
SR
285#ifdef CONFIG_PPC_ISERIES
286 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
287 iommu_vio_init();
288 vio_bus_device.iommu_table = &vio_iommu_table;
289 iSeries_vio_dev = &vio_bus_device.dev;
290 }
291#endif
6312236f 292
1da177e4
LT
293 err = bus_register(&vio_bus_type);
294 if (err) {
295 printk(KERN_ERR "failed to register VIO bus\n");
296 return err;
297 }
298
5c0b4b87
SR
299 /*
300 * The fake parent of all vio devices, just to give us
3e494c80
SR
301 * a nice directory
302 */
ac5b33c9 303 err = device_register(&vio_bus_device.dev);
1da177e4 304 if (err) {
3e494c80
SR
305 printk(KERN_WARNING "%s: device_register returned %i\n",
306 __FUNCTION__, err);
1da177e4
LT
307 return err;
308 }
309
e10fa773
SR
310 node_vroot = find_devices("vdevice");
311 if (node_vroot) {
312 struct device_node *of_node;
313
314 /*
315 * Create struct vio_devices for each virtual device in
316 * the device tree. Drivers will associate with them later.
317 */
318 for (of_node = node_vroot->child; of_node != NULL;
319 of_node = of_node->sibling) {
320 printk(KERN_DEBUG "%s: processing %p\n",
321 __FUNCTION__, of_node);
322 vio_register_device_node(of_node);
323 }
324 }
325
3e494c80
SR
326 return 0;
327}
c7f0e8cb 328__initcall(vio_bus_init);
1da177e4 329
e10fa773 330static ssize_t name_show(struct device *dev,
5c0b4b87 331 struct device_attribute *attr, char *buf)
1da177e4
LT
332{
333 return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
334}
e10fa773
SR
335
336static ssize_t devspec_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 struct device_node *of_node = dev->platform_data;
340
341 return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
342}
343
344static struct device_attribute vio_dev_attrs[] = {
345 __ATTR_RO(name),
346 __ATTR_RO(devspec),
347 __ATTR_NULL
348};
1da177e4 349
1da177e4
LT
350void __devinit vio_unregister_device(struct vio_dev *viodev)
351{
1da177e4
LT
352 device_unregister(&viodev->dev);
353}
354EXPORT_SYMBOL(vio_unregister_device);
355
1da177e4
LT
356static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
357 size_t size, enum dma_data_direction direction)
358{
359 return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
7daa411b 360 ~0ul, direction);
1da177e4
LT
361}
362
363static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
364 size_t size, enum dma_data_direction direction)
365{
366 iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
367 direction);
368}
369
370static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
371 int nelems, enum dma_data_direction direction)
372{
373 return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
7daa411b 374 nelems, ~0ul, direction);
1da177e4
LT
375}
376
377static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
378 int nelems, enum dma_data_direction direction)
379{
380 iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
381}
382
383static void *vio_alloc_coherent(struct device *dev, size_t size,
dd0fc66f 384 dma_addr_t *dma_handle, gfp_t flag)
1da177e4
LT
385{
386 return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
8eb6c6e3 387 dma_handle, ~0ul, flag, -1);
1da177e4
LT
388}
389
390static void vio_free_coherent(struct device *dev, size_t size,
391 void *vaddr, dma_addr_t dma_handle)
392{
393 iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
394 dma_handle);
395}
396
397static int vio_dma_supported(struct device *dev, u64 mask)
398{
399 return 1;
400}
401
402struct dma_mapping_ops vio_dma_ops = {
403 .alloc_coherent = vio_alloc_coherent,
404 .free_coherent = vio_free_coherent,
405 .map_single = vio_map_single,
406 .unmap_single = vio_unmap_single,
407 .map_sg = vio_map_sg,
408 .unmap_sg = vio_unmap_sg,
409 .dma_supported = vio_dma_supported,
410};
411
412static int vio_bus_match(struct device *dev, struct device_driver *drv)
413{
414 const struct vio_dev *vio_dev = to_vio_dev(dev);
415 struct vio_driver *vio_drv = to_vio_driver(drv);
416 const struct vio_device_id *ids = vio_drv->id_table;
1da177e4 417
5c0b4b87 418 return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
1da177e4
LT
419}
420
143dcec2
OH
421static int vio_hotplug(struct device *dev, char **envp, int num_envp,
422 char *buffer, int buffer_size)
423{
424 const struct vio_dev *vio_dev = to_vio_dev(dev);
e10fa773 425 struct device_node *dn = dev->platform_data;
143dcec2
OH
426 char *cp;
427 int length;
428
429 if (!num_envp)
430 return -ENOMEM;
431
e10fa773 432 if (!dn)
143dcec2 433 return -ENODEV;
e10fa773 434 cp = (char *)get_property(dn, "compatible", &length);
143dcec2
OH
435 if (!cp)
436 return -ENODEV;
437
438 envp[0] = buffer;
439 length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
440 vio_dev->type, cp);
e10fa773 441 if ((buffer_size - length) <= 0)
143dcec2
OH
442 return -ENOMEM;
443 envp[1] = NULL;
444 return 0;
445}
446
1da177e4
LT
447struct bus_type vio_bus_type = {
448 .name = "vio",
e10fa773 449 .dev_attrs = vio_dev_attrs,
312c004d 450 .uevent = vio_hotplug,
1da177e4 451 .match = vio_bus_match,
2f53a80f
RK
452 .probe = vio_bus_probe,
453 .remove = vio_bus_remove,
454 .shutdown = vio_bus_shutdown,
1da177e4 455};
e10fa773
SR
456
457/**
458 * vio_get_attribute: - get attribute for virtual device
459 * @vdev: The vio device to get property.
460 * @which: The property/attribute to be extracted.
461 * @length: Pointer to length of returned data size (unused if NULL).
462 *
463 * Calls prom.c's get_property() to return the value of the
464 * attribute specified by @which
465*/
466const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
467{
468 return get_property(vdev->dev.platform_data, which, length);
469}
470EXPORT_SYMBOL(vio_get_attribute);
c7f0e8cb
SR
471
472#ifdef CONFIG_PPC_PSERIES
473/* vio_find_name() - internal because only vio.c knows how we formatted the
474 * kobject name
475 * XXX once vio_bus_type.devices is actually used as a kset in
476 * drivers/base/bus.c, this function should be removed in favor of
477 * "device_find(kobj_name, &vio_bus_type)"
478 */
479static struct vio_dev *vio_find_name(const char *kobj_name)
480{
481 struct kobject *found;
482
483 found = kset_find_obj(&devices_subsys.kset, kobj_name);
484 if (!found)
485 return NULL;
486
487 return to_vio_dev(container_of(found, struct device, kobj));
488}
489
490/**
491 * vio_find_node - find an already-registered vio_dev
492 * @vnode: device_node of the virtual device we're looking for
493 */
494struct vio_dev *vio_find_node(struct device_node *vnode)
495{
496 uint32_t *unit_address;
497 char kobj_name[BUS_ID_SIZE];
498
499 /* construct the kobject name from the device node */
500 unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
501 if (!unit_address)
502 return NULL;
503 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
504
505 return vio_find_name(kobj_name);
506}
507EXPORT_SYMBOL(vio_find_node);
508
509int vio_enable_interrupts(struct vio_dev *dev)
510{
511 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
512 if (rc != H_SUCCESS)
513 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
514 return rc;
515}
516EXPORT_SYMBOL(vio_enable_interrupts);
517
518int vio_disable_interrupts(struct vio_dev *dev)
519{
520 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
521 if (rc != H_SUCCESS)
522 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
523 return rc;
524}
525EXPORT_SYMBOL(vio_disable_interrupts);
526#endif /* CONFIG_PPC_PSERIES */