uio_dmem_genirq.c 9.1 KB

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