diff options
Diffstat (limited to 'arch/powerpc/kernel/vio.c')
-rw-r--r-- | arch/powerpc/kernel/vio.c | 344 |
1 files changed, 291 insertions, 53 deletions
diff --git a/arch/powerpc/kernel/vio.c b/arch/powerpc/kernel/vio.c index 971020cf3f7d..cdf5867838a6 100644 --- a/arch/powerpc/kernel/vio.c +++ b/arch/powerpc/kernel/vio.c | |||
@@ -13,27 +13,116 @@ | |||
13 | * 2 of the License, or (at your option) any later version. | 13 | * 2 of the License, or (at your option) any later version. |
14 | */ | 14 | */ |
15 | 15 | ||
16 | #include <linux/types.h> | ||
17 | #include <linux/device.h> | ||
16 | #include <linux/init.h> | 18 | #include <linux/init.h> |
17 | #include <linux/console.h> | 19 | #include <linux/console.h> |
18 | #include <linux/module.h> | 20 | #include <linux/module.h> |
19 | #include <linux/mm.h> | 21 | #include <linux/mm.h> |
20 | #include <linux/dma-mapping.h> | 22 | #include <linux/dma-mapping.h> |
23 | #include <linux/kobject.h> | ||
24 | |||
21 | #include <asm/iommu.h> | 25 | #include <asm/iommu.h> |
22 | #include <asm/dma.h> | 26 | #include <asm/dma.h> |
23 | #include <asm/vio.h> | 27 | #include <asm/vio.h> |
24 | #include <asm/prom.h> | 28 | #include <asm/prom.h> |
25 | 29 | #include <asm/firmware.h> | |
26 | static const struct vio_device_id *vio_match_device( | 30 | #include <asm/tce.h> |
27 | const struct vio_device_id *, const struct vio_dev *); | 31 | #include <asm/abs_addr.h> |
28 | 32 | #include <asm/page.h> | |
29 | struct vio_dev vio_bus_device = { /* fake "parent" device */ | 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 | |||
40 | extern struct subsystem devices_subsys; /* needed for vio_find_name() */ | ||
41 | |||
42 | static struct vio_dev vio_bus_device = { /* fake "parent" device */ | ||
30 | .name = vio_bus_device.dev.bus_id, | 43 | .name = vio_bus_device.dev.bus_id, |
31 | .type = "", | 44 | .type = "", |
32 | .dev.bus_id = "vio", | 45 | .dev.bus_id = "vio", |
33 | .dev.bus = &vio_bus_type, | 46 | .dev.bus = &vio_bus_type, |
34 | }; | 47 | }; |
35 | 48 | ||
36 | static struct vio_bus_ops vio_bus_ops; | 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 | |||
63 | if (!iommu_init_table(&veth_iommu_table, -1)) | ||
64 | printk("Virtual Bus VETH TCE table failed.\n"); | ||
65 | if (!iommu_init_table(&vio_iommu_table, -1)) | ||
66 | printk("Virtual Bus VIO TCE table failed.\n"); | ||
67 | } | ||
68 | #endif | ||
69 | |||
70 | static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev) | ||
71 | { | ||
72 | #ifdef CONFIG_PPC_ISERIES | ||
73 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { | ||
74 | if (strcmp(dev->type, "network") == 0) | ||
75 | return &veth_iommu_table; | ||
76 | return &vio_iommu_table; | ||
77 | } else | ||
78 | #endif | ||
79 | { | ||
80 | unsigned char *dma_window; | ||
81 | struct iommu_table *tbl; | ||
82 | unsigned long offset, size; | ||
83 | |||
84 | dma_window = get_property(dev->dev.platform_data, | ||
85 | "ibm,my-dma-window", NULL); | ||
86 | if (!dma_window) | ||
87 | return NULL; | ||
88 | |||
89 | tbl = kmalloc(sizeof(*tbl), GFP_KERNEL); | ||
90 | |||
91 | of_parse_dma_window(dev->dev.platform_data, dma_window, | ||
92 | &tbl->it_index, &offset, &size); | ||
93 | |||
94 | /* TCE table size - measured in tce entries */ | ||
95 | tbl->it_size = size >> PAGE_SHIFT; | ||
96 | /* offset for VIO should always be 0 */ | ||
97 | tbl->it_offset = offset >> PAGE_SHIFT; | ||
98 | tbl->it_busno = 0; | ||
99 | tbl->it_type = TCE_VB; | ||
100 | |||
101 | return iommu_init_table(tbl, -1); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * vio_match_device: - Tell if a VIO device has a matching | ||
107 | * VIO device id structure. | ||
108 | * @ids: array of VIO device id structures to search in | ||
109 | * @dev: the VIO device structure to match against | ||
110 | * | ||
111 | * Used by a driver to check whether a VIO device present in the | ||
112 | * system is in its list of supported devices. Returns the matching | ||
113 | * vio_device_id structure or NULL if there is no match. | ||
114 | */ | ||
115 | static const struct vio_device_id *vio_match_device( | ||
116 | const struct vio_device_id *ids, const struct vio_dev *dev) | ||
117 | { | ||
118 | while (ids->type[0] != '\0') { | ||
119 | if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) && | ||
120 | device_is_compatible(dev->dev.platform_data, ids->compat)) | ||
121 | return ids; | ||
122 | ids++; | ||
123 | } | ||
124 | return NULL; | ||
125 | } | ||
37 | 126 | ||
38 | /* | 127 | /* |
39 | * Convert from struct device to struct vio_dev and pass to driver. | 128 | * Convert from struct device to struct vio_dev and pass to driver. |
@@ -106,35 +195,110 @@ void vio_unregister_driver(struct vio_driver *viodrv) | |||
106 | } | 195 | } |
107 | EXPORT_SYMBOL(vio_unregister_driver); | 196 | EXPORT_SYMBOL(vio_unregister_driver); |
108 | 197 | ||
198 | /* vio_dev refcount hit 0 */ | ||
199 | static void __devinit vio_dev_release(struct device *dev) | ||
200 | { | ||
201 | if (dev->platform_data) { | ||
202 | /* XXX free TCE table */ | ||
203 | of_node_put(dev->platform_data); | ||
204 | } | ||
205 | kfree(to_vio_dev(dev)); | ||
206 | } | ||
207 | |||
109 | /** | 208 | /** |
110 | * vio_match_device: - Tell if a VIO device has a matching | 209 | * vio_register_device_node: - Register a new vio device. |
111 | * VIO device id structure. | 210 | * @of_node: The OF node for this device. |
112 | * @ids: array of VIO device id structures to search in | ||
113 | * @dev: the VIO device structure to match against | ||
114 | * | 211 | * |
115 | * Used by a driver to check whether a VIO device present in the | 212 | * Creates and initializes a vio_dev structure from the data in |
116 | * system is in its list of supported devices. Returns the matching | 213 | * of_node (dev.platform_data) and adds it to the list of virtual devices. |
117 | * vio_device_id structure or NULL if there is no match. | 214 | * Returns a pointer to the created vio_dev or NULL if node has |
215 | * NULL device_type or compatible fields. | ||
118 | */ | 216 | */ |
119 | static const struct vio_device_id *vio_match_device( | 217 | struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node) |
120 | const struct vio_device_id *ids, const struct vio_dev *dev) | ||
121 | { | 218 | { |
122 | while (ids->type[0] != '\0') { | 219 | struct vio_dev *viodev; |
123 | if (vio_bus_ops.match(ids, dev)) | 220 | unsigned int *unit_address; |
124 | return ids; | 221 | unsigned int *irq_p; |
125 | ids++; | 222 | |
223 | /* we need the 'device_type' property, in order to match with drivers */ | ||
224 | if (of_node->type == NULL) { | ||
225 | printk(KERN_WARNING "%s: node %s missing 'device_type'\n", | ||
226 | __FUNCTION__, | ||
227 | of_node->name ? of_node->name : "<unknown>"); | ||
228 | return NULL; | ||
126 | } | 229 | } |
127 | return NULL; | 230 | |
231 | unit_address = (unsigned int *)get_property(of_node, "reg", NULL); | ||
232 | if (unit_address == NULL) { | ||
233 | printk(KERN_WARNING "%s: node %s missing 'reg'\n", | ||
234 | __FUNCTION__, | ||
235 | of_node->name ? of_node->name : "<unknown>"); | ||
236 | return NULL; | ||
237 | } | ||
238 | |||
239 | /* allocate a vio_dev for this node */ | ||
240 | viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL); | ||
241 | if (viodev == NULL) | ||
242 | return NULL; | ||
243 | |||
244 | viodev->dev.platform_data = of_node_get(of_node); | ||
245 | |||
246 | viodev->irq = NO_IRQ; | ||
247 | irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL); | ||
248 | if (irq_p) { | ||
249 | int virq = virt_irq_create_mapping(*irq_p); | ||
250 | if (virq == NO_IRQ) { | ||
251 | printk(KERN_ERR "Unable to allocate interrupt " | ||
252 | "number for %s\n", of_node->full_name); | ||
253 | } else | ||
254 | viodev->irq = irq_offset_up(virq); | ||
255 | } | ||
256 | |||
257 | snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address); | ||
258 | viodev->name = of_node->name; | ||
259 | viodev->type = of_node->type; | ||
260 | viodev->unit_address = *unit_address; | ||
261 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { | ||
262 | unit_address = (unsigned int *)get_property(of_node, | ||
263 | "linux,unit_address", NULL); | ||
264 | if (unit_address != NULL) | ||
265 | viodev->unit_address = *unit_address; | ||
266 | } | ||
267 | viodev->iommu_table = vio_build_iommu_table(viodev); | ||
268 | |||
269 | /* init generic 'struct device' fields: */ | ||
270 | viodev->dev.parent = &vio_bus_device.dev; | ||
271 | viodev->dev.bus = &vio_bus_type; | ||
272 | viodev->dev.release = vio_dev_release; | ||
273 | |||
274 | /* register with generic device framework */ | ||
275 | if (device_register(&viodev->dev)) { | ||
276 | printk(KERN_ERR "%s: failed to register device %s\n", | ||
277 | __FUNCTION__, viodev->dev.bus_id); | ||
278 | /* XXX free TCE table */ | ||
279 | kfree(viodev); | ||
280 | return NULL; | ||
281 | } | ||
282 | |||
283 | return viodev; | ||
128 | } | 284 | } |
285 | EXPORT_SYMBOL(vio_register_device_node); | ||
129 | 286 | ||
130 | /** | 287 | /** |
131 | * vio_bus_init: - Initialize the virtual IO bus | 288 | * vio_bus_init: - Initialize the virtual IO bus |
132 | */ | 289 | */ |
133 | int __init vio_bus_init(struct vio_bus_ops *ops) | 290 | static int __init vio_bus_init(void) |
134 | { | 291 | { |
135 | int err; | 292 | int err; |
293 | struct device_node *node_vroot; | ||
136 | 294 | ||
137 | vio_bus_ops = *ops; | 295 | #ifdef CONFIG_PPC_ISERIES |
296 | if (firmware_has_feature(FW_FEATURE_ISERIES)) { | ||
297 | iommu_vio_init(); | ||
298 | vio_bus_device.iommu_table = &vio_iommu_table; | ||
299 | iSeries_vio_dev = &vio_bus_device.dev; | ||
300 | } | ||
301 | #endif | ||
138 | 302 | ||
139 | err = bus_register(&vio_bus_type); | 303 | err = bus_register(&vio_bus_type); |
140 | if (err) { | 304 | if (err) { |
@@ -153,47 +317,48 @@ int __init vio_bus_init(struct vio_bus_ops *ops) | |||
153 | return err; | 317 | return err; |
154 | } | 318 | } |
155 | 319 | ||
156 | return 0; | 320 | node_vroot = find_devices("vdevice"); |
157 | } | 321 | if (node_vroot) { |
322 | struct device_node *of_node; | ||
323 | |||
324 | /* | ||
325 | * Create struct vio_devices for each virtual device in | ||
326 | * the device tree. Drivers will associate with them later. | ||
327 | */ | ||
328 | for (of_node = node_vroot->child; of_node != NULL; | ||
329 | of_node = of_node->sibling) { | ||
330 | printk(KERN_DEBUG "%s: processing %p\n", | ||
331 | __FUNCTION__, of_node); | ||
332 | vio_register_device_node(of_node); | ||
333 | } | ||
334 | } | ||
158 | 335 | ||
159 | /* vio_dev refcount hit 0 */ | 336 | return 0; |
160 | static void __devinit vio_dev_release(struct device *dev) | ||
161 | { | ||
162 | if (vio_bus_ops.release_device) | ||
163 | vio_bus_ops.release_device(dev); | ||
164 | kfree(to_vio_dev(dev)); | ||
165 | } | 337 | } |
338 | __initcall(vio_bus_init); | ||
166 | 339 | ||
167 | static ssize_t viodev_show_name(struct device *dev, | 340 | static ssize_t name_show(struct device *dev, |
168 | struct device_attribute *attr, char *buf) | 341 | struct device_attribute *attr, char *buf) |
169 | { | 342 | { |
170 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); | 343 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); |
171 | } | 344 | } |
172 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); | ||
173 | 345 | ||
174 | struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev) | 346 | static ssize_t devspec_show(struct device *dev, |
347 | struct device_attribute *attr, char *buf) | ||
175 | { | 348 | { |
176 | /* init generic 'struct device' fields: */ | 349 | struct device_node *of_node = dev->platform_data; |
177 | viodev->dev.parent = &vio_bus_device.dev; | ||
178 | viodev->dev.bus = &vio_bus_type; | ||
179 | viodev->dev.release = vio_dev_release; | ||
180 | |||
181 | /* register with generic device framework */ | ||
182 | if (device_register(&viodev->dev)) { | ||
183 | printk(KERN_ERR "%s: failed to register device %s\n", | ||
184 | __FUNCTION__, viodev->dev.bus_id); | ||
185 | return NULL; | ||
186 | } | ||
187 | device_create_file(&viodev->dev, &dev_attr_name); | ||
188 | 350 | ||
189 | return viodev; | 351 | return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none"); |
190 | } | 352 | } |
191 | 353 | ||
354 | static struct device_attribute vio_dev_attrs[] = { | ||
355 | __ATTR_RO(name), | ||
356 | __ATTR_RO(devspec), | ||
357 | __ATTR_NULL | ||
358 | }; | ||
359 | |||
192 | void __devinit vio_unregister_device(struct vio_dev *viodev) | 360 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
193 | { | 361 | { |
194 | if (vio_bus_ops.unregister_device) | ||
195 | vio_bus_ops.unregister_device(viodev); | ||
196 | device_remove_file(&viodev->dev, &dev_attr_name); | ||
197 | device_unregister(&viodev->dev); | 362 | device_unregister(&viodev->dev); |
198 | } | 363 | } |
199 | EXPORT_SYMBOL(vio_unregister_device); | 364 | EXPORT_SYMBOL(vio_unregister_device); |
@@ -229,7 +394,7 @@ static void *vio_alloc_coherent(struct device *dev, size_t size, | |||
229 | dma_addr_t *dma_handle, gfp_t flag) | 394 | dma_addr_t *dma_handle, gfp_t flag) |
230 | { | 395 | { |
231 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, | 396 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, |
232 | dma_handle, ~0ul, flag); | 397 | dma_handle, ~0ul, flag, -1); |
233 | } | 398 | } |
234 | 399 | ||
235 | static void vio_free_coherent(struct device *dev, size_t size, | 400 | static void vio_free_coherent(struct device *dev, size_t size, |
@@ -267,22 +432,23 @@ static int vio_hotplug(struct device *dev, char **envp, int num_envp, | |||
267 | char *buffer, int buffer_size) | 432 | char *buffer, int buffer_size) |
268 | { | 433 | { |
269 | const struct vio_dev *vio_dev = to_vio_dev(dev); | 434 | const struct vio_dev *vio_dev = to_vio_dev(dev); |
435 | struct device_node *dn = dev->platform_data; | ||
270 | char *cp; | 436 | char *cp; |
271 | int length; | 437 | int length; |
272 | 438 | ||
273 | if (!num_envp) | 439 | if (!num_envp) |
274 | return -ENOMEM; | 440 | return -ENOMEM; |
275 | 441 | ||
276 | if (!vio_dev->dev.platform_data) | 442 | if (!dn) |
277 | return -ENODEV; | 443 | return -ENODEV; |
278 | cp = (char *)get_property(vio_dev->dev.platform_data, "compatible", &length); | 444 | cp = (char *)get_property(dn, "compatible", &length); |
279 | if (!cp) | 445 | if (!cp) |
280 | return -ENODEV; | 446 | return -ENODEV; |
281 | 447 | ||
282 | envp[0] = buffer; | 448 | envp[0] = buffer; |
283 | length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s", | 449 | length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s", |
284 | vio_dev->type, cp); | 450 | vio_dev->type, cp); |
285 | if (buffer_size - length <= 0) | 451 | if ((buffer_size - length) <= 0) |
286 | return -ENOMEM; | 452 | return -ENOMEM; |
287 | envp[1] = NULL; | 453 | envp[1] = NULL; |
288 | return 0; | 454 | return 0; |
@@ -290,9 +456,81 @@ static int vio_hotplug(struct device *dev, char **envp, int num_envp, | |||
290 | 456 | ||
291 | struct bus_type vio_bus_type = { | 457 | struct bus_type vio_bus_type = { |
292 | .name = "vio", | 458 | .name = "vio", |
459 | .dev_attrs = vio_dev_attrs, | ||
293 | .uevent = vio_hotplug, | 460 | .uevent = vio_hotplug, |
294 | .match = vio_bus_match, | 461 | .match = vio_bus_match, |
295 | .probe = vio_bus_probe, | 462 | .probe = vio_bus_probe, |
296 | .remove = vio_bus_remove, | 463 | .remove = vio_bus_remove, |
297 | .shutdown = vio_bus_shutdown, | 464 | .shutdown = vio_bus_shutdown, |
298 | }; | 465 | }; |
466 | |||
467 | /** | ||
468 | * vio_get_attribute: - get attribute for virtual device | ||
469 | * @vdev: The vio device to get property. | ||
470 | * @which: The property/attribute to be extracted. | ||
471 | * @length: Pointer to length of returned data size (unused if NULL). | ||
472 | * | ||
473 | * Calls prom.c's get_property() to return the value of the | ||
474 | * attribute specified by @which | ||
475 | */ | ||
476 | const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length) | ||
477 | { | ||
478 | return get_property(vdev->dev.platform_data, which, length); | ||
479 | } | ||
480 | EXPORT_SYMBOL(vio_get_attribute); | ||
481 | |||
482 | #ifdef CONFIG_PPC_PSERIES | ||
483 | /* vio_find_name() - internal because only vio.c knows how we formatted the | ||
484 | * kobject name | ||
485 | * XXX once vio_bus_type.devices is actually used as a kset in | ||
486 | * drivers/base/bus.c, this function should be removed in favor of | ||
487 | * "device_find(kobj_name, &vio_bus_type)" | ||
488 | */ | ||
489 | static struct vio_dev *vio_find_name(const char *kobj_name) | ||
490 | { | ||
491 | struct kobject *found; | ||
492 | |||
493 | found = kset_find_obj(&devices_subsys.kset, kobj_name); | ||
494 | if (!found) | ||
495 | return NULL; | ||
496 | |||
497 | return to_vio_dev(container_of(found, struct device, kobj)); | ||
498 | } | ||
499 | |||
500 | /** | ||
501 | * vio_find_node - find an already-registered vio_dev | ||
502 | * @vnode: device_node of the virtual device we're looking for | ||
503 | */ | ||
504 | struct vio_dev *vio_find_node(struct device_node *vnode) | ||
505 | { | ||
506 | uint32_t *unit_address; | ||
507 | char kobj_name[BUS_ID_SIZE]; | ||
508 | |||
509 | /* construct the kobject name from the device node */ | ||
510 | unit_address = (uint32_t *)get_property(vnode, "reg", NULL); | ||
511 | if (!unit_address) | ||
512 | return NULL; | ||
513 | snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address); | ||
514 | |||
515 | return vio_find_name(kobj_name); | ||
516 | } | ||
517 | EXPORT_SYMBOL(vio_find_node); | ||
518 | |||
519 | int vio_enable_interrupts(struct vio_dev *dev) | ||
520 | { | ||
521 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE); | ||
522 | if (rc != H_SUCCESS) | ||
523 | printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc); | ||
524 | return rc; | ||
525 | } | ||
526 | EXPORT_SYMBOL(vio_enable_interrupts); | ||
527 | |||
528 | int vio_disable_interrupts(struct vio_dev *dev) | ||
529 | { | ||
530 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE); | ||
531 | if (rc != H_SUCCESS) | ||
532 | printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc); | ||
533 | return rc; | ||
534 | } | ||
535 | EXPORT_SYMBOL(vio_disable_interrupts); | ||
536 | #endif /* CONFIG_PPC_PSERIES */ | ||