vio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 int (*is_match)(const struct vio_device_id *id,
  32. const struct vio_dev *dev);
  33. static void (*unregister_device_callback)(struct vio_dev *dev);
  34. static void (*release_device_callback)(struct device *dev);
  35. /*
  36. * Convert from struct device to struct vio_dev and pass to driver.
  37. * dev->driver has already been set by generic code because vio_bus_match
  38. * succeeded.
  39. */
  40. static int vio_bus_probe(struct device *dev)
  41. {
  42. struct vio_dev *viodev = to_vio_dev(dev);
  43. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  44. const struct vio_device_id *id;
  45. int error = -ENODEV;
  46. if (!viodrv->probe)
  47. return error;
  48. id = vio_match_device(viodrv->id_table, viodev);
  49. if (id)
  50. error = viodrv->probe(viodev, id);
  51. return error;
  52. }
  53. /* convert from struct device to struct vio_dev and pass to driver. */
  54. static int vio_bus_remove(struct device *dev)
  55. {
  56. struct vio_dev *viodev = to_vio_dev(dev);
  57. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  58. if (viodrv->remove)
  59. return viodrv->remove(viodev);
  60. /* driver can't remove */
  61. return 1;
  62. }
  63. /**
  64. * vio_register_driver: - Register a new vio driver
  65. * @drv: The vio_driver structure to be registered.
  66. */
  67. int vio_register_driver(struct vio_driver *viodrv)
  68. {
  69. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  70. viodrv->name);
  71. /* fill in 'struct driver' fields */
  72. viodrv->driver.name = viodrv->name;
  73. viodrv->driver.bus = &vio_bus_type;
  74. viodrv->driver.probe = vio_bus_probe;
  75. viodrv->driver.remove = vio_bus_remove;
  76. return driver_register(&viodrv->driver);
  77. }
  78. EXPORT_SYMBOL(vio_register_driver);
  79. /**
  80. * vio_unregister_driver - Remove registration of vio driver.
  81. * @driver: The vio_driver struct to be removed form registration
  82. */
  83. void vio_unregister_driver(struct vio_driver *viodrv)
  84. {
  85. driver_unregister(&viodrv->driver);
  86. }
  87. EXPORT_SYMBOL(vio_unregister_driver);
  88. /**
  89. * vio_match_device: - Tell if a VIO device has a matching
  90. * VIO device id structure.
  91. * @ids: array of VIO device id structures to search in
  92. * @dev: the VIO device structure to match against
  93. *
  94. * Used by a driver to check whether a VIO device present in the
  95. * system is in its list of supported devices. Returns the matching
  96. * vio_device_id structure or NULL if there is no match.
  97. */
  98. static const struct vio_device_id *vio_match_device(
  99. const struct vio_device_id *ids, const struct vio_dev *dev)
  100. {
  101. while (ids->type) {
  102. if (is_match(ids, dev))
  103. return ids;
  104. ids++;
  105. }
  106. return NULL;
  107. }
  108. /**
  109. * vio_bus_init: - Initialize the virtual IO bus
  110. */
  111. int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
  112. const struct vio_dev *dev),
  113. void (*unregister_dev)(struct vio_dev *),
  114. void (*release_dev)(struct device *))
  115. {
  116. int err;
  117. is_match = match_func;
  118. unregister_device_callback = unregister_dev;
  119. release_device_callback = release_dev;
  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 (release_device_callback)
  141. release_device_callback(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 (unregister_device_callback)
  168. unregister_device_callback(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, unsigned int __nocast 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. };