vio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* convert from struct device to struct vio_dev and pass to driver. */
  61. static void vio_bus_shutdown(struct device *dev)
  62. {
  63. struct vio_dev *viodev = to_vio_dev(dev);
  64. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  65. if (viodrv->shutdown)
  66. viodrv->shutdown(viodev);
  67. }
  68. /**
  69. * vio_register_driver: - Register a new vio driver
  70. * @drv: The vio_driver structure to be registered.
  71. */
  72. int vio_register_driver(struct vio_driver *viodrv)
  73. {
  74. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  75. viodrv->driver.name);
  76. /* fill in 'struct driver' fields */
  77. viodrv->driver.bus = &vio_bus_type;
  78. viodrv->driver.probe = vio_bus_probe;
  79. viodrv->driver.remove = vio_bus_remove;
  80. viodrv->driver.shutdown = vio_bus_shutdown;
  81. return driver_register(&viodrv->driver);
  82. }
  83. EXPORT_SYMBOL(vio_register_driver);
  84. /**
  85. * vio_unregister_driver - Remove registration of vio driver.
  86. * @driver: The vio_driver struct to be removed form registration
  87. */
  88. void vio_unregister_driver(struct vio_driver *viodrv)
  89. {
  90. driver_unregister(&viodrv->driver);
  91. }
  92. EXPORT_SYMBOL(vio_unregister_driver);
  93. /**
  94. * vio_match_device: - Tell if a VIO device has a matching
  95. * VIO device id structure.
  96. * @ids: array of VIO device id structures to search in
  97. * @dev: the VIO device structure to match against
  98. *
  99. * Used by a driver to check whether a VIO device present in the
  100. * system is in its list of supported devices. Returns the matching
  101. * vio_device_id structure or NULL if there is no match.
  102. */
  103. static const struct vio_device_id *vio_match_device(
  104. const struct vio_device_id *ids, const struct vio_dev *dev)
  105. {
  106. while (ids->type[0] != '\0') {
  107. if (vio_bus_ops.match(ids, dev))
  108. return ids;
  109. ids++;
  110. }
  111. return NULL;
  112. }
  113. /**
  114. * vio_bus_init: - Initialize the virtual IO bus
  115. */
  116. int __init vio_bus_init(struct vio_bus_ops *ops)
  117. {
  118. int err;
  119. vio_bus_ops = *ops;
  120. err = bus_register(&vio_bus_type);
  121. if (err) {
  122. printk(KERN_ERR "failed to register VIO bus\n");
  123. return err;
  124. }
  125. /*
  126. * The fake parent of all vio devices, just to give us
  127. * a nice directory
  128. */
  129. err = device_register(&vio_bus_device.dev);
  130. if (err) {
  131. printk(KERN_WARNING "%s: device_register returned %i\n",
  132. __FUNCTION__, err);
  133. return err;
  134. }
  135. return 0;
  136. }
  137. /* vio_dev refcount hit 0 */
  138. static void __devinit vio_dev_release(struct device *dev)
  139. {
  140. if (vio_bus_ops.release_device)
  141. vio_bus_ops.release_device(dev);
  142. kfree(to_vio_dev(dev));
  143. }
  144. static ssize_t viodev_show_name(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  148. }
  149. DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
  150. struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
  151. {
  152. /* init generic 'struct device' fields: */
  153. viodev->dev.parent = &vio_bus_device.dev;
  154. viodev->dev.bus = &vio_bus_type;
  155. viodev->dev.release = vio_dev_release;
  156. /* register with generic device framework */
  157. if (device_register(&viodev->dev)) {
  158. printk(KERN_ERR "%s: failed to register device %s\n",
  159. __FUNCTION__, viodev->dev.bus_id);
  160. return NULL;
  161. }
  162. device_create_file(&viodev->dev, &dev_attr_name);
  163. return viodev;
  164. }
  165. void __devinit vio_unregister_device(struct vio_dev *viodev)
  166. {
  167. if (vio_bus_ops.unregister_device)
  168. vio_bus_ops.unregister_device(viodev);
  169. device_remove_file(&viodev->dev, &dev_attr_name);
  170. device_unregister(&viodev->dev);
  171. }
  172. EXPORT_SYMBOL(vio_unregister_device);
  173. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  174. size_t size, enum dma_data_direction direction)
  175. {
  176. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  177. direction);
  178. }
  179. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  180. size_t size, enum dma_data_direction direction)
  181. {
  182. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  183. direction);
  184. }
  185. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  186. int nelems, enum dma_data_direction direction)
  187. {
  188. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  189. nelems, direction);
  190. }
  191. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  192. int nelems, enum dma_data_direction direction)
  193. {
  194. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  195. }
  196. static void *vio_alloc_coherent(struct device *dev, size_t size,
  197. dma_addr_t *dma_handle, gfp_t flag)
  198. {
  199. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  200. dma_handle, flag);
  201. }
  202. static void vio_free_coherent(struct device *dev, size_t size,
  203. void *vaddr, dma_addr_t dma_handle)
  204. {
  205. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  206. dma_handle);
  207. }
  208. static int vio_dma_supported(struct device *dev, u64 mask)
  209. {
  210. return 1;
  211. }
  212. struct dma_mapping_ops vio_dma_ops = {
  213. .alloc_coherent = vio_alloc_coherent,
  214. .free_coherent = vio_free_coherent,
  215. .map_single = vio_map_single,
  216. .unmap_single = vio_unmap_single,
  217. .map_sg = vio_map_sg,
  218. .unmap_sg = vio_unmap_sg,
  219. .dma_supported = vio_dma_supported,
  220. };
  221. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  222. {
  223. const struct vio_dev *vio_dev = to_vio_dev(dev);
  224. struct vio_driver *vio_drv = to_vio_driver(drv);
  225. const struct vio_device_id *ids = vio_drv->id_table;
  226. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  227. }
  228. struct bus_type vio_bus_type = {
  229. .name = "vio",
  230. .match = vio_bus_match,
  231. };