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