vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/types.h>
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/console.h>
  19. #include <linux/module.h>
  20. #include <linux/mm.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/kobject.h>
  23. #include <asm/iommu.h>
  24. #include <asm/dma.h>
  25. #include <asm/vio.h>
  26. #include <asm/prom.h>
  27. #include <asm/firmware.h>
  28. #include <asm/tce.h>
  29. #include <asm/abs_addr.h>
  30. #include <asm/page.h>
  31. #include <asm/hvcall.h>
  32. #include <asm/iseries/vio.h>
  33. #include <asm/iseries/hv_types.h>
  34. #include <asm/iseries/hv_lp_config.h>
  35. #include <asm/iseries/hv_call_xm.h>
  36. #include <asm/iseries/iommu.h>
  37. static struct bus_type vio_bus_type;
  38. static struct vio_dev vio_bus_device = { /* fake "parent" device */
  39. .name = vio_bus_device.dev.bus_id,
  40. .type = "",
  41. .dev.bus_id = "vio",
  42. .dev.bus = &vio_bus_type,
  43. };
  44. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  45. {
  46. const unsigned char *dma_window;
  47. struct iommu_table *tbl;
  48. unsigned long offset, size;
  49. if (firmware_has_feature(FW_FEATURE_ISERIES))
  50. return vio_build_iommu_table_iseries(dev);
  51. dma_window = of_get_property(dev->dev.archdata.of_node,
  52. "ibm,my-dma-window", NULL);
  53. if (!dma_window)
  54. return NULL;
  55. tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
  56. of_parse_dma_window(dev->dev.archdata.of_node, dma_window,
  57. &tbl->it_index, &offset, &size);
  58. /* TCE table size - measured in tce entries */
  59. tbl->it_size = size >> IOMMU_PAGE_SHIFT;
  60. /* offset for VIO should always be 0 */
  61. tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
  62. tbl->it_busno = 0;
  63. tbl->it_type = TCE_VB;
  64. return iommu_init_table(tbl, -1);
  65. }
  66. /**
  67. * vio_match_device: - Tell if a VIO device has a matching
  68. * VIO device id structure.
  69. * @ids: array of VIO device id structures to search in
  70. * @dev: the VIO device structure to match against
  71. *
  72. * Used by a driver to check whether a VIO device present in the
  73. * system is in its list of supported devices. Returns the matching
  74. * vio_device_id structure or NULL if there is no match.
  75. */
  76. static const struct vio_device_id *vio_match_device(
  77. const struct vio_device_id *ids, const struct vio_dev *dev)
  78. {
  79. while (ids->type[0] != '\0') {
  80. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  81. of_device_is_compatible(dev->dev.archdata.of_node,
  82. ids->compat))
  83. return ids;
  84. ids++;
  85. }
  86. return NULL;
  87. }
  88. /*
  89. * Convert from struct device to struct vio_dev and pass to driver.
  90. * dev->driver has already been set by generic code because vio_bus_match
  91. * succeeded.
  92. */
  93. static int vio_bus_probe(struct device *dev)
  94. {
  95. struct vio_dev *viodev = to_vio_dev(dev);
  96. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  97. const struct vio_device_id *id;
  98. int error = -ENODEV;
  99. if (!viodrv->probe)
  100. return error;
  101. id = vio_match_device(viodrv->id_table, viodev);
  102. if (id)
  103. error = viodrv->probe(viodev, id);
  104. return error;
  105. }
  106. /* convert from struct device to struct vio_dev and pass to driver. */
  107. static int vio_bus_remove(struct device *dev)
  108. {
  109. struct vio_dev *viodev = to_vio_dev(dev);
  110. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  111. if (viodrv->remove)
  112. return viodrv->remove(viodev);
  113. /* driver can't remove */
  114. return 1;
  115. }
  116. /**
  117. * vio_register_driver: - Register a new vio driver
  118. * @drv: The vio_driver structure to be registered.
  119. */
  120. int vio_register_driver(struct vio_driver *viodrv)
  121. {
  122. printk(KERN_DEBUG "%s: driver %s registering\n", __func__,
  123. viodrv->driver.name);
  124. /* fill in 'struct driver' fields */
  125. viodrv->driver.bus = &vio_bus_type;
  126. return driver_register(&viodrv->driver);
  127. }
  128. EXPORT_SYMBOL(vio_register_driver);
  129. /**
  130. * vio_unregister_driver - Remove registration of vio driver.
  131. * @driver: The vio_driver struct to be removed form registration
  132. */
  133. void vio_unregister_driver(struct vio_driver *viodrv)
  134. {
  135. driver_unregister(&viodrv->driver);
  136. }
  137. EXPORT_SYMBOL(vio_unregister_driver);
  138. /* vio_dev refcount hit 0 */
  139. static void __devinit vio_dev_release(struct device *dev)
  140. {
  141. /* XXX should free TCE table */
  142. of_node_put(dev->archdata.of_node);
  143. kfree(to_vio_dev(dev));
  144. }
  145. /**
  146. * vio_register_device_node: - Register a new vio device.
  147. * @of_node: The OF node for this device.
  148. *
  149. * Creates and initializes a vio_dev structure from the data in
  150. * of_node and adds it to the list of virtual devices.
  151. * Returns a pointer to the created vio_dev or NULL if node has
  152. * NULL device_type or compatible fields.
  153. */
  154. struct vio_dev *vio_register_device_node(struct device_node *of_node)
  155. {
  156. struct vio_dev *viodev;
  157. const unsigned int *unit_address;
  158. /* we need the 'device_type' property, in order to match with drivers */
  159. if (of_node->type == NULL) {
  160. printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
  161. __func__,
  162. of_node->name ? of_node->name : "<unknown>");
  163. return NULL;
  164. }
  165. unit_address = of_get_property(of_node, "reg", NULL);
  166. if (unit_address == NULL) {
  167. printk(KERN_WARNING "%s: node %s missing 'reg'\n",
  168. __func__,
  169. of_node->name ? of_node->name : "<unknown>");
  170. return NULL;
  171. }
  172. /* allocate a vio_dev for this node */
  173. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  174. if (viodev == NULL)
  175. return NULL;
  176. viodev->irq = irq_of_parse_and_map(of_node, 0);
  177. snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
  178. viodev->name = of_node->name;
  179. viodev->type = of_node->type;
  180. viodev->unit_address = *unit_address;
  181. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  182. unit_address = of_get_property(of_node,
  183. "linux,unit_address", NULL);
  184. if (unit_address != NULL)
  185. viodev->unit_address = *unit_address;
  186. }
  187. viodev->dev.archdata.of_node = of_node_get(of_node);
  188. viodev->dev.archdata.dma_ops = &dma_iommu_ops;
  189. viodev->dev.archdata.dma_data = vio_build_iommu_table(viodev);
  190. viodev->dev.archdata.numa_node = of_node_to_nid(of_node);
  191. /* init generic 'struct device' fields: */
  192. viodev->dev.parent = &vio_bus_device.dev;
  193. viodev->dev.bus = &vio_bus_type;
  194. viodev->dev.release = vio_dev_release;
  195. /* register with generic device framework */
  196. if (device_register(&viodev->dev)) {
  197. printk(KERN_ERR "%s: failed to register device %s\n",
  198. __func__, viodev->dev.bus_id);
  199. /* XXX free TCE table */
  200. kfree(viodev);
  201. return NULL;
  202. }
  203. return viodev;
  204. }
  205. EXPORT_SYMBOL(vio_register_device_node);
  206. /**
  207. * vio_bus_init: - Initialize the virtual IO bus
  208. */
  209. static int __init vio_bus_init(void)
  210. {
  211. int err;
  212. struct device_node *node_vroot;
  213. err = bus_register(&vio_bus_type);
  214. if (err) {
  215. printk(KERN_ERR "failed to register VIO bus\n");
  216. return err;
  217. }
  218. /*
  219. * The fake parent of all vio devices, just to give us
  220. * a nice directory
  221. */
  222. err = device_register(&vio_bus_device.dev);
  223. if (err) {
  224. printk(KERN_WARNING "%s: device_register returned %i\n",
  225. __func__, err);
  226. return err;
  227. }
  228. node_vroot = of_find_node_by_name(NULL, "vdevice");
  229. if (node_vroot) {
  230. struct device_node *of_node;
  231. /*
  232. * Create struct vio_devices for each virtual device in
  233. * the device tree. Drivers will associate with them later.
  234. */
  235. for (of_node = node_vroot->child; of_node != NULL;
  236. of_node = of_node->sibling)
  237. vio_register_device_node(of_node);
  238. of_node_put(node_vroot);
  239. }
  240. return 0;
  241. }
  242. __initcall(vio_bus_init);
  243. static ssize_t name_show(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  247. }
  248. static ssize_t devspec_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct device_node *of_node = dev->archdata.of_node;
  252. return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
  253. }
  254. static struct device_attribute vio_dev_attrs[] = {
  255. __ATTR_RO(name),
  256. __ATTR_RO(devspec),
  257. __ATTR_NULL
  258. };
  259. void __devinit vio_unregister_device(struct vio_dev *viodev)
  260. {
  261. device_unregister(&viodev->dev);
  262. }
  263. EXPORT_SYMBOL(vio_unregister_device);
  264. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  265. {
  266. const struct vio_dev *vio_dev = to_vio_dev(dev);
  267. struct vio_driver *vio_drv = to_vio_driver(drv);
  268. const struct vio_device_id *ids = vio_drv->id_table;
  269. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  270. }
  271. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  272. {
  273. const struct vio_dev *vio_dev = to_vio_dev(dev);
  274. struct device_node *dn;
  275. const char *cp;
  276. dn = dev->archdata.of_node;
  277. if (!dn)
  278. return -ENODEV;
  279. cp = of_get_property(dn, "compatible", NULL);
  280. if (!cp)
  281. return -ENODEV;
  282. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
  283. return 0;
  284. }
  285. static struct bus_type vio_bus_type = {
  286. .name = "vio",
  287. .dev_attrs = vio_dev_attrs,
  288. .uevent = vio_hotplug,
  289. .match = vio_bus_match,
  290. .probe = vio_bus_probe,
  291. .remove = vio_bus_remove,
  292. };
  293. /**
  294. * vio_get_attribute: - get attribute for virtual device
  295. * @vdev: The vio device to get property.
  296. * @which: The property/attribute to be extracted.
  297. * @length: Pointer to length of returned data size (unused if NULL).
  298. *
  299. * Calls prom.c's of_get_property() to return the value of the
  300. * attribute specified by @which
  301. */
  302. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  303. {
  304. return of_get_property(vdev->dev.archdata.of_node, which, length);
  305. }
  306. EXPORT_SYMBOL(vio_get_attribute);
  307. #ifdef CONFIG_PPC_PSERIES
  308. /* vio_find_name() - internal because only vio.c knows how we formatted the
  309. * kobject name
  310. */
  311. static struct vio_dev *vio_find_name(const char *name)
  312. {
  313. struct device *found;
  314. found = bus_find_device_by_name(&vio_bus_type, NULL, name);
  315. if (!found)
  316. return NULL;
  317. return to_vio_dev(found);
  318. }
  319. /**
  320. * vio_find_node - find an already-registered vio_dev
  321. * @vnode: device_node of the virtual device we're looking for
  322. */
  323. struct vio_dev *vio_find_node(struct device_node *vnode)
  324. {
  325. const uint32_t *unit_address;
  326. char kobj_name[BUS_ID_SIZE];
  327. /* construct the kobject name from the device node */
  328. unit_address = of_get_property(vnode, "reg", NULL);
  329. if (!unit_address)
  330. return NULL;
  331. snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
  332. return vio_find_name(kobj_name);
  333. }
  334. EXPORT_SYMBOL(vio_find_node);
  335. int vio_enable_interrupts(struct vio_dev *dev)
  336. {
  337. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  338. if (rc != H_SUCCESS)
  339. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  340. return rc;
  341. }
  342. EXPORT_SYMBOL(vio_enable_interrupts);
  343. int vio_disable_interrupts(struct vio_dev *dev)
  344. {
  345. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  346. if (rc != H_SUCCESS)
  347. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  348. return rc;
  349. }
  350. EXPORT_SYMBOL(vio_disable_interrupts);
  351. #endif /* CONFIG_PPC_PSERIES */