uio_dmem_genirq.c 9.2 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. }
  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. spin_unlock_irqrestore(&priv->lock, flags);
  117. } else {
  118. if (!test_and_set_bit(0, &priv->flags)) {
  119. spin_unlock_irqrestore(&priv->lock, flags);
  120. disable_irq(dev_info->irq);
  121. }
  122. }
  123. return 0;
  124. }
  125. static int uio_dmem_genirq_probe(struct platform_device *pdev)
  126. {
  127. struct uio_dmem_genirq_pdata *pdata = dev_get_platdata(&pdev->dev);
  128. struct uio_info *uioinfo = &pdata->uioinfo;
  129. struct uio_dmem_genirq_platdata *priv;
  130. struct uio_mem *uiomem;
  131. int ret = -EINVAL;
  132. int i;
  133. if (pdev->dev.of_node) {
  134. int irq;
  135. /* alloc uioinfo for one device */
  136. uioinfo = kzalloc(sizeof(*uioinfo), GFP_KERNEL);
  137. if (!uioinfo) {
  138. ret = -ENOMEM;
  139. dev_err(&pdev->dev, "unable to kmalloc\n");
  140. goto bad2;
  141. }
  142. uioinfo->name = pdev->dev.of_node->name;
  143. uioinfo->version = "devicetree";
  144. /* Multiple IRQs are not supported */
  145. irq = platform_get_irq(pdev, 0);
  146. if (irq == -ENXIO)
  147. uioinfo->irq = UIO_IRQ_NONE;
  148. else
  149. uioinfo->irq = irq;
  150. }
  151. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  152. dev_err(&pdev->dev, "missing platform_data\n");
  153. goto bad0;
  154. }
  155. if (uioinfo->handler || uioinfo->irqcontrol ||
  156. uioinfo->irq_flags & IRQF_SHARED) {
  157. dev_err(&pdev->dev, "interrupt configuration error\n");
  158. goto bad0;
  159. }
  160. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  161. if (!priv) {
  162. ret = -ENOMEM;
  163. dev_err(&pdev->dev, "unable to kmalloc\n");
  164. goto bad0;
  165. }
  166. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  167. priv->uioinfo = uioinfo;
  168. spin_lock_init(&priv->lock);
  169. priv->flags = 0; /* interrupt is enabled to begin with */
  170. priv->pdev = pdev;
  171. mutex_init(&priv->alloc_lock);
  172. if (!uioinfo->irq) {
  173. ret = platform_get_irq(pdev, 0);
  174. if (ret < 0) {
  175. dev_err(&pdev->dev, "failed to get IRQ\n");
  176. goto bad1;
  177. }
  178. uioinfo->irq = ret;
  179. }
  180. uiomem = &uioinfo->mem[0];
  181. for (i = 0; i < pdev->num_resources; ++i) {
  182. struct resource *r = &pdev->resource[i];
  183. if (r->flags != IORESOURCE_MEM)
  184. continue;
  185. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  186. dev_warn(&pdev->dev, "device has more than "
  187. __stringify(MAX_UIO_MAPS)
  188. " I/O memory resources.\n");
  189. break;
  190. }
  191. uiomem->memtype = UIO_MEM_PHYS;
  192. uiomem->addr = r->start;
  193. uiomem->size = resource_size(r);
  194. ++uiomem;
  195. }
  196. priv->dmem_region_start = uiomem - &uioinfo->mem[0];
  197. priv->num_dmem_regions = pdata->num_dynamic_regions;
  198. for (i = 0; i < pdata->num_dynamic_regions; ++i) {
  199. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  200. dev_warn(&pdev->dev, "device has more than "
  201. __stringify(MAX_UIO_MAPS)
  202. " dynamic and fixed memory regions.\n");
  203. break;
  204. }
  205. uiomem->memtype = UIO_MEM_PHYS;
  206. uiomem->addr = DMEM_MAP_ERROR;
  207. uiomem->size = pdata->dynamic_region_sizes[i];
  208. ++uiomem;
  209. }
  210. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  211. uiomem->size = 0;
  212. ++uiomem;
  213. }
  214. /* This driver requires no hardware specific kernel code to handle
  215. * interrupts. Instead, the interrupt handler simply disables the
  216. * interrupt in the interrupt controller. User space is responsible
  217. * for performing hardware specific acknowledge and re-enabling of
  218. * the interrupt in the interrupt controller.
  219. *
  220. * Interrupt sharing is not supported.
  221. */
  222. uioinfo->handler = uio_dmem_genirq_handler;
  223. uioinfo->irqcontrol = uio_dmem_genirq_irqcontrol;
  224. uioinfo->open = uio_dmem_genirq_open;
  225. uioinfo->release = uio_dmem_genirq_release;
  226. uioinfo->priv = priv;
  227. /* Enable Runtime PM for this device:
  228. * The device starts in suspended state to allow the hardware to be
  229. * turned off by default. The Runtime PM bus code should power on the
  230. * hardware and enable clocks at open().
  231. */
  232. pm_runtime_enable(&pdev->dev);
  233. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  234. if (ret) {
  235. dev_err(&pdev->dev, "unable to register uio device\n");
  236. pm_runtime_disable(&pdev->dev);
  237. goto bad1;
  238. }
  239. platform_set_drvdata(pdev, priv);
  240. return 0;
  241. bad1:
  242. kfree(priv);
  243. bad0:
  244. /* kfree uioinfo for OF */
  245. if (pdev->dev.of_node)
  246. kfree(uioinfo);
  247. bad2:
  248. return ret;
  249. }
  250. static int uio_dmem_genirq_remove(struct platform_device *pdev)
  251. {
  252. struct uio_dmem_genirq_platdata *priv = platform_get_drvdata(pdev);
  253. uio_unregister_device(priv->uioinfo);
  254. pm_runtime_disable(&pdev->dev);
  255. priv->uioinfo->handler = NULL;
  256. priv->uioinfo->irqcontrol = NULL;
  257. /* kfree uioinfo for OF */
  258. if (pdev->dev.of_node)
  259. kfree(priv->uioinfo);
  260. kfree(priv);
  261. return 0;
  262. }
  263. static int uio_dmem_genirq_runtime_nop(struct device *dev)
  264. {
  265. /* Runtime PM callback shared between ->runtime_suspend()
  266. * and ->runtime_resume(). Simply returns success.
  267. *
  268. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  269. * are used at open() and release() time. This allows the
  270. * Runtime PM code to turn off power to the device while the
  271. * device is unused, ie before open() and after release().
  272. *
  273. * This Runtime PM callback does not need to save or restore
  274. * any registers since user space is responsbile for hardware
  275. * register reinitialization after open().
  276. */
  277. return 0;
  278. }
  279. static const struct dev_pm_ops uio_dmem_genirq_dev_pm_ops = {
  280. .runtime_suspend = uio_dmem_genirq_runtime_nop,
  281. .runtime_resume = uio_dmem_genirq_runtime_nop,
  282. };
  283. #ifdef CONFIG_OF
  284. static const struct of_device_id uio_of_genirq_match[] = {
  285. { /* empty for now */ },
  286. };
  287. MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
  288. #endif
  289. static struct platform_driver uio_dmem_genirq = {
  290. .probe = uio_dmem_genirq_probe,
  291. .remove = uio_dmem_genirq_remove,
  292. .driver = {
  293. .name = DRIVER_NAME,
  294. .pm = &uio_dmem_genirq_dev_pm_ops,
  295. .of_match_table = of_match_ptr(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);