pSeries_vio.c 7.8 KB

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