pSeries_vio.c 7.7 KB

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