vio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include <asm/prom.h>
  24. static const struct vio_device_id *vio_match_device(
  25. const struct vio_device_id *, const struct vio_dev *);
  26. struct vio_dev vio_bus_device = { /* fake "parent" device */
  27. .name = vio_bus_device.dev.bus_id,
  28. .type = "",
  29. .dev.bus_id = "vio",
  30. .dev.bus = &vio_bus_type,
  31. };
  32. static struct vio_bus_ops vio_bus_ops;
  33. /*
  34. * Convert from struct device to struct vio_dev and pass to driver.
  35. * dev->driver has already been set by generic code because vio_bus_match
  36. * succeeded.
  37. */
  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. return error;
  50. }
  51. /* convert from struct device to struct vio_dev and pass to driver. */
  52. static int vio_bus_remove(struct device *dev)
  53. {
  54. struct vio_dev *viodev = to_vio_dev(dev);
  55. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  56. if (viodrv->remove)
  57. return viodrv->remove(viodev);
  58. /* driver can't remove */
  59. return 1;
  60. }
  61. /* convert from struct device to struct vio_dev and pass to driver. */
  62. static void vio_bus_shutdown(struct device *dev)
  63. {
  64. struct vio_dev *viodev = to_vio_dev(dev);
  65. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  66. if (dev->driver && viodrv->shutdown)
  67. viodrv->shutdown(viodev);
  68. }
  69. /**
  70. * vio_register_driver: - Register a new vio driver
  71. * @drv: The vio_driver structure to be registered.
  72. */
  73. int vio_register_driver(struct vio_driver *viodrv)
  74. {
  75. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  76. viodrv->driver.name);
  77. /* fill in 'struct driver' fields */
  78. viodrv->driver.bus = &vio_bus_type;
  79. return driver_register(&viodrv->driver);
  80. }
  81. EXPORT_SYMBOL(vio_register_driver);
  82. /**
  83. * vio_unregister_driver - Remove registration of vio driver.
  84. * @driver: The vio_driver struct to be removed form registration
  85. */
  86. void vio_unregister_driver(struct vio_driver *viodrv)
  87. {
  88. driver_unregister(&viodrv->driver);
  89. }
  90. EXPORT_SYMBOL(vio_unregister_driver);
  91. /**
  92. * vio_match_device: - Tell if a VIO device has a matching
  93. * VIO device id structure.
  94. * @ids: array of VIO device id structures to search in
  95. * @dev: the VIO device structure to match against
  96. *
  97. * Used by a driver to check whether a VIO device present in the
  98. * system is in its list of supported devices. Returns the matching
  99. * vio_device_id structure or NULL if there is no match.
  100. */
  101. static const struct vio_device_id *vio_match_device(
  102. const struct vio_device_id *ids, const struct vio_dev *dev)
  103. {
  104. while (ids->type[0] != '\0') {
  105. if (vio_bus_ops.match(ids, dev))
  106. return ids;
  107. ids++;
  108. }
  109. return NULL;
  110. }
  111. /**
  112. * vio_bus_init: - Initialize the virtual IO bus
  113. */
  114. int __init vio_bus_init(struct vio_bus_ops *ops)
  115. {
  116. int err;
  117. vio_bus_ops = *ops;
  118. err = bus_register(&vio_bus_type);
  119. if (err) {
  120. printk(KERN_ERR "failed to register VIO bus\n");
  121. return err;
  122. }
  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 (vio_bus_ops.release_device)
  139. vio_bus_ops.release_device(dev);
  140. kfree(to_vio_dev(dev));
  141. }
  142. static ssize_t viodev_show_name(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  146. }
  147. DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
  148. struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
  149. {
  150. /* init generic 'struct device' fields: */
  151. viodev->dev.parent = &vio_bus_device.dev;
  152. viodev->dev.bus = &vio_bus_type;
  153. viodev->dev.release = vio_dev_release;
  154. /* register with generic device framework */
  155. if (device_register(&viodev->dev)) {
  156. printk(KERN_ERR "%s: failed to register device %s\n",
  157. __FUNCTION__, viodev->dev.bus_id);
  158. return NULL;
  159. }
  160. device_create_file(&viodev->dev, &dev_attr_name);
  161. return viodev;
  162. }
  163. void __devinit vio_unregister_device(struct vio_dev *viodev)
  164. {
  165. if (vio_bus_ops.unregister_device)
  166. vio_bus_ops.unregister_device(viodev);
  167. device_remove_file(&viodev->dev, &dev_attr_name);
  168. device_unregister(&viodev->dev);
  169. }
  170. EXPORT_SYMBOL(vio_unregister_device);
  171. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  172. size_t size, enum dma_data_direction direction)
  173. {
  174. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  175. direction);
  176. }
  177. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  178. size_t size, enum dma_data_direction direction)
  179. {
  180. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  181. direction);
  182. }
  183. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  184. int nelems, enum dma_data_direction direction)
  185. {
  186. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  187. nelems, direction);
  188. }
  189. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  190. int nelems, enum dma_data_direction direction)
  191. {
  192. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  193. }
  194. static void *vio_alloc_coherent(struct device *dev, size_t size,
  195. dma_addr_t *dma_handle, gfp_t flag)
  196. {
  197. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  198. dma_handle, flag);
  199. }
  200. static void vio_free_coherent(struct device *dev, size_t size,
  201. void *vaddr, dma_addr_t dma_handle)
  202. {
  203. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  204. dma_handle);
  205. }
  206. static int vio_dma_supported(struct device *dev, u64 mask)
  207. {
  208. return 1;
  209. }
  210. struct dma_mapping_ops vio_dma_ops = {
  211. .alloc_coherent = vio_alloc_coherent,
  212. .free_coherent = vio_free_coherent,
  213. .map_single = vio_map_single,
  214. .unmap_single = vio_unmap_single,
  215. .map_sg = vio_map_sg,
  216. .unmap_sg = vio_unmap_sg,
  217. .dma_supported = vio_dma_supported,
  218. };
  219. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  220. {
  221. const struct vio_dev *vio_dev = to_vio_dev(dev);
  222. struct vio_driver *vio_drv = to_vio_driver(drv);
  223. const struct vio_device_id *ids = vio_drv->id_table;
  224. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  225. }
  226. static int vio_hotplug(struct device *dev, char **envp, int num_envp,
  227. char *buffer, int buffer_size)
  228. {
  229. const struct vio_dev *vio_dev = to_vio_dev(dev);
  230. char *cp;
  231. int length;
  232. if (!num_envp)
  233. return -ENOMEM;
  234. if (!vio_dev->dev.platform_data)
  235. return -ENODEV;
  236. cp = (char *)get_property(vio_dev->dev.platform_data, "compatible", &length);
  237. if (!cp)
  238. return -ENODEV;
  239. envp[0] = buffer;
  240. length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
  241. vio_dev->type, cp);
  242. if (buffer_size - length <= 0)
  243. return -ENOMEM;
  244. envp[1] = NULL;
  245. return 0;
  246. }
  247. struct bus_type vio_bus_type = {
  248. .name = "vio",
  249. .uevent = vio_hotplug,
  250. .match = vio_bus_match,
  251. .probe = vio_bus_probe,
  252. .remove = vio_bus_remove,
  253. .shutdown = vio_bus_shutdown,
  254. };