vio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * IBM PowerPC Virtual I/O Infrastructure Support.
  3. *
  4. * Copyright (c) 2003-2005 IBM Corp.
  5. * Dave Engebretsen engebret@us.ibm.com
  6. * Santiago Leon santil@us.ibm.com
  7. * Hollis Blanchard <hollisb@us.ibm.com>
  8. * Stephen Rothwell
  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. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/dma-mapping.h>
  20. #include <asm/iommu.h>
  21. #include <asm/dma.h>
  22. #include <asm/vio.h>
  23. static const struct vio_device_id *vio_match_device(
  24. const struct vio_device_id *, const struct vio_dev *);
  25. struct vio_dev vio_bus_device = { /* fake "parent" device */
  26. .name = vio_bus_device.dev.bus_id,
  27. .type = "",
  28. .dev.bus_id = "vio",
  29. .dev.bus = &vio_bus_type,
  30. };
  31. static struct vio_bus_ops vio_bus_ops;
  32. /*
  33. * Convert from struct device to struct vio_dev and pass to driver.
  34. * dev->driver has already been set by generic code because vio_bus_match
  35. * succeeded.
  36. */
  37. static int vio_bus_probe(struct device *dev)
  38. {
  39. struct vio_dev *viodev = to_vio_dev(dev);
  40. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  41. const struct vio_device_id *id;
  42. int error = -ENODEV;
  43. if (!viodrv->probe)
  44. return error;
  45. id = vio_match_device(viodrv->id_table, viodev);
  46. if (id)
  47. error = viodrv->probe(viodev, id);
  48. return error;
  49. }
  50. /* convert from struct device to struct vio_dev and pass to driver. */
  51. static int vio_bus_remove(struct device *dev)
  52. {
  53. struct vio_dev *viodev = to_vio_dev(dev);
  54. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  55. if (viodrv->remove)
  56. return viodrv->remove(viodev);
  57. /* driver can't remove */
  58. return 1;
  59. }
  60. /**
  61. * vio_register_driver: - Register a new vio driver
  62. * @drv: The vio_driver structure to be registered.
  63. */
  64. int vio_register_driver(struct vio_driver *viodrv)
  65. {
  66. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  67. viodrv->name);
  68. /* fill in 'struct driver' fields */
  69. viodrv->driver.name = viodrv->name;
  70. viodrv->driver.bus = &vio_bus_type;
  71. viodrv->driver.probe = vio_bus_probe;
  72. viodrv->driver.remove = vio_bus_remove;
  73. return driver_register(&viodrv->driver);
  74. }
  75. EXPORT_SYMBOL(vio_register_driver);
  76. /**
  77. * vio_unregister_driver - Remove registration of vio driver.
  78. * @driver: The vio_driver struct to be removed form registration
  79. */
  80. void vio_unregister_driver(struct vio_driver *viodrv)
  81. {
  82. driver_unregister(&viodrv->driver);
  83. }
  84. EXPORT_SYMBOL(vio_unregister_driver);
  85. /**
  86. * vio_match_device: - Tell if a VIO device has a matching
  87. * VIO device id structure.
  88. * @ids: array of VIO device id structures to search in
  89. * @dev: the VIO device structure to match against
  90. *
  91. * Used by a driver to check whether a VIO device present in the
  92. * system is in its list of supported devices. Returns the matching
  93. * vio_device_id structure or NULL if there is no match.
  94. */
  95. static const struct vio_device_id *vio_match_device(
  96. const struct vio_device_id *ids, const struct vio_dev *dev)
  97. {
  98. while (ids->type[0] != '\0') {
  99. if (vio_bus_ops.match(ids, dev))
  100. return ids;
  101. ids++;
  102. }
  103. return NULL;
  104. }
  105. /**
  106. * vio_bus_init: - Initialize the virtual IO bus
  107. */
  108. int __init vio_bus_init(struct vio_bus_ops *ops)
  109. {
  110. int err;
  111. vio_bus_ops = *ops;
  112. err = bus_register(&vio_bus_type);
  113. if (err) {
  114. printk(KERN_ERR "failed to register VIO bus\n");
  115. return err;
  116. }
  117. /*
  118. * The fake parent of all vio devices, just to give us
  119. * a nice directory
  120. */
  121. err = device_register(&vio_bus_device.dev);
  122. if (err) {
  123. printk(KERN_WARNING "%s: device_register returned %i\n",
  124. __FUNCTION__, err);
  125. return err;
  126. }
  127. return 0;
  128. }
  129. /* vio_dev refcount hit 0 */
  130. static void __devinit vio_dev_release(struct device *dev)
  131. {
  132. if (vio_bus_ops.release_device)
  133. vio_bus_ops.release_device(dev);
  134. kfree(to_vio_dev(dev));
  135. }
  136. static ssize_t viodev_show_name(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  140. }
  141. DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
  142. struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
  143. {
  144. /* init generic 'struct device' fields: */
  145. viodev->dev.parent = &vio_bus_device.dev;
  146. viodev->dev.bus = &vio_bus_type;
  147. viodev->dev.release = vio_dev_release;
  148. /* register with generic device framework */
  149. if (device_register(&viodev->dev)) {
  150. printk(KERN_ERR "%s: failed to register device %s\n",
  151. __FUNCTION__, viodev->dev.bus_id);
  152. return NULL;
  153. }
  154. device_create_file(&viodev->dev, &dev_attr_name);
  155. return viodev;
  156. }
  157. void __devinit vio_unregister_device(struct vio_dev *viodev)
  158. {
  159. if (vio_bus_ops.unregister_device)
  160. vio_bus_ops.unregister_device(viodev);
  161. device_remove_file(&viodev->dev, &dev_attr_name);
  162. device_unregister(&viodev->dev);
  163. }
  164. EXPORT_SYMBOL(vio_unregister_device);
  165. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  166. size_t size, enum dma_data_direction direction)
  167. {
  168. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  169. direction);
  170. }
  171. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  172. size_t size, enum dma_data_direction direction)
  173. {
  174. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  175. direction);
  176. }
  177. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  178. int nelems, enum dma_data_direction direction)
  179. {
  180. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  181. nelems, direction);
  182. }
  183. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  184. int nelems, enum dma_data_direction direction)
  185. {
  186. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  187. }
  188. static void *vio_alloc_coherent(struct device *dev, size_t size,
  189. dma_addr_t *dma_handle, unsigned int __nocast flag)
  190. {
  191. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  192. dma_handle, flag);
  193. }
  194. static void vio_free_coherent(struct device *dev, size_t size,
  195. void *vaddr, dma_addr_t dma_handle)
  196. {
  197. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  198. dma_handle);
  199. }
  200. static int vio_dma_supported(struct device *dev, u64 mask)
  201. {
  202. return 1;
  203. }
  204. struct dma_mapping_ops vio_dma_ops = {
  205. .alloc_coherent = vio_alloc_coherent,
  206. .free_coherent = vio_free_coherent,
  207. .map_single = vio_map_single,
  208. .unmap_single = vio_unmap_single,
  209. .map_sg = vio_map_sg,
  210. .unmap_sg = vio_unmap_sg,
  211. .dma_supported = vio_dma_supported,
  212. };
  213. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  214. {
  215. const struct vio_dev *vio_dev = to_vio_dev(dev);
  216. struct vio_driver *vio_drv = to_vio_driver(drv);
  217. const struct vio_device_id *ids = vio_drv->id_table;
  218. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  219. }
  220. struct bus_type vio_bus_type = {
  221. .name = "vio",
  222. .match = vio_bus_match,
  223. };