vio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * IBM PowerPC pSeries 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/module.h>
  17. #include <linux/mm.h>
  18. #include <linux/kobject.h>
  19. #include <asm/iommu.h>
  20. #include <asm/dma.h>
  21. #include <asm/prom.h>
  22. #include <asm/vio.h>
  23. #include <asm/hvcall.h>
  24. #include <asm/tce.h>
  25. extern struct subsystem devices_subsys; /* needed for vio_find_name() */
  26. static void probe_bus_pseries(void)
  27. {
  28. struct device_node *node_vroot, *of_node;
  29. node_vroot = find_devices("vdevice");
  30. if ((node_vroot == NULL) || (node_vroot->child == NULL))
  31. /* this machine doesn't do virtual IO, and that's ok */
  32. return;
  33. /*
  34. * Create struct vio_devices for each virtual device in the device tree.
  35. * Drivers will associate with them later.
  36. */
  37. for (of_node = node_vroot->child; of_node != NULL;
  38. of_node = of_node->sibling) {
  39. printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
  40. vio_register_device_node(of_node);
  41. }
  42. }
  43. /**
  44. * vio_match_device_pseries: - Tell if a pSeries VIO device matches a
  45. * vio_device_id
  46. */
  47. static int vio_match_device_pseries(const struct vio_device_id *id,
  48. const struct vio_dev *dev)
  49. {
  50. return (strncmp(dev->type, id->type, strlen(id->type)) == 0) &&
  51. device_is_compatible(dev->dev.platform_data, id->compat);
  52. }
  53. static void vio_release_device_pseries(struct device *dev)
  54. {
  55. /* XXX free TCE table */
  56. of_node_put(dev->platform_data);
  57. }
  58. static ssize_t viodev_show_devspec(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. struct device_node *of_node = dev->platform_data;
  62. return sprintf(buf, "%s\n", of_node->full_name);
  63. }
  64. DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL);
  65. static void vio_unregister_device_pseries(struct vio_dev *viodev)
  66. {
  67. device_remove_file(&viodev->dev, &dev_attr_devspec);
  68. }
  69. static struct vio_bus_ops vio_bus_ops_pseries = {
  70. .match = vio_match_device_pseries,
  71. .unregister_device = vio_unregister_device_pseries,
  72. .release_device = vio_release_device_pseries,
  73. };
  74. /**
  75. * vio_bus_init_pseries: - Initialize the pSeries virtual IO bus
  76. */
  77. static int __init vio_bus_init_pseries(void)
  78. {
  79. int err;
  80. err = vio_bus_init(&vio_bus_ops_pseries);
  81. if (err == 0)
  82. probe_bus_pseries();
  83. return err;
  84. }
  85. __initcall(vio_bus_init_pseries);
  86. /**
  87. * vio_build_iommu_table: - gets the dma information from OF and
  88. * builds the TCE tree.
  89. * @dev: the virtual device.
  90. *
  91. * Returns a pointer to the built tce tree, or NULL if it can't
  92. * find property.
  93. */
  94. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  95. {
  96. unsigned int *dma_window;
  97. struct iommu_table *newTceTable;
  98. unsigned long offset;
  99. int dma_window_property_size;
  100. dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size);
  101. if(!dma_window) {
  102. return NULL;
  103. }
  104. newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  105. /* There should be some code to extract the phys-encoded offset
  106. using prom_n_addr_cells(). However, according to a comment
  107. on earlier versions, it's always zero, so we don't bother */
  108. offset = dma_window[1] >> PAGE_SHIFT;
  109. /* TCE table size - measured in tce entries */
  110. newTceTable->it_size = dma_window[4] >> PAGE_SHIFT;
  111. /* offset for VIO should always be 0 */
  112. newTceTable->it_offset = offset;
  113. newTceTable->it_busno = 0;
  114. newTceTable->it_index = (unsigned long)dma_window[0];
  115. newTceTable->it_type = TCE_VB;
  116. return iommu_init_table(newTceTable);
  117. }
  118. /**
  119. * vio_register_device_node: - Register a new vio device.
  120. * @of_node: The OF node for this device.
  121. *
  122. * Creates and initializes a vio_dev structure from the data in
  123. * of_node (dev.platform_data) and adds it to the list of virtual devices.
  124. * Returns a pointer to the created vio_dev or NULL if node has
  125. * NULL device_type or compatible fields.
  126. */
  127. struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
  128. {
  129. struct vio_dev *viodev;
  130. unsigned int *unit_address;
  131. unsigned int *irq_p;
  132. /* we need the 'device_type' property, in order to match with drivers */
  133. if ((NULL == of_node->type)) {
  134. printk(KERN_WARNING
  135. "%s: node %s missing 'device_type'\n", __FUNCTION__,
  136. of_node->name ? of_node->name : "<unknown>");
  137. return NULL;
  138. }
  139. unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
  140. if (!unit_address) {
  141. printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
  142. of_node->name ? of_node->name : "<unknown>");
  143. return NULL;
  144. }
  145. /* allocate a vio_dev for this node */
  146. viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
  147. if (!viodev) {
  148. return NULL;
  149. }
  150. memset(viodev, 0, sizeof(struct vio_dev));
  151. viodev->dev.platform_data = of_node_get(of_node);
  152. viodev->irq = NO_IRQ;
  153. irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
  154. if (irq_p) {
  155. int virq = virt_irq_create_mapping(*irq_p);
  156. if (virq == NO_IRQ) {
  157. printk(KERN_ERR "Unable to allocate interrupt "
  158. "number for %s\n", of_node->full_name);
  159. } else
  160. viodev->irq = irq_offset_up(virq);
  161. }
  162. snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
  163. viodev->name = of_node->name;
  164. viodev->type = of_node->type;
  165. viodev->unit_address = *unit_address;
  166. viodev->iommu_table = vio_build_iommu_table(viodev);
  167. /* register with generic device framework */
  168. if (vio_register_device(viodev) == NULL) {
  169. /* XXX free TCE table */
  170. kfree(viodev);
  171. return NULL;
  172. }
  173. device_create_file(&viodev->dev, &dev_attr_devspec);
  174. return viodev;
  175. }
  176. EXPORT_SYMBOL(vio_register_device_node);
  177. /**
  178. * vio_get_attribute: - get attribute for virtual device
  179. * @vdev: The vio device to get property.
  180. * @which: The property/attribute to be extracted.
  181. * @length: Pointer to length of returned data size (unused if NULL).
  182. *
  183. * Calls prom.c's get_property() to return the value of the
  184. * attribute specified by the preprocessor constant @which
  185. */
  186. const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
  187. {
  188. return get_property(vdev->dev.platform_data, (char*)which, length);
  189. }
  190. EXPORT_SYMBOL(vio_get_attribute);
  191. /* vio_find_name() - internal because only vio.c knows how we formatted the
  192. * kobject name
  193. * XXX once vio_bus_type.devices is actually used as a kset in
  194. * drivers/base/bus.c, this function should be removed in favor of
  195. * "device_find(kobj_name, &vio_bus_type)"
  196. */
  197. static struct vio_dev *vio_find_name(const char *kobj_name)
  198. {
  199. struct kobject *found;
  200. found = kset_find_obj(&devices_subsys.kset, kobj_name);
  201. if (!found)
  202. return NULL;
  203. return to_vio_dev(container_of(found, struct device, kobj));
  204. }
  205. /**
  206. * vio_find_node - find an already-registered vio_dev
  207. * @vnode: device_node of the virtual device we're looking for
  208. */
  209. struct vio_dev *vio_find_node(struct device_node *vnode)
  210. {
  211. uint32_t *unit_address;
  212. char kobj_name[BUS_ID_SIZE];
  213. /* construct the kobject name from the device node */
  214. unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
  215. if (!unit_address)
  216. return NULL;
  217. snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
  218. return vio_find_name(kobj_name);
  219. }
  220. EXPORT_SYMBOL(vio_find_node);
  221. int vio_enable_interrupts(struct vio_dev *dev)
  222. {
  223. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  224. if (rc != H_SUCCESS)
  225. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  226. return rc;
  227. }
  228. EXPORT_SYMBOL(vio_enable_interrupts);
  229. int vio_disable_interrupts(struct vio_dev *dev)
  230. {
  231. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  232. if (rc != H_SUCCESS)
  233. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  234. return rc;
  235. }
  236. EXPORT_SYMBOL(vio_disable_interrupts);