uio_dmem_genirq.c 8.9 KB

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