vio.c 11 KB

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