vio.c 12 KB

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