pSeries_vio.c 7.6 KB

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