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