vio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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))
  56. printk("Virtual Bus VETH TCE table failed.\n");
  57. if (!iommu_init_table(&vio_iommu_table))
  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, "vlan") == 0)
  66. return &veth_iommu_table;
  67. return &vio_iommu_table;
  68. } else
  69. #endif
  70. {
  71. unsigned int *dma_window;
  72. struct iommu_table *newTceTable;
  73. unsigned long offset;
  74. int dma_window_property_size;
  75. dma_window = (unsigned int *)get_property(
  76. dev->dev.platform_data, "ibm,my-dma-window",
  77. &dma_window_property_size);
  78. if (!dma_window)
  79. return NULL;
  80. newTceTable = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  81. /*
  82. * There should be some code to extract the phys-encoded
  83. * offset using prom_n_addr_cells(). However, according to
  84. * a comment on earlier versions, it's always zero, so we
  85. * don't bother
  86. */
  87. offset = dma_window[1] >> PAGE_SHIFT;
  88. /* TCE table size - measured in tce entries */
  89. newTceTable->it_size = dma_window[4] >> PAGE_SHIFT;
  90. /* offset for VIO should always be 0 */
  91. newTceTable->it_offset = offset;
  92. newTceTable->it_busno = 0;
  93. newTceTable->it_index = (unsigned long)dma_window[0];
  94. newTceTable->it_type = TCE_VB;
  95. return iommu_init_table(newTceTable);
  96. }
  97. }
  98. /**
  99. * vio_match_device: - Tell if a VIO device has a matching
  100. * VIO device id structure.
  101. * @ids: array of VIO device id structures to search in
  102. * @dev: the VIO device structure to match against
  103. *
  104. * Used by a driver to check whether a VIO device present in the
  105. * system is in its list of supported devices. Returns the matching
  106. * vio_device_id structure or NULL if there is no match.
  107. */
  108. static const struct vio_device_id *vio_match_device(
  109. const struct vio_device_id *ids, const struct vio_dev *dev)
  110. {
  111. while (ids->type[0] != '\0') {
  112. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  113. device_is_compatible(dev->dev.platform_data, 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. if (dev->platform_data) {
  181. /* XXX free TCE table */
  182. of_node_put(dev->platform_data);
  183. }
  184. kfree(to_vio_dev(dev));
  185. }
  186. /**
  187. * vio_register_device_node: - Register a new vio device.
  188. * @of_node: The OF node for this device.
  189. *
  190. * Creates and initializes a vio_dev structure from the data in
  191. * of_node (dev.platform_data) and adds it to the list of virtual devices.
  192. * Returns a pointer to the created vio_dev or NULL if node has
  193. * NULL device_type or compatible fields.
  194. */
  195. struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
  196. {
  197. struct vio_dev *viodev;
  198. unsigned int *unit_address;
  199. unsigned int *irq_p;
  200. /* we need the 'device_type' property, in order to match with drivers */
  201. if (of_node->type == NULL) {
  202. printk(KERN_WARNING "%s: node %s missing 'device_type'\n",
  203. __FUNCTION__,
  204. of_node->name ? of_node->name : "<unknown>");
  205. return NULL;
  206. }
  207. unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
  208. if (unit_address == NULL) {
  209. printk(KERN_WARNING "%s: node %s missing 'reg'\n",
  210. __FUNCTION__,
  211. of_node->name ? of_node->name : "<unknown>");
  212. return NULL;
  213. }
  214. /* allocate a vio_dev for this node */
  215. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  216. if (viodev == NULL)
  217. return NULL;
  218. viodev->dev.platform_data = of_node_get(of_node);
  219. viodev->irq = NO_IRQ;
  220. irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
  221. if (irq_p) {
  222. int virq = virt_irq_create_mapping(*irq_p);
  223. if (virq == NO_IRQ) {
  224. printk(KERN_ERR "Unable to allocate interrupt "
  225. "number for %s\n", of_node->full_name);
  226. } else
  227. viodev->irq = irq_offset_up(virq);
  228. }
  229. snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
  230. viodev->name = of_node->name;
  231. viodev->type = of_node->type;
  232. viodev->unit_address = *unit_address;
  233. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  234. unit_address = (unsigned int *)get_property(of_node,
  235. "linux,unit_address", NULL);
  236. if (unit_address != NULL)
  237. viodev->unit_address = *unit_address;
  238. }
  239. viodev->iommu_table = vio_build_iommu_table(viodev);
  240. /* init generic 'struct device' fields: */
  241. viodev->dev.parent = &vio_bus_device.dev;
  242. viodev->dev.bus = &vio_bus_type;
  243. viodev->dev.release = vio_dev_release;
  244. /* register with generic device framework */
  245. if (device_register(&viodev->dev)) {
  246. printk(KERN_ERR "%s: failed to register device %s\n",
  247. __FUNCTION__, viodev->dev.bus_id);
  248. /* XXX free TCE table */
  249. kfree(viodev);
  250. return NULL;
  251. }
  252. return viodev;
  253. }
  254. EXPORT_SYMBOL(vio_register_device_node);
  255. /**
  256. * vio_bus_init: - Initialize the virtual IO bus
  257. */
  258. static int __init vio_bus_init(void)
  259. {
  260. int err;
  261. struct device_node *node_vroot;
  262. #ifdef CONFIG_PPC_ISERIES
  263. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  264. iommu_vio_init();
  265. vio_bus_device.iommu_table = &vio_iommu_table;
  266. iSeries_vio_dev = &vio_bus_device.dev;
  267. }
  268. #endif
  269. err = bus_register(&vio_bus_type);
  270. if (err) {
  271. printk(KERN_ERR "failed to register VIO bus\n");
  272. return err;
  273. }
  274. /*
  275. * The fake parent of all vio devices, just to give us
  276. * a nice directory
  277. */
  278. err = device_register(&vio_bus_device.dev);
  279. if (err) {
  280. printk(KERN_WARNING "%s: device_register returned %i\n",
  281. __FUNCTION__, err);
  282. return err;
  283. }
  284. node_vroot = find_devices("vdevice");
  285. if (node_vroot) {
  286. struct device_node *of_node;
  287. /*
  288. * Create struct vio_devices for each virtual device in
  289. * the device tree. Drivers will associate with them later.
  290. */
  291. for (of_node = node_vroot->child; of_node != NULL;
  292. of_node = of_node->sibling) {
  293. printk(KERN_DEBUG "%s: processing %p\n",
  294. __FUNCTION__, of_node);
  295. vio_register_device_node(of_node);
  296. }
  297. }
  298. return 0;
  299. }
  300. __initcall(vio_bus_init);
  301. static ssize_t name_show(struct device *dev,
  302. struct device_attribute *attr, char *buf)
  303. {
  304. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  305. }
  306. static ssize_t devspec_show(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. struct device_node *of_node = dev->platform_data;
  310. return sprintf(buf, "%s\n", of_node ? of_node->full_name : "none");
  311. }
  312. static struct device_attribute vio_dev_attrs[] = {
  313. __ATTR_RO(name),
  314. __ATTR_RO(devspec),
  315. __ATTR_NULL
  316. };
  317. void __devinit vio_unregister_device(struct vio_dev *viodev)
  318. {
  319. device_unregister(&viodev->dev);
  320. }
  321. EXPORT_SYMBOL(vio_unregister_device);
  322. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  323. size_t size, enum dma_data_direction direction)
  324. {
  325. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  326. ~0ul, direction);
  327. }
  328. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  329. size_t size, enum dma_data_direction direction)
  330. {
  331. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  332. direction);
  333. }
  334. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  335. int nelems, enum dma_data_direction direction)
  336. {
  337. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  338. nelems, ~0ul, direction);
  339. }
  340. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  341. int nelems, enum dma_data_direction direction)
  342. {
  343. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  344. }
  345. static void *vio_alloc_coherent(struct device *dev, size_t size,
  346. dma_addr_t *dma_handle, gfp_t flag)
  347. {
  348. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  349. dma_handle, ~0ul, flag);
  350. }
  351. static void vio_free_coherent(struct device *dev, size_t size,
  352. void *vaddr, dma_addr_t dma_handle)
  353. {
  354. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  355. dma_handle);
  356. }
  357. static int vio_dma_supported(struct device *dev, u64 mask)
  358. {
  359. return 1;
  360. }
  361. struct dma_mapping_ops vio_dma_ops = {
  362. .alloc_coherent = vio_alloc_coherent,
  363. .free_coherent = vio_free_coherent,
  364. .map_single = vio_map_single,
  365. .unmap_single = vio_unmap_single,
  366. .map_sg = vio_map_sg,
  367. .unmap_sg = vio_unmap_sg,
  368. .dma_supported = vio_dma_supported,
  369. };
  370. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  371. {
  372. const struct vio_dev *vio_dev = to_vio_dev(dev);
  373. struct vio_driver *vio_drv = to_vio_driver(drv);
  374. const struct vio_device_id *ids = vio_drv->id_table;
  375. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  376. }
  377. static int vio_hotplug(struct device *dev, char **envp, int num_envp,
  378. char *buffer, int buffer_size)
  379. {
  380. const struct vio_dev *vio_dev = to_vio_dev(dev);
  381. struct device_node *dn = dev->platform_data;
  382. char *cp;
  383. int length;
  384. if (!num_envp)
  385. return -ENOMEM;
  386. if (!dn)
  387. return -ENODEV;
  388. cp = (char *)get_property(dn, "compatible", &length);
  389. if (!cp)
  390. return -ENODEV;
  391. envp[0] = buffer;
  392. length = scnprintf(buffer, buffer_size, "MODALIAS=vio:T%sS%s",
  393. vio_dev->type, cp);
  394. if ((buffer_size - length) <= 0)
  395. return -ENOMEM;
  396. envp[1] = NULL;
  397. return 0;
  398. }
  399. struct bus_type vio_bus_type = {
  400. .name = "vio",
  401. .dev_attrs = vio_dev_attrs,
  402. .uevent = vio_hotplug,
  403. .match = vio_bus_match,
  404. .probe = vio_bus_probe,
  405. .remove = vio_bus_remove,
  406. .shutdown = vio_bus_shutdown,
  407. };
  408. /**
  409. * vio_get_attribute: - get attribute for virtual device
  410. * @vdev: The vio device to get property.
  411. * @which: The property/attribute to be extracted.
  412. * @length: Pointer to length of returned data size (unused if NULL).
  413. *
  414. * Calls prom.c's get_property() to return the value of the
  415. * attribute specified by @which
  416. */
  417. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  418. {
  419. return get_property(vdev->dev.platform_data, which, length);
  420. }
  421. EXPORT_SYMBOL(vio_get_attribute);
  422. #ifdef CONFIG_PPC_PSERIES
  423. /* vio_find_name() - internal because only vio.c knows how we formatted the
  424. * kobject name
  425. * XXX once vio_bus_type.devices is actually used as a kset in
  426. * drivers/base/bus.c, this function should be removed in favor of
  427. * "device_find(kobj_name, &vio_bus_type)"
  428. */
  429. static struct vio_dev *vio_find_name(const char *kobj_name)
  430. {
  431. struct kobject *found;
  432. found = kset_find_obj(&devices_subsys.kset, kobj_name);
  433. if (!found)
  434. return NULL;
  435. return to_vio_dev(container_of(found, struct device, kobj));
  436. }
  437. /**
  438. * vio_find_node - find an already-registered vio_dev
  439. * @vnode: device_node of the virtual device we're looking for
  440. */
  441. struct vio_dev *vio_find_node(struct device_node *vnode)
  442. {
  443. uint32_t *unit_address;
  444. char kobj_name[BUS_ID_SIZE];
  445. /* construct the kobject name from the device node */
  446. unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
  447. if (!unit_address)
  448. return NULL;
  449. snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
  450. return vio_find_name(kobj_name);
  451. }
  452. EXPORT_SYMBOL(vio_find_node);
  453. int vio_enable_interrupts(struct vio_dev *dev)
  454. {
  455. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  456. if (rc != H_SUCCESS)
  457. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  458. return rc;
  459. }
  460. EXPORT_SYMBOL(vio_enable_interrupts);
  461. int vio_disable_interrupts(struct vio_dev *dev)
  462. {
  463. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  464. if (rc != H_SUCCESS)
  465. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  466. return rc;
  467. }
  468. EXPORT_SYMBOL(vio_disable_interrupts);
  469. #endif /* CONFIG_PPC_PSERIES */