vio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * IBM PowerPC Virtual I/O Infrastructure Support.
  3. *
  4. * Copyright (c) 2003 IBM Corp.
  5. * Dave Engebretsen engebret@us.ibm.com
  6. * Santiago Leon santil@us.ibm.com
  7. * Hollis Blanchard <hollisb@us.ibm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/console.h>
  16. #include <linux/version.h>
  17. #include <linux/module.h>
  18. #include <linux/kobject.h>
  19. #include <linux/mm.h>
  20. #include <linux/dma-mapping.h>
  21. #include <asm/rtas.h>
  22. #include <asm/iommu.h>
  23. #include <asm/dma.h>
  24. #include <asm/ppcdebug.h>
  25. #include <asm/vio.h>
  26. #include <asm/hvcall.h>
  27. #define DBGENTER() pr_debug("%s entered\n", __FUNCTION__)
  28. extern struct subsystem devices_subsys; /* needed for vio_find_name() */
  29. static const struct vio_device_id *vio_match_device(
  30. const struct vio_device_id *, const struct vio_dev *);
  31. #ifdef CONFIG_PPC_PSERIES
  32. static struct iommu_table *vio_build_iommu_table(struct vio_dev *);
  33. static int vio_num_address_cells;
  34. #endif
  35. struct vio_dev vio_bus_device = { /* fake "parent" device */
  36. .name = vio_bus_device.dev.bus_id,
  37. .type = "",
  38. .dev.bus_id = "vio",
  39. .dev.bus = &vio_bus_type,
  40. };
  41. static int (*is_match)(const struct vio_device_id *id,
  42. const struct vio_dev *dev);
  43. /* convert from struct device to struct vio_dev and pass to driver.
  44. * dev->driver has already been set by generic code because vio_bus_match
  45. * succeeded. */
  46. static int vio_bus_probe(struct device *dev)
  47. {
  48. struct vio_dev *viodev = to_vio_dev(dev);
  49. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  50. const struct vio_device_id *id;
  51. int error = -ENODEV;
  52. DBGENTER();
  53. if (!viodrv->probe)
  54. return error;
  55. id = vio_match_device(viodrv->id_table, viodev);
  56. if (id) {
  57. error = viodrv->probe(viodev, id);
  58. }
  59. return error;
  60. }
  61. /* convert from struct device to struct vio_dev and pass to driver. */
  62. static int vio_bus_remove(struct device *dev)
  63. {
  64. struct vio_dev *viodev = to_vio_dev(dev);
  65. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  66. DBGENTER();
  67. if (viodrv->remove) {
  68. return viodrv->remove(viodev);
  69. }
  70. /* driver can't remove */
  71. return 1;
  72. }
  73. /**
  74. * vio_register_driver: - Register a new vio driver
  75. * @drv: The vio_driver structure to be registered.
  76. */
  77. int vio_register_driver(struct vio_driver *viodrv)
  78. {
  79. printk(KERN_DEBUG "%s: driver %s registering\n", __FUNCTION__,
  80. viodrv->name);
  81. /* fill in 'struct driver' fields */
  82. viodrv->driver.name = viodrv->name;
  83. viodrv->driver.bus = &vio_bus_type;
  84. viodrv->driver.probe = vio_bus_probe;
  85. viodrv->driver.remove = vio_bus_remove;
  86. return driver_register(&viodrv->driver);
  87. }
  88. EXPORT_SYMBOL(vio_register_driver);
  89. /**
  90. * vio_unregister_driver - Remove registration of vio driver.
  91. * @driver: The vio_driver struct to be removed form registration
  92. */
  93. void vio_unregister_driver(struct vio_driver *viodrv)
  94. {
  95. driver_unregister(&viodrv->driver);
  96. }
  97. EXPORT_SYMBOL(vio_unregister_driver);
  98. /**
  99. * vio_match_device: - Tell if a VIO device has a matching 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(const struct vio_device_id *ids,
  108. const struct vio_dev *dev)
  109. {
  110. DBGENTER();
  111. while (ids->type) {
  112. if (is_match(ids, dev))
  113. return ids;
  114. ids++;
  115. }
  116. return NULL;
  117. }
  118. #ifdef CONFIG_PPC_PSERIES
  119. static void probe_bus_pseries(void)
  120. {
  121. struct device_node *node_vroot, *of_node;
  122. node_vroot = find_devices("vdevice");
  123. if ((node_vroot == NULL) || (node_vroot->child == NULL))
  124. /* this machine doesn't do virtual IO, and that's ok */
  125. return;
  126. vio_num_address_cells = prom_n_addr_cells(node_vroot->child);
  127. /*
  128. * Create struct vio_devices for each virtual device in the device tree.
  129. * Drivers will associate with them later.
  130. */
  131. for (of_node = node_vroot->child; of_node != NULL;
  132. of_node = of_node->sibling) {
  133. printk(KERN_DEBUG "%s: processing %p\n", __FUNCTION__, of_node);
  134. vio_register_device_node(of_node);
  135. }
  136. }
  137. #endif
  138. /**
  139. * vio_bus_init: - Initialize the virtual IO bus
  140. */
  141. int __init vio_bus_init(int (*match_func)(const struct vio_device_id *id,
  142. const struct vio_dev *dev))
  143. {
  144. int err;
  145. is_match = match_func;
  146. err = bus_register(&vio_bus_type);
  147. if (err) {
  148. printk(KERN_ERR "failed to register VIO bus\n");
  149. return err;
  150. }
  151. /* the fake parent of all vio devices, just to give us
  152. * a nice directory
  153. */
  154. err = device_register(&vio_bus_device.dev);
  155. if (err) {
  156. printk(KERN_WARNING "%s: device_register returned %i\n",
  157. __FUNCTION__, err);
  158. return err;
  159. }
  160. return 0;
  161. }
  162. #ifdef CONFIG_PPC_PSERIES
  163. /**
  164. * vio_match_device_pseries: - Tell if a pSeries VIO device matches a
  165. * vio_device_id
  166. */
  167. static int vio_match_device_pseries(const struct vio_device_id *id,
  168. const struct vio_dev *dev)
  169. {
  170. return (strncmp(dev->type, id->type, strlen(id->type)) == 0) &&
  171. device_is_compatible(dev->dev.platform_data, id->compat);
  172. }
  173. /**
  174. * vio_bus_init_pseries: - Initialize the pSeries virtual IO bus
  175. */
  176. static int __init vio_bus_init_pseries(void)
  177. {
  178. int err;
  179. err = vio_bus_init(vio_match_device_pseries);
  180. if (err == 0)
  181. probe_bus_pseries();
  182. return err;
  183. }
  184. __initcall(vio_bus_init_pseries);
  185. #endif
  186. /* vio_dev refcount hit 0 */
  187. static void __devinit vio_dev_release(struct device *dev)
  188. {
  189. DBGENTER();
  190. #ifdef CONFIG_PPC_PSERIES
  191. /* XXX free TCE table */
  192. of_node_put(dev->platform_data);
  193. #endif
  194. kfree(to_vio_dev(dev));
  195. }
  196. #ifdef CONFIG_PPC_PSERIES
  197. static ssize_t viodev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  198. {
  199. struct device_node *of_node = dev->platform_data;
  200. return sprintf(buf, "%s\n", of_node->full_name);
  201. }
  202. DEVICE_ATTR(devspec, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_devspec, NULL);
  203. #endif
  204. static ssize_t viodev_show_name(struct device *dev, struct device_attribute *attr, char *buf)
  205. {
  206. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  207. }
  208. DEVICE_ATTR(name, S_IRUSR | S_IRGRP | S_IROTH, viodev_show_name, NULL);
  209. struct vio_dev * __devinit vio_register_device_common(
  210. struct vio_dev *viodev, char *name, char *type,
  211. uint32_t unit_address, struct iommu_table *iommu_table)
  212. {
  213. DBGENTER();
  214. viodev->name = name;
  215. viodev->type = type;
  216. viodev->unit_address = unit_address;
  217. viodev->iommu_table = iommu_table;
  218. /* init generic 'struct device' fields: */
  219. viodev->dev.parent = &vio_bus_device.dev;
  220. viodev->dev.bus = &vio_bus_type;
  221. viodev->dev.release = vio_dev_release;
  222. /* register with generic device framework */
  223. if (device_register(&viodev->dev)) {
  224. printk(KERN_ERR "%s: failed to register device %s\n",
  225. __FUNCTION__, viodev->dev.bus_id);
  226. return NULL;
  227. }
  228. device_create_file(&viodev->dev, &dev_attr_name);
  229. return viodev;
  230. }
  231. #ifdef CONFIG_PPC_PSERIES
  232. /**
  233. * vio_register_device_node: - Register a new vio device.
  234. * @of_node: The OF node for this device.
  235. *
  236. * Creates and initializes a vio_dev structure from the data in
  237. * of_node (dev.platform_data) and adds it to the list of virtual devices.
  238. * Returns a pointer to the created vio_dev or NULL if node has
  239. * NULL device_type or compatible fields.
  240. */
  241. struct vio_dev * __devinit vio_register_device_node(struct device_node *of_node)
  242. {
  243. struct vio_dev *viodev;
  244. unsigned int *unit_address;
  245. unsigned int *irq_p;
  246. DBGENTER();
  247. /* we need the 'device_type' property, in order to match with drivers */
  248. if ((NULL == of_node->type)) {
  249. printk(KERN_WARNING
  250. "%s: node %s missing 'device_type'\n", __FUNCTION__,
  251. of_node->name ? of_node->name : "<unknown>");
  252. return NULL;
  253. }
  254. unit_address = (unsigned int *)get_property(of_node, "reg", NULL);
  255. if (!unit_address) {
  256. printk(KERN_WARNING "%s: node %s missing 'reg'\n", __FUNCTION__,
  257. of_node->name ? of_node->name : "<unknown>");
  258. return NULL;
  259. }
  260. /* allocate a vio_dev for this node */
  261. viodev = kmalloc(sizeof(struct vio_dev), GFP_KERNEL);
  262. if (!viodev) {
  263. return NULL;
  264. }
  265. memset(viodev, 0, sizeof(struct vio_dev));
  266. viodev->dev.platform_data = of_node_get(of_node);
  267. viodev->irq = NO_IRQ;
  268. irq_p = (unsigned int *)get_property(of_node, "interrupts", NULL);
  269. if (irq_p) {
  270. int virq = virt_irq_create_mapping(*irq_p);
  271. if (virq == NO_IRQ) {
  272. printk(KERN_ERR "Unable to allocate interrupt "
  273. "number for %s\n", of_node->full_name);
  274. } else
  275. viodev->irq = irq_offset_up(virq);
  276. }
  277. snprintf(viodev->dev.bus_id, BUS_ID_SIZE, "%x", *unit_address);
  278. /* register with generic device framework */
  279. if (vio_register_device_common(viodev, of_node->name, of_node->type,
  280. *unit_address, vio_build_iommu_table(viodev))
  281. == NULL) {
  282. /* XXX free TCE table */
  283. kfree(viodev);
  284. return NULL;
  285. }
  286. device_create_file(&viodev->dev, &dev_attr_devspec);
  287. return viodev;
  288. }
  289. EXPORT_SYMBOL(vio_register_device_node);
  290. #endif
  291. void __devinit vio_unregister_device(struct vio_dev *viodev)
  292. {
  293. DBGENTER();
  294. #ifdef CONFIG_PPC_PSERIES
  295. device_remove_file(&viodev->dev, &dev_attr_devspec);
  296. #endif
  297. device_remove_file(&viodev->dev, &dev_attr_name);
  298. device_unregister(&viodev->dev);
  299. }
  300. EXPORT_SYMBOL(vio_unregister_device);
  301. #ifdef CONFIG_PPC_PSERIES
  302. /**
  303. * vio_get_attribute: - get attribute for virtual device
  304. * @vdev: The vio device to get property.
  305. * @which: The property/attribute to be extracted.
  306. * @length: Pointer to length of returned data size (unused if NULL).
  307. *
  308. * Calls prom.c's get_property() to return the value of the
  309. * attribute specified by the preprocessor constant @which
  310. */
  311. const void * vio_get_attribute(struct vio_dev *vdev, void* which, int* length)
  312. {
  313. return get_property(vdev->dev.platform_data, (char*)which, length);
  314. }
  315. EXPORT_SYMBOL(vio_get_attribute);
  316. /* vio_find_name() - internal because only vio.c knows how we formatted the
  317. * kobject name
  318. * XXX once vio_bus_type.devices is actually used as a kset in
  319. * drivers/base/bus.c, this function should be removed in favor of
  320. * "device_find(kobj_name, &vio_bus_type)"
  321. */
  322. static struct vio_dev *vio_find_name(const char *kobj_name)
  323. {
  324. struct kobject *found;
  325. found = kset_find_obj(&devices_subsys.kset, kobj_name);
  326. if (!found)
  327. return NULL;
  328. return to_vio_dev(container_of(found, struct device, kobj));
  329. }
  330. /**
  331. * vio_find_node - find an already-registered vio_dev
  332. * @vnode: device_node of the virtual device we're looking for
  333. */
  334. struct vio_dev *vio_find_node(struct device_node *vnode)
  335. {
  336. uint32_t *unit_address;
  337. char kobj_name[BUS_ID_SIZE];
  338. /* construct the kobject name from the device node */
  339. unit_address = (uint32_t *)get_property(vnode, "reg", NULL);
  340. if (!unit_address)
  341. return NULL;
  342. snprintf(kobj_name, BUS_ID_SIZE, "%x", *unit_address);
  343. return vio_find_name(kobj_name);
  344. }
  345. EXPORT_SYMBOL(vio_find_node);
  346. /**
  347. * vio_build_iommu_table: - gets the dma information from OF and builds the TCE tree.
  348. * @dev: the virtual device.
  349. *
  350. * Returns a pointer to the built tce tree, or NULL if it can't
  351. * find property.
  352. */
  353. static struct iommu_table * vio_build_iommu_table(struct vio_dev *dev)
  354. {
  355. unsigned int *dma_window;
  356. struct iommu_table *newTceTable;
  357. unsigned long offset;
  358. int dma_window_property_size;
  359. dma_window = (unsigned int *) get_property(dev->dev.platform_data, "ibm,my-dma-window", &dma_window_property_size);
  360. if(!dma_window) {
  361. return NULL;
  362. }
  363. newTceTable = (struct iommu_table *) kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  364. /* There should be some code to extract the phys-encoded offset
  365. using prom_n_addr_cells(). However, according to a comment
  366. on earlier versions, it's always zero, so we don't bother */
  367. offset = dma_window[1] >> PAGE_SHIFT;
  368. /* TCE table size - measured in tce entries */
  369. newTceTable->it_size = dma_window[4] >> PAGE_SHIFT;
  370. /* offset for VIO should always be 0 */
  371. newTceTable->it_offset = offset;
  372. newTceTable->it_busno = 0;
  373. newTceTable->it_index = (unsigned long)dma_window[0];
  374. newTceTable->it_type = TCE_VB;
  375. return iommu_init_table(newTceTable);
  376. }
  377. int vio_enable_interrupts(struct vio_dev *dev)
  378. {
  379. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  380. if (rc != H_Success) {
  381. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  382. }
  383. return rc;
  384. }
  385. EXPORT_SYMBOL(vio_enable_interrupts);
  386. int vio_disable_interrupts(struct vio_dev *dev)
  387. {
  388. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  389. if (rc != H_Success) {
  390. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  391. }
  392. return rc;
  393. }
  394. EXPORT_SYMBOL(vio_disable_interrupts);
  395. #endif
  396. static dma_addr_t vio_map_single(struct device *dev, void *vaddr,
  397. size_t size, enum dma_data_direction direction)
  398. {
  399. return iommu_map_single(to_vio_dev(dev)->iommu_table, vaddr, size,
  400. direction);
  401. }
  402. static void vio_unmap_single(struct device *dev, dma_addr_t dma_handle,
  403. size_t size, enum dma_data_direction direction)
  404. {
  405. iommu_unmap_single(to_vio_dev(dev)->iommu_table, dma_handle, size,
  406. direction);
  407. }
  408. static int vio_map_sg(struct device *dev, struct scatterlist *sglist,
  409. int nelems, enum dma_data_direction direction)
  410. {
  411. return iommu_map_sg(dev, to_vio_dev(dev)->iommu_table, sglist,
  412. nelems, direction);
  413. }
  414. static void vio_unmap_sg(struct device *dev, struct scatterlist *sglist,
  415. int nelems, enum dma_data_direction direction)
  416. {
  417. iommu_unmap_sg(to_vio_dev(dev)->iommu_table, sglist, nelems, direction);
  418. }
  419. static void *vio_alloc_coherent(struct device *dev, size_t size,
  420. dma_addr_t *dma_handle, unsigned int __nocast flag)
  421. {
  422. return iommu_alloc_coherent(to_vio_dev(dev)->iommu_table, size,
  423. dma_handle, flag);
  424. }
  425. static void vio_free_coherent(struct device *dev, size_t size,
  426. void *vaddr, dma_addr_t dma_handle)
  427. {
  428. iommu_free_coherent(to_vio_dev(dev)->iommu_table, size, vaddr,
  429. dma_handle);
  430. }
  431. static int vio_dma_supported(struct device *dev, u64 mask)
  432. {
  433. return 1;
  434. }
  435. struct dma_mapping_ops vio_dma_ops = {
  436. .alloc_coherent = vio_alloc_coherent,
  437. .free_coherent = vio_free_coherent,
  438. .map_single = vio_map_single,
  439. .unmap_single = vio_unmap_single,
  440. .map_sg = vio_map_sg,
  441. .unmap_sg = vio_unmap_sg,
  442. .dma_supported = vio_dma_supported,
  443. };
  444. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  445. {
  446. const struct vio_dev *vio_dev = to_vio_dev(dev);
  447. struct vio_driver *vio_drv = to_vio_driver(drv);
  448. const struct vio_device_id *ids = vio_drv->id_table;
  449. const struct vio_device_id *found_id;
  450. DBGENTER();
  451. if (!ids)
  452. return 0;
  453. found_id = vio_match_device(ids, vio_dev);
  454. if (found_id)
  455. return 1;
  456. return 0;
  457. }
  458. struct bus_type vio_bus_type = {
  459. .name = "vio",
  460. .match = vio_bus_match,
  461. };