vio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /* convert from struct device to struct vio_dev and pass to driver.
  36. * dev->driver has already been set by generic code because vio_bus_match
  37. * succeeded. */
  38. static int vio_bus_probe(struct device *dev)
  39. {
  40. struct vio_dev *viodev = to_vio_dev(dev);
  41. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  42. const struct vio_device_id *id;
  43. int error = -ENODEV;
  44. if (!viodrv->probe)
  45. return error;
  46. id = vio_match_device(viodrv->id_table, viodev);
  47. if (id) {
  48. error = viodrv->probe(viodev, id);
  49. }
  50. return error;
  51. }
  52. /* convert from struct device to struct vio_dev and pass to driver. */
  53. static int vio_bus_remove(struct device *dev)
  54. {
  55. struct vio_dev *viodev = to_vio_dev(dev);
  56. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  57. if (viodrv->remove) {
  58. return viodrv->remove(viodev);
  59. }
  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 VIO device id structure.
  90. * @ids: array of VIO device id structures to search in
  91. * @dev: the VIO device structure to match against
  92. *
  93. * Used by a driver to check whether a VIO device present in the
  94. * system is in its list of supported devices. Returns the matching
  95. * vio_device_id structure or NULL if there is no match.
  96. */
  97. static const struct vio_device_id * vio_match_device(const struct vio_device_id *ids,
  98. const struct vio_dev *dev)
  99. {
  100. while (ids->type) {
  101. if (is_match(ids, dev))
  102. return ids;
  103. ids++;
  104. }
  105. return NULL;
  106. }
  107. /**
  108. * vio_bus_init: - Initialize the virtual IO bus
  109. */
  110. int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
  111. const struct vio_dev *dev),
  112. void (*unregister_dev)(struct vio_dev *),
  113. void (*release_dev)(struct device *))
  114. {
  115. int err;
  116. is_match = match_func;
  117. unregister_device_callback = unregister_dev;
  118. release_device_callback = release_dev;
  119. err = bus_register(&vio_bus_type);
  120. if (err) {
  121. printk(KERN_ERR "failed to register VIO bus\n");
  122. return err;
  123. }
  124. /* the fake parent of all vio devices, just to give us
  125. * a nice directory
  126. */
  127. err = device_register(&vio_bus_device.dev);
  128. if (err) {
  129. printk(KERN_WARNING "%s: device_register returned %i\n",
  130. __FUNCTION__, err);
  131. return err;
  132. }
  133. return 0;
  134. }
  135. /* vio_dev refcount hit 0 */
  136. static void __devinit vio_dev_release(struct device *dev)
  137. {
  138. if (release_device_callback)
  139. release_device_callback(dev);
  140. kfree(to_vio_dev(dev));
  141. }
  142. static ssize_t viodev_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  143. {
  144. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  145. }
  146. DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
  147. struct vio_dev * __devinit vio_register_device_common(
  148. struct vio_dev *viodev, char *name, char *type,
  149. uint32_t unit_address, struct iommu_table *iommu_table)
  150. {
  151. viodev->name = name;
  152. viodev->type = type;
  153. viodev->unit_address = unit_address;
  154. viodev->iommu_table = iommu_table;
  155. /* init generic 'struct device' fields: */
  156. viodev->dev.parent = &vio_bus_device.dev;
  157. viodev->dev.bus = &vio_bus_type;
  158. viodev->dev.release = vio_dev_release;
  159. /* register with generic device framework */
  160. if (device_register(&viodev->dev)) {
  161. printk(KERN_ERR "%s: failed to register device %s\n",
  162. __FUNCTION__, viodev->dev.bus_id);
  163. return NULL;
  164. }
  165. device_create_file(&viodev->dev, &dev_attr_name);
  166. return viodev;
  167. }
  168. void __devinit vio_unregister_device(struct vio_dev *viodev)
  169. {
  170. if (unregister_device_callback)
  171. unregister_device_callback(viodev);
  172. device_remove_file(&viodev->dev, &dev_attr_name);
  173. device_unregister(&viodev->dev);
  174. }
  175. EXPORT_SYMBOL(vio_unregister_device);
  176. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  177. size_t size, enum dma_data_direction direction)
  178. {
  179. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  180. direction);
  181. }
  182. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  183. size_t size, enum dma_data_direction direction)
  184. {
  185. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  186. direction);
  187. }
  188. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  189. int nelems, enum dma_data_direction direction)
  190. {
  191. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  192. nelems, direction);
  193. }
  194. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  195. int nelems, enum dma_data_direction direction)
  196. {
  197. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  198. }
  199. static void *vio_alloc_coherent(struct device *dev, size_t size,
  200. dma_addr_t *dma_handle, unsigned int __nocast flag)
  201. {
  202. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  203. dma_handle, flag);
  204. }
  205. static void vio_free_coherent(struct device *dev, size_t size,
  206. void *vaddr, dma_addr_t dma_handle)
  207. {
  208. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  209. dma_handle);
  210. }
  211. static int vio_dma_supported(struct device *dev, u64 mask)
  212. {
  213. return 1;
  214. }
  215. struct dma_mapping_ops vio_dma_ops = {
  216. .alloc_coherent = vio_alloc_coherent,
  217. .free_coherent = vio_free_coherent,
  218. .map_single = vio_map_single,
  219. .unmap_single = vio_unmap_single,
  220. .map_sg = vio_map_sg,
  221. .unmap_sg = vio_unmap_sg,
  222. .dma_supported = vio_dma_supported,
  223. };
  224. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  225. {
  226. const struct vio_dev *vio_dev = to_vio_dev(dev);
  227. struct vio_driver *vio_drv = to_vio_driver(drv);
  228. const struct vio_device_id *ids = vio_drv->id_table;
  229. const struct vio_device_id *found_id;
  230. if (!ids)
  231. return 0;
  232. found_id = vio_match_device(ids, vio_dev);
  233. if (found_id)
  234. return 1;
  235. return 0;
  236. }
  237. struct bus_type vio_bus_type = {
  238. .name = "vio",
  239. .match = vio_bus_match,
  240. };