2 * IBM PowerPC Virtual I/O Infrastructure Support.
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>
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.
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>
21 #include <linux/dma-mapping.h>
23 #include <asm/iommu.h>
25 #include <asm/ppcdebug.h>
27 #include <asm/hvcall.h>
28 #include <asm/iSeries/vio.h>
29 #include <asm/iSeries/HvTypes.h>
30 #include <asm/iSeries/HvCallXm.h>
31 #include <asm/iSeries/HvLpConfig.h>
33 #define DBGENTER() pr_debug("%s entered\n", __FUNCTION__)
35 extern struct subsystem devices_subsys
; /* needed for vio_find_name() */
37 static const struct vio_device_id
*vio_match_device(
38 const struct vio_device_id
*, const struct vio_dev
*);
40 #ifdef CONFIG_PPC_PSERIES
41 static struct iommu_table
*vio_build_iommu_table(struct vio_dev
*);
42 static int vio_num_address_cells
;
44 #ifdef CONFIG_PPC_ISERIES
45 static struct iommu_table veth_iommu_table
;
46 static struct iommu_table vio_iommu_table
;
48 static struct vio_dev vio_bus_device
= { /* fake "parent" device */
49 .name
= vio_bus_device
.dev
.bus_id
,
51 #ifdef CONFIG_PPC_ISERIES
52 .iommu_table
= &vio_iommu_table
,
55 .dev
.bus
= &vio_bus_type
,
58 #ifdef CONFIG_PPC_ISERIES
59 static struct vio_dev
*__init
vio_register_device_iseries(char *type
,
62 struct device
*iSeries_vio_dev
= &vio_bus_device
.dev
;
63 EXPORT_SYMBOL(iSeries_vio_dev
);
65 #define device_is_compatible(a, b) 1
69 /* convert from struct device to struct vio_dev and pass to driver.
70 * dev->driver has already been set by generic code because vio_bus_match
72 static int vio_bus_probe(struct device
*dev
)
74 struct vio_dev
*viodev
= to_vio_dev(dev
);
75 struct vio_driver
*viodrv
= to_vio_driver(dev
->driver
);
76 const struct vio_device_id
*id
;
84 id
= vio_match_device(viodrv
->id_table
, viodev
);
86 error
= viodrv
->probe(viodev
, id
);
92 /* convert from struct device to struct vio_dev and pass to driver. */
93 static int vio_bus_remove(struct device
*dev
)
95 struct vio_dev
*viodev
= to_vio_dev(dev
);
96 struct vio_driver
*viodrv
= to_vio_driver(dev
->driver
);
100 if (viodrv
->remove
) {
101 return viodrv
->remove(viodev
);
104 /* driver can't remove */
109 * vio_register_driver: - Register a new vio driver
110 * @drv: The vio_driver structure to be registered.
112 int vio_register_driver(struct vio_driver
*viodrv
)
114 printk(KERN_DEBUG
"%s: driver %s registering\n", __FUNCTION__
,
117 /* fill in 'struct driver' fields */
118 viodrv
->driver
.name
= viodrv
->name
;
119 viodrv
->driver
.bus
= &vio_bus_type
;
120 viodrv
->driver
.probe
= vio_bus_probe
;
121 viodrv
->driver
.remove
= vio_bus_remove
;
123 return driver_register(&viodrv
->driver
);
125 EXPORT_SYMBOL(vio_register_driver
);
128 * vio_unregister_driver - Remove registration of vio driver.
129 * @driver: The vio_driver struct to be removed form registration
131 void vio_unregister_driver(struct vio_driver
*viodrv
)
133 driver_unregister(&viodrv
->driver
);
135 EXPORT_SYMBOL(vio_unregister_driver
);
138 * vio_match_device: - Tell if a VIO device has a matching VIO device id structure.
139 * @ids: array of VIO device id structures to search in
140 * @dev: the VIO device structure to match against
142 * Used by a driver to check whether a VIO device present in the
143 * system is in its list of supported devices. Returns the matching
144 * vio_device_id structure or NULL if there is no match.
146 static const struct vio_device_id
* vio_match_device(const struct vio_device_id
*ids
,
147 const struct vio_dev
*dev
)
152 if ((strncmp(dev
->type
, ids
->type
, strlen(ids
->type
)) == 0) &&
153 device_is_compatible(dev
->dev
.platform_data
, ids
->compat
))
160 #ifdef CONFIG_PPC_ISERIES
161 void __init
iommu_vio_init(void)
163 struct iommu_table
*t
;
164 struct iommu_table_cb cb
;
166 unsigned long itc_entries
;
168 cb
.itc_busno
= 255; /* Bus 255 is the virtual bus */
169 cb
.itc_virtbus
= 0xff; /* Ask for virtual bus */
171 cbp
= virt_to_abs(&cb
);
172 HvCallXm_getTceTableParms(cbp
);
174 itc_entries
= cb
.itc_size
* PAGE_SIZE
/ sizeof(union tce_entry
);
175 veth_iommu_table
.it_size
= itc_entries
/ 2;
176 veth_iommu_table
.it_busno
= cb
.itc_busno
;
177 veth_iommu_table
.it_offset
= cb
.itc_offset
;
178 veth_iommu_table
.it_index
= cb
.itc_index
;
179 veth_iommu_table
.it_type
= TCE_VB
;
180 veth_iommu_table
.it_blocksize
= 1;
182 t
= iommu_init_table(&veth_iommu_table
);
185 printk("Virtual Bus VETH TCE table failed.\n");
187 vio_iommu_table
.it_size
= itc_entries
- veth_iommu_table
.it_size
;
188 vio_iommu_table
.it_busno
= cb
.itc_busno
;
189 vio_iommu_table
.it_offset
= cb
.itc_offset
+
190 veth_iommu_table
.it_size
;
191 vio_iommu_table
.it_index
= cb
.itc_index
;
192 vio_iommu_table
.it_type
= TCE_VB
;
193 vio_iommu_table
.it_blocksize
= 1;
195 t
= iommu_init_table(&vio_iommu_table
);
198 printk("Virtual Bus VIO TCE table failed.\n");
202 #ifdef CONFIG_PPC_PSERIES
203 static void probe_bus_pseries(void)
205 struct device_node
*node_vroot
, *of_node
;
207 node_vroot
= find_devices("vdevice");
208 if ((node_vroot
== NULL
) || (node_vroot
->child
== NULL
))
209 /* this machine doesn't do virtual IO, and that's ok */
212 vio_num_address_cells
= prom_n_addr_cells(node_vroot
->child
);
215 * Create struct vio_devices for each virtual device in the device tree.
216 * Drivers will associate with them later.
218 for (of_node
= node_vroot
->child
; of_node
!= NULL
;
219 of_node
= of_node
->sibling
) {
220 printk(KERN_DEBUG
"%s: processing %p\n", __FUNCTION__
, of_node
);
221 vio_register_device_node(of_node
);
226 #ifdef CONFIG_PPC_ISERIES
227 static void probe_bus_iseries(void)
229 HvLpIndexMap vlan_map
= HvLpConfig_getVirtualLanIndexMap();
230 struct vio_dev
*viodev
;
233 /* there is only one of each of these */
234 vio_register_device_iseries("viocons", 0);
235 vio_register_device_iseries("vscsi", 0);
237 vlan_map
= HvLpConfig_getVirtualLanIndexMap();
238 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALLANS
; i
++) {
239 if ((vlan_map
& (0x8000 >> i
)) == 0)
241 viodev
= vio_register_device_iseries("vlan", i
);
242 /* veth is special and has it own iommu_table */
243 viodev
->iommu_table
= &veth_iommu_table
;
245 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALDISKS
; i
++)
246 vio_register_device_iseries("viodasd", i
);
247 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALCDROMS
; i
++)
248 vio_register_device_iseries("viocd", i
);
249 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALTAPES
; i
++)
250 vio_register_device_iseries("viotape", i
);
255 * vio_bus_init: - Initialize the virtual IO bus
257 static int __init
vio_bus_init(void)
261 err
= bus_register(&vio_bus_type
);
263 printk(KERN_ERR
"failed to register VIO bus\n");
267 /* the fake parent of all vio devices, just to give us a nice directory */
268 err
= device_register(&vio_bus_device
.dev
);
270 printk(KERN_WARNING
"%s: device_register returned %i\n", __FUNCTION__
,
275 #ifdef CONFIG_PPC_PSERIES
278 #ifdef CONFIG_PPC_ISERIES
285 __initcall(vio_bus_init
);
287 /* vio_dev refcount hit 0 */
288 static void __devinit
vio_dev_release(struct device
*dev
)
292 #ifdef CONFIG_PPC_PSERIES
293 /* XXX free TCE table */
294 of_node_put(dev
->platform_data
);
296 kfree(to_vio_dev(dev
));
299 #ifdef CONFIG_PPC_PSERIES
300 static ssize_t
viodev_show_devspec(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
302 struct device_node
*of_node
= dev
->platform_data
;
304 return sprintf(buf
, "%s\n", of_node
->full_name
);
306 DEVICE_ATTR(devspec
, S_IRUSR
| S_IRGRP
| S_IROTH
, viodev_show_devspec
, NULL
);
309 static ssize_t
viodev_show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
311 return sprintf(buf
, "%s\n", to_vio_dev(dev
)->name
);
313 DEVICE_ATTR(name
, S_IRUSR
| S_IRGRP
| S_IROTH
, viodev_show_name
, NULL
);
315 static struct vio_dev
* __devinit
vio_register_device_common(
316 struct vio_dev
*viodev
, char *name
, char *type
,
317 uint32_t unit_address
, struct iommu_table
*iommu_table
)
323 viodev
->unit_address
= unit_address
;
324 viodev
->iommu_table
= iommu_table
;
325 /* init generic 'struct device' fields: */
326 viodev
->dev
.parent
= &vio_bus_device
.dev
;
327 viodev
->dev
.bus
= &vio_bus_type
;
328 viodev
->dev
.release
= vio_dev_release
;
330 /* register with generic device framework */
331 if (device_register(&viodev
->dev
)) {
332 printk(KERN_ERR
"%s: failed to register device %s\n",
333 __FUNCTION__
, viodev
->dev
.bus_id
);
336 device_create_file(&viodev
->dev
, &dev_attr_name
);
341 #ifdef CONFIG_PPC_PSERIES
343 * vio_register_device_node: - Register a new vio device.
344 * @of_node: The OF node for this device.
346 * Creates and initializes a vio_dev structure from the data in
347 * of_node (dev.platform_data) and adds it to the list of virtual devices.
348 * Returns a pointer to the created vio_dev or NULL if node has
349 * NULL device_type or compatible fields.
351 struct vio_dev
* __devinit
vio_register_device_node(struct device_node
*of_node
)
353 struct vio_dev
*viodev
;
354 unsigned int *unit_address
;
359 /* we need the 'device_type' property, in order to match with drivers */
360 if ((NULL
== of_node
->type
)) {
362 "%s: node %s missing 'device_type'\n", __FUNCTION__
,
363 of_node
->name
? of_node
->name
: "<unknown>");
367 unit_address
= (unsigned int *)get_property(of_node
, "reg", NULL
);
369 printk(KERN_WARNING
"%s: node %s missing 'reg'\n", __FUNCTION__
,
370 of_node
->name
? of_node
->name
: "<unknown>");
374 /* allocate a vio_dev for this node */
375 viodev
= kmalloc(sizeof(struct vio_dev
), GFP_KERNEL
);
379 memset(viodev
, 0, sizeof(struct vio_dev
));
381 viodev
->dev
.platform_data
= of_node_get(of_node
);
383 viodev
->irq
= NO_IRQ
;
384 irq_p
= (unsigned int *)get_property(of_node
, "interrupts", NULL
);
386 int virq
= virt_irq_create_mapping(*irq_p
);
387 if (virq
== NO_IRQ
) {
388 printk(KERN_ERR
"Unable to allocate interrupt "
389 "number for %s\n", of_node
->full_name
);
391 viodev
->irq
= irq_offset_up(virq
);
394 snprintf(viodev
->dev
.bus_id
, BUS_ID_SIZE
, "%x", *unit_address
);
396 /* register with generic device framework */
397 if (vio_register_device_common(viodev
, of_node
->name
, of_node
->type
,
398 *unit_address
, vio_build_iommu_table(viodev
))
400 /* XXX free TCE table */
404 device_create_file(&viodev
->dev
, &dev_attr_devspec
);
408 EXPORT_SYMBOL(vio_register_device_node
);
411 #ifdef CONFIG_PPC_ISERIES
413 * vio_register_device: - Register a new vio device.
414 * @voidev: The device to register.
416 static struct vio_dev
*__init
vio_register_device_iseries(char *type
,
419 struct vio_dev
*viodev
;
423 /* allocate a vio_dev for this node */
424 viodev
= kmalloc(sizeof(struct vio_dev
), GFP_KERNEL
);
427 memset(viodev
, 0, sizeof(struct vio_dev
));
429 snprintf(viodev
->dev
.bus_id
, BUS_ID_SIZE
, "%s%d", type
, unit_num
);
431 return vio_register_device_common(viodev
, viodev
->dev
.bus_id
, type
,
432 unit_num
, &vio_iommu_table
);
436 void __devinit
vio_unregister_device(struct vio_dev
*viodev
)
439 #ifdef CONFIG_PPC_PSERIES
440 device_remove_file(&viodev
->dev
, &dev_attr_devspec
);
442 device_remove_file(&viodev
->dev
, &dev_attr_name
);
443 device_unregister(&viodev
->dev
);
445 EXPORT_SYMBOL(vio_unregister_device
);
447 #ifdef CONFIG_PPC_PSERIES
449 * vio_get_attribute: - get attribute for virtual device
450 * @vdev: The vio device to get property.
451 * @which: The property/attribute to be extracted.
452 * @length: Pointer to length of returned data size (unused if NULL).
454 * Calls prom.c's get_property() to return the value of the
455 * attribute specified by the preprocessor constant @which
457 const void * vio_get_attribute(struct vio_dev
*vdev
, void* which
, int* length
)
459 return get_property(vdev
->dev
.platform_data
, (char*)which
, length
);
461 EXPORT_SYMBOL(vio_get_attribute
);
463 /* vio_find_name() - internal because only vio.c knows how we formatted the
465 * XXX once vio_bus_type.devices is actually used as a kset in
466 * drivers/base/bus.c, this function should be removed in favor of
467 * "device_find(kobj_name, &vio_bus_type)"
469 static struct vio_dev
*vio_find_name(const char *kobj_name
)
471 struct kobject
*found
;
473 found
= kset_find_obj(&devices_subsys
.kset
, kobj_name
);
477 return to_vio_dev(container_of(found
, struct device
, kobj
));
481 * vio_find_node - find an already-registered vio_dev
482 * @vnode: device_node of the virtual device we're looking for
484 struct vio_dev
*vio_find_node(struct device_node
*vnode
)
486 uint32_t *unit_address
;
487 char kobj_name
[BUS_ID_SIZE
];
489 /* construct the kobject name from the device node */
490 unit_address
= (uint32_t *)get_property(vnode
, "reg", NULL
);
493 snprintf(kobj_name
, BUS_ID_SIZE
, "%x", *unit_address
);
495 return vio_find_name(kobj_name
);
497 EXPORT_SYMBOL(vio_find_node
);
500 * vio_build_iommu_table: - gets the dma information from OF and builds the TCE tree.
501 * @dev: the virtual device.
503 * Returns a pointer to the built tce tree, or NULL if it can't
506 static struct iommu_table
* vio_build_iommu_table(struct vio_dev
*dev
)
508 unsigned int *dma_window
;
509 struct iommu_table
*newTceTable
;
510 unsigned long offset
;
511 int dma_window_property_size
;
513 dma_window
= (unsigned int *) get_property(dev
->dev
.platform_data
, "ibm,my-dma-window", &dma_window_property_size
);
518 newTceTable
= (struct iommu_table
*) kmalloc(sizeof(struct iommu_table
), GFP_KERNEL
);
520 /* There should be some code to extract the phys-encoded offset
521 using prom_n_addr_cells(). However, according to a comment
522 on earlier versions, it's always zero, so we don't bother */
523 offset
= dma_window
[1] >> PAGE_SHIFT
;
525 /* TCE table size - measured in tce entries */
526 newTceTable
->it_size
= dma_window
[4] >> PAGE_SHIFT
;
527 /* offset for VIO should always be 0 */
528 newTceTable
->it_offset
= offset
;
529 newTceTable
->it_busno
= 0;
530 newTceTable
->it_index
= (unsigned long)dma_window
[0];
531 newTceTable
->it_type
= TCE_VB
;
533 return iommu_init_table(newTceTable
);
536 int vio_enable_interrupts(struct vio_dev
*dev
)
538 int rc
= h_vio_signal(dev
->unit_address
, VIO_IRQ_ENABLE
);
539 if (rc
!= H_Success
) {
540 printk(KERN_ERR
"vio: Error 0x%x enabling interrupts\n", rc
);
544 EXPORT_SYMBOL(vio_enable_interrupts
);
546 int vio_disable_interrupts(struct vio_dev
*dev
)
548 int rc
= h_vio_signal(dev
->unit_address
, VIO_IRQ_DISABLE
);
549 if (rc
!= H_Success
) {
550 printk(KERN_ERR
"vio: Error 0x%x disabling interrupts\n", rc
);
554 EXPORT_SYMBOL(vio_disable_interrupts
);
557 static dma_addr_t
vio_map_single(struct device
*dev
, void *vaddr
,
558 size_t size
, enum dma_data_direction direction
)
560 return iommu_map_single(to_vio_dev(dev
)->iommu_table
, vaddr
, size
,
564 static void vio_unmap_single(struct device
*dev
, dma_addr_t dma_handle
,
565 size_t size
, enum dma_data_direction direction
)
567 iommu_unmap_single(to_vio_dev(dev
)->iommu_table
, dma_handle
, size
,
571 static int vio_map_sg(struct device
*dev
, struct scatterlist
*sglist
,
572 int nelems
, enum dma_data_direction direction
)
574 return iommu_map_sg(dev
, to_vio_dev(dev
)->iommu_table
, sglist
,
578 static void vio_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
579 int nelems
, enum dma_data_direction direction
)
581 iommu_unmap_sg(to_vio_dev(dev
)->iommu_table
, sglist
, nelems
, direction
);
584 static void *vio_alloc_coherent(struct device
*dev
, size_t size
,
585 dma_addr_t
*dma_handle
, unsigned int __nocast flag
)
587 return iommu_alloc_coherent(to_vio_dev(dev
)->iommu_table
, size
,
591 static void vio_free_coherent(struct device
*dev
, size_t size
,
592 void *vaddr
, dma_addr_t dma_handle
)
594 iommu_free_coherent(to_vio_dev(dev
)->iommu_table
, size
, vaddr
,
598 static int vio_dma_supported(struct device
*dev
, u64 mask
)
603 struct dma_mapping_ops vio_dma_ops
= {
604 .alloc_coherent
= vio_alloc_coherent
,
605 .free_coherent
= vio_free_coherent
,
606 .map_single
= vio_map_single
,
607 .unmap_single
= vio_unmap_single
,
608 .map_sg
= vio_map_sg
,
609 .unmap_sg
= vio_unmap_sg
,
610 .dma_supported
= vio_dma_supported
,
613 static int vio_bus_match(struct device
*dev
, struct device_driver
*drv
)
615 const struct vio_dev
*vio_dev
= to_vio_dev(dev
);
616 struct vio_driver
*vio_drv
= to_vio_driver(drv
);
617 const struct vio_device_id
*ids
= vio_drv
->id_table
;
618 const struct vio_device_id
*found_id
;
625 found_id
= vio_match_device(ids
, vio_dev
);
632 struct bus_type vio_bus_type
= {
634 .match
= vio_bus_match
,