vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #include <asm/firmware.h>
  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. * vio_match_device: - Tell if a VIO device has a matching
  34. * VIO device id structure.
  35. * @ids: array of VIO device id structures to search in
  36. * @dev: the VIO device structure to match against
  37. *
  38. * Used by a driver to check whether a VIO device present in the
  39. * system is in its list of supported devices. Returns the matching
  40. * vio_device_id structure or NULL if there is no match.
  41. */
  42. static const struct vio_device_id *vio_match_device(
  43. const struct vio_device_id *ids, const struct vio_dev *dev)
  44. {
  45. while (ids->type[0] != '\0') {
  46. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  47. device_is_compatible(dev->dev.platform_data, ids->compat))
  48. return ids;
  49. ids++;
  50. }
  51. return NULL;
  52. }
  53. /*
  54. * Convert from struct device to struct vio_dev and pass to driver.
  55. * dev->driver has already been set by generic code because vio_bus_match
  56. * succeeded.
  57. */
  58. static int vio_bus_probe(struct device *dev)
  59. {
  60. struct vio_dev *viodev = to_vio_dev(dev);
  61. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  62. const struct vio_device_id *id;
  63. int error = -ENODEV;
  64. if (!viodrv->probe)
  65. return error;
  66. id = vio_match_device(viodrv->id_table, viodev);
  67. if (id)
  68. error = viodrv->probe(viodev, id);
  69. return error;
  70. }
  71. /* convert from struct device to struct vio_dev and pass to driver. */
  72. static int vio_bus_remove(struct device *dev)
  73. {
  74. struct vio_dev *viodev = to_vio_dev(dev);
  75. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  76. if (viodrv->remove)
  77. return viodrv->remove(viodev);
  78. /* driver can't remove */
  79. return 1;
  80. }
  81. /* convert from struct device to struct vio_dev and pass to driver. */
  82. static void vio_bus_shutdown(struct device *dev)
  83. {
  84. struct vio_dev *viodev = to_vio_dev(dev);
  85. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  86. if (dev->driver && viodrv->shutdown)
  87. viodrv->shutdown(viodev);
  88. }
  89. /**
  90. * vio_register_driver: - Register a new vio driver
  91. * @drv: The vio_driver structure to be registered.
  92. */
  93. int vio_register_driver(struct vio_driver *viodrv)
  94. {
  95. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  96. viodrv->driver.name);
  97. /* fill in 'struct driver' fields */
  98. viodrv->driver.bus = &vio_bus_type;
  99. return driver_register(&viodrv->driver);
  100. }
  101. EXPORT_SYMBOL(vio_register_driver);
  102. /**
  103. * vio_unregister_driver - Remove registration of vio driver.
  104. * @driver: The vio_driver struct to be removed form registration
  105. */
  106. void vio_unregister_driver(struct vio_driver *viodrv)
  107. {
  108. driver_unregister(&viodrv->driver);
  109. }
  110. EXPORT_SYMBOL(vio_unregister_driver);
  111. /**
  112. * vio_register_device_node: - Register a new vio device.
  113. * @of_node: The OF node for this device.
  114. *
  115. * Creates and initializes a vio_dev structure from the data in
  116. * of_node (dev.platform_data) and adds it to the list of virtual devices.
  117. * Returns a pointer to the created vio_dev or NULL if node has
  118. * NULL device_type or compatible fields.
  119. */
  120. struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
  121. {
  122. struct vio_dev *viodev;
  123. unsigned int *unit_address;
  124. unsigned int *irq_p;
  125. /* we need the 'device_type' property, in order to match with drivers */
  126. if (of_node->type == NULL) {
  127. printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
  128. __FUNCTION__,
  129. of_node->name ? of_node->name : "<unknown>");
  130. return NULL;
  131. }
  132. unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
  133. if (unit_address == NULL) {
  134. printk(KERN_WARNING "%s: node %s missing 'reg'\n",
  135. __FUNCTION__,
  136. of_node->name ? of_node->name : "<unknown>");
  137. return NULL;
  138. }
  139. /* allocate a vio_dev for this node */
  140. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  141. if (viodev == NULL)
  142. return NULL;
  143. viodev->dev.platform_data = of_node_get(of_node);
  144. viodev->irq = NO_IRQ;
  145. irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
  146. if (irq_p) {
  147. int virq = virt_irq_create_mapping(*irq_p);
  148. if (virq == NO_IRQ) {
  149. printk(KERN_ERR "Unable to allocate interrupt "
  150. "number for %s\n", of_node->full_name);
  151. } else
  152. viodev->irq = irq_offset_up(virq);
  153. }
  154. snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
  155. viodev->name = of_node->name;
  156. viodev->type = of_node->type;
  157. viodev->unit_address = *unit_address;
  158. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  159. unit_address = (unsigned int *)get_property(of_node,
  160. "linux,unit_address", NULL);
  161. if (unit_address != NULL)
  162. viodev->unit_address = *unit_address;
  163. }
  164. viodev->iommu_table = vio_bus_ops.build_iommu_table(viodev);
  165. /* register with generic device framework */
  166. if (vio_register_device(viodev) == NULL) {
  167. /* XXX free TCE table */
  168. kfree(viodev);
  169. return NULL;
  170. }
  171. return viodev;
  172. }
  173. EXPORT_SYMBOL(vio_register_device_node);
  174. /**
  175. * vio_bus_init: - Initialize the virtual IO bus
  176. */
  177. int __init vio_bus_init(struct vio_bus_ops *ops)
  178. {
  179. int err;
  180. struct device_node *node_vroot;
  181. vio_bus_ops = *ops;
  182. err = bus_register(&vio_bus_type);
  183. if (err) {
  184. printk(KERN_ERR "failed to register VIO bus\n");
  185. return err;
  186. }
  187. /*
  188. * The fake parent of all vio devices, just to give us
  189. * a nice directory
  190. */
  191. err = device_register(&vio_bus_device.dev);
  192. if (err) {
  193. printk(KERN_WARNING "%s: device_register returned %i\n",
  194. __FUNCTION__, err);
  195. return err;
  196. }
  197. node_vroot = find_devices("vdevice");
  198. if (node_vroot) {
  199. struct device_node *of_node;
  200. /*
  201. * Create struct vio_devices for each virtual device in
  202. * the device tree. Drivers will associate with them later.
  203. */
  204. for (of_node = node_vroot->child; of_node != NULL;
  205. of_node = of_node->sibling) {
  206. printk(KERN_DEBUG "%s: processing %p\n",
  207. __FUNCTION__, of_node);
  208. vio_register_device_node(of_node);
  209. }
  210. }
  211. return 0;
  212. }
  213. /* vio_dev refcount hit 0 */
  214. static void __devinit vio_dev_release(struct device *dev)
  215. {
  216. if (dev->platform_data) {
  217. /* XXX free TCE table */
  218. of_node_put(dev->platform_data);
  219. }
  220. kfree(to_vio_dev(dev));
  221. }
  222. static ssize_t name_show(struct device *dev,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  226. }
  227. static ssize_t devspec_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct device_node *of_node = dev->platform_data;
  231. return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
  232. }
  233. static struct device_attribute vio_dev_attrs[] = {
  234. __ATTR_RO(name),
  235. __ATTR_RO(devspec),
  236. __ATTR_NULL
  237. };
  238. struct vio_dev * __devinit vio_register_device(struct vio_dev *viodev)
  239. {
  240. /* init generic 'struct device' fields: */
  241. viodev->dev.parent = &vio_bus_device.dev;
  242. viodev->dev.bus = &vio_bus_type;
  243. viodev->dev.release = vio_dev_release;
  244. /* register with generic device framework */
  245. if (device_register(&viodev->dev)) {
  246. printk(KERN_ERR "%s: failed to register device %s\n",
  247. __FUNCTION__, viodev->dev.bus_id);
  248. return NULL;
  249. }
  250. return viodev;
  251. }
  252. void __devinit vio_unregister_device(struct vio_dev *viodev)
  253. {
  254. device_unregister(&viodev->dev);
  255. }
  256. EXPORT_SYMBOL(vio_unregister_device);
  257. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  258. size_t size, enum dma_data_direction direction)
  259. {
  260. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  261. ~0ul, direction);
  262. }
  263. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  264. size_t size, enum dma_data_direction direction)
  265. {
  266. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  267. direction);
  268. }
  269. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  270. int nelems, enum dma_data_direction direction)
  271. {
  272. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  273. nelems, ~0ul, direction);
  274. }
  275. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  276. int nelems, enum dma_data_direction direction)
  277. {
  278. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  279. }
  280. static void *vio_alloc_coherent(struct device *dev, size_t size,
  281. dma_addr_t *dma_handle, gfp_t flag)
  282. {
  283. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  284. dma_handle, ~0ul, flag);
  285. }
  286. static void vio_free_coherent(struct device *dev, size_t size,
  287. void *vaddr, dma_addr_t dma_handle)
  288. {
  289. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  290. dma_handle);
  291. }
  292. static int vio_dma_supported(struct device *dev, u64 mask)
  293. {
  294. return 1;
  295. }
  296. struct dma_mapping_ops vio_dma_ops = {
  297. .alloc_coherent = vio_alloc_coherent,
  298. .free_coherent = vio_free_coherent,
  299. .map_single = vio_map_single,
  300. .unmap_single = vio_unmap_single,
  301. .map_sg = vio_map_sg,
  302. .unmap_sg = vio_unmap_sg,
  303. .dma_supported = vio_dma_supported,
  304. };
  305. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  306. {
  307. const struct vio_dev *vio_dev = to_vio_dev(dev);
  308. struct vio_driver *vio_drv = to_vio_driver(drv);
  309. const struct vio_device_id *ids = vio_drv->id_table;
  310. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  311. }
  312. static int vio_hotplug(struct device *dev, char **envp, int num_envp,
  313. char *buffer, int buffer_size)
  314. {
  315. const struct vio_dev *vio_dev = to_vio_dev(dev);
  316. struct device_node *dn = dev->platform_data;
  317. char *cp;
  318. int length;
  319. if (!num_envp)
  320. return -ENOMEM;
  321. if (!dn)
  322. return -ENODEV;
  323. cp = (char *)get_property(dn, "compatible", &length);
  324. if (!cp)
  325. return -ENODEV;
  326. envp[0] = buffer;
  327. length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
  328. vio_dev->type, cp);
  329. if ((buffer_size - length) <= 0)
  330. return -ENOMEM;
  331. envp[1] = NULL;
  332. return 0;
  333. }
  334. struct bus_type vio_bus_type = {
  335. .name = "vio",
  336. .dev_attrs = vio_dev_attrs,
  337. .uevent = vio_hotplug,
  338. .match = vio_bus_match,
  339. .probe = vio_bus_probe,
  340. .remove = vio_bus_remove,
  341. .shutdown = vio_bus_shutdown,
  342. };
  343. /**
  344. * vio_get_attribute: - get attribute for virtual device
  345. * @vdev: The vio device to get property.
  346. * @which: The property/attribute to be extracted.
  347. * @length: Pointer to length of returned data size (unused if NULL).
  348. *
  349. * Calls prom.c's get_property() to return the value of the
  350. * attribute specified by @which
  351. */
  352. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  353. {
  354. return get_property(vdev->dev.platform_data, which, length);
  355. }
  356. EXPORT_SYMBOL(vio_get_attribute);