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