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