Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IBM PowerPC Virtual I/O Infrastructure Support. | |
3 | * | |
4 | * Copyright (c) 2003 IBM Corp. | |
5 | * Dave Engebretsen engebret@us.ibm.com | |
6 | * Santiago Leon santil@us.ibm.com | |
7 | * Hollis Blanchard <hollisb@us.ibm.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * as published by the Free Software Foundation; either version | |
12 | * 2 of the License, or (at your option) any later version. | |
13 | */ | |
14 | ||
15 | #include <linux/init.h> | |
16 | #include <linux/console.h> | |
17 | #include <linux/version.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/kobject.h> | |
20 | #include <linux/mm.h> | |
21 | #include <linux/dma-mapping.h> | |
22 | #include <asm/rtas.h> | |
23 | #include <asm/iommu.h> | |
24 | #include <asm/dma.h> | |
25 | #include <asm/ppcdebug.h> | |
26 | #include <asm/vio.h> | |
27 | #include <asm/hvcall.h> | |
1da177e4 LT |
28 | |
29 | #define DBGENTER() pr_debug("%s entered\n", __FUNCTION__) | |
30 | ||
31 | extern struct subsystem devices_subsys; /* needed for vio_find_name() */ | |
32 | ||
33 | static const struct vio_device_id *vio_match_device( | |
34 | const struct vio_device_id *, const struct vio_dev *); | |
35 | ||
36 | #ifdef CONFIG_PPC_PSERIES | |
37 | static struct iommu_table *vio_build_iommu_table(struct vio_dev *); | |
38 | static int vio_num_address_cells; | |
39 | #endif | |
3e494c80 | 40 | struct vio_dev vio_bus_device = { /* fake "parent" device */ |
ac5b33c9 SR |
41 | .name = vio_bus_device.dev.bus_id, |
42 | .type = "", | |
ac5b33c9 SR |
43 | .dev.bus_id = "vio", |
44 | .dev.bus = &vio_bus_type, | |
1da177e4 | 45 | }; |
ac5b33c9 | 46 | |
6312236f SR |
47 | static int (*is_match)(const struct vio_device_id *id, |
48 | const struct vio_dev *dev); | |
1da177e4 LT |
49 | |
50 | /* convert from struct device to struct vio_dev and pass to driver. | |
51 | * dev->driver has already been set by generic code because vio_bus_match | |
52 | * succeeded. */ | |
53 | static int vio_bus_probe(struct device *dev) | |
54 | { | |
55 | struct vio_dev *viodev = to_vio_dev(dev); | |
56 | struct vio_driver *viodrv = to_vio_driver(dev->driver); | |
57 | const struct vio_device_id *id; | |
58 | int error = -ENODEV; | |
59 | ||
60 | DBGENTER(); | |
61 | ||
62 | if (!viodrv->probe) | |
63 | return error; | |
64 | ||
65 | id = vio_match_device(viodrv->id_table, viodev); | |
66 | if (id) { | |
67 | error = viodrv->probe(viodev, id); | |
68 | } | |
69 | ||
70 | return error; | |
71 | } | |
72 | ||
73 | /* convert from struct device to struct vio_dev and pass to driver. */ | |
74 | static int vio_bus_remove(struct device *dev) | |
75 | { | |
76 | struct vio_dev *viodev = to_vio_dev(dev); | |
77 | struct vio_driver *viodrv = to_vio_driver(dev->driver); | |
78 | ||
79 | DBGENTER(); | |
80 | ||
81 | if (viodrv->remove) { | |
82 | return viodrv->remove(viodev); | |
83 | } | |
84 | ||
85 | /* driver can't remove */ | |
86 | return 1; | |
87 | } | |
88 | ||
89 | /** | |
90 | * vio_register_driver: - Register a new vio driver | |
91 | * @drv: The vio_driver structure to be registered. | |
92 | */ | |
93 | int vio_register_driver(struct vio_driver *viodrv) | |
94 | { | |
95 | printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__, | |
96 | viodrv->name); | |
97 | ||
98 | /* fill in 'struct driver' fields */ | |
99 | viodrv->driver.name = viodrv->name; | |
100 | viodrv->driver.bus = &vio_bus_type; | |
101 | viodrv->driver.probe = vio_bus_probe; | |
102 | viodrv->driver.remove = vio_bus_remove; | |
103 | ||
104 | return driver_register(&viodrv->driver); | |
105 | } | |
106 | EXPORT_SYMBOL(vio_register_driver); | |
107 | ||
108 | /** | |
109 | * vio_unregister_driver - Remove registration of vio driver. | |
110 | * @driver: The vio_driver struct to be removed form registration | |
111 | */ | |
112 | void vio_unregister_driver(struct vio_driver *viodrv) | |
113 | { | |
114 | driver_unregister(&viodrv->driver); | |
115 | } | |
116 | EXPORT_SYMBOL(vio_unregister_driver); | |
117 | ||
118 | /** | |
119 | * vio_match_device: - Tell if a VIO device has a matching VIO device id structure. | |
120 | * @ids: array of VIO device id structures to search in | |
121 | * @dev: the VIO device structure to match against | |
122 | * | |
123 | * Used by a driver to check whether a VIO device present in the | |
124 | * system is in its list of supported devices. Returns the matching | |
125 | * vio_device_id structure or NULL if there is no match. | |
126 | */ | |
127 | static const struct vio_device_id * vio_match_device(const struct vio_device_id *ids, | |
128 | const struct vio_dev *dev) | |
129 | { | |
130 | DBGENTER(); | |
131 | ||
132 | while (ids->type) { | |
6312236f | 133 | if (is_match(ids, dev)) |
1da177e4 LT |
134 | return ids; |
135 | ids++; | |
136 | } | |
137 | return NULL; | |
138 | } | |
139 | ||
1da177e4 LT |
140 | #ifdef CONFIG_PPC_PSERIES |
141 | static void probe_bus_pseries(void) | |
142 | { | |
143 | struct device_node *node_vroot, *of_node; | |
144 | ||
145 | node_vroot = find_devices("vdevice"); | |
146 | if ((node_vroot == NULL) || (node_vroot->child == NULL)) | |
147 | /* this machine doesn't do virtual IO, and that's ok */ | |
148 | return; | |
149 | ||
150 | vio_num_address_cells = prom_n_addr_cells(node_vroot->child); | |
151 | ||
152 | /* | |
153 | * Create struct vio_devices for each virtual device in the device tree. | |
154 | * Drivers will associate with them later. | |
155 | */ | |
156 | for (of_node = node_vroot->child; of_node != NULL; | |
157 | of_node = of_node->sibling) { | |
158 | printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node); | |
159 | vio_register_device_node(of_node); | |
160 | } | |
161 | } | |
162 | #endif | |
163 | ||
1da177e4 LT |
164 | /** |
165 | * vio_bus_init: - Initialize the virtual IO bus | |
166 | */ | |
6312236f SR |
167 | int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id, |
168 | const struct vio_dev *dev)) | |
1da177e4 LT |
169 | { |
170 | int err; | |
171 | ||
6312236f SR |
172 | is_match = match_func; |
173 | ||
1da177e4 LT |
174 | err = bus_register(&vio_bus_type); |
175 | if (err) { | |
176 | printk(KERN_ERR "failed to register VIO bus\n"); | |
177 | return err; | |
178 | } | |
179 | ||
3e494c80 SR |
180 | /* the fake parent of all vio devices, just to give us |
181 | * a nice directory | |
182 | */ | |
ac5b33c9 | 183 | err = device_register(&vio_bus_device.dev); |
1da177e4 | 184 | if (err) { |
3e494c80 SR |
185 | printk(KERN_WARNING "%s: device_register returned %i\n", |
186 | __FUNCTION__, err); | |
1da177e4 LT |
187 | return err; |
188 | } | |
189 | ||
3e494c80 SR |
190 | return 0; |
191 | } | |
192 | ||
1da177e4 | 193 | #ifdef CONFIG_PPC_PSERIES |
6312236f SR |
194 | /** |
195 | * vio_match_device_pseries: - Tell if a pSeries VIO device matches a | |
196 | * vio_device_id | |
197 | */ | |
198 | static int vio_match_device_pseries(const struct vio_device_id *id, | |
199 | const struct vio_dev *dev) | |
200 | { | |
201 | return (strncmp(dev->type, id->type, strlen(id->type)) == 0) && | |
202 | device_is_compatible(dev->dev.platform_data, id->compat); | |
203 | } | |
204 | ||
3e494c80 SR |
205 | /** |
206 | * vio_bus_init_pseries: - Initialize the pSeries virtual IO bus | |
207 | */ | |
208 | static int __init vio_bus_init_pseries(void) | |
209 | { | |
210 | int err; | |
1da177e4 | 211 | |
6312236f | 212 | err = vio_bus_init(vio_match_device_pseries); |
3e494c80 SR |
213 | if (err == 0) |
214 | probe_bus_pseries(); | |
215 | return err; | |
1da177e4 LT |
216 | } |
217 | ||
3e494c80 SR |
218 | __initcall(vio_bus_init_pseries); |
219 | #endif | |
1da177e4 LT |
220 | |
221 | /* vio_dev refcount hit 0 */ | |
222 | static void __devinit vio_dev_release(struct device *dev) | |
223 | { | |
224 | DBGENTER(); | |
225 | ||
226 | #ifdef CONFIG_PPC_PSERIES | |
227 | /* XXX free TCE table */ | |
228 | of_node_put(dev->platform_data); | |
229 | #endif | |
230 | kfree(to_vio_dev(dev)); | |
231 | } | |
232 | ||
233 | #ifdef CONFIG_PPC_PSERIES | |
ff381d22 | 234 | static ssize_t viodev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
235 | { |
236 | struct device_node *of_node = dev->platform_data; | |
237 | ||
238 | return sprintf(buf, "%s\n", of_node->full_name); | |
239 | } | |
240 | DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL); | |
241 | #endif | |
242 | ||
ff381d22 | 243 | static ssize_t viodev_show_name(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
244 | { |
245 | return sprintf(buf, "%s\n", to_vio_dev(dev)->name); | |
246 | } | |
247 | DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL); | |
248 | ||
3e494c80 | 249 | struct vio_dev * __devinit vio_register_device_common( |
1da177e4 LT |
250 | struct vio_dev *viodev, char *name, char *type, |
251 | uint32_t unit_address, struct iommu_table *iommu_table) | |
252 | { | |
253 | DBGENTER(); | |
254 | ||
255 | viodev->name = name; | |
256 | viodev->type = type; | |
257 | viodev->unit_address = unit_address; | |
258 | viodev->iommu_table = iommu_table; | |
259 | /* init generic 'struct device' fields: */ | |
ac5b33c9 | 260 | viodev->dev.parent = &vio_bus_device.dev; |
1da177e4 LT |
261 | viodev->dev.bus = &vio_bus_type; |
262 | viodev->dev.release = vio_dev_release; | |
263 | ||
264 | /* register with generic device framework */ | |
265 | if (device_register(&viodev->dev)) { | |
266 | printk(KERN_ERR "%s: failed to register device %s\n", | |
267 | __FUNCTION__, viodev->dev.bus_id); | |
268 | return NULL; | |
269 | } | |
270 | device_create_file(&viodev->dev, &dev_attr_name); | |
271 | ||
272 | return viodev; | |
273 | } | |
274 | ||
275 | #ifdef CONFIG_PPC_PSERIES | |
276 | /** | |
277 | * vio_register_device_node: - Register a new vio device. | |
278 | * @of_node: The OF node for this device. | |
279 | * | |
280 | * Creates and initializes a vio_dev structure from the data in | |
281 | * of_node (dev.platform_data) and adds it to the list of virtual devices. | |
282 | * Returns a pointer to the created vio_dev or NULL if node has | |
283 | * NULL device_type or compatible fields. | |
284 | */ | |
285 | struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node) | |
286 | { | |
287 | struct vio_dev *viodev; | |
288 | unsigned int *unit_address; | |
289 | unsigned int *irq_p; | |
290 | ||
291 | DBGENTER(); | |
292 | ||
293 | /* we need the 'device_type' property, in order to match with drivers */ | |
294 | if ((NULL == of_node->type)) { | |
295 | printk(KERN_WARNING | |
296 | "%s: node %s missing 'device_type'\n", __FUNCTION__, | |
297 | of_node->name ? of_node->name : "<unknown>"); | |
298 | return NULL; | |
299 | } | |
300 | ||
301 | unit_address = (unsigned int *)get_property(of_node, "reg", NULL); | |
302 | if (!unit_address) { | |
303 | printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__, | |
304 | of_node->name ? of_node->name : "<unknown>"); | |
305 | return NULL; | |
306 | } | |
307 | ||
308 | /* allocate a vio_dev for this node */ | |
309 | viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL); | |
310 | if (!viodev) { | |
311 | return NULL; | |
312 | } | |
313 | memset(viodev, 0, sizeof(struct vio_dev)); | |
314 | ||
315 | viodev->dev.platform_data = of_node_get(of_node); | |
316 | ||
317 | viodev->irq = NO_IRQ; | |
318 | irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL); | |
319 | if (irq_p) { | |
320 | int virq = virt_irq_create_mapping(*irq_p); | |
321 | if (virq == NO_IRQ) { | |
322 | printk(KERN_ERR "Unable to allocate interrupt " | |
323 | "number for %s\n", of_node->full_name); | |
324 | } else | |
325 | viodev->irq = irq_offset_up(virq); | |
326 | } | |
327 | ||
328 | snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address); | |
329 | ||
330 | /* register with generic device framework */ | |
331 | if (vio_register_device_common(viodev, of_node->name, of_node->type, | |
332 | *unit_address, vio_build_iommu_table(viodev)) | |
333 | == NULL) { | |
334 | /* XXX free TCE table */ | |
335 | kfree(viodev); | |
336 | return NULL; | |
337 | } | |
338 | device_create_file(&viodev->dev, &dev_attr_devspec); | |
339 | ||
340 | return viodev; | |
341 | } | |
342 | EXPORT_SYMBOL(vio_register_device_node); | |
343 | #endif | |
344 | ||
1da177e4 LT |
345 | void __devinit vio_unregister_device(struct vio_dev *viodev) |
346 | { | |
347 | DBGENTER(); | |
348 | #ifdef CONFIG_PPC_PSERIES | |
349 | device_remove_file(&viodev->dev, &dev_attr_devspec); | |
350 | #endif | |
351 | device_remove_file(&viodev->dev, &dev_attr_name); | |
352 | device_unregister(&viodev->dev); | |
353 | } | |
354 | EXPORT_SYMBOL(vio_unregister_device); | |
355 | ||
356 | #ifdef CONFIG_PPC_PSERIES | |
357 | /** | |
358 | * vio_get_attribute: - get attribute for virtual device | |
359 | * @vdev: The vio device to get property. | |
360 | * @which: The property/attribute to be extracted. | |
361 | * @length: Pointer to length of returned data size (unused if NULL). | |
362 | * | |
363 | * Calls prom.c's get_property() to return the value of the | |
364 | * attribute specified by the preprocessor constant @which | |
365 | */ | |
366 | const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length) | |
367 | { | |
368 | return get_property(vdev->dev.platform_data, (char*)which, length); | |
369 | } | |
370 | EXPORT_SYMBOL(vio_get_attribute); | |
371 | ||
372 | /* vio_find_name() - internal because only vio.c knows how we formatted the | |
373 | * kobject name | |
374 | * XXX once vio_bus_type.devices is actually used as a kset in | |
375 | * drivers/base/bus.c, this function should be removed in favor of | |
376 | * "device_find(kobj_name, &vio_bus_type)" | |
377 | */ | |
378 | static struct vio_dev *vio_find_name(const char *kobj_name) | |
379 | { | |
380 | struct kobject *found; | |
381 | ||
382 | found = kset_find_obj(&devices_subsys.kset, kobj_name); | |
383 | if (!found) | |
384 | return NULL; | |
385 | ||
386 | return to_vio_dev(container_of(found, struct device, kobj)); | |
387 | } | |
388 | ||
389 | /** | |
390 | * vio_find_node - find an already-registered vio_dev | |
391 | * @vnode: device_node of the virtual device we're looking for | |
392 | */ | |
393 | struct vio_dev *vio_find_node(struct device_node *vnode) | |
394 | { | |
395 | uint32_t *unit_address; | |
396 | char kobj_name[BUS_ID_SIZE]; | |
397 | ||
398 | /* construct the kobject name from the device node */ | |
399 | unit_address = (uint32_t *)get_property(vnode, "reg", NULL); | |
400 | if (!unit_address) | |
401 | return NULL; | |
402 | snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address); | |
403 | ||
404 | return vio_find_name(kobj_name); | |
405 | } | |
406 | EXPORT_SYMBOL(vio_find_node); | |
407 | ||
408 | /** | |
409 | * vio_build_iommu_table: - gets the dma information from OF and builds the TCE tree. | |
410 | * @dev: the virtual device. | |
411 | * | |
412 | * Returns a pointer to the built tce tree, or NULL if it can't | |
413 | * find property. | |
414 | */ | |
415 | static struct iommu_table * vio_build_iommu_table(struct vio_dev *dev) | |
416 | { | |
417 | unsigned int *dma_window; | |
418 | struct iommu_table *newTceTable; | |
419 | unsigned long offset; | |
420 | int dma_window_property_size; | |
421 | ||
422 | dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size); | |
423 | if(!dma_window) { | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL); | |
428 | ||
429 | /* There should be some code to extract the phys-encoded offset | |
430 | using prom_n_addr_cells(). However, according to a comment | |
431 | on earlier versions, it's always zero, so we don't bother */ | |
432 | offset = dma_window[1] >> PAGE_SHIFT; | |
433 | ||
434 | /* TCE table size - measured in tce entries */ | |
435 | newTceTable->it_size = dma_window[4] >> PAGE_SHIFT; | |
436 | /* offset for VIO should always be 0 */ | |
437 | newTceTable->it_offset = offset; | |
438 | newTceTable->it_busno = 0; | |
439 | newTceTable->it_index = (unsigned long)dma_window[0]; | |
440 | newTceTable->it_type = TCE_VB; | |
441 | ||
442 | return iommu_init_table(newTceTable); | |
443 | } | |
444 | ||
445 | int vio_enable_interrupts(struct vio_dev *dev) | |
446 | { | |
447 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE); | |
448 | if (rc != H_Success) { | |
449 | printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc); | |
450 | } | |
451 | return rc; | |
452 | } | |
453 | EXPORT_SYMBOL(vio_enable_interrupts); | |
454 | ||
455 | int vio_disable_interrupts(struct vio_dev *dev) | |
456 | { | |
457 | int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE); | |
458 | if (rc != H_Success) { | |
459 | printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc); | |
460 | } | |
461 | return rc; | |
462 | } | |
463 | EXPORT_SYMBOL(vio_disable_interrupts); | |
464 | #endif | |
465 | ||
466 | static dma_addr_t vio_map_single(struct device *dev, void *vaddr, | |
467 | size_t size, enum dma_data_direction direction) | |
468 | { | |
469 | return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size, | |
470 | direction); | |
471 | } | |
472 | ||
473 | static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle, | |
474 | size_t size, enum dma_data_direction direction) | |
475 | { | |
476 | iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size, | |
477 | direction); | |
478 | } | |
479 | ||
480 | static int vio_map_sg(struct device *dev, struct scatterlist *sglist, | |
481 | int nelems, enum dma_data_direction direction) | |
482 | { | |
483 | return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist, | |
484 | nelems, direction); | |
485 | } | |
486 | ||
487 | static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist, | |
488 | int nelems, enum dma_data_direction direction) | |
489 | { | |
490 | iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction); | |
491 | } | |
492 | ||
493 | static void *vio_alloc_coherent(struct device *dev, size_t size, | |
494 | dma_addr_t *dma_handle, unsigned int __nocast flag) | |
495 | { | |
496 | return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size, | |
497 | dma_handle, flag); | |
498 | } | |
499 | ||
500 | static void vio_free_coherent(struct device *dev, size_t size, | |
501 | void *vaddr, dma_addr_t dma_handle) | |
502 | { | |
503 | iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr, | |
504 | dma_handle); | |
505 | } | |
506 | ||
507 | static int vio_dma_supported(struct device *dev, u64 mask) | |
508 | { | |
509 | return 1; | |
510 | } | |
511 | ||
512 | struct dma_mapping_ops vio_dma_ops = { | |
513 | .alloc_coherent = vio_alloc_coherent, | |
514 | .free_coherent = vio_free_coherent, | |
515 | .map_single = vio_map_single, | |
516 | .unmap_single = vio_unmap_single, | |
517 | .map_sg = vio_map_sg, | |
518 | .unmap_sg = vio_unmap_sg, | |
519 | .dma_supported = vio_dma_supported, | |
520 | }; | |
521 | ||
522 | static int vio_bus_match(struct device *dev, struct device_driver *drv) | |
523 | { | |
524 | const struct vio_dev *vio_dev = to_vio_dev(dev); | |
525 | struct vio_driver *vio_drv = to_vio_driver(drv); | |
526 | const struct vio_device_id *ids = vio_drv->id_table; | |
527 | const struct vio_device_id *found_id; | |
528 | ||
529 | DBGENTER(); | |
530 | ||
531 | if (!ids) | |
532 | return 0; | |
533 | ||
534 | found_id = vio_match_device(ids, vio_dev); | |
535 | if (found_id) | |
536 | return 1; | |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
541 | struct bus_type vio_bus_type = { | |
542 | .name = "vio", | |
543 | .match = vio_bus_match, | |
544 | }; |