[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / arch / powerpc / platforms / pseries / vio.c
blob866379b80c09029ab78d21e9744699afe2484ee6
1 /*
2 * IBM PowerPC pSeries Virtual I/O Infrastructure Support.
4 * Copyright (c) 2003-2005 IBM Corp.
5 * Dave Engebretsen engebret@us.ibm.com
6 * Santiago Leon santil@us.ibm.com
7 * Hollis Blanchard <hollisb@us.ibm.com>
8 * Stephen Rothwell
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.
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mm.h>
19 #include <linux/kobject.h>
20 #include <asm/iommu.h>
21 #include <asm/dma.h>
22 #include <asm/prom.h>
23 #include <asm/vio.h>
24 #include <asm/hvcall.h>
25 #include <asm/tce.h>
27 extern struct subsystem devices_subsys; /* needed for vio_find_name() */
29 static void probe_bus_pseries(void)
31 struct device_node *node_vroot, *of_node;
33 node_vroot = find_devices("vdevice");
34 if ((node_vroot == NULL) || (node_vroot->child == NULL))
35 /* this machine doesn't do virtual IO, and that's ok */
36 return;
39 * Create struct vio_devices for each virtual device in the device tree.
40 * Drivers will associate with them later.
42 for (of_node = node_vroot->child; of_node != NULL;
43 of_node = of_node->sibling) {
44 printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
45 vio_register_device_node(of_node);
49 /**
50 * vio_match_device_pseries: - Tell if a pSeries VIO device matches a
51 * vio_device_id
53 static int vio_match_device_pseries(const struct vio_device_id *id,
54 const struct vio_dev *dev)
56 return (strncmp(dev->type, id->type, strlen(id->type)) == 0) &&
57 device_is_compatible(dev->dev.platform_data, id->compat);
60 static void vio_release_device_pseries(struct device *dev)
62 /* XXX free TCE table */
63 of_node_put(dev->platform_data);
66 static ssize_t viodev_show_devspec(struct device *dev,
67 struct device_attribute *attr, char *buf)
69 struct device_node *of_node = dev->platform_data;
71 return sprintf(buf, "%s\n", of_node->full_name);
73 DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL);
75 static void vio_unregister_device_pseries(struct vio_dev *viodev)
77 device_remove_file(&viodev->dev, &dev_attr_devspec);
80 static struct vio_bus_ops vio_bus_ops_pseries = {
81 .match = vio_match_device_pseries,
82 .unregister_device = vio_unregister_device_pseries,
83 .release_device = vio_release_device_pseries,
86 /**
87 * vio_bus_init_pseries: - Initialize the pSeries virtual IO bus
89 static int __init vio_bus_init_pseries(void)
91 int err;
93 err = vio_bus_init(&vio_bus_ops_pseries);
94 if (err == 0)
95 probe_bus_pseries();
96 return err;
99 __initcall(vio_bus_init_pseries);
102 * vio_build_iommu_table: - gets the dma information from OF and
103 * builds the TCE tree.
104 * @dev: the virtual device.
106 * Returns a pointer to the built tce tree, or NULL if it can't
107 * find property.
109 static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
111 unsigned int *dma_window;
112 struct iommu_table *newTceTable;
113 unsigned long offset;
114 int dma_window_property_size;
116 dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size);
117 if(!dma_window) {
118 return NULL;
121 newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
123 /* There should be some code to extract the phys-encoded offset
124 using prom_n_addr_cells(). However, according to a comment
125 on earlier versions, it's always zero, so we don't bother */
126 offset = dma_window[1] >> PAGE_SHIFT;
128 /* TCE table size - measured in tce entries */
129 newTceTable->it_size = dma_window[4] >> PAGE_SHIFT;
130 /* offset for VIO should always be 0 */
131 newTceTable->it_offset = offset;
132 newTceTable->it_busno = 0;
133 newTceTable->it_index = (unsigned long)dma_window[0];
134 newTceTable->it_type = TCE_VB;
136 return iommu_init_table(newTceTable);
140 * vio_register_device_node: - Register a new vio device.
141 * @of_node: The OF node for this device.
143 * Creates and initializes a vio_dev structure from the data in
144 * of_node (dev.platform_data) and adds it to the list of virtual devices.
145 * Returns a pointer to the created vio_dev or NULL if node has
146 * NULL device_type or compatible fields.
148 struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
150 struct vio_dev *viodev;
151 unsigned int *unit_address;
152 unsigned int *irq_p;
154 /* we need the 'device_type' property, in order to match with drivers */
155 if ((NULL == of_node->type)) {
156 printk(KERN_WARNING
157 "%s: node %s missing 'device_type'\n", __FUNCTION__,
158 of_node->name ? of_node->name : "<unknown>");
159 return NULL;
162 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
163 if (!unit_address) {
164 printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
165 of_node->name ? of_node->name : "<unknown>");
166 return NULL;
169 /* allocate a vio_dev for this node */
170 viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
171 if (!viodev) {
172 return NULL;
174 memset(viodev, 0, sizeof(struct vio_dev));
176 viodev->dev.platform_data = of_node_get(of_node);
178 viodev->irq = NO_IRQ;
179 irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
180 if (irq_p) {
181 int virq = virt_irq_create_mapping(*irq_p);
182 if (virq == NO_IRQ) {
183 printk(KERN_ERR "Unable to allocate interrupt "
184 "number for %s\n", of_node->full_name);
185 } else
186 viodev->irq = irq_offset_up(virq);
189 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
190 viodev->name = of_node->name;
191 viodev->type = of_node->type;
192 viodev->unit_address = *unit_address;
193 viodev->iommu_table = vio_build_iommu_table(viodev);
195 /* register with generic device framework */
196 if (vio_register_device(viodev) == NULL) {
197 /* XXX free TCE table */
198 kfree(viodev);
199 return NULL;
201 device_create_file(&viodev->dev, &dev_attr_devspec);
203 return viodev;
205 EXPORT_SYMBOL(vio_register_device_node);
208 * vio_get_attribute: - get attribute for virtual device
209 * @vdev: The vio device to get property.
210 * @which: The property/attribute to be extracted.
211 * @length: Pointer to length of returned data size (unused if NULL).
213 * Calls prom.c's get_property() to return the value of the
214 * attribute specified by the preprocessor constant @which
216 const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
218 return get_property(vdev->dev.platform_data, (char*)which, length);
220 EXPORT_SYMBOL(vio_get_attribute);
222 /* vio_find_name() - internal because only vio.c knows how we formatted the
223 * kobject name
224 * XXX once vio_bus_type.devices is actually used as a kset in
225 * drivers/base/bus.c, this function should be removed in favor of
226 * "device_find(kobj_name, &vio_bus_type)"
228 static struct vio_dev *vio_find_name(const char *kobj_name)
230 struct kobject *found;
232 found = kset_find_obj(&devices_subsys.kset, kobj_name);
233 if (!found)
234 return NULL;
236 return to_vio_dev(container_of(found, struct device, kobj));
240 * vio_find_node - find an already-registered vio_dev
241 * @vnode: device_node of the virtual device we're looking for
243 struct vio_dev *vio_find_node(struct device_node *vnode)
245 uint32_t *unit_address;
246 char kobj_name[BUS_ID_SIZE];
248 /* construct the kobject name from the device node */
249 unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
250 if (!unit_address)
251 return NULL;
252 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
254 return vio_find_name(kobj_name);
256 EXPORT_SYMBOL(vio_find_node);
258 int vio_enable_interrupts(struct vio_dev *dev)
260 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
261 if (rc != H_Success)
262 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
263 return rc;
265 EXPORT_SYMBOL(vio_enable_interrupts);
267 int vio_disable_interrupts(struct vio_dev *dev)
269 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
270 if (rc != H_Success)
271 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
272 return rc;
274 EXPORT_SYMBOL(vio_disable_interrupts);