uio_dmem_genirq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * drivers/uio/uio_dmem_genirq.c
  3. *
  4. * Userspace I/O platform driver with generic IRQ handling code.
  5. *
  6. * Copyright (C) 2012 Damian Hobson-Garcia
  7. *
  8. * Based on uio_pdrv_genirq.c by Magnus Damm
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/uio_driver.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/bitops.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_data/uio_dmem_genirq.h>
  21. #include <linux/stringify.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_address.h>
  28. #define DRIVER_NAME "uio_dmem_genirq"
  29. #define DMEM_MAP_ERROR (~0)
  30. struct uio_dmem_genirq_platdata {
  31. struct uio_info *uioinfo;
  32. spinlock_t lock;
  33. unsigned long flags;
  34. struct platform_device *pdev;
  35. unsigned int dmem_region_start;
  36. unsigned int num_dmem_regions;
  37. void *dmem_region_vaddr[MAX_UIO_MAPS];
  38. struct mutex alloc_lock;
  39. unsigned int refcnt;
  40. };
  41. static int uio_dmem_genirq_open(struct uio_info *info, struct inode *inode)
  42. {
  43. struct uio_dmem_genirq_platdata *priv = info->priv;
  44. struct uio_mem *uiomem;
  45. int ret = 0;
  46. int dmem_region = priv->dmem_region_start;
  47. uiomem = &priv->uioinfo->mem[priv->dmem_region_start];
  48. mutex_lock(&priv->alloc_lock);
  49. while (!priv->refcnt && uiomem < &priv->uioinfo->mem[MAX_UIO_MAPS]) {
  50. void *addr;
  51. if (!uiomem->size)
  52. break;
  53. addr = dma_alloc_coherent(&priv->pdev->dev, uiomem->size,
  54. (dma_addr_t *)&uiomem->addr, GFP_KERNEL);
  55. if (!addr) {
  56. uiomem->addr = DMEM_MAP_ERROR;
  57. }
  58. priv->dmem_region_vaddr[dmem_region++] = addr;
  59. ++uiomem;
  60. }
  61. priv->refcnt++;
  62. mutex_unlock(&priv->alloc_lock);
  63. /* Wait until the Runtime PM code has woken up the device */
  64. pm_runtime_get_sync(&priv->pdev->dev);
  65. return ret;
  66. }
  67. static int uio_dmem_genirq_release(struct uio_info *info, struct inode *inode)
  68. {
  69. struct uio_dmem_genirq_platdata *priv = info->priv;
  70. struct uio_mem *uiomem;
  71. int dmem_region = priv->dmem_region_start;
  72. /* Tell the Runtime PM code that the device has become idle */
  73. pm_runtime_put_sync(&priv->pdev->dev);
  74. uiomem = &priv->uioinfo->mem[priv->dmem_region_start];
  75. mutex_lock(&priv->alloc_lock);
  76. priv->refcnt--;
  77. while (!priv->refcnt && uiomem < &priv->uioinfo->mem[MAX_UIO_MAPS]) {
  78. if (!uiomem->size)
  79. break;
  80. if (priv->dmem_region_vaddr[dmem_region]) {
  81. dma_free_coherent(&priv->pdev->dev, uiomem->size,
  82. priv->dmem_region_vaddr[dmem_region],
  83. uiomem->addr);
  84. }
  85. uiomem->addr = DMEM_MAP_ERROR;
  86. ++dmem_region;
  87. ++uiomem;
  88. }
  89. mutex_unlock(&priv->alloc_lock);
  90. return 0;
  91. }
  92. static irqreturn_t uio_dmem_genirq_handler(int irq, struct uio_info *dev_info)
  93. {
  94. struct uio_dmem_genirq_platdata *priv = dev_info->priv;
  95. /* Just disable the interrupt in the interrupt controller, and
  96. * remember the state so we can allow user space to enable it later.
  97. */
  98. if (!test_and_set_bit(0, &priv->flags))
  99. disable_irq_nosync(irq);
  100. return IRQ_HANDLED;
  101. }
  102. static int uio_dmem_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  103. {
  104. struct uio_dmem_genirq_platdata *priv = dev_info->priv;
  105. unsigned long flags;
  106. /* Allow user space to enable and disable the interrupt
  107. * in the interrupt controller, but keep track of the
  108. * state to prevent per-irq depth damage.
  109. *
  110. * Serialize this operation to support multiple tasks.
  111. */
  112. spin_lock_irqsave(&priv->lock, flags);
  113. if (irq_on) {
  114. if (test_and_clear_bit(0, &priv->flags))
  115. enable_irq(dev_info->irq);
  116. } else {
  117. if (!test_and_set_bit(0, &priv->flags))
  118. disable_irq(dev_info->irq);
  119. }
  120. spin_unlock_irqrestore(&priv->lock, flags);
  121. return 0;
  122. }
  123. static int uio_dmem_genirq_probe(struct platform_device *pdev)
  124. {
  125. struct uio_dmem_genirq_pdata *pdata = dev_get_platdata(&pdev->dev);
  126. struct uio_info *uioinfo = &pdata->uioinfo;
  127. struct uio_dmem_genirq_platdata *priv;
  128. struct uio_mem *uiomem;
  129. int ret = -EINVAL;
  130. int i;
  131. if (pdev->dev.of_node) {
  132. int irq;
  133. /* alloc uioinfo for one device */
  134. uioinfo = kzalloc(sizeof(*uioinfo), GFP_KERNEL);
  135. if (!uioinfo) {
  136. ret = -ENOMEM;
  137. dev_err(&pdev->dev, "unable to kmalloc\n");
  138. goto bad2;
  139. }
  140. uioinfo->name = pdev->dev.of_node->name;
  141. uioinfo->version = "devicetree";
  142. /* Multiple IRQs are not supported */
  143. irq = platform_get_irq(pdev, 0);
  144. if (irq == -ENXIO)
  145. uioinfo->irq = UIO_IRQ_NONE;
  146. else
  147. uioinfo->irq = irq;
  148. }
  149. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  150. dev_err(&pdev->dev, "missing platform_data\n");
  151. goto bad0;
  152. }
  153. if (uioinfo->handler || uioinfo->irqcontrol ||
  154. uioinfo->irq_flags & IRQF_SHARED) {
  155. dev_err(&pdev->dev, "interrupt configuration error\n");
  156. goto bad0;
  157. }
  158. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  159. if (!priv) {
  160. ret = -ENOMEM;
  161. dev_err(&pdev->dev, "unable to kmalloc\n");
  162. goto bad0;
  163. }
  164. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  165. priv->uioinfo = uioinfo;
  166. spin_lock_init(&priv->lock);
  167. priv->flags = 0; /* interrupt is enabled to begin with */
  168. priv->pdev = pdev;
  169. mutex_init(&priv->alloc_lock);
  170. if (!uioinfo->irq) {
  171. ret = platform_get_irq(pdev, 0);
  172. if (ret < 0) {
  173. dev_err(&pdev->dev, "failed to get IRQ\n");
  174. goto bad1;
  175. }
  176. uioinfo->irq = ret;
  177. }
  178. uiomem = &uioinfo->mem[0];
  179. for (i = 0; i < pdev->num_resources; ++i) {
  180. struct resource *r = &pdev->resource[i];
  181. if (r->flags != IORESOURCE_MEM)
  182. continue;
  183. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  184. dev_warn(&pdev->dev, "device has more than "
  185. __stringify(MAX_UIO_MAPS)
  186. " I/O memory resources.\n");
  187. break;
  188. }
  189. uiomem->memtype = UIO_MEM_PHYS;
  190. uiomem->addr = r->start;
  191. uiomem->size = resource_size(r);
  192. ++uiomem;
  193. }
  194. priv->dmem_region_start = i;
  195. priv->num_dmem_regions = pdata->num_dynamic_regions;
  196. for (i = 0; i < pdata->num_dynamic_regions; ++i) {
  197. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  198. dev_warn(&pdev->dev, "device has more than "
  199. __stringify(MAX_UIO_MAPS)
  200. " dynamic and fixed memory regions.\n");
  201. break;
  202. }
  203. uiomem->memtype = UIO_MEM_PHYS;
  204. uiomem->addr = DMEM_MAP_ERROR;
  205. uiomem->size = pdata->dynamic_region_sizes[i];
  206. ++uiomem;
  207. }
  208. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  209. uiomem->size = 0;
  210. ++uiomem;
  211. }
  212. /* This driver requires no hardware specific kernel code to handle
  213. * interrupts. Instead, the interrupt handler simply disables the
  214. * interrupt in the interrupt controller. User space is responsible
  215. * for performing hardware specific acknowledge and re-enabling of
  216. * the interrupt in the interrupt controller.
  217. *
  218. * Interrupt sharing is not supported.
  219. */
  220. uioinfo->handler = uio_dmem_genirq_handler;
  221. uioinfo->irqcontrol = uio_dmem_genirq_irqcontrol;
  222. uioinfo->open = uio_dmem_genirq_open;
  223. uioinfo->release = uio_dmem_genirq_release;
  224. uioinfo->priv = priv;
  225. /* Enable Runtime PM for this device:
  226. * The device starts in suspended state to allow the hardware to be
  227. * turned off by default. The Runtime PM bus code should power on the
  228. * hardware and enable clocks at open().
  229. */
  230. pm_runtime_enable(&pdev->dev);
  231. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  232. if (ret) {
  233. dev_err(&pdev->dev, "unable to register uio device\n");
  234. pm_runtime_disable(&pdev->dev);
  235. goto bad1;
  236. }
  237. platform_set_drvdata(pdev, priv);
  238. return 0;
  239. bad1:
  240. kfree(priv);
  241. bad0:
  242. /* kfree uioinfo for OF */
  243. if (pdev->dev.of_node)
  244. kfree(uioinfo);
  245. bad2:
  246. return ret;
  247. }
  248. static int uio_dmem_genirq_remove(struct platform_device *pdev)
  249. {
  250. struct uio_dmem_genirq_platdata *priv = platform_get_drvdata(pdev);
  251. uio_unregister_device(priv->uioinfo);
  252. pm_runtime_disable(&pdev->dev);
  253. priv->uioinfo->handler = NULL;
  254. priv->uioinfo->irqcontrol = NULL;
  255. /* kfree uioinfo for OF */
  256. if (pdev->dev.of_node)
  257. kfree(priv->uioinfo);
  258. kfree(priv);
  259. return 0;
  260. }
  261. static int uio_dmem_genirq_runtime_nop(struct device *dev)
  262. {
  263. /* Runtime PM callback shared between ->runtime_suspend()
  264. * and ->runtime_resume(). Simply returns success.
  265. *
  266. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  267. * are used at open() and release() time. This allows the
  268. * Runtime PM code to turn off power to the device while the
  269. * device is unused, ie before open() and after release().
  270. *
  271. * This Runtime PM callback does not need to save or restore
  272. * any registers since user space is responsbile for hardware
  273. * register reinitialization after open().
  274. */
  275. return 0;
  276. }
  277. static const struct dev_pm_ops uio_dmem_genirq_dev_pm_ops = {
  278. .runtime_suspend = uio_dmem_genirq_runtime_nop,
  279. .runtime_resume = uio_dmem_genirq_runtime_nop,
  280. };
  281. #ifdef CONFIG_OF
  282. static const struct of_device_id uio_of_genirq_match[] = {
  283. { /* empty for now */ },
  284. };
  285. MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
  286. #endif
  287. static struct platform_driver uio_dmem_genirq = {
  288. .probe = uio_dmem_genirq_probe,
  289. .remove = uio_dmem_genirq_remove,
  290. .driver = {
  291. .name = DRIVER_NAME,
  292. .pm = &uio_dmem_genirq_dev_pm_ops,
  293. .of_match_table = of_match_ptr(uio_of_genirq_match),
  294. },
  295. };
  296. module_platform_driver(uio_dmem_genirq);
  297. MODULE_AUTHOR("Damian Hobson-Garcia");
  298. MODULE_DESCRIPTION("Userspace I/O platform driver with dynamic memory.");
  299. MODULE_LICENSE("GPL v2");
  300. MODULE_ALIAS("platform:" DRIVER_NAME);