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