blob: 81e94f8aa84684862edacb2fafff9cf9dca6878d [file] [log] [blame]
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +10001/*
2 * IBM PowerPC pSeries Virtual I/O Infrastructure Support.
3 *
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
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
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>
Stephen Rothwellb877b902005-08-17 16:40:12 +100022#include <asm/prom.h>
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +100023#include <asm/vio.h>
24#include <asm/hvcall.h>
25
26extern struct subsystem devices_subsys; /* needed for vio_find_name() */
27
28static void probe_bus_pseries(void)
29{
30 struct device_node *node_vroot, *of_node;
31
32 node_vroot = find_devices("vdevice");
33 if ((node_vroot == NULL) || (node_vroot->child == NULL))
34 /* this machine doesn't do virtual IO, and that's ok */
35 return;
36
37 /*
38 * Create struct vio_devices for each virtual device in the device tree.
39 * Drivers will associate with them later.
40 */
41 for (of_node = node_vroot->child; of_node != NULL;
42 of_node = of_node->sibling) {
43 printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
44 vio_register_device_node(of_node);
45 }
46}
47
48/**
49 * vio_match_device_pseries: - Tell if a pSeries VIO device matches a
50 * vio_device_id
51 */
52static int vio_match_device_pseries(const struct vio_device_id *id,
53 const struct vio_dev *dev)
54{
55 return (strncmp(dev->type, id->type, strlen(id->type)) == 0) &&
56 device_is_compatible(dev->dev.platform_data, id->compat);
57}
58
59static void vio_release_device_pseries(struct device *dev)
60{
61 /* XXX free TCE table */
62 of_node_put(dev->platform_data);
63}
64
65static ssize_t viodev_show_devspec(struct device *dev,
66 struct device_attribute *attr, char *buf)
67{
68 struct device_node *of_node = dev->platform_data;
69
70 return sprintf(buf, "%s\n", of_node->full_name);
71}
72DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL);
73
74static void vio_unregister_device_pseries(struct vio_dev *viodev)
75{
76 device_remove_file(&viodev->dev, &dev_attr_devspec);
77}
78
79/**
80 * vio_bus_init_pseries: - Initialize the pSeries virtual IO bus
81 */
82static int __init vio_bus_init_pseries(void)
83{
84 int err;
85
86 err = vio_bus_init(vio_match_device_pseries,
87 vio_unregister_device_pseries,
88 vio_release_device_pseries);
89 if (err == 0)
90 probe_bus_pseries();
91 return err;
92}
93
94__initcall(vio_bus_init_pseries);
95
96/**
97 * vio_build_iommu_table: - gets the dma information from OF and
98 * builds the TCE tree.
99 * @dev: the virtual device.
100 *
101 * Returns a pointer to the built tce tree, or NULL if it can't
102 * find property.
103*/
104static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
105{
106 unsigned int *dma_window;
107 struct iommu_table *newTceTable;
108 unsigned long offset;
109 int dma_window_property_size;
110
111 dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size);
112 if(!dma_window) {
113 return NULL;
114 }
115
116 newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
117
118 /* There should be some code to extract the phys-encoded offset
119 using prom_n_addr_cells(). However, according to a comment
120 on earlier versions, it's always zero, so we don't bother */
121 offset = dma_window[1] >> PAGE_SHIFT;
122
123 /* TCE table size - measured in tce entries */
124 newTceTable->it_size = dma_window[4] >> PAGE_SHIFT;
125 /* offset for VIO should always be 0 */
126 newTceTable->it_offset = offset;
127 newTceTable->it_busno = 0;
128 newTceTable->it_index = (unsigned long)dma_window[0];
129 newTceTable->it_type = TCE_VB;
130
131 return iommu_init_table(newTceTable);
132}
133
134/**
135 * vio_register_device_node: - Register a new vio device.
136 * @of_node: The OF node for this device.
137 *
138 * Creates and initializes a vio_dev structure from the data in
139 * of_node (dev.platform_data) and adds it to the list of virtual devices.
140 * Returns a pointer to the created vio_dev or NULL if node has
141 * NULL device_type or compatible fields.
142 */
143struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
144{
145 struct vio_dev *viodev;
146 unsigned int *unit_address;
147 unsigned int *irq_p;
148
149 /* we need the 'device_type' property, in order to match with drivers */
150 if ((NULL == of_node->type)) {
151 printk(KERN_WARNING
152 "%s: node %s missing 'device_type'\n", __FUNCTION__,
153 of_node->name ? of_node->name : "<unknown>");
154 return NULL;
155 }
156
157 unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
158 if (!unit_address) {
159 printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
160 of_node->name ? of_node->name : "<unknown>");
161 return NULL;
162 }
163
164 /* allocate a vio_dev for this node */
165 viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
166 if (!viodev) {
167 return NULL;
168 }
169 memset(viodev, 0, sizeof(struct vio_dev));
170
171 viodev->dev.platform_data = of_node_get(of_node);
172
173 viodev->irq = NO_IRQ;
174 irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
175 if (irq_p) {
176 int virq = virt_irq_create_mapping(*irq_p);
177 if (virq == NO_IRQ) {
178 printk(KERN_ERR "Unable to allocate interrupt "
179 "number for %s\n", of_node->full_name);
180 } else
181 viodev->irq = irq_offset_up(virq);
182 }
183
184 snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
Stephen Rothwellb877b902005-08-17 16:40:12 +1000185 viodev->name = of_node->name;
186 viodev->type = of_node->type;
187 viodev->unit_address = *unit_address;
188 viodev->iommu_table = vio_build_iommu_table(viodev);
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +1000189
190 /* register with generic device framework */
Stephen Rothwellb877b902005-08-17 16:40:12 +1000191 if (vio_register_device(viodev) == NULL) {
Stephen Rothwell19dbd0f2005-07-12 17:50:26 +1000192 /* XXX free TCE table */
193 kfree(viodev);
194 return NULL;
195 }
196 device_create_file(&viodev->dev, &dev_attr_devspec);
197
198 return viodev;
199}
200EXPORT_SYMBOL(vio_register_device_node);
201
202/**
203 * vio_get_attribute: - get attribute for virtual device
204 * @vdev: The vio device to get property.
205 * @which: The property/attribute to be extracted.
206 * @length: Pointer to length of returned data size (unused if NULL).
207 *
208 * Calls prom.c's get_property() to return the value of the
209 * attribute specified by the preprocessor constant @which
210*/
211const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
212{
213 return get_property(vdev->dev.platform_data, (char*)which, length);
214}
215EXPORT_SYMBOL(vio_get_attribute);
216
217/* vio_find_name() - internal because only vio.c knows how we formatted the
218 * kobject name
219 * XXX once vio_bus_type.devices is actually used as a kset in
220 * drivers/base/bus.c, this function should be removed in favor of
221 * "device_find(kobj_name, &vio_bus_type)"
222 */
223static struct vio_dev *vio_find_name(const char *kobj_name)
224{
225 struct kobject *found;
226
227 found = kset_find_obj(&devices_subsys.kset, kobj_name);
228 if (!found)
229 return NULL;
230
231 return to_vio_dev(container_of(found, struct device, kobj));
232}
233
234/**
235 * vio_find_node - find an already-registered vio_dev
236 * @vnode: device_node of the virtual device we're looking for
237 */
238struct vio_dev *vio_find_node(struct device_node *vnode)
239{
240 uint32_t *unit_address;
241 char kobj_name[BUS_ID_SIZE];
242
243 /* construct the kobject name from the device node */
244 unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
245 if (!unit_address)
246 return NULL;
247 snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
248
249 return vio_find_name(kobj_name);
250}
251EXPORT_SYMBOL(vio_find_node);
252
253int vio_enable_interrupts(struct vio_dev *dev)
254{
255 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
256 if (rc != H_Success)
257 printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
258 return rc;
259}
260EXPORT_SYMBOL(vio_enable_interrupts);
261
262int vio_disable_interrupts(struct vio_dev *dev)
263{
264 int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
265 if (rc != H_Success)
266 printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
267 return rc;
268}
269EXPORT_SYMBOL(vio_disable_interrupts);