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